{{- $name := .Get "name" | default "" -}} {{- $basePath := .Site.Params.docs.basePath -}} {{- $file := .Get "file" | default "" -}} {{- if hasPrefix $file "./" -}} {{- $file = path.Clean $file -}} {{- else -}} {{- $file = path.Join $basePath (path.Clean $file) -}} {{- end -}} {{- $extension := strings.TrimLeft "." (path.Ext $file) }} {{- $capture_start := "" -}} {{- $capture_end := "" -}} {{- $id := printf "docs-collapse-%d" .Ordinal -}} {{ with .Get "id" }} {{ $id = . }} {{ end }} {{- $supportedExtensions := slice "js" "scss" "toml" -}} {{- if in $supportedExtensions $extension -}} {{- if eq $extension "toml" }} {{- $capture_start = printf "# toml-docs-start %s" $name -}} {{- $capture_end = printf "# toml-docs-end %s" $name -}} {{- else -}} {{- $capture_start = printf "// %s-docs-start %s" $extension $name -}} {{- $capture_end = printf "// %s-docs-end %s" $extension $name -}} {{- end -}} {{- else -}} {{- errorf "File format not supported (line %s): %s" .Position $file -}} {{- end -}} {{ $supportedFlags := slice "true" "false" -}} {{ $showParam := "true" -}} {{ $show := true -}} {{ with .Get "show" }}{{ $showParam = . }}{{ end -}} {{ if in $supportedFlags $showParam -}} {{ if eq $showParam "true" }}{{ $show = true }}{{ else }}{{ $show = false }}{{ end -}} {{ else -}} {{ errorf "Invalid value for param 'show': %s" $showParam -}} {{ end -}} {{ $fullParam := "true" -}} {{ $full := true -}} {{ with .Get "full" }}{{ $fullParam = . }}{{ end -}} {{ if in $supportedFlags $fullParam -}} {{ if eq $fullParam "true" }}{{ $full = true }}{{ else }}{{ $full = false }}{{ end -}} {{ else -}} {{ errorf "Invalid value for param 'full': %s" $fullParam -}} {{ end -}} {{- $class := .Get "class" | default "" -}} {{- /* If any parameters are missing, print an error and exit */ -}} {{- if or (not $name) (not $file) -}} {{- errorf "%s: %q: Missing required parameters! Got: name=%q file=%q!" .Position .Name $name $file -}} {{- else -}} {{- /* Force-check if the file exists */ -}} {{ $tmp := os.Stat $file }} {{- $regex := printf `%s((?:.|\n)*)%s` $capture_start $capture_end -}} {{- $match := findRE $regex (readFile $file) -}} {{- $match = index $match 0 -}} {{- if not $match -}} {{- errorf "%s: %q: Got no matches for name=%q in file=%q!" .Position .Name $name $file -}} {{- end -}} {{- $match = replace $match $capture_start "" -}} {{- $match = replace $match $capture_end "" -}}
{{- highlight (trim $match "\r\n") $extension "" -}}
{{- end -}}