From 19550ef1b24a42f7009af11ee67f6a24dbde61b7 Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Mon, 18 May 2026 21:15:19 -0700 Subject: [PATCH] Use Hugo's partial-returning where possible Signed-off-by: Danila Fedorin --- layouts/_partials/geturl.html | 19 +++++++++---------- layouts/_partials/linerangestr.html | 18 +++++++++--------- layouts/_partials/nameinseries.html | 6 +++--- layouts/_partials/post.html | 8 +++----- layouts/_partials/serieslink.html | 4 +--- layouts/_partials/uniquebyseries.html | 8 ++++---- layouts/_shortcodes/codeblock.html | 7 +++---- layouts/_shortcodes/codelines.html | 10 +++++----- layouts/_shortcodes/codeurl.html | 5 ++--- layouts/_shortcodes/githubsnippet.html | 5 ++--- layouts/_shortcodes/rawblock.html | 7 +++---- layouts/home.html | 4 ++-- 12 files changed, 46 insertions(+), 55 deletions(-) diff --git a/layouts/_partials/geturl.html b/layouts/_partials/geturl.html index 386a417..9180aa7 100644 --- a/layouts/_partials/geturl.html +++ b/layouts/_partials/geturl.html @@ -1,27 +1,26 @@ {{- $siteSourceUrl := site.Params.siteSourceUrl -}} {{- $submoduleLinks := hugo.Data.submodules -}} {{- $hostData := hugo.Data.hosts -}} -{{- $scratch := .scratch -}} {{- $lines := .lines -}} -{{- $scratch.Set "bestLength" -1 -}} -{{- $scratch.Set "bestUrl" (printf "%s/code/%s" $siteSourceUrl .path) -}} -{{- $scratch.Set "bestPath" "" -}} +{{- $bestLength := -1 -}} +{{- $bestUrl := printf "%s/code/%s" $siteSourceUrl .path -}} +{{- $bestPath := "" -}} {{- $filePath := .path -}} {{- range $path, $url := $submoduleLinks -}} - {{- $bestLength := $scratch.Get "bestLength" -}} {{- if and (le $bestLength (len $path)) (hasPrefix $filePath $path) -}} - {{- $scratch.Set "bestLength" (len $path) -}} - {{- $scratch.Set "bestPath" $path -}} - {{- $scratch.Set "bestUrl" (printf "%s%s" $url (strings.TrimPrefix $path $filePath)) -}} + {{- $bestLength = len $path -}} + {{- $bestPath = $path -}} + {{- $bestUrl = printf "%s%s" $url (strings.TrimPrefix $path $filePath) -}} {{- end -}} {{- end -}} {{- if .lines -}} {{- /* If the user provided a line range, see if we can tack it on to the end of the link. */ -}} {{- range $host, $data := $hostData -}} - {{- if hasPrefix ($scratch.Get "bestUrl") $host -}} + {{- if hasPrefix $bestUrl $host -}} {{- with $data.linesSuffix -}} - {{- $scratch.Add "bestUrl" (printf . (index $lines 0) (index $lines 1)) -}} + {{- $bestUrl = add $bestUrl (printf . (index $lines 0) (index $lines 1)) -}} {{- end -}} {{- end -}} {{- end -}} {{- end -}} +{{- return (dict "url" $bestUrl "basePath" $bestPath) -}} diff --git a/layouts/_partials/linerangestr.html b/layouts/_partials/linerangestr.html index f42b5e6..9801517 100644 --- a/layouts/_partials/linerangestr.html +++ b/layouts/_partials/linerangestr.html @@ -1,9 +1,9 @@ -{{ if eq .from .to }} -{{ .scratch.Set "comment" (printf ", line %d" .from) }} -{{ else }} -{{ if eq .from 0 }} -{{ .scratch.Set "comment" (printf ", around line %d" .to) }} -{{ else }} -{{ .scratch.Set "comment" (printf ", lines %d through %d" .from .to) }} -{{ end }} -{{ end }} +{{- $comment := "" -}} +{{- if eq .from .to -}} +{{- $comment = printf ", line %d" .from -}} +{{- else if eq .from 0 -}} +{{- $comment = printf ", around line %d" .to -}} +{{- else -}} +{{- $comment = printf ", lines %d through %d" .from .to -}} +{{- end -}} +{{- return $comment -}} diff --git a/layouts/_partials/nameinseries.html b/layouts/_partials/nameinseries.html index e70b162..2962a56 100644 --- a/layouts/_partials/nameinseries.html +++ b/layouts/_partials/nameinseries.html @@ -1,8 +1,8 @@ {{- $term := index (.page.GetTerms "series") 0 -}} {{- $divider := $term.Params.divider -}} +{{- $name := .page.Title -}} {{- if (and $divider (strings.Contains .page.Title $divider)) -}} {{- $rest := after 1 (split .page.Title $divider) -}} -{{- .scratch.Set "name" (delimit $rest $divider) -}} -{{- else -}} -{{- .scratch.Set "name" .page.Title -}} +{{- $name = delimit $rest $divider -}} {{- end -}} +{{- return $name -}} diff --git a/layouts/_partials/post.html b/layouts/_partials/post.html index cf981af..8dbb045 100644 --- a/layouts/_partials/post.html +++ b/layouts/_partials/post.html @@ -1,11 +1,9 @@
  • - {{- $scratch := newScratch -}} + {{- $name := .page.Title -}} {{- if .seriesName -}} - {{- partial "nameinseries" (dict "page" .page "scratch" $scratch) -}} - {{- else -}} - {{- $scratch.Set "name" (.page.Title) -}} + {{- $name = partial "nameinseries" (dict "page" .page) -}} {{- end -}} - {{ if .page.Params.favorite }}{{ partial "icon.html" "star" }}{{ end }} {{ $scratch.Get "name" }} + {{ if .page.Params.favorite }}{{ partial "icon.html" "star" }}{{ end }} {{ $name }} {{ if (not (eq .page.WordCount 0)) }}

    {{ i18n "nWords" .page.WordCount }}, {{ i18n "nMinutesToRead" .page.ReadingTime }}.

    {{ end }} diff --git a/layouts/_partials/serieslink.html b/layouts/_partials/serieslink.html index 87ee82f..40c0cf9 100644 --- a/layouts/_partials/serieslink.html +++ b/layouts/_partials/serieslink.html @@ -3,9 +3,7 @@ {{ if eq .direction "previous" }}{{ partial "icon.html" "chevrons-left" }}{{ end }}
    {{ title .direction }} in Series - {{ $scratch := newScratch -}} - {{- partial "nameinseries.html" (dict "page" .page "scratch" $scratch) -}} -
    {{ $scratch.Get "name" }}
    +
    {{ partial "nameinseries.html" (dict "page" .page) }}
    {{ if eq .direction "next" }}{{ partial "icon.html" "chevrons-right" }}{{ end }} diff --git a/layouts/_partials/uniquebyseries.html b/layouts/_partials/uniquebyseries.html index 4b53b96..e380bac 100644 --- a/layouts/_partials/uniquebyseries.html +++ b/layouts/_partials/uniquebyseries.html @@ -1,14 +1,14 @@ -{{- $scratch := .scratch -}} -{{- $scratch.Set "pages" slice -}} +{{- $pages := slice -}} {{- $tmpScratch := newScratch -}} {{- range $post := (where (where site.Pages.ByDate.Reverse "Section" "blog") ".Kind" "!=" "section") -}} {{- $term := index ($post.GetTerms "series") 0 -}} {{- if (and $term (not ($term.Param "donotunique"))) -}} {{- if not ($tmpScratch.Get $term.Permalink) -}} {{- $tmpScratch.Set $term.Permalink true -}} -{{- $scratch.Add "pages" $post -}} +{{- $pages = $pages | append $post -}} {{- end -}} {{- else -}} -{{- $scratch.Add "pages" $post -}} +{{- $pages = $pages | append $post -}} {{- end -}} {{- end -}} +{{- return $pages -}} diff --git a/layouts/_shortcodes/codeblock.html b/layouts/_shortcodes/codeblock.html index fc3731b..3a7d291 100644 --- a/layouts/_shortcodes/codeblock.html +++ b/layouts/_shortcodes/codeblock.html @@ -1,8 +1,7 @@ -{{- $scratch := newScratch -}} -{{- partial "geturl.html" (dict "scratch" $scratch "path" (.Get 1)) -}} +{{- $url := partial "geturl.html" (dict "path" (.Get 1)) -}} {{- $groupconfig := dict - "url" ($scratch.Get "bestUrl") - "basePath" ($scratch.Get "bestPath") + "url" $url.url + "basePath" $url.basePath "path" (.Get 1) "comment" ", entire file" "code" (readFile (printf "code/%s" (.Get 1))) diff --git a/layouts/_shortcodes/codelines.html b/layouts/_shortcodes/codelines.html index 15ba14c..e8a6a86 100644 --- a/layouts/_shortcodes/codelines.html +++ b/layouts/_shortcodes/codelines.html @@ -35,17 +35,17 @@ {{- $joinedLines = delimit $lines "\n" -}} {{- end -}} -{{- partial "geturl.html" (dict "scratch" $scratch "path" (.Get 1) "lines" (slice (.Get 2) (.Get 3))) -}} -{{- partial "linerangestr.html" (dict "scratch" $scratch "from" (.Get 2) "to" (.Get 3)) -}} +{{- $url := partial "geturl.html" (dict "path" (.Get 1) "lines" (slice (.Get 2) (.Get 3))) -}} +{{- $comment := partial "linerangestr.html" (dict "from" (.Get 2) "to" (.Get 3)) -}} {{- with ($scratch.Get "hidden") -}}
    {{- . | markdownify -}} {{- end -}} {{- $groupconfig := dict - "url" ($scratch.Get "bestUrl") - "basePath" ($scratch.Get "bestPath") + "url" $url.url + "basePath" $url.basePath "path" (.Get 1) - "comment" ($scratch.Get "comment") + "comment" $comment "code" $joinedLines "firstLine" (int (.Get 2)) "lastLine" (int (.Get 3)) diff --git a/layouts/_shortcodes/codeurl.html b/layouts/_shortcodes/codeurl.html index f97d5a5..835268a 100644 --- a/layouts/_shortcodes/codeurl.html +++ b/layouts/_shortcodes/codeurl.html @@ -1,3 +1,2 @@ -{{- $scratch := newScratch -}} -{{- partial "geturl.html" (dict "scratch" $scratch "path" (.Get 0)) -}} -{{- $scratch.Get "bestUrl" -}} +{{- $url := partial "geturl.html" (dict "path" (.Get 0)) -}} +{{- $url.url -}} diff --git a/layouts/_shortcodes/githubsnippet.html b/layouts/_shortcodes/githubsnippet.html index 6ffc901..80a5ccc 100644 --- a/layouts/_shortcodes/githubsnippet.html +++ b/layouts/_shortcodes/githubsnippet.html @@ -5,7 +5,6 @@ {{- $line := .Get 4 -}} {{- $lines := split (trim .Inner "\n") "\n" -}} -{{- $scratch := newScratch -}} {{- $url := printf "https://github.com/%s/blob/%s/%s#L%d" $repo $commit $file $line -}} -{{- partial "linerangestr.html" (dict "scratch" $scratch "from" 0 "to" $line) -}} -{{- partial "highlightgroup.html" (dict "url" $url "path" $file "comment" ($scratch.Get "comment") "code" (trim .Inner "\n") "language" $lang) -}} +{{- $comment := partial "linerangestr.html" (dict "from" 0 "to" $line) -}} +{{- partial "highlightgroup.html" (dict "url" $url "path" $file "comment" $comment "code" (trim .Inner "\n") "language" $lang) -}} diff --git a/layouts/_shortcodes/rawblock.html b/layouts/_shortcodes/rawblock.html index 2673b88..bda0515 100644 --- a/layouts/_shortcodes/rawblock.html +++ b/layouts/_shortcodes/rawblock.html @@ -1,8 +1,7 @@ -{{- $scratch := newScratch -}} -{{- partial "geturl.html" (dict "scratch" $scratch "path" (.Get 0) "siteSourceUrl" .Site.Params.siteSourceUrl "submoduleLinks" hugo.Data.submodules) -}} +{{- $url := partial "geturl.html" (dict "path" (.Get 0)) -}} {{- $groupconfig := dict - "url" ($scratch.Get "bestUrl") - "basePath" ($scratch.Get "bestPath") + "url" $url.url + "basePath" $url.basePath "path" (.Get 0) "comment" ", entire file" "content" (safeHTML (printf "
    %s
    " (htmlEscape (readFile (printf "code/%s" (.Get 0)))))) diff --git a/layouts/home.html b/layouts/home.html index a34db76..311e7f1 100644 --- a/layouts/home.html +++ b/layouts/home.html @@ -2,9 +2,9 @@ {{ .Content }} {{ i18n "recentPosts" }}: -{{ partial "uniquebyseries.html" (dict "scratch" .Store) }} +{{ $pages := partial "uniquebyseries.html" . }}
      - {{ range first 10 (.Store.Get "pages") }} + {{ range first 10 $pages }} {{ partial "post.html" (dict "page" . "linkSeries" true) }} {{ end }}