From d1550bdddb2c2fcab3e9ed1daec9d55c82db2d22 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B6ren=20Weber?= Date: Wed, 6 Jul 2022 00:15:33 +0200 Subject: [PATCH] archetypes: introducing hooks #300 --- archetypes/chapter.md | 1 - exampleSite/content/basics/_index.en.md | 1 - exampleSite/content/basics/_index.pir.md | 1 - .../content/basics/migration/_index.en.md | 4 ++-- exampleSite/content/cont/_index.en.md | 1 - exampleSite/content/cont/_index.pir.md | 1 - .../content/cont/archetypes/_index.en.md | 20 +++++++++++++------ exampleSite/content/cont/pages/_index.en.md | 2 -- exampleSite/content/shortcodes/_index.en.md | 1 - exampleSite/content/shortcodes/_index.pir.md | 1 - exampleSite/content/tests/_index.en.md | 1 - exampleSite/content/tests/_index.pir.md | 1 - layouts/{ => _default}/index.html | 2 +- layouts/{ => _default}/index.json | 0 layouts/{ => _default}/index.print.html | 2 +- layouts/_default/list.html | 2 +- layouts/_default/list.print.html | 2 +- layouts/_default/single.html | 2 +- layouts/_default/single.print.html | 2 +- layouts/partials/_index.html | 9 --------- layouts/partials/_list.html | 7 ------- layouts/partials/{_single.html => _main.html} | 4 +++- layouts/partials/archetype.html | 14 +++++++++++++ .../{chapter.html => chapter/article.html} | 4 ++-- .../archetypes/chapter/styleclass.html | 1 + .../{default.html => default/article.html} | 2 +- .../archetypes/default/styleclass.html | 1 + .../article.html} | 0 .../deprecated-chapter/styleclass.html | 1 + .../article.html} | 0 .../deprecated-home/styleclass.html | 1 + .../{home.html => home/article.html} | 2 +- .../partials/archetypes/home/styleclass.html | 1 + layouts/partials/article.html | 13 +----------- layouts/partials/header.html | 2 +- layouts/partials/initial.html | 9 ++++----- 36 files changed, 54 insertions(+), 64 deletions(-) rename layouts/{ => _default}/index.html (62%) rename layouts/{ => _default}/index.json (100%) rename layouts/{ => _default}/index.print.html (62%) delete mode 100644 layouts/partials/_index.html delete mode 100644 layouts/partials/_list.html rename layouts/partials/{_single.html => _main.html} (56%) create mode 100644 layouts/partials/archetype.html rename layouts/partials/archetypes/{chapter.html => chapter/article.html} (60%) create mode 100644 layouts/partials/archetypes/chapter/styleclass.html rename layouts/partials/archetypes/{default.html => default/article.html} (73%) create mode 100644 layouts/partials/archetypes/default/styleclass.html rename layouts/partials/archetypes/{chapter-deprecated.html => deprecated-chapter/article.html} (100%) create mode 100644 layouts/partials/archetypes/deprecated-chapter/styleclass.html rename layouts/partials/archetypes/{home-deprecated.html => deprecated-home/article.html} (100%) create mode 100644 layouts/partials/archetypes/deprecated-home/styleclass.html rename layouts/partials/archetypes/{home.html => home/article.html} (72%) create mode 100644 layouts/partials/archetypes/home/styleclass.html diff --git a/archetypes/chapter.md b/archetypes/chapter.md index f9526d715c..180c61ce1f 100644 --- a/archetypes/chapter.md +++ b/archetypes/chapter.md @@ -1,6 +1,5 @@ +++ archetype = "chapter" -narrow = true title = "{{ replace .Name "-" " " | title }}" weight = X +++ diff --git a/exampleSite/content/basics/_index.en.md b/exampleSite/content/basics/_index.en.md index e5b064c981..9fb8254742 100755 --- a/exampleSite/content/basics/_index.en.md +++ b/exampleSite/content/basics/_index.en.md @@ -1,6 +1,5 @@ +++ archetype = "chapter" -narrow = true title = "Basics" weight = 1 +++ diff --git a/exampleSite/content/basics/_index.pir.md b/exampleSite/content/basics/_index.pir.md index b7d8e5a691..e7fd897587 100644 --- a/exampleSite/content/basics/_index.pir.md +++ b/exampleSite/content/basics/_index.pir.md @@ -1,6 +1,5 @@ +++ archetype = "chapter" -narrow = true title = "Basics" weight = 1 +++ diff --git a/exampleSite/content/basics/migration/_index.en.md b/exampleSite/content/basics/migration/_index.en.md index c38c70607f..d0720ea362 100644 --- a/exampleSite/content/basics/migration/_index.en.md +++ b/exampleSite/content/basics/migration/_index.en.md @@ -16,9 +16,9 @@ This document shows you what's new in the latest release. For a detailed list of ## 5.0.0 -- **Change**: The way [archetypes]({{% relref "cont/archetypes" %}}) are used to generate output has changed. Your existing markdown files will still work like before and therefore you don't need to change anything after the upgrade. +- **Change**: The way [archetypes]({{% relref "cont/archetypes" %}}) are used to generate output has changed. The new systems allows you, to redefine existing archetypes or even generate your own ones. - Nevertheless, it is recommended to adapt your existing markdown files to the new way as follows: + Your existing markdown files will still work like before and therefore you don't need to change anything after the upgrade. Nevertheless, it is recommended to adapt your existing markdown files to the new way as follows: - for your home page, add the frontmatter parameter `archetype = "home"` and remove the leading heading diff --git a/exampleSite/content/cont/_index.en.md b/exampleSite/content/cont/_index.en.md index b1af945088..7e7194ee48 100755 --- a/exampleSite/content/cont/_index.en.md +++ b/exampleSite/content/cont/_index.en.md @@ -1,6 +1,5 @@ +++ archetype = "chapter" -narrow = true title = "Content" weight = 2 +++ diff --git a/exampleSite/content/cont/_index.pir.md b/exampleSite/content/cont/_index.pir.md index ad488aae31..91668dc477 100644 --- a/exampleSite/content/cont/_index.pir.md +++ b/exampleSite/content/cont/_index.pir.md @@ -1,6 +1,5 @@ +++ archetype = "chapter" -narrow = true title = "Rambl'n" weight = 2 +++ diff --git a/exampleSite/content/cont/archetypes/_index.en.md b/exampleSite/content/cont/archetypes/_index.en.md index 4d2e39d07a..4fef32e448 100644 --- a/exampleSite/content/cont/archetypes/_index.en.md +++ b/exampleSite/content/cont/archetypes/_index.en.md @@ -49,7 +49,6 @@ This leads to a file with the following content ```markdown +++ archetype = "chapter" -narrow = true title = "{{ replace .Name "-" " " | title }}" weight = X +++ @@ -57,7 +56,7 @@ weight = X Lorem Ipsum. ``` -Replace the `X` with a number. Because this number will be used to generate the subtitle of the content page, set the number to a consecutive value starting at 1 for each new chapter level. +Replace the `X` with a number. Because this number will be used to generate the subtitle of the chapter page, set the number to a consecutive value starting at 1 for each new chapter level. ### Default {#archetypes-default} @@ -110,10 +109,19 @@ Afterwards you can generate new content files of that kind with the follwing com hugo new --kind /_index.md ``` -### Partial +### Partials -To define how your archetypes are rendered, define a corresponding file in your project at `layouts/partials/archetypes/.html`. - -Take a look at the existing archetypes of this theme to get an idea how to utilize it. +To define how your archetypes are rendered, define corresponding partial files in your projects directory `layouts/partials/archetypes/`. If you use an unknown archetype in your frontmatter, the `default` archetype will be used to generate the page. + +Related to each archetype, several _hook_ partial files can be given. If a partial for a specific hook is missing, no output is generated for this hook. + +The following hooks are used: + +| Name | Notes | +|:---------------------|:------------| +| styleclass | Defines a set of CSS classes to be added to the HTML's `
` element. You can use these classes to define own CSS rules in your `custom-header.html` | +| article | Defines the HTML how to render your content | + +Take a look at the existing archetypes of this theme to get an idea how to utilize it. diff --git a/exampleSite/content/cont/pages/_index.en.md b/exampleSite/content/cont/pages/_index.en.md index 7447282683..d7c63bd39a 100644 --- a/exampleSite/content/cont/pages/_index.en.md +++ b/exampleSite/content/cont/pages/_index.en.md @@ -76,8 +76,6 @@ headingPost = "" menuPre = "" # The title of the page in menu will be postfixed by this HTML content menuPost = "" -# Makes the content more narrow, increases font-size and alignment -narrow = false # Hide a menu entry by setting this to true hidden = false # Display name of this page modifier. If set, it will be displayed in the footer. diff --git a/exampleSite/content/shortcodes/_index.en.md b/exampleSite/content/shortcodes/_index.en.md index 78f5eadcfa..c394bd4553 100644 --- a/exampleSite/content/shortcodes/_index.en.md +++ b/exampleSite/content/shortcodes/_index.en.md @@ -1,6 +1,5 @@ +++ archetype = "chapter" -narrow = true title = "Shortcodes" weight = 3 +++ diff --git a/exampleSite/content/shortcodes/_index.pir.md b/exampleSite/content/shortcodes/_index.pir.md index 1b4286dbe8..5f0312ed6a 100644 --- a/exampleSite/content/shortcodes/_index.pir.md +++ b/exampleSite/content/shortcodes/_index.pir.md @@ -1,6 +1,5 @@ +++ archetype = "chapter" -narrow = true title = "Shorrrtcodes" weight = 3 +++ diff --git a/exampleSite/content/tests/_index.en.md b/exampleSite/content/tests/_index.en.md index 10db936e67..8178b05af6 100644 --- a/exampleSite/content/tests/_index.en.md +++ b/exampleSite/content/tests/_index.en.md @@ -1,7 +1,6 @@ +++ archetype = "chapter" hidden = true -narrow = true title = "Tests" weight = 4 +++ diff --git a/exampleSite/content/tests/_index.pir.md b/exampleSite/content/tests/_index.pir.md index c01a250ea1..da36406522 100644 --- a/exampleSite/content/tests/_index.pir.md +++ b/exampleSite/content/tests/_index.pir.md @@ -1,7 +1,6 @@ +++ archetype = "chapter" hidden = true -narrow = true title = "Tests" weight = 4 +++ diff --git a/layouts/index.html b/layouts/_default/index.html similarity index 62% rename from layouts/index.html rename to layouts/_default/index.html index 8eae7b7a35..3ac47b1d8c 100644 --- a/layouts/index.html +++ b/layouts/_default/index.html @@ -1,2 +1,2 @@ {{- .Scratch.Set "relearnOutputFormat" "HTML" }} -{{- partial "_index.html" . }} \ No newline at end of file +{{- partial "_main.html" . }} \ No newline at end of file diff --git a/layouts/index.json b/layouts/_default/index.json similarity index 100% rename from layouts/index.json rename to layouts/_default/index.json diff --git a/layouts/index.print.html b/layouts/_default/index.print.html similarity index 62% rename from layouts/index.print.html rename to layouts/_default/index.print.html index 6d7193eb94..611dc5b42f 100644 --- a/layouts/index.print.html +++ b/layouts/_default/index.print.html @@ -1,2 +1,2 @@ {{- .Scratch.Set "relearnOutputFormat" "PRINT" }} -{{- partial "_index.html" . }} \ No newline at end of file +{{- partial "_main.html" . }} \ No newline at end of file diff --git a/layouts/_default/list.html b/layouts/_default/list.html index 7a50069c46..3ac47b1d8c 100644 --- a/layouts/_default/list.html +++ b/layouts/_default/list.html @@ -1,2 +1,2 @@ {{- .Scratch.Set "relearnOutputFormat" "HTML" }} -{{- partial "_list.html" . }} \ No newline at end of file +{{- partial "_main.html" . }} \ No newline at end of file diff --git a/layouts/_default/list.print.html b/layouts/_default/list.print.html index eac72a3b86..611dc5b42f 100644 --- a/layouts/_default/list.print.html +++ b/layouts/_default/list.print.html @@ -1,2 +1,2 @@ {{- .Scratch.Set "relearnOutputFormat" "PRINT" }} -{{- partial "_list.html" . }} \ No newline at end of file +{{- partial "_main.html" . }} \ No newline at end of file diff --git a/layouts/_default/single.html b/layouts/_default/single.html index d792465bfc..3ac47b1d8c 100644 --- a/layouts/_default/single.html +++ b/layouts/_default/single.html @@ -1,2 +1,2 @@ {{- .Scratch.Set "relearnOutputFormat" "HTML" }} -{{- partial "_single.html" . }} \ No newline at end of file +{{- partial "_main.html" . }} \ No newline at end of file diff --git a/layouts/_default/single.print.html b/layouts/_default/single.print.html index 9403344056..611dc5b42f 100644 --- a/layouts/_default/single.print.html +++ b/layouts/_default/single.print.html @@ -1,2 +1,2 @@ {{- .Scratch.Set "relearnOutputFormat" "PRINT" }} -{{- partial "_single.html" . }} \ No newline at end of file +{{- partial "_main.html" . }} \ No newline at end of file diff --git a/layouts/partials/_index.html b/layouts/partials/_index.html deleted file mode 100644 index 3e7831c24b..0000000000 --- a/layouts/partials/_index.html +++ /dev/null @@ -1,9 +0,0 @@ -{{- partial "header.html" . }} -{{- if not .Site.Home.Content }} - {{- partial "initial.html" .Site.Home }} -{{- else if eq (.Scratch.Get "relearnOutputFormat") "PRINT" }} - {{- partial "body.print.html" .Site.Home }} -{{- else }} - {{- partial "body.html" .Site.Home }} -{{- end }} -{{- partial "footer.html" . }} \ No newline at end of file diff --git a/layouts/partials/_list.html b/layouts/partials/_list.html deleted file mode 100644 index 3f7b1dfb5b..0000000000 --- a/layouts/partials/_list.html +++ /dev/null @@ -1,7 +0,0 @@ -{{- partial "header.html" . }} -{{- if eq (.Scratch.Get "relearnOutputFormat") "PRINT" }} - {{- partial "body.print.html" . }} -{{- else }} - {{- partial "body.html" . }} -{{- end }} -{{- partial "footer.html" . }} \ No newline at end of file diff --git a/layouts/partials/_single.html b/layouts/partials/_main.html similarity index 56% rename from layouts/partials/_single.html rename to layouts/partials/_main.html index 3f7b1dfb5b..08a7d99658 100644 --- a/layouts/partials/_single.html +++ b/layouts/partials/_main.html @@ -1,5 +1,7 @@ {{- partial "header.html" . }} -{{- if eq (.Scratch.Get "relearnOutputFormat") "PRINT" }} +{{- if not .File }} + {{- partial "initial.html" . }} +{{- else if eq (.Scratch.Get "relearnOutputFormat") "PRINT" }} {{- partial "body.print.html" . }} {{- else }} {{- partial "body.html" . }} diff --git a/layouts/partials/archetype.html b/layouts/partials/archetype.html new file mode 100644 index 0000000000..a2d6e07e32 --- /dev/null +++ b/layouts/partials/archetype.html @@ -0,0 +1,14 @@ +{{- $archetype := "default" }} +{{- if .page.Params.archetype }} + {{- $archetype = .page.Params.archetype }} +{{- else if .page.Params.chapter }} + {{- $archetype = "deprecated-chapter" }} +{{- else if .page.IsHome }} + {{- $archetype = "deprecated-home" }} +{{- end }} +{{- if not (fileExists (printf "/layouts/partials/archetypes/%s" $archetype)) }} + {{- $archetype = "default" }} +{{- end }} +{{- if (fileExists (printf "/layouts/partials/archetypes/%s/%s.html" $archetype .hook)) }} + {{- partial (printf "archetypes/%s/%s.html" $archetype .hook) . }} +{{- end }} \ No newline at end of file diff --git a/layouts/partials/archetypes/chapter.html b/layouts/partials/archetypes/chapter/article.html similarity index 60% rename from layouts/partials/archetypes/chapter.html rename to layouts/partials/archetypes/chapter/article.html index 04f0696290..fef69d96e9 100644 --- a/layouts/partials/archetypes/chapter.html +++ b/layouts/partials/archetypes/chapter/article.html @@ -1,9 +1,9 @@ {{- $content := .content }} {{- with .page }}
+{{ partial "heading-pre.html" . }}
{{ T "Chapter" .Params.Weight }}
+

{{ .Title }}

{{ partial "heading-post.html" . }} -{{- partial "heading-pre.html" . }}
{{ T "Chapter" .Params.Weight }}
-

{{ .Title }}

{{- partial "heading-post.html" . }} {{ $content | safeHTML }}