Skip to content

Instantly share code, notes, and snippets.

@harrygallagher4
Created March 2, 2023 22:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save harrygallagher4/783d1e1fbdca840c9dec16742285cae6 to your computer and use it in GitHub Desktop.
Save harrygallagher4/783d1e1fbdca840c9dec16742285cae6 to your computer and use it in GitHub Desktop.
#lang racket
(define (get-renderers render-element render-elements)
`((#:document .
,(λ (doc)
`(article
,@(render-elements (document-body doc))
,@(render-element `(footnotes '() ,@(document-footnotes doc))))))
(#:fallback . ,(λ (tag att els children) `(,tag ,att ,@(children))))
(link .
,(λ (att els children)
(match-let ((`((dest ,dest) (title ,title)) att))
`(a ((href ,dest) ,@(if title `((title ,title)) '())) ,@(children)))))
(image .
,(λ (att els children)
(match-let ((`((src ,src) (title ,title) (desc ,desc)) att))
`(img ((src ,src)
,@(if desc `((alt ,desc)) '())
,@(if title `((title ,title)) '()))))))
(footnote-reference .
,(λ (att els children)
(match-let ((`((label ,label) (defn-num ,defn-num) (ref-num ,ref-num)) att))
`(sup (a ((href ,(~a "#" (fn-def-anchor label)))
(id ,(fn-ref-anchor label ref-num)))
,(number->string defn-num))))))
(footnote-definition .
,(λ (att els children*)
(match-let ((`((label ,label) (ref-count ,ref-count)) attrs)
(backrefs (make-backrefs ref-count))
(children (children*)))
`(li ((id ,(fn-def-anchor label)))
,@(match children
((list blocks ... (list 'p inline ...))
`(,@blocks (p ,@inline ,@backrefs)))
(_ (append children backrefs)))))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment