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

75 lines
2.8 KiB
HTML

{{- $config := .Attributes | merge (.Page.Param "codeblock") -}}
{{- $result := transform.HighlightCodeBlock . -}}
{{- $wrapper := $result.Wrapped -}}
{{- if $config.wrapper -}}
{{- $mode := $config.mode | default "classic" -}}
{{- $class := "highlight" -}}
{{- with $config.class -}}{{ $class = add $class " " . }}{{- end -}}
{{- $targetClass := add "code-block " $class -}}
{{- with $config.wrapperclass -}}{{ $targetClass = add $targetClass " " . }}{{- end -}}
{{- /* add code wrapper */ -}}
{{- $REin := cond (strings.Contains $wrapper `<table class="lntable">`)
`(<table class="lntable">[\s\S]*?<\/table>)`
`(<pre tabindex="0" class="chroma">[\s\S]*?<\/pre>)` -}}
{{- $REout := printf `<div class="code-wrapper">$1</div>` -}}
{{- $wrapper = replaceRE $REin $REout $wrapper -}}
{{- /* hide line numbers if linenos=false */ -}}
{{- if not (strings.Contains $wrapper `class="lnt"`) | and (not (strings.Contains $wrapper `class="ln"`)) -}}
{{- $targetClass = add $targetClass " line-nos-hidden" -}}
{{- end -}}
{{- /* add code expand button */ -}}
{{- $lines := cond .Inner (len (split .Inner "\n")) 0 -}}
{{- $maxShownLines := $config.maxshownlines -}}
{{- if (gt $lines $maxShownLines) | and (gt $lines 0) | and (gt $maxShownLines 0) -}}
{{- $wrapper = dict "Wrapper" $wrapper | partial "function/code-expand-btn.html" -}}
{{- end -}}
{{- /* render for different modes */ -}}
{{- if eq $mode "classic" -}}
{{- $wrapper = dict "Wrapper" $wrapper "Config" $config "Type" .Type | partial "function/code-header.html" -}}
{{- else -}}
{{- $wrapper = dict "Wrapper" $wrapper "Config" $config | partial "function/code-copy-btn.html" -}}
{{- end -}}
{{- /* add data attributes and class */ -}}
{{- if $config.editable -}}
{{- $wrapper =
replace $wrapper
(printf `class="%s"` $class)
(printf `class="%s" data-editable="%v"` $class $config.editable)
-}}
{{- end -}}
{{- if $config.copyable -}}
{{- $wrapper =
replace $wrapper
(printf `class="%s"` $class)
(printf `class="%s" data-copyable="%v"` $class $config.copyable)
-}}
{{- end -}}
{{- if ne .Options.linenostart nil -}}
{{- $wrapper =
replace $wrapper
(printf `class="%s"` $class)
(printf `class="%s" data-linenostart="%v"` $class .Options.linenostart)
-}}
{{- end -}}
{{- $wrapper =
replace $wrapper
(printf `class="%s"` $class)
(printf `class="%s" data-mode="%s" data-max="%v" data-lines="%d"` $targetClass $mode $maxShownLines $lines)
-}}
{{- /* remove redundant attributes */ -}}
{{- $configKeys := slice -}}
{{- range $key, $_ := .Page.Param "codeblock" -}}
{{- $configKeys = $configKeys | append $key -}}
{{- end -}}
{{- $wrapper = replaceRE (printf `\s+(?:%s|name)="[^"]*"` (delimit $configKeys "|")) "" $wrapper -}}
{{- end -}}
{{- $wrapper | safeHTML -}}