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

{{- $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") -}}
<details><summary>{{- . | markdownify -}}</summary>
{{- end -}}
{{- $groupconfig := dict
       "url" $url.url
       "basePath" $url.basePath
       "path" (.Get 1)
       "comment" $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 -}}
