vanilla-hugo/layouts/shortcodes/codelines.html
Danila Fedorin b06b695653 Use mutable variables instead of scratch where possible
Signed-off-by: Danila Fedorin <danila.fedorin@gmail.com>
2025-03-30 22:45:24 -07:00

60 lines
1.9 KiB
HTML

{{- $source := (readFile (printf "code/%s" (.Get 1))) -}}
{{- $allLines := split $source "\n" -}}
{{- $scratch := newScratch -}}
{{- $remLines := $allLines -}}
{{- if not (eq (int (.Get 2)) 1) -}}
{{- $remLines = after (sub (int (.Get 2)) 1) $allLines -}}
{{- end -}}
{{- $lines := first (add (sub (int (.Get 3)) (int (.Get 2))) 1) $remLines -}}
{{- $opts := "" -}}
{{- if (.Get 4) -}}
{{- $opts = printf ",%s" (.Get 4) -}}
{{- end -}}
{{- if (.Get 5) -}}
{{- $scratch.Set "hidden" (.Get 5) -}}
{{- end -}}
{{- $prefixLength := "" -}}
{{- $joinedLines := "" -}}
{{- if or (.Page.Params.left_align_code) (.Get 6) -}}
{{- $prefixLength = -1 -}}
{{- range $line := $lines -}}
{{- $leading := sub (len $line) (len (strings.TrimLeft " " $line)) -}}
{{- if and (ne $line "") (or (eq $prefixLength -1) (le $leading $prefixLength)) -}}
{{- $prefixLength = $leading -}}
{{- end -}}
{{- end -}}
{{- range $line := $lines -}}
{{- $joinedLines = add $joinedLines (substr $line $prefixLength) -}}
{{- $joinedLines = add $joinedLines "\n" -}}
{{- end -}}
{{- else -}}
{{- $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)) -}}
{{- with ($scratch.Get "hidden") -}}
<details><summary>{{- . | markdownify -}}</summary>
{{- end -}}
{{- $groupconfig := dict
"url" ($scratch.Get "bestUrl")
"basePath" ($scratch.Get "bestPath")
"path" (.Get 1)
"comment" ($scratch.Get "comment")
"code" $joinedLines
"firstLine" (int (.Get 2))
"lastLine" (int (.Get 3))
"language" (.Get 0)
"offset" $prefixLength
"opts" (printf "linenos=table,linenostart=%d%s" (.Get 2) $opts)
-}}
{{- partial "highlightgroup.html" $groupconfig -}}
{{- with ($scratch.Get "hidden") -}}
</details>
{{- end -}}