Commit 4e465857 authored by Michael Bryant's avatar Michael Bryant

Modularize shortcodes for better integration

Allow specifying CSS and JS to be included for specific shortcodes, plus
code to load those things only when necessary.
parent d662de1c
......@@ -15,6 +15,13 @@
print_header_style = "sans-serif"
print_body_style = "serif"
[params.BluestNight.shortcodes]
[params.BluestNight.shortcodes.katex]
css = "shortcodes/katex/head.html"
js = "shortcodes/katex/foot.html"
[params.BluestNight.shortcodes.tabgroup]
js = "shortcodes/tabgroup/js.html"
[params.BluestNight.sidebar]
[params.BluestNight.sidebar.categories]
path = "sidebar/widgets/categories/widget.html"
......
......@@ -56,6 +56,24 @@
{{- .Scratch.Add "css" (partial "css/figure.css" .Scratch) -}}
{{- .Scratch.Add "css" (partial "css/iubenda.css" .Scratch) -}}
{{- /* Add any CSS from shortcodes */ -}}
{{- partial "shortcodes/get_list.html" $ -}}
{{- range $name := ($.Scratch.Get "shortcodes") -}}
{{- $.Scratch.Set "page_has_shortcode" false -}}
{{- range $page := $.Site.Pages -}}
{{- if and (ne ($.Scratch.Get "page_has_shortcode") true) ($page.HasShortcode $name) -}}
{{- $.Scratch.Set "page_has_shortcode" true -}}
{{- $.Scratch.Set "shortcode_name" $name -}}
{{- partial "shortcodes/definition.html" $ -}}
{{- $shortcode := $.Scratch.Get "shortcode" -}}
{{- with (index $shortcode "css") -}}
{{- $.Scratch.Add "css" (partial . $.Scratch) -}}
{{- end -}}
{{- end -}}
{{- end -}}
{{- end -}}
{{- if $.Site.Params.no_minimize -}}
{{- .Scratch.Get "css" -}}
{{- else -}}
......
......@@ -5,8 +5,6 @@
{{ partial "fontawesome/head.html" . }}
{{ partial "katex/head.html" . }}
{{ partial "matomo/head.html" . }}
{{ partial "iubenda/head.html" . }}
......@@ -38,11 +36,24 @@
{{- $.Scratch.Set "widget_name" $name -}}
{{- partial "sidebar/widget_definition.html" $ -}}
{{- $widget := $.Scratch.Get "widget" -}}
{{ with (index $widget "css") }}
{{ with $widget.css }}
{{- partial . $ -}}
{{- end -}}
{{- end -}}
{{- /* Include any code from shortcodes */ -}}
{{- partial "shortcodes/get_list.html" $ -}}
{{- range $name := ($.Scratch.Get "shortcodes") -}}
{{- if $.Page.HasShortcode $name -}}
{{- $.Scratch.Set "shortcode_name" $name -}}
{{- partial "shortcodes/definition.html" $ -}}
{{- $shortcode := $.Scratch.Get "shortcode" -}}
{{- with $shortcode.css_extern -}}
{{- partial . $ -}}
{{- end -}}
{{- end -}}
{{- end -}}
{{ partial "inline_css.html" . }}
{{ if .Content -}}
......
......@@ -24,4 +24,16 @@
{{- end -}}
{{- end -}}
{{- end -}}
{{- /* Include any code from shortcodes */ -}}
{{- partial "shortcodes/get_list.html" $ -}}
{{- range $name := ($.Scratch.Get "shortcodes") -}}
{{- if $.Page.HasShortcode $name -}}
{{- $.Scratch.Set "shortcode_name" $name -}}
{{- partial "shortcodes/definition.html" $ -}}
{{- $shortcode := $.Scratch.Get "shortcode" -}}
{{- with $shortcode.css_extern -}}
{{- partial . $ -}}
{{- end -}}
{{- end -}}
{{- end -}}
</style>
......@@ -9,15 +9,15 @@
{{ end }}
{{- end -}}
{{ partial "katex/foot.html" . }}
{{- /* Include any code from sidebar widgets */ -}}
{{- partial "sidebar/widget_list.html" . -}}
{{- range $name := $.Scratch.Get "widget_list" -}}
{{- $.Scratch.Set "widget_name" $name -}}
{{- partial "sidebar/widget_definition.html" $ -}}
{{- $widget := $.Scratch.Get "widget" -}}
{{ with (index $widget "js") }}
{{- partial . $ -}}
{{- /* Include any code from shortcodes */ -}}
{{- partial "shortcodes/get_list.html" $ -}}
{{- range $name := ($.Scratch.Get "shortcodes") -}}
{{- if $.Page.HasShortcode $name -}}
{{- $.Scratch.Set "shortcode_name" $name -}}
{{- partial "shortcodes/definition.html" $ -}}
{{- $shortcode := $.Scratch.Get "shortcode" -}}
{{- with $shortcode.js -}}
{{- partial . $ -}}
{{- end -}}
{{- end -}}
{{- end -}}
{{- $name := $.Scratch.Get "shortcode_name" -}}
{{- $sitecodes := $.Site.Params.shortcodes -}}
{{- $bncodes := $.Site.Params.BluestNight.shortcodes -}}
{{- if (not (isset $.Site.Params "shortcodes")) -}}
{{- $.Scratch.Set "shortcodes" $bncodes -}}
{{- else if (not (isset $.Site.Params.shortcodes $name)) -}}
{{- $.Scratch.Set "shortcodes" $bncodes -}}
{{- else if (not (isset (index $.Site.Params.shortcodes $name) "path")) -}}
{{- $.Scratch.Set "shortcodes" $bncodes -}}
{{- else -}}
{{- $.Scratch.Set "shortcodes" $sitecodes -}}
{{- end -}}
{{- $sb := ($.Scratch.Get "shortcodes") -}}
{{- if eq nil $sb -}}
{{- errorf "shortcode CSS/JS definitions are nil: %#v" $sb -}}
{{- else if eq nil (index $sb $name) -}}
{{- errorf "Could not find definition for widget: %s" $name -}}
{{- end -}}
{{- $.Scratch.Set "shortcode" (index $sb $name) -}}
{{- range $name, $ignore := $.Site.Params.BluestNight.shortcodes -}}
{{- $.Scratch.Add "shortcode_list" $name -}}
{{- $.Scratch.Add "shortcode_list" "," -}}
{{- end -}}
{{- range $name, $ignore := $.Site.Params.shortcodes -}}
{{- $.Scratch.Add "shortcode_list" $name -}}
{{- $.Scratch.Add "shortcode_list" "," -}}
{{- end -}}
{{- .Scratch.Set "shortcodes" (split (strings.TrimSuffix "," ($.Scratch.Get "shortcode_list")) ",") -}}
{{ if (in (string .Content) "class=\"katex-container\"") }}
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/KaTeX/0.8.3/katex.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/KaTeX/0.8.3/contrib/auto-render.min.js"></script>
<script>
......@@ -15,5 +14,4 @@ for (var i = 0; i < elems.length; i++) {
);
}
</script>
{{ end }}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment