From abb7307a311f04de5e97d0fda6cafa49c0d9ddf7 Mon Sep 17 00:00:00 2001 From: Andreas Deininger Date: Thu, 13 Oct 2022 12:30:22 +0200 Subject: [PATCH] Shortcodes for tabbed panes: improved whitespace handling --- layouts/shortcodes/tab.html | 96 +++++++------- layouts/shortcodes/tabpane.html | 217 ++++++++++++++++---------------- 2 files changed, 155 insertions(+), 158 deletions(-) diff --git a/layouts/shortcodes/tab.html b/layouts/shortcodes/tab.html index c947ac48c0..8ec7e09d5e 100644 --- a/layouts/shortcodes/tab.html +++ b/layouts/shortcodes/tab.html @@ -1,52 +1,52 @@ - -{{ with $.Parent }} - {{- if ne $.Parent.Name "tabpane" -}} - {{- errorf "shortcode 'tab' must be used within a 'tabpane' block" -}} - {{- end -}} -{{- end -}} +{{- /* Make sure that we are enclosed within a tabpane shortcode block */ -}} +{{ with $.Parent -}} +{{ if ne $.Parent.Name "tabpane" -}} + {{ errorf "shortcode 'tab' must be used within a 'tabpane' block" -}} + {{ end -}} +{{ end -}} -{{ $header := "Tab" }} -{{ if and (not .IsNamedParams) (.Get 0) }} - {{ $header = (.Get 0) }} -{{ else }} - - {{ $header = default (printf "Tab %v" ( add $.Ordinal 1)) (.Get "header") }} -{{ end }} +{{ $header := "Tab" -}} +{{ if and (not .IsNamedParams) (.Get 0) -}} + {{ $header = (.Get 0) -}} +{{ else -}} + {{/* Prefill header if not given as named or unnamed parameter */ -}} + {{ $header = default (printf "Tab %v" ( add $.Ordinal 1)) (.Get "header") -}} +{{ end -}} - -{{ $tab := dict "header" $header }} -{{ with $.Get "lang" }} - {{ $tab = merge $tab (dict "language" ($.Get "lang")) }} -{{ end }} -{{ with $.Get "highlight" }} - {{ $tab = merge $tab (dict "highlight" ($.Get "highlight")) }} -{{ end }} -{{ with $.Get "text" }} - {{ if ne ( printf "%T" . ) "bool" }} - {{- errorf "shortcode tab: parameter 'text' must be either true or false" -}} - {{ end }} - {{ $tab = merge $tab (dict "text" ($.Get "text")) }} -{{ end }} -{{ with $.Get "right" }} - {{ if ne ( printf "%T" . ) "bool" }} - {{- errorf "shortcode tab: parameter 'right' must be either true or false" -}} - {{ end }} - {{ $tab = merge $tab (dict "rightpush" ($.Get "right")) }} -{{ end }} -{{ with $.Get "disabled" }} - {{ if ne ( printf "%T" . ) "bool" }} - {{- errorf "shortcode tab: parameter 'disabled' must be either true or false" -}} - {{ end }} - {{ $tab = merge $tab (dict "disabled" ($.Get "disabled")) }} -{{ end }} +{{/* store all tab info in dict tab */ -}} +{{ $tab := dict "header" $header -}} +{{ with $.Get "lang" -}} + {{ $tab = merge $tab (dict "language" ($.Get "lang")) -}} +{{ end -}} +{{ with $.Get "highlight" -}} + {{ $tab = merge $tab (dict "highlight" ($.Get "highlight")) -}} +{{ end -}} +{{ with $.Get "text" -}} + {{ if ne ( printf "%T" . ) "bool" -}} + {{ errorf "shortcode tab: parameter 'text' must be either true or false" -}} + {{ end -}} + {{ $tab = merge $tab (dict "text" ($.Get "text")) -}} +{{ end -}} +{{ with $.Get "right" -}} + {{ if ne ( printf "%T" . ) "bool" -}} + {{ errorf "shortcode tab: parameter 'right' must be either true or false" -}} + {{ end -}} + {{ $tab = merge $tab (dict "rightpush" ($.Get "right")) -}} +{{ end -}} +{{ with $.Get "disabled" -}} + {{ if ne ( printf "%T" . ) "bool" -}} + {{ errorf "shortcode tab: parameter 'disabled' must be either true or false" -}} + {{ end -}} + {{ $tab = merge $tab (dict "disabled" ($.Get "disabled")) -}} +{{ end -}} -{{ with $.Inner }} - - {{ $tab = merge $tab (dict "content" $.Inner ) }} -{{ end }} +{{ with $.Inner -}} + {{/* Trim any leading and trailing newlines from .Inner, this avoids + spurious lines during syntax highlighting */ -}} + {{ $tab = merge $tab (dict "content" $.Inner) -}} +{{ end -}} - -{{ with .Parent }} - {{- $.Parent.Scratch.SetInMap "tabs" (printf "%02v" $.Ordinal) $tab -}} -{{ end }} +{{/* add dict tab to parent's scratchpad */ -}} +{{ with .Parent -}} + {{ $.Parent.Scratch.SetInMap "tabs" (printf "%02v" $.Ordinal) $tab -}} +{{ end -}} diff --git a/layouts/shortcodes/tabpane.html b/layouts/shortcodes/tabpane.html index 4836f62675..a25aab1e32 100644 --- a/layouts/shortcodes/tabpane.html +++ b/layouts/shortcodes/tabpane.html @@ -1,130 +1,127 @@ - -{{ with .Get "langEqualsHeader" }} -{{ if ne ( printf "%T" . ) "bool" }} -{{- errorf "shortcode tabpane: parameter 'langEqualsHeader' must be either true or false" -}} -{{ end }} -{{ end }} - -{{ with .Get "text" }} -{{ if ne ( printf "%T" . ) "bool" }} -{{- errorf "shortcode tabpane: parameter 'text' must be either true or false" -}} -{{ end }} -{{ end }} - -{{ with .Get "persistLang" }} -{{ if ne ( printf "%T" . ) "bool" }} -{{- errorf "shortcode tabpane: parameter 'persistLang' must be either true or false" -}} -{{ end }} -{{ end }} - -{{ with .Get "right" }} -{{ if ne ( printf "%T" . ) "bool" }} -{{- errorf "shortcode tabpane: parameter 'right' must be either true or false" -}} -{{ end }} -{{ end }} - - -{{- $langPane := default "" ($.Get "lang") -}} -{{- $hloptionsPane := default "" ($.Get "highlight") -}} -{{- $textPane := default false ($.Get "text") -}} -{{- $langEqualsHeader := default false ($.Get "langEqualsHeader") -}} -{{- $persistLang := default true ($.Get "persistLang") -}} -{{- $rightPane := default false ($.Get "right") -}} -{{- $disabled := false -}} -{{- $rightpush := false -}} -{{- $activeSet := false -}} - - +{{/* Check parameter types */ -}} +{{ with .Get "langEqualsHeader" -}} +{{ if ne ( printf "%T" . ) "bool" -}} +{{ errorf "shortcode tabpane: parameter 'langEqualsHeader' must be either true or false" -}} +{{ end -}} +{{ end -}} + +{{ with .Get "text" -}} +{{ if ne ( printf "%T" . ) "bool" -}} +{{ errorf "shortcode tabpane: parameter 'text' must be either true or false" -}} +{{ end -}} +{{ end -}} + +{{ with .Get "persistLang" -}} +{{ if ne ( printf "%T" . ) "bool" -}} +{{ errorf "shortcode tabpane: parameter 'persistLang' must be either true or false" -}} +{{ end -}} +{{ end -}} + +{{ with .Get "right" -}} +{{ if ne ( printf "%T" . ) "bool" -}} +{{ errorf "shortcode tabpane: parameter 'right' must be either true or false" -}} +{{ end -}} +{{ end -}} + +{{/* Set values given defined within tabpane */ -}} +{{ $langPane := default "" ($.Get "lang") -}} +{{ $hloptionsPane := default "" ($.Get "highlight") -}} +{{ $textPane := default false ($.Get "text") -}} +{{ $langEqualsHeader := default false ($.Get "langEqualsHeader") -}} +{{ $persistLang := default true ($.Get "persistLang") -}} +{{ $rightPane := default false ($.Get "right") -}} +{{ $disabled := false -}} +{{ $rightpush := false -}} +{{ $activeSet := false -}} +{{- /* Scratchpad gets populated through call to .Inner */ -}} {{- .Inner -}} -{{ $activeSet = false }} +{{ $activeSet = false -}} - -
+{{/* Inner content */ -}} +
{{- range $index, $element := $.Scratch.Get "tabs" -}} - {{- $lang := $langPane -}} - {{- if $langEqualsHeader -}} - {{- $lang = $element.header -}} - {{end}} - {{- with $element.language -}} - {{- $lang = $element.language -}} - {{- end -}} - - {{- $disabled := false -}} - {{- with $element.disabled -}} - {{- $disabled = . }} - {{- end -}} - - {{- $hloptions := $hloptionsPane -}} - {{- with $element.highlight -}} - {{- $hloptions = $element.highlight -}} - {{- end -}} - - {{- $text := $textPane -}} - {{- with $element.text -}} - {{- $text = . }} - {{- end -}} - - {{- $tabid := printf "tabs-%02v-%v-tab" $.Ordinal $index | anchorize -}} - {{- $entryid := printf "tabs-%02v-%v" $.Ordinal $index | anchorize -}} - + {{ $lang := $langPane -}} + {{ if $langEqualsHeader -}} + {{ $lang = $element.header -}} + {{ end -}} + {{ with $element.language -}} + {{ $lang = $element.language -}} + {{ end -}} + + {{ $disabled := false -}} + {{ with $element.disabled -}} + {{ $disabled = . -}} + {{ end -}} + + {{ $hloptions := $hloptionsPane -}} + {{ with $element.highlight -}} + {{ $hloptions = $element.highlight -}} + {{ end -}} + + {{ $text := $textPane -}} + {{ with $element.text -}} + {{ $text = . }} + {{ end -}} + + {{ $tabid := printf "tabs-%02v-%v-tab" $.Ordinal $index | anchorize -}} + {{ $entryid := printf "tabs-%02v-%v" $.Ordinal $index | anchorize }}
- {{ if $text }} - {{- index . "content" -}} - {{- else -}} - {{- highlight (trim (index . "content") "\n") $lang $hloptions -}} - {{- end -}} + {{ if $text -}} + {{ index . "content" -}} + {{ else -}} + {{ highlight (trim (index . "content") "\n") $lang $hloptions -}} + {{ end }}
- {{ if not $disabled }} - {{ $activeSet = true }} - {{ end }} + {{- if not $disabled -}} + {{ $activeSet = true -}} + {{ end -}} - {{ end }} + {{- end }}