Skip to content

Commit

Permalink
Merge pull request #8 from texmacs/feature/marginal-figures
Browse files Browse the repository at this point in the history
Adds support for marginal figures in hugo. Fixes numbering problem.
  • Loading branch information
mdbenito authored May 21, 2021
2 parents a3f37ff + 40d5399 commit c8e19da
Show file tree
Hide file tree
Showing 5 changed files with 163 additions and 21 deletions.
76 changes: 76 additions & 0 deletions extensions/hugo/shortcodes/sidefigure.html.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
{{/* Figures at the margin.

Batteries not included. Requires lots of styles.
Some content breaks paragraphs!

Usage:

{{< sidefigure class="optional CSS class" valign=<top,center,bottom>
numbered=numbered raw=raw header="Some text" >}}
Content here.
{{< /sidefigure >}}
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.
markdownify: whether to process .Inner with the markdown renderer. This is a
HACK, remove it. Will break nested shortcodes.
raw: set to true if there are nested shortcodes, e.g. {{<figure>}} or {{<toc>}}
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 := "" -}}
{{- $anchor := "" -}}
{{- with .Get "numbered" -}}
{{- $this.Page.Scratch.Add "sidenote-counter" 1 -}}
{{- $refText = printf "Figure %d. " ($this.Page.Scratch.Get "sidenote-counter") -}}
{{- $anchor = printf "<span class=\"sidenote-ref\">%s</span>" $refText -}}
{{- end -}}
{{- $anchor | safeHTML -}}
<span class="{{$class}}">
<span class="sidefigure">
{{ $src := .Get "src"}}
{{- with .Page.Resources.GetMatch $src }}
{{- $src = .RelPermalink }}
{{- end }}
<img src="{{$src | safeURL}}" alt="{{.Get "title"}}">
</span>
<span class="sidefigure-caption">
{{- with .Get "title" -}}
<span class="sidenote-title">{{.}}</span>
{{- end -}}
{{ if or (.Get "caption") .Inner }}
<span class="sidenote-paragraph text-center">
{{- $anchor | safeHTML -}}
<span class="font-italic smaller">
{{ with .Get "caption"}}
{{ . | markdownify }}
{{ end }}
{{ with .Inner }}
{{ if $.Get "markdownify" }}
{{ . | markdownify }}
{{ else }}
{{ . }}
{{ end }}
{{ end }}
</span>
</span>
{{ end }}
</span>
</span>
{{- /**/ -}}
60 changes: 43 additions & 17 deletions progs/markdownout.scm
Original file line number Diff line number Diff line change
Expand Up @@ -429,18 +429,31 @@
(alt (if (list-2? payload) (second payload) "")))
(string-append "![" alt "](" (force-string src) ")")))

(define (md-figure-sub x type . args)
(let* ((payload (cdr x))
(src (force-string (car payload)))
; FIXME: clear this mess with figures, don't expect img as content, etc.
(define (md-figure-sub payload)
(let* ((src (force-string (car payload)))
(title
(with-globals 'num-line-breaks 0 ; Don't break lines in 'document
(string-concatenate (map serialize-markdown* (cdr payload))))))
(if (hugo-extensions?)
(md-hugo-shortcode `(,type (src ,src) ,@args) title)
(md-image (list 'image src title)))))
(list src title)))

(define (md-figure type . args)
(lambda (x) (md-figure-sub x type args)))
(lambda (x)
(with params (md-figure-sub (cdr x))
(if (hugo-extensions?)
(md-hugo-shortcode `(,type (src ,(car params)) ,args) (cadr params))
(md-image (list 'image (car params) (cadr params)))))))

(define (md-marginal-figure type . args)
(lambda (x)
(let ((params (md-figure-sub (cddr x)))
(vpos (cadr x)))
(if (hugo-extensions?)
(md-hugo-shortcode `(,type (valign ,(marginal-style vpos))
(src ,(car params))
,args)
(cadr params))
(md-image (list 'image (car params) (cadr params)))))))


(define (md-footnote x)
Expand Down Expand Up @@ -487,8 +500,10 @@
(string-append (serialize-markdown* inner)
"{{</" (symbol->string (car x)) ">}}"))))
(string-trim-both
(string-recompose-space
`("{{<" ,shortcode ,@(map process-one arguments) ">}}" ,content))))))
(string-append
(string-recompose-space
`("{{<" ,shortcode ,@(map process-one arguments) ">}}"))
content)))))

(define (md-toc x)
(if (hugo-extensions?)
Expand All @@ -500,15 +515,20 @@
(md-hugo-shortcode '(references))
(md-style '(strong "Bibliography not implemented for raw Markdown"))))

(define marginal-styles-table
(list->ahash-table '(("b" . "bottom") ("c" . "center")
("t" . "top") ("normal" . "right"))))

(define (marginal-style s)
(ahash-ref marginal-styles-table s))

(define (md-sidenote-sub x numbered?)
(if (hugo-extensions?)
(let ((styles (list->ahash-table '(("b" . "bottom") ("c" . "center")
("t" . "top") ("normal" . "right"))))
(numbered (if numbered? '(numbered "numbered") '()))
(let ((numbered (if numbered? '((numbered "numbered")) '()))
(args (cdr x)))
(md-hugo-shortcode
(append `(sidenote (halign ,(ahash-ref styles (first args)))
(valign ,(ahash-ref styles (second args))))
(append `(sidenote (halign ,(marginal-style (first args)))
(valign ,(marginal-style (second args))))
numbered)
(third args)))
(md-footnote (list 'footnote (third (cdr x))))))
Expand Down Expand Up @@ -640,9 +660,15 @@
(list 'footnote md-footnote)
(list 'todo md-todo)
(list 'image md-image)
(list 'small-figure (md-figure 'tmfigure))
(list 'big-figure (md-figure 'tmfigure))
(list 'wide-figure (md-figure 'tmfigure 'class "wide-figure"))
(list 'small-figure (md-figure 'tmfigure 'numbered "numbered"))
(list 'small-figure* (md-figure 'tmfigure))
(list 'big-figure (md-figure 'tmfigure 'numbered "numbered"))
(list 'big-figure* (md-figure 'tmfigure))
(list 'wide-figure (md-figure 'tmfigure 'class "wide-figure"
'numbered "numbered"))
(list 'wide-figure* (md-figure 'tmfigure 'class "wide-figure"))
(list 'marginal-figure (md-marginal-figure 'sidefigure))
(list 'marginal-figure* (md-marginal-figure 'sidefigure))
(list 'hlink md-hlink)
(list 'tags md-hugo-tags) ; Hugo extension (DEPRECATED)
(list 'hugo-short md-hugo-shortcode) ; Hugo extension
Expand Down
23 changes: 19 additions & 4 deletions progs/tmmarkdown.scm
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ first empty label"
(counter-new 'equation)
(counter-new 'figure))


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Helper functions for the transformation of strees and dispatcher
;; TODO: use TeXmacs' logic-dispatch, export sessions, bibliography
Expand Down Expand Up @@ -187,7 +186,7 @@ first empty label"
(numbered-unnumbered-append (figure-tag-list)))
(not (string-contains? (symbol->string (car x)) "table"))))

(define (parse-figure x)
(define (parse-figure-sub x)
; Example input:
; (big-figure (image "path-to.jpeg" "251px" "251px" "" "")
; (document "caption"))
Expand All @@ -198,11 +197,22 @@ first empty label"
; implementing Figure text as TeXmacs.
(let* ((offset (if (is-figure? x) 0 2))
(img (tm-ref x offset))
(caption (texmacs->markdown* (tm-ref x (+ 1 offset))))
(caption `(concat (strong "Figure "
,(counter-label current-counter)
". ")
,(texmacs->markdown* (tm-ref x (+ 1 offset)))))
(src (if (tm-is? img 'image)
(tm-ref (parse-image img) 0)
'(document "Wrong image src"))))
(list (car x) src caption)))
(list src caption)))

(define (parse-figure x)
`(,(car x) ,@(parse-figure-sub x)))

(define (parse-marginal-figure x)
(let* ((vpos (first (cdr x)))
(args (parse-figure-sub `(small-figure ,@(cddr x)))))
`(,(car x) ,vpos ,@args)))

(define (parse-with x)
; HACK: we end up calling ourselves with (with "stuff"), which
Expand Down Expand Up @@ -400,10 +410,15 @@ first empty label"
(list 'reference parse-reference)
(list 'image parse-image)
(list 'small-figure (count parse-figure 'figure))
(list 'small-figure* parse-figure)
(list 'render-small-figure parse-figure)
(list 'big-figure (count parse-figure 'figure))
(list 'big-figure* parse-figure)
(list 'render-big-figure parse-figure)
(list 'wide-figure (count parse-figure 'figure))
(list 'wide-figure* parse-figure)
(list 'marginal-figure (count parse-marginal-figure 'figure))
(list 'marginal-figure* parse-marginal-figure)
(list 'footnote keep)
(list 'marginal-note keep)
(list 'marginal-note* keep)
Expand Down
10 changes: 10 additions & 0 deletions tests/hugo/marginal-figure.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---

---

Is it possible to characterize ultra-universal, pseudo-unique, partially
regular measure spaces? Recently, there has been much interest in the
computation of partially empty arrows. In [14], the authors address the
surjectivity{{< sidefigure valign="top" src="NaiveMCShapley1000itersBoston.png"
numbered="numberered" >}}This is a numbered margin figure with
caption{{</sidefigure>}} of unconditionally regular functors.
15 changes: 15 additions & 0 deletions tests/hugo/marginal-figure.tm
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<TeXmacs|1.99.20>

<style|<tuple|generic|aai-tfl>>

<\body>
Is it possible to characterize ultra-universal, pseudo-unique, partially
regular measure spaces? Recently, there has been much interest in the
computation of partially empty arrows. In [14], the authors address the
surjectivity<marginal-figure|t|<image|NaiveMCShapley1000itersBoston.png|50mm|||>|This
is a numbered margin figure with caption> of unconditionally regular
functors.
</body>

<initial|<\collection>
</collection>>

0 comments on commit c8e19da

Please sign in to comment.