4 Rendering
A Punct document is format-independent; when you want to use it in an output file, it must be rendered into that output file’s format.
Punct includes an HTML renderer and a plain-text renderer, and is designed to include renderers for more formats in the future.
4.1 Rendering HTML
(require punct/render/html) | package: punct-lib |
For more information on using the fallback argument to render custom elements, see Rendering custom elements.
procedure
(doc->html-xexpr pdoc [fallback]) → xexpr?
pdoc : document? fallback : (-> symbol? list? list? xexpr?) = default-html-tag
For more information on using the fallback argument to render custom elements, see Rendering custom elements.
procedure
(default-html-tag tag attributes elements) → xexpr?
tag : symbol? attributes : (listof (listof symbol? string?)) elements : list?
> (default-html-tag 'kbd '() '("Enter")) '(kbd "Enter")
> (default-html-tag 'a '((href "http://example.com")) '("Link")) '(a ((href "http://example.com")) "Link")
4.2 Rendering plain text
(require punct/render/plaintext) | package: punct-lib |
Sometimes you want to convert a document into a text format that is even plainer than Markdown, such as when generating the plaintext version of an email newsletter.
procedure
(doc->plaintext pdoc line-width [fallback]) → string?
pdoc : document? line-width : exact-nonnegative-integer?
fallback : (symbol? (listof (listof symbol? string?)) list? . -> . string?) = (make-plaintext-fallback line-width)
The function applies very rudimentary text formatting which usually looks as you would expect, but which often discards information.
Level 1 headings are underlined with =, and all other headings are underlined with -.
Link destination URLs are given inside parentheses directly following the link text.
Code blocks are indented with four spaces, and the language name, if any, is discarded.
Images are replaced with their “alt” text, prefixed by "Image: " and wrapped in parentheses; the source URL and title are discarded.
For more information on using the fallback argument to render custom elements, see Rendering custom elements.
> (require punct/render/plaintext) > (define email (parse-markup-elements (hasheq) '("# Issue No. 1\n\nHowdy!"))) > (display (doc->plaintext email 72))
Issue No. 1
===========
Howdy!
procedure
(make-plaintext-fallback width)
→ (symbol? (listof (listof symbol? string?)) list? . -> . string?) width : exact-nonnegative-integer?
> (define foo (make-plaintext-fallback 72)) > (foo 'kbd '() '("Enter")) "[kbd] Enter\n\n"
> (foo 'a '((href "http://example.com")) '("Link")) "[a] Link\n\n"