Skip to content

Commit

Permalink
Merge develop into master
Browse files Browse the repository at this point in the history
Marginal figures, numbered sections, improved counters, fixes
  • Loading branch information
mdbenito authored May 21, 2021
2 parents bb51eed + c8e19da commit 3ee7295
Show file tree
Hide file tree
Showing 12 changed files with 321 additions and 85 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>
{{- /**/ -}}
14 changes: 8 additions & 6 deletions progs/init-markdown.scm
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
(define-preferences
("texmacs->markdown:flavour" "vanilla" ignore)
("texmacs->markdown:paragraph-width" 79 ignore)
("texmacs->markdown:show-menu" "off" ignore))
("texmacs->markdown:show-menu" "off" ignore)
("texmacs->markdown:numbered-sections" "on" ignore))

(define-format markdown
(:name "Markdown")
Expand Down Expand Up @@ -54,9 +55,10 @@
(lazy-menu (markdown-menus) markdown-menu tools-menu)

(define (markdown-menu-show?)
(== (get-preference "texmacs->markdown:show-menu") "on"))
(preference-on? "texmacs->markdown:show-menu"))

(delayed (:idle 1)
(menu-bind tools-menu
(former)
("Markdown plugin" (toggle-preference "texmacs->markdown:show-menu"))))
(delayed (:idle 1000)
(lazy-define-force tools-menu)
(menu-bind tools-menu
(former)
("Markdown plugin" (toggle-preference "texmacs->markdown:show-menu"))))
1 change: 1 addition & 0 deletions progs/markdown-menus.scm
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
("Vanilla" (markdown-set-flavour "vanilla"))
("Hugo" (markdown-set-flavour "hugo")))
("Paragraph width" (interactive markdown-set-paragraph-width))
("Numbered sections" (toggle-preference "texmacs->markdown:numbered-sections"))
---
("Help" (load-help-article "markdown")))

Expand Down
85 changes: 59 additions & 26 deletions progs/markdownout.scm
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@
(lambda (x)
(cond ((tm-is? x 'date) (second x))
((tm-is? x 'tuple)
(string-append "\n" (list->yaml (cdr x) 2)))
(string-append "\n" (list->yaml (list-sort (cdr x) string<=?) 2)))
((tuple? x) (string-append "\n" (list->yaml x 2)))
((bool? x) x)
((string? x) (string-quote x))))) ; quote everything else
Expand Down Expand Up @@ -196,15 +196,14 @@
(define (md-doc-title x)
(with title (md-string (serialize-markdown* (cdr x)))
(if (hugo-extensions?)
(md-hugo-frontmatter `(hugo-front "title" ,title))
title)))
(serialize-markdown* `(hugo-front "title" ,title))
(serialize-markdown* `(document ,title "")))))

(define (md-doc-subtitle x)
(display "FIXME: append subtitle to title")
(with subtitle (md-string (serialize-markdown* (cdr x)))
(if (hugo-extensions?)
(md-hugo-frontmatter `(hugo-front "subtitle" ,subtitle))
subtitle)))
(serialize-markdown* `(hugo-front "subtitle" ,subtitle))
(serialize-markdown* `(document ,subtitle)))))

(define (md-doc-author x)
; TODO? We might want to extract other info
Expand Down Expand Up @@ -235,7 +234,7 @@
(define (md-abstract x)
(if (hugo-extensions?)
(md-hugo-frontmatter `(hugo-front "summary" ,(serialize-markdown* (cdr x))))
(md-document (md-style `(em ,(cdr x))))))
(md-paragraph `(concat (strong "Abstract: ") (em ,(cdr x))))))

(define (md-paragraph p)
;; FIXME: arguments of Hugo shortcodes shouldn't be split
Expand Down Expand Up @@ -430,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 @@ -488,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 @@ -501,16 +515,29 @@
(md-hugo-shortcode '(references))
(md-style '(strong "Bibliography not implemented for raw Markdown"))))

(define (md-sidenote x)
(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"))))
(let ((numbered (if numbered? '((numbered "numbered")) '()))
(args (cdr x)))
(md-hugo-shortcode
`(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 args)))))
(md-footnote (list 'footnote (third (cdr x))))))

(define (md-sidenote x)
(md-sidenote-sub x #t))

(define (md-sidenote* x)
(md-sidenote-sub x #f))

(define (md-explain-macro x)
; FIXME: this will break with nested macros (tt style will be interrupted)
Expand Down Expand Up @@ -633,17 +660,23 @@
(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
(list 'hugo-front md-hugo-frontmatter) ; Hugo extension
(list 'table-of-contents md-toc) ; Hugo extension
(list 'bibliography md-bibliography) ; TfL extension
(list 'marginal-note md-sidenote) ; TfL extension
(list 'marginal-note* md-sidenote) ; TfL extension (TO DO)
(list 'marginal-note* md-sidenote*) ; TfL extension
(list 'explain-macro md-explain-macro)
(list 'tmdoc-copyright md-tmdoc-copyright)
))
Expand Down
Loading

0 comments on commit 3ee7295

Please sign in to comment.