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 -}}
{{- $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) -}}

View File

@@ -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 -}}

View File

@@ -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 -}}

View File

@@ -1,11 +1,9 @@
<li>
{{- $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 -}}
<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)) }}
<p class="post-wordcount">{{ i18n "nWords" .page.WordCount }}, {{ i18n "nMinutesToRead" .page.ReadingTime }}.</p>
{{ end }}

View File

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

View File

@@ -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 -}}