Use Hugo's partial-returning where possible

Signed-off-by: Danila Fedorin <danila.fedorin@gmail.com>
This commit is contained in:
2026-05-18 21:15:19 -07:00
parent 2032e1cdc2
commit 19550ef1b2
12 changed files with 46 additions and 55 deletions

View File

@@ -1,27 +1,26 @@
{{- $siteSourceUrl := site.Params.siteSourceUrl -}} {{- $siteSourceUrl := site.Params.siteSourceUrl -}}
{{- $submoduleLinks := hugo.Data.submodules -}} {{- $submoduleLinks := hugo.Data.submodules -}}
{{- $hostData := hugo.Data.hosts -}} {{- $hostData := hugo.Data.hosts -}}
{{- $scratch := .scratch -}}
{{- $lines := .lines -}} {{- $lines := .lines -}}
{{- $scratch.Set "bestLength" -1 -}} {{- $bestLength := -1 -}}
{{- $scratch.Set "bestUrl" (printf "%s/code/%s" $siteSourceUrl .path) -}} {{- $bestUrl := printf "%s/code/%s" $siteSourceUrl .path -}}
{{- $scratch.Set "bestPath" "" -}} {{- $bestPath := "" -}}
{{- $filePath := .path -}} {{- $filePath := .path -}}
{{- range $path, $url := $submoduleLinks -}} {{- range $path, $url := $submoduleLinks -}}
{{- $bestLength := $scratch.Get "bestLength" -}}
{{- if and (le $bestLength (len $path)) (hasPrefix $filePath $path) -}} {{- if and (le $bestLength (len $path)) (hasPrefix $filePath $path) -}}
{{- $scratch.Set "bestLength" (len $path) -}} {{- $bestLength = len $path -}}
{{- $scratch.Set "bestPath" $path -}} {{- $bestPath = $path -}}
{{- $scratch.Set "bestUrl" (printf "%s%s" $url (strings.TrimPrefix $path $filePath)) -}} {{- $bestUrl = printf "%s%s" $url (strings.TrimPrefix $path $filePath) -}}
{{- end -}} {{- end -}}
{{- end -}} {{- end -}}
{{- if .lines -}} {{- if .lines -}}
{{- /* If the user provided a line range, see if we can tack it on to the end of the link. */ -}} {{- /* If the user provided a line range, see if we can tack it on to the end of the link. */ -}}
{{- range $host, $data := $hostData -}} {{- range $host, $data := $hostData -}}
{{- if hasPrefix ($scratch.Get "bestUrl") $host -}} {{- if hasPrefix $bestUrl $host -}}
{{- with $data.linesSuffix -}} {{- 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 -}}
{{- end -}} {{- end -}}
{{- end -}} {{- end -}}
{{- return (dict "url" $bestUrl "basePath" $bestPath) -}}

View File

@@ -1,9 +1,9 @@
{{ if eq .from .to }} {{- $comment := "" -}}
{{ .scratch.Set "comment" (printf ", line %d" .from) }} {{- if eq .from .to -}}
{{ else }} {{- $comment = printf ", line %d" .from -}}
{{ if eq .from 0 }} {{- else if eq .from 0 -}}
{{ .scratch.Set "comment" (printf ", around line %d" .to) }} {{- $comment = printf ", around line %d" .to -}}
{{ else }} {{- else -}}
{{ .scratch.Set "comment" (printf ", lines %d through %d" .from .to) }} {{- $comment = printf ", lines %d through %d" .from .to -}}
{{ end }} {{- end -}}
{{ end }} {{- return $comment -}}

View File

@@ -1,8 +1,8 @@
{{- $term := index (.page.GetTerms "series") 0 -}} {{- $term := index (.page.GetTerms "series") 0 -}}
{{- $divider := $term.Params.divider -}} {{- $divider := $term.Params.divider -}}
{{- $name := .page.Title -}}
{{- if (and $divider (strings.Contains .page.Title $divider)) -}} {{- if (and $divider (strings.Contains .page.Title $divider)) -}}
{{- $rest := after 1 (split .page.Title $divider) -}} {{- $rest := after 1 (split .page.Title $divider) -}}
{{- .scratch.Set "name" (delimit $rest $divider) -}} {{- $name = delimit $rest $divider -}}
{{- else -}}
{{- .scratch.Set "name" .page.Title -}}
{{- end -}} {{- end -}}
{{- return $name -}}

View File

@@ -1,11 +1,9 @@
<li> <li>
{{- $scratch := newScratch -}} {{- $name := .page.Title -}}
{{- if .seriesName -}} {{- if .seriesName -}}
{{- partial "nameinseries" (dict "page" .page "scratch" $scratch) -}} {{- $name = partial "nameinseries" (dict "page" .page) -}}
{{- else -}}
{{- $scratch.Set "name" (.page.Title) -}}
{{- end -}} {{- end -}}
<a href="{{ .page.Permalink }}" class="post-title">{{ if .page.Params.favorite }}{{ partial "icon.html" "star" }}{{ end }} {{ $scratch.Get "name" }}</a> <a href="{{ .page.Permalink }}" class="post-title">{{ if .page.Params.favorite }}{{ partial "icon.html" "star" }}{{ end }} {{ $name }}</a>
{{ if (not (eq .page.WordCount 0)) }} {{ if (not (eq .page.WordCount 0)) }}
<p class="post-wordcount">{{ i18n "nWords" .page.WordCount }}, {{ i18n "nMinutesToRead" .page.ReadingTime }}.</p> <p class="post-wordcount">{{ i18n "nWords" .page.WordCount }}, {{ i18n "nMinutesToRead" .page.ReadingTime }}.</p>
{{ end }} {{ end }}

View File

@@ -3,9 +3,7 @@
{{ if eq .direction "previous" }}{{ partial "icon.html" "chevrons-left" }}{{ end }} {{ if eq .direction "previous" }}{{ partial "icon.html" "chevrons-left" }}{{ end }}
<div class="title-subtitle"> <div class="title-subtitle">
{{ title .direction }} in Series {{ title .direction }} in Series
{{ $scratch := newScratch -}} <div class="title">{{ partial "nameinseries.html" (dict "page" .page) }}</div>
{{- partial "nameinseries.html" (dict "page" .page "scratch" $scratch) -}}
<div class="title">{{ $scratch.Get "name" }}</div>
</div> </div>
{{ if eq .direction "next" }}{{ partial "icon.html" "chevrons-right" }}{{ end }} {{ if eq .direction "next" }}{{ partial "icon.html" "chevrons-right" }}{{ end }}
</a> </a>

View File

@@ -1,14 +1,14 @@
{{- $scratch := .scratch -}} {{- $pages := slice -}}
{{- $scratch.Set "pages" slice -}}
{{- $tmpScratch := newScratch -}} {{- $tmpScratch := newScratch -}}
{{- range $post := (where (where site.Pages.ByDate.Reverse "Section" "blog") ".Kind" "!=" "section") -}} {{- range $post := (where (where site.Pages.ByDate.Reverse "Section" "blog") ".Kind" "!=" "section") -}}
{{- $term := index ($post.GetTerms "series") 0 -}} {{- $term := index ($post.GetTerms "series") 0 -}}
{{- if (and $term (not ($term.Param "donotunique"))) -}} {{- if (and $term (not ($term.Param "donotunique"))) -}}
{{- if not ($tmpScratch.Get $term.Permalink) -}} {{- if not ($tmpScratch.Get $term.Permalink) -}}
{{- $tmpScratch.Set $term.Permalink true -}} {{- $tmpScratch.Set $term.Permalink true -}}
{{- $scratch.Add "pages" $post -}} {{- $pages = $pages | append $post -}}
{{- end -}} {{- end -}}
{{- else -}} {{- else -}}
{{- $scratch.Add "pages" $post -}} {{- $pages = $pages | append $post -}}
{{- end -}} {{- end -}}
{{- end -}} {{- end -}}
{{- return $pages -}}

View File

@@ -1,8 +1,7 @@
{{- $scratch := newScratch -}} {{- $url := partial "geturl.html" (dict "path" (.Get 1)) -}}
{{- partial "geturl.html" (dict "scratch" $scratch "path" (.Get 1)) -}}
{{- $groupconfig := dict {{- $groupconfig := dict
"url" ($scratch.Get "bestUrl") "url" $url.url
"basePath" ($scratch.Get "bestPath") "basePath" $url.basePath
"path" (.Get 1) "path" (.Get 1)
"comment" ", entire file" "comment" ", entire file"
"code" (readFile (printf "code/%s" (.Get 1))) "code" (readFile (printf "code/%s" (.Get 1)))

View File

@@ -35,17 +35,17 @@
{{- $joinedLines = delimit $lines "\n" -}} {{- $joinedLines = delimit $lines "\n" -}}
{{- end -}} {{- end -}}
{{- partial "geturl.html" (dict "scratch" $scratch "path" (.Get 1) "lines" (slice (.Get 2) (.Get 3))) -}} {{- $url := partial "geturl.html" (dict "path" (.Get 1) "lines" (slice (.Get 2) (.Get 3))) -}}
{{- partial "linerangestr.html" (dict "scratch" $scratch "from" (.Get 2) "to" (.Get 3)) -}} {{- $comment := partial "linerangestr.html" (dict "from" (.Get 2) "to" (.Get 3)) -}}
{{- with ($scratch.Get "hidden") -}} {{- with ($scratch.Get "hidden") -}}
<details><summary>{{- . | markdownify -}}</summary> <details><summary>{{- . | markdownify -}}</summary>
{{- end -}} {{- end -}}
{{- $groupconfig := dict {{- $groupconfig := dict
"url" ($scratch.Get "bestUrl") "url" $url.url
"basePath" ($scratch.Get "bestPath") "basePath" $url.basePath
"path" (.Get 1) "path" (.Get 1)
"comment" ($scratch.Get "comment") "comment" $comment
"code" $joinedLines "code" $joinedLines
"firstLine" (int (.Get 2)) "firstLine" (int (.Get 2))
"lastLine" (int (.Get 3)) "lastLine" (int (.Get 3))

View File

@@ -1,3 +1,2 @@
{{- $scratch := newScratch -}} {{- $url := partial "geturl.html" (dict "path" (.Get 0)) -}}
{{- partial "geturl.html" (dict "scratch" $scratch "path" (.Get 0)) -}} {{- $url.url -}}
{{- $scratch.Get "bestUrl" -}}

View File

@@ -5,7 +5,6 @@
{{- $line := .Get 4 -}} {{- $line := .Get 4 -}}
{{- $lines := split (trim .Inner "\n") "\n" -}} {{- $lines := split (trim .Inner "\n") "\n" -}}
{{- $scratch := newScratch -}}
{{- $url := printf "https://github.com/%s/blob/%s/%s#L%d" $repo $commit $file $line -}} {{- $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) -}} {{- $comment := partial "linerangestr.html" (dict "from" 0 "to" $line) -}}
{{- partial "highlightgroup.html" (dict "url" $url "path" $file "comment" ($scratch.Get "comment") "code" (trim .Inner "\n") "language" $lang) -}} {{- partial "highlightgroup.html" (dict "url" $url "path" $file "comment" $comment "code" (trim .Inner "\n") "language" $lang) -}}

View File

@@ -1,8 +1,7 @@
{{- $scratch := newScratch -}} {{- $url := partial "geturl.html" (dict "path" (.Get 0)) -}}
{{- partial "geturl.html" (dict "scratch" $scratch "path" (.Get 0) "siteSourceUrl" .Site.Params.siteSourceUrl "submoduleLinks" hugo.Data.submodules) -}}
{{- $groupconfig := dict {{- $groupconfig := dict
"url" ($scratch.Get "bestUrl") "url" $url.url
"basePath" ($scratch.Get "bestPath") "basePath" $url.basePath
"path" (.Get 0) "path" (.Get 0)
"comment" ", entire file" "comment" ", entire file"
"content" (safeHTML (printf "<pre><code>%s</code></pre>" (htmlEscape (readFile (printf "code/%s" (.Get 0)))))) "content" (safeHTML (printf "<pre><code>%s</code></pre>" (htmlEscape (readFile (printf "code/%s" (.Get 0))))))

View File

@@ -2,9 +2,9 @@
{{ .Content }} {{ .Content }}
{{ i18n "recentPosts" }}: {{ i18n "recentPosts" }}:
{{ partial "uniquebyseries.html" (dict "scratch" .Store) }} {{ $pages := partial "uniquebyseries.html" . }}
<ul class="post-list"> <ul class="post-list">
{{ range first 10 (.Store.Get "pages") }} {{ range first 10 $pages }}
{{ partial "post.html" (dict "page" . "linkSeries" true) }} {{ partial "post.html" (dict "page" . "linkSeries" true) }}
{{ end }} {{ end }}
</ul> </ul>