{{- $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 */ -}} {{- $wrapper = partial "function/code-wrapper.html" $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 -}}