Files
FixIt/layouts/_markup/render-codeblock-toggle.html
T

65 lines
2.8 KiB
HTML

{{- $parsed := dict -}}
{{- $ok := false -}}
{{- with try (transform.Unmarshal .Inner) -}}
{{- with .Err -}}
{{- warnf "Toggle code block: unable to parse content at %s" $.Position -}}
{{- else with .Value -}}
{{- $parsed = . -}}
{{- $ok = true -}}
{{- end -}}
{{- end -}}
{{- if $ok -}}
{{- $groupId := printf "tab-%s" (md5 (dict "Page" .Page | partial "function/id.html")) -}}
{{- $activeFormat := "" -}}
{{- $raw := strings.TrimSpace .Inner -}}
{{- range $candidate := slice "json" "toml" "yaml" -}}
{{- if not $activeFormat -}}
{{- with try (transform.Unmarshal (dict "format" $candidate) $raw) -}}
{{- if not .Err -}}
{{- $activeFormat = $candidate -}}
{{- end -}}
{{- end -}}
{{- end -}}
{{- end -}}
{{- $commonAttrsStr := printf ", group=%q" $groupId -}}
{{- if ne .Attributes.copyable nil -}}
{{- $commonAttrsStr = printf "%s, copyable=%v" $commonAttrsStr .Attributes.copyable -}}
{{- end -}}
{{- if ne .Attributes.downloadable nil -}}
{{- $commonAttrsStr = printf "%s, downloadable=%v" $commonAttrsStr .Attributes.downloadable -}}
{{- end -}}
{{- if ne .Attributes.fullscreen nil -}}
{{- $commonAttrsStr = printf "%s, fullscreen=%v" $commonAttrsStr .Attributes.fullscreen -}}
{{- end -}}
{{- if ne .Attributes.linenostoggler nil -}}
{{- $commonAttrsStr = printf "%s, lineNosToggler=%v" $commonAttrsStr .Attributes.linenostoggler -}}
{{- end -}}
{{- if ne .Attributes.linewraptoggler nil -}}
{{- $commonAttrsStr = printf "%s, lineWrapToggler=%v" $commonAttrsStr .Attributes.linewraptoggler -}}
{{- end -}}
{{- if ne .Attributes.editable nil -}}
{{- $commonAttrsStr = printf "%s, editable=%v" $commonAttrsStr .Attributes.editable -}}
{{- end -}}
{{- with .Attributes.before_tabs -}}
{{- $commonAttrsStr = printf "%s, before_tabs=%q" $commonAttrsStr . -}}
{{- end -}}
{{- $renderText := "" -}}
{{- range $format := slice "toml" "yaml" "json" -}}
{{- $content := transform.Remarshal $format $parsed -}}
{{- if eq $format "toml" -}}
{{- $content = replaceRE `(?m)^[\t ]+` "" $content -}}
{{- else if eq $format "json" -}}
{{- $content = $parsed | jsonify (dict "indent" " ") -}}
{{- end -}}
{{- $active := cond (eq $format $activeFormat) ", .active" "" -}}
{{- $jsonViewer := cond (eq $format "json") ", enable=false" "" -}}
{{- $labelName := cond (ne $.Attributes.before_tabs nil) $format (upper $format) -}}
{{- $block := printf "```%s {data-code-toggle=true, name=%q%s%s%s}\n%s\n```\n\n" $format $labelName $active $jsonViewer $commonAttrsStr $content -}}
{{- $renderText = add $renderText $block -}}
{{- end -}}
{{- $renderText | .Page.RenderString -}}
{{- else -}}
{{- partial "plugin/code-block-wrapper.html" . -}}
{{- end -}}