From 51019bbb278b235fa41b40be233edfa5e5ee3022 Mon Sep 17 00:00:00 2001 From: Miguel de Benito Delgado Date: Fri, 21 May 2021 18:47:41 +0200 Subject: [PATCH] Add sidenote shortcode for hugo. Improve test --- extensions/hugo/shortcodes/sidenote.html.html | 69 +++++++++++++++++++ tests/{tfl => hugo}/marginal-note.md | 2 +- tests/{tfl => hugo}/marginal-note.tm | 0 tests/run.scm | 1 - 4 files changed, 70 insertions(+), 2 deletions(-) create mode 100644 extensions/hugo/shortcodes/sidenote.html.html rename tests/{tfl => hugo}/marginal-note.md (50%) rename tests/{tfl => hugo}/marginal-note.tm (100%) diff --git a/extensions/hugo/shortcodes/sidenote.html.html b/extensions/hugo/shortcodes/sidenote.html.html new file mode 100644 index 0000000..ed692d9 --- /dev/null +++ b/extensions/hugo/shortcodes/sidenote.html.html @@ -0,0 +1,69 @@ +{{/* Simple sidenotes. +Assumes main content to be col-md-9 and the rows to be justify-content-end. +Some content breaks paragraphs! + +Usage: + +{{< sidenote class="optional CSS class" valign= + numbered=numbered raw=raw header="Some text" >}} +Content here. +{{< /sidenote >}} +Parameters: + class: one or more CSS class names + valign: where the bottom of the sidenote will be placed wrt the inline content. + "top" is the default: the sidenote starts at the height it is inserted. + "center" shifts it by 50% upwards, "bottom" by 100%. + numbered: whether to prefix the content with an autoincreased counter and + display it in the main text. + raw: set to true if there are nested shortcodes, e.g. {{
}} or {{}} + header: This will be rendered bold and centered above the content +*/}} +{{- $this := . -}} +{{- $class := "sidenote" -}} +{{- with .Get "valign" -}} + {{- if (eq . "center") -}} + {{- $class = printf "%s %s" $class (safeHTMLAttr "sidenote-center") -}} + {{- end -}} + {{- if (eq . "bottom") -}} + {{- $class = printf "%s %s" $class (safeHTMLAttr "sidenote-bottom") -}} + {{- end -}} +{{- end -}} +{{- with .Get "class" -}} + {{- $class = printf "%s %s" $class (safeHTMLAttr .) -}} +{{- end -}} +{{- $refText := "" -}} +{{- $content := .Inner -}} +{{- with .Get "numbered" -}} + {{- $this.Page.Scratch.Add "sidenote-counter" 1 -}} + {{- $refText = printf "%d" ($this.Page.Scratch.Get "sidenote-counter") -}} + {{- $content = printf "%s %s" $refText $content -}} +{{- end -}} +{{- if not (.Get "raw") -}} + {{- $content = $content | markdownify -}} + {{- $content = replace $content "

" "" -}} + {{- $content = replace $content "

" "" -}} +{{- end -}} +{{- if .Inner -}} + {{/*HACKY and incomplete. See https://stackoverflow.com/a/9852381/493464*/}} + {{- $hasNonPhrasingContent := findRE "<(p|div|pre|h[1-5]|figure|nav)" $content -}} + {{- if or $hasNonPhrasingContent (.Get "raw") -}} + + {{- else -}} + + {{- with .Get "header" -}} + {{.}} + {{- end -}} + {{- $content | safeHTML -}} + + {{- end -}} + {{- $refText -}} +{{- else -}} + +{{- end -}} diff --git a/tests/tfl/marginal-note.md b/tests/hugo/marginal-note.md similarity index 50% rename from tests/tfl/marginal-note.md rename to tests/hugo/marginal-note.md index 855d850..0d0afb8 100644 --- a/tests/tfl/marginal-note.md +++ b/tests/hugo/marginal-note.md @@ -4,4 +4,4 @@ title: "Some title" -This text should have a sidenote next to it.{{< sidenote halign="right" valign="top" >}}This is the note, it is top aligned.{{}} +This text should have a sidenote next to it.{{< sidenote halign="right" valign="top" numbered="numbered" >}}This is the note, it is top aligned.{{}} \ No newline at end of file diff --git a/tests/tfl/marginal-note.tm b/tests/hugo/marginal-note.tm similarity index 100% rename from tests/tfl/marginal-note.tm rename to tests/hugo/marginal-note.tm diff --git a/tests/run.scm b/tests/run.scm index 9236295..2f9c9a5 100644 --- a/tests/run.scm +++ b/tests/run.scm @@ -29,6 +29,5 @@ (set-preference "texmacs->markdown:flavour" "hugo") (set-preference "texmacs->markdown:paragraph-width" #f) (ftw "./hugo/" run-test) - (ftw "./tfl/" run-test) (set-preference "texmacs->markdown:hugo-extensions" save-hugo) (set-preference "texmacs->markdown:paragraph-width" save-width)))