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

Create pages from _content.gotmpl #12440

Merged
merged 1 commit into from
May 14, 2024
Merged

Conversation

bep
Copy link
Member

@bep bep commented Apr 26, 2024

  • Call partials.
  • Call i18n for current language.
  • Add pages
  • Add resources; from string and existing resource object (wrapper)
  • Throw error if lang is defined.
  • Throw error if path starts with a slash
  • Support date (front matter) defaults/mapping
  • Add mediaType for page content (also add type defs for Asciidoc etc.)
  • Add mediaType for resource content
  • Handle disabled languages
  • Add a Site wrapper (no page collections)
  • make markdownify work without Home (this needs to wait)
  • cascade
  • build opts

Some API/implementation notes:

  • .Page.File.IsContentAdapter (new) returns whether the backing file is a content adapter.

@bep bep force-pushed the feat/pagesfromtemplate branch 5 times, most recently from cbac41a to a882237 Compare April 29, 2024 09:05
@bep bep force-pushed the feat/pagesfromtemplate branch 2 times, most recently from 0bce7e5 to 02c63e5 Compare May 8, 2024 09:56
@bep bep force-pushed the feat/pagesfromtemplate branch 19 times, most recently from 113971e to 4b1c839 Compare May 12, 2024 13:04
@bep bep force-pushed the feat/pagesfromtemplate branch 2 times, most recently from 80ec32c to 651b34f Compare May 12, 2024 13:55
@jmooring
Copy link
Member

Question

Should we disallow markup in the map passed to .AddPage. Not sure what would happen if both markup and content.mediaType are specified. Maybe ignoring it is fine.

Problem

You may already be aware of this... I am unable to capture a remote resource as a page resource using AddResource... maybe I'm doing it wrong. This example uses a data source and images that are now available on gohugo.io (I wanted to create an example in the docs that everyone can try and it will just work):

git clone --single-branch -b hugo-github-issue-12440 https://github.com/jmooring/hugo-testing hugo-github-issue-12440
cd hugo-github-issue-12440
hugo && tree public

The remote resources appear in the root with their original names, and they should be placed within their respective bundles at the specified path.

@bep bep force-pushed the feat/pagesfromtemplate branch 3 times, most recently from f2cd607 to 235086f Compare May 13, 2024 11:14
@bep bep marked this pull request as ready for review May 13, 2024 11:45
@bep bep force-pushed the feat/pagesfromtemplate branch 3 times, most recently from 1bcae26 to 1d7fd9f Compare May 13, 2024 12:29
@bep
Copy link
Member Author

bep commented May 13, 2024

Should we disallow markup in the map passed to .AddPage

I have added a check for this.

You may already be aware of this... I am unable to capture a remote resource as a page resource using AddResource... maybe I'm doing it wrong.

No, that is currently not supported, and will not be in v1 of this (I don't need it, I suspect few do, and ... I'm a little bit short on time). The tests are green now, and I'm mostly happy, so I will only fix major issues/bugs with this.

As to content, the schema is:

  • value (for Page, string only, for Resource either string or an existing Resource (will be wrapped)
  • mediaType (no markup)

@jmooring
Copy link
Member

jmooring commented May 13, 2024

that is currently not supported

FYI...

This works almost perfectly:

{{ with resources.GetRemote "foo" }}
  {{ $content := dict "type" .MediaType.Type "value" .Content }}
  {{ $resource := dict
    "content" $content
    "path" (printf "%s/cover.%s" $item.title .MediaType.SubType)
  }}
  {{ $.AddResource $resource }}
{{ end }}

In the above, $item.title is something like "Interpreting the French Revolution".

The only problem is figuring out how to set the path. With the example site referenced above, I'm getting this:

public/books/
├── Interpreting the French Revolution/
│   └── cover.webp
├── Les Misérables/
│   └── cover.webp
├── The Ancien Régime and the Revolution/
│   └── cover.webp
├── The Hunchback of Notre Dame/
│   └── cover.webp
├── interpreting-the-french-revolution/
│   └── index.html
├── les-misérables/
│   └── index.html
├── the-ancien-régime-and-the-revolution/
│   └── index.html
├── the-hunchback-of-notre-dame/
│   └── index.html
└── index.html

Instead of this:

public/books/
├── interpreting-the-french-revolution/
│   ├── cover.webp
│   └── index.html
├── les-misérables/
│   ├── cover.webp
│   └── index.html
├── the-ancien-régime-and-the-revolution/
│   ├── cover.webp
│   └── index.html
├── the-hunchback-of-notre-dame/
│   ├── cover.webp
│   └── index.html
└── index.html

The path in the AddPage map is transformed to a logical path (lower case, hyphens instead of spaces, etc.).

The path is the AddResource map is not transformed to a logical path, or at least the way I did it.

@jmooring
Copy link
Member

jmooring commented May 13, 2024

for Resource either string or an existing Resource

This...

{{ with resources.Get "a.jpg" }}
  {{ $content := dict
    "mediaType" .MediaType.Type
    "value" .
  }}
  {{ $resource := dict
    "content" $content
    "path" "the-hunchback-of-notre-dame/cover.jpg"
  }}
  {{ $.AddResource $resource }}
{{ end }}

...produces this:

public/
├── books/
│   ├── the-hunchback-of-notre-dame/
│   │   └── index.html
│   └── index.html
├── a.jpg   <-- wrong path
└── index.html

I can get the desired/expected result by passing the resource's .Content:

{{ with resources.Get "a.jpg" }}
  {{ $content := dict
    "mediaType" .MediaType.Type
    "value" .Content
  }}
  {{ $resource := dict
    "content" $content
    "path" "the-hunchback-of-notre-dame/cover.jpg"
  }}
  {{ $.AddResource $resource }}
{{ end }}

Which produces this:

public/
├── books/
│   ├── the-hunchback-of-notre-dame/
│   │   ├── cover.jpg
│   │   └── index.html
│   └── index.html
└── index.html

@bep
Copy link
Member Author

bep commented May 13, 2024

I can get the desired/expected result by passing the resource's .Content:

So, the desire may be in the eye of the beholder, but:

  • if you pass an Resource as value, then the .RelPermalink will not change (why duplicate when we can reuse?). This is by design. You can reach it with .Resources.Get $name.
  • If you pass a string (or .Content) as value, a new Resource is created.

@jmooring
Copy link
Member

Regarding resource as value vs. string... thanks. This is great. I'll update the docs.

@jmooring
Copy link
Member

jmooring commented May 13, 2024

{{ with resources.Get "a.jpg" }}
  {{ $content := dict
    "mediaType" .MediaType.Type
    "value" .
  }}
  {{ $resource := dict
    "content" $content
    "path" "the-hunchback-of-notre-dame/cover.jpg"
    "name" "cover.jpg"
  }}
  {{ $.AddResource $resource }}
{{ end }}

In the above, I have to specify name in order to use Resources.Get $name. It seems redundant as the name is already defined in the last path segment. Can name default to the last path segment? That way I can use name only when I need to override the value.

@bep
Copy link
Member Author

bep commented May 13, 2024

@jmooring yea, that was a bug. See d413cb6 -- now both cases should work the same when name is not set (which is also in line with how file resources work).

@jmooring
Copy link
Member

jmooring commented May 13, 2024

I've got an updated, working example of adding pages and page resources:

git clone --single-branch -b hugo-github-issue-12440 https://github.com/jmooring/hugo-testing hugo-github-issue-12440
cd hugo-github-issue-12440
hugo server

Navigate through the books section, and you'll see content and images as expected... works great!

What is unexpected is the structure of the public directory...

public/books/
├── interpreting-the-french-revolution/
│   ├── books/
│   │   └── interpreting-the-french-revolution/
│   │       └── cover.webp
│   └── index.html
├── les-misérables/
│   ├── books/
│   │   └── les-misérables/
│   │       └── cover.webp
│   └── index.html
├── the-ancien-régime-and-the-revolution/
│   ├── books/
│   │   └── the-ancien-régime-and-the-revolution/
│   │       └── cover.webp
│   └── index.html
├── the-hunchback-of-notre-dame/
│   ├── books/
│   │   └── the-hunchback-of-notre-dame/
│   │       └── cover.webp
│   └── index.html
└── index.html

The same things happens whether I'm adding a global resource (assets directory) or a remote resource. Again, the site works, it's just the public structure that's surprising.

@bep
Copy link
Member Author

bep commented May 13, 2024

, it's just the public structure that's surprising.

Hmm... That doesn't look right .... I will fix. But it's a little odd, because I see no such issues in my tests ... I'll look at your example.

@bep
Copy link
Member Author

bep commented May 13, 2024

@jmooring I will see if I can improve/fix this (tomorrow), but you can adjust your example to make the paths "compatible", e.g.

"path" (.title | anchorize)

@jmooring
Copy link
Member

jmooring commented May 13, 2024

make the paths "compatible",

I don't know if this is possible, but it would be convenient if the value I give to path in the AddResource map were "logicalized" (but keep the file extension). That way I woudn't have to anchorize anything, which would be good because anchorize varies depending on site configuration.

With path in the AddPage map, if I set the value to "A B C" it is "logicalized" to "some-dir/a-b-c". So symmetrical behavior would be a nice-to-have.

@bep
Copy link
Member Author

bep commented May 14, 2024

I don't know if this is possible, but it would be convenient if the value I give to path in the AddResource map were "logicalized"

@jmooring yea, that was the intention, but I was tripped by how we handle resources from "file bundles", where we "tip toe" a little around mixed case filenames. cdb9828 should fix/improve this.

I have tested your setup with this template OK:

{{/* Get remote data. */}}
{{ $data := dict }}
{{ $url := "https://gohugo.io/shared/examples/data/books.json" }}
{{ with resources.GetRemote $url }}
{{ with.Err }}
{{ errorf "Unable to get remote resource %s: %s" $url . }}
{{ else }}
{{ $data = . | transform.Unmarshal }}
{{ end }}
{{ else }}
{{ errorf "Unable to get remote resource %s" $url }}
{{ end }}

{{/* Add pages and page resources. */}}
{{ range $data }}

{{/* Add page. */}}
{{ $content := dict "mediaType" "text/markdown" "value" .summary }}
{{ $dates := dict "date" (time.AsTime .date) }}
{{ $params := dict "author" .author "isbn" .isbn "rating" .rating "tags" .tags }}
{{ $page := dict
    "content" $content
    "dates" $dates
    "kind" "page"
    "params" $params
    "path" .title
    "title" .title
}}
{{ $.AddPage $page }}

{{/* Add page resource. */}}
{{ $item := . }}
{{ with $url := $item.cover }}
{{ with resources.GetRemote $url }}
{{ with.Err }}
{{ errorf "Unable to get remote resource %s: %s" $url . }}
{{ else }}
{{ $content := dict "type" .MediaType.Type "value" .Content }}
{{ $params := dict "alt" $item.title }}
{{ $resource := dict
          "content" $content
          "params" $params
          "path" (printf "%s/cover.%s" $item.title .MediaType.SubType)
}}
{{ $.AddResource $resource }}
{{ end }}
{{ else }}
{{ errorf "Unable to get remote resource %s" $url }}
{{ end }}
{{ end }}

{{ end }}

@jmooring
Copy link
Member

This is working really well. I'm sure we'll run into other things later, but this looks good to me.

@bep bep force-pushed the feat/pagesfromtemplate branch 3 times, most recently from 772f14d to 0590671 Compare May 14, 2024 11:08
@bep bep merged commit e2d66e3 into gohugoio:master May 14, 2024
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants