{{ $raw := .Get "path" }} {{ if not $raw }}
Missing param: path
{{ else }} {{ $rel := $raw }} {{ $lang := .Get "lang" | default "kotlin" }} {{ $file := resources.Get (printf "%s" $rel) }} {{ if not $file }}File not found: {{ $rel }}
{{ else }} {{/* Compute slice range */}} {{ $lines := split $file.Content "\n" }} {{ $start := 1 }} {{ with .Get "start" }}{{ $start = int . }}{{ end }} {{ $end := len $lines }} {{ with .Get "end" }}{{ $end = int . }}{{ end }} {{ if lt $start 1 }}{{ $start = 1 }}{{ end }} {{ if gt $end (len $lines) }}{{ $end = (len $lines) }}{{ end }} {{ if lt $end $start }}{{ $end = $start }}{{ end }} {{/* Slice to the requested lines */}} {{ $s := newScratch }}{{ $s.Set "sub" (slice) }} {{ range $i, $l := $lines }} {{ $ln := add $i 1 }} {{ if and (ge $ln $start) (le $ln $end) }} {{ $s.Set "sub" ( $s.Get "sub" | append $l ) }} {{ end }} {{ end }} {{/* ===== Dedent: remove common leading whitespace ===== */}} {{ $block := $s.Get "sub" }} {{/* slice of selected lines */}} {{ $indent := 1000000 }} {{ range $line := $block }} {{ $trimmed := strings.TrimSpace $line }} {{ if ne $trimmed "" }} {{ $m := findRE "^([ \\t]+)" $line }} {{ $n := 0 }} {{ if gt (len $m) 0 }} {{ $n = len (index $m 0) }} {{ end }} {{ if lt $n $indent }} {{ $indent = $n }} {{ end }} {{ end }} {{ end }} {{ if eq $indent 1000000 }}{{ $indent = 0 }}{{ end }} {{ $ded := newScratch }}{{ $ded.Set "out" (slice) }} {{ $pat := printf "^[ \\t]{0,%d}" $indent }} {{ range $line := $block }} {{ $ded.Set "out" ( $ded.Get "out" | append (replaceRE $pat "" $line) ) }} {{ end }} {{ $code := delimit ($ded.Get "out") "\n" }} {{/* ===== end Dedent ===== */}} {{ highlight $code $lang "noClasses=true" | safeHTML }} {{/* Deep-link to GitHub line range (same as before) */}} {{ $base := site.Params.github_repo }} {{ if $base }}{{ end }} {{ end }} {{ end }}