Skip to content

Commit

Permalink
Shortcodes for tabbed panes: improved whitespace handling
Browse files Browse the repository at this point in the history
  • Loading branch information
deining authored and geriom committed Oct 21, 2022
1 parent 99eacb0 commit abb7307
Show file tree
Hide file tree
Showing 2 changed files with 155 additions and 158 deletions.
96 changes: 48 additions & 48 deletions layouts/shortcodes/tab.html
Original file line number Diff line number Diff line change
@@ -1,52 +1,52 @@
<!-- 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 -}}
{{- /* 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 }}
<!-- Prefill header if not given as named or unnamed parameter -->
{{ $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 -}}

<!-- 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 }}
{{/* 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 }}
<!-- Trim any leading and trailing newlines from .Inner, this avoids
spurious lines during syntax highlighting -->
{{ $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 -}}

<!-- add dict tab to parent's scratchpad -->
{{ 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 -}}
217 changes: 107 additions & 110 deletions layouts/shortcodes/tabpane.html
Original file line number Diff line number Diff line change
@@ -1,130 +1,127 @@
<!-- 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 -->
{{/* 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 -}}

<ul class="nav nav-tabs{{ if $rightPane }} justify-content-end{{ end }}" id="tabs-{{- $.Ordinal -}}" role="tablist">
{{- 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 -}}

{{- $rightpush := false -}}
{{- with $element.rightpush -}}
{{- $rightpush = . }}
{{- end -}}

<!-- Replace space and +, not valid for css selectors -->
{{- $lang := replaceRE "[\\s+]" "-" $lang -}}

<li class="nav-item{{ if $rightpush }} ml-auto{{ end }}">
<!-- Generate the IDs for the <a> and the <div> elements -->
{{- $tabid := printf "tabs-%02v-%v-tab" $.Ordinal $index | anchorize -}}
{{- $entryid := printf "tabs-%02v-%v" $.Ordinal $index | anchorize -}}
{{ 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 -}}

{{ $rightpush := false -}}
{{ with $element.rightpush -}}
{{ $rightpush = . -}}
{{ end -}}

{{/* Replace space and +, not valid for css selectors */ -}}
{{ $lang := replaceRE "[\\s+]" "-" $lang -}}
<li class="nav-item{{ if $rightpush }} ml-auto{{ end -}}">
{{/* Generate the IDs for the <a> and the <div> elements */ -}}
{{ $tabid := printf "tabs-%02v-%v-tab" $.Ordinal $index | anchorize -}}
{{ $entryid := printf "tabs-%02v-%v" $.Ordinal $index | anchorize -}}

<a class="nav-link{{ if and ( not $activeSet ) ( not $disabled ) }} active{{ end }}{{ if $disabled }} disabled{{ end }}{{ if ne $lang "" }}{{ if $persistLang }} persistLang-{{- $lang -}}{{ end }}{{ end }}"
id="{{- $tabid -}}" data-toggle="tab" href="#{{ $entryid }}" role="tab"
{{ if ne $lang "" }}{{- if $persistLang -}}onclick="persistLang({{- $lang -}});"{{end}}{{end}}
aria-controls="{{- $entryid -}}" aria-selected="{{- and ( not $activeSet ) ( not $disabled ) "true" "false" -}}">
{{- index . "header" | markdownify -}}
id="{{ $tabid }}" data-toggle="tab" href="#{{ $entryid }}" role="tab"
{{ if ne $lang "" }}{{ if $persistLang }}onclick="persistLang({{ $lang }});"{{ end }}{{ end -}}
aria-controls="{{- $entryid -}}" aria-selected="{{- and ( not $activeSet ) ( not $disabled ) "true" "false" -}}">
{{ index . "header" | markdownify }}
</a>
</li>

{{ if not $disabled }}
{{ $activeSet = true }}
{{ end }}
{{- if not $disabled -}}
{{ $activeSet = true -}}
{{ end -}}

{{- end -}}
{{- end }}
</ul>

{{ $activeSet = false }}
{{ $activeSet = false -}}

<!-- Inner content -->
<div class="tab-content" id="tabs-{{- $.Ordinal -}}-content">
{{/* Inner content */ -}}
<div class="tab-content" id="tabs-{{ $.Ordinal }}-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 }}
<div class="{{ if $text }}tab-body {{end}}tab-pane fade{{ if and ( not $activeSet ) ( not $disabled ) }} show active{{ end }}"
id="{{ $entryid }}" role="tabpanel" aria-labelled-by="{{ $tabid }}">
{{ 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 }}
</div>

{{ if not $disabled }}
{{ $activeSet = true }}
{{ end }}
{{- if not $disabled -}}
{{ $activeSet = true -}}
{{ end -}}

{{ end }}
{{- end }}
</div>

0 comments on commit abb7307

Please sign in to comment.