Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add CLI command (hugo render) for printing the source files in a custom format #10924

Open
nachtjasmin opened this issue May 2, 2023 · 1 comment
Labels
Milestone

Comments

@nachtjasmin
Copy link

Note: Someone with a better knowledge of the English language should edit this title, its intent isn't that clear right now. Thanks.

Problem

As of today, there's no "official" way for rendering the list of pages in another format as CSV. hugo list all renders all pages just fine, it's just very limited, as there's no configuration for the output, access to the params, etc.

Parsing the CSV is possible, but due to the mentioned problems, not suitable for all solutions.

We can render the pages in any format using resources.FromString and resources.ExecuteAsTemplate as described in the forum. However, this has the downside that we need to generate the whole page, even if we just want a single file. Especially with a lot of pages, image hooks, etc. this can take a lot of time.

Proposal

Other Go-based tools, for example kubectl allow to pass a Go Template string which is used for rendering the output. I'd like to propose such a feature to the Hugo CLI as well. It would allow to render the pages/resources/whatever in any format.

A possible CLI implementation might look like this:

$ hugo render "<some Go Template syntax>"

or

$ hugo render < my-template.tmpl

Scenarios

This is useful for the following scenarios:

Rendering a .htaccess

Contents of htaccess.tmpl:

# Automatic generated .htaccess using Hugo on {{ now }}

{{ range .Site.AllPages -}}
    {{- $p := . -}}
    {{- with .Params.redirect }}
# {{ printf "Redirect %q using .Params.redirect" $p.Title }}
Redirect 301 "{{ $p.RelPermalink }}" "{{ . }}"
    {{ end -}}
    {{- range .Aliases }}
# {{ printf "Redirect %q using aliases" $p.Title }}
Redirect 301 "{{ . }}" "{{ $p.RelPermalink }}"
    {{ end -}}
{{- end -}}
$ hugo render < htaccess.tmpl > .htaccess # renders the .htaccess

Validating that each page has a title

Contents of validate.tmpl:

[
{{ range .Site.AllPages }}
  {{ if (eq .Title "") }}
    {{ errorf "The page %s does not have a title." .File.Path }}
  {{ end }}
{{ end }}
]
$ hugo render < validate.tmpl # returns with failing status code, causing the CI job to fail

Benefits

  • Faster rendering of such templates
  • No need to generate all images/folders, just the one file, might be combined with --renderToMemory

Risks

I can't see any risks at the moment.

Copy link

github-actions bot commented May 2, 2024

This issue has been automatically marked as stale because it has not had recent activity. The resources of the Hugo team are limited, and so we are asking for your help.
If this is a bug and you can still reproduce this error on the master branch, please reply with all of the information you have about it in order to keep the issue open.
If this is a feature request, and you feel that it is still relevant and valuable, please tell us why.
This issue will automatically be closed in the near future if no further activity occurs. Thank you for all your contributions.

@github-actions github-actions bot added the Stale label May 2, 2024
@bep bep added this to the v0.126.0 milestone May 2, 2024
@bep bep modified the milestones: v0.126.0, v0.127.0 May 15, 2024
@bep bep modified the milestones: v0.127.0, v0.128.0 Jun 8, 2024
@bep bep modified the milestones: v0.128.0, v0.129.0 Jun 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants