{{ $raw := .Get "path" }} {{ $tag := .Get "tag" | default (.Get "anchor") }} {{ if not $raw }}
Missing param: path
{{ else if not $tag }}Missing param: tag
{{ else }} {{ $lang := .Get "lang" | default "kotlin" }} {{ $count_param := .Get "count" | default "0" }} {{ $rel := $raw }} {{ $file := resources.Get (printf "%s" $rel) }} {{ if not $file }}File not found: {{ $rel }}
{{ else }} {{/* Read file content and split into lines */}} {{ $content := $file.Content }} {{ $lines := split $content "\n" }} {{/* 1) Find marker line by *substring* "tag::Tag not found: {{ $tag }} in {{ $rel }}
{{ else }} {{/* 2) Extract [LINES] by hard slicing around the first '[' and ']' */}} {{ $count := 10 }} {{ if ne $count_param "0" }} {{ $count = int $count_param }} {{ else }} {{ $hasBrack := in $markerLine "[" }} {{ if $hasBrack }} {{ $after := index (split $markerLine "[") 1 | default "" }} {{ if ne $after "" }} {{ $inside := index (split $after "]") 0 | default "" }} {{/* guard: ensure it is digits only (avoid int conversion error) */}} {{ $digits := findRE `^\s*\d+\s*$` $inside }} {{ if gt (len $digits) 0 }} {{ $count = int (trim $inside " \t") }} {{ end }} {{ end }} {{ end }} {{ end }} {{/* 3) Compute slice (start after tag line) and clamp end */}} {{ $start := add $idx 1 }} {{ $end := add $start (sub $count 1) }} {{ if lt $start 1 }}{{ $start = 1 }}{{ end }} {{ if gt $end (len $lines) }}{{ $end = (len $lines) }}{{ end }} {{/* 4) Build the requested slice */}} {{ $slice := (slice) }} {{ $s2 := newScratch }}{{ $s2.Set "sub" $slice }} {{ range $i, $l := $lines }} {{ $ln := add $i 1 }} {{ if and (ge $ln $start) (le $ln $end) }} {{ $s2.Set "sub" ( $s2.Get "sub" | append $l ) }} {{ end }} {{ end }} {{/* 5) Dedent: remove common leading whitespace for nicer rendering */}} {{ $block := $s2.Get "sub" }} {{ $scr := newScratch }}{{ $scr.Set "indent" 9999 }} {{ range $line := $block }} {{ $trimmed := strings.TrimSpace $line }} {{ if ne $trimmed "" }} {{ $lead := replaceRE "^([ \\t]+).*" "$1" $line }} {{ $n := len $lead }} {{ if lt $n ($scr.Get "indent") }}{{ $scr.Set "indent" $n }}{{ end }} {{ end }} {{ end }} {{ $common := $scr.Get "indent" }}{{ if eq $common 9999 }}{{ $common = 0 }}{{ end }} {{ $ded := newScratch }}{{ $ded.Set "out" (slice) }} {{ $pat := printf "^[ \\t]{0,%d}" $common }} {{ range $line := $block }} {{ $ded.Set "out" ( $ded.Get "out" | append (replaceRE $pat "" $line) ) }} {{ end }} {{ $code := delimit ($ded.Get "out") "\n" }} {{/* 6) Render code */}} {{ highlight $code $lang "noClasses=true" }} {{/* 7) GitHub deep-link (branch 'main'); keep your styles */}} {{ $base := site.Params.github_repo }} {{ if $base }}{{ end }} {{ end }} {{ end }} {{ end }}