4 Rendering Output
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 currently includes an HTML renderer and a plain-text renderer. Both are based on a “base” renderer. You can extend any Punct renderer or the base renderer to customize the process of converting documents to your target output format(s).
4.1 Rendering HTML
(require punct/render/html) | package: punct-lib |
procedure
pdoc : document?
fallback :
(-> symbol? (listof (list/c symbol? string?)) (listof xexpr?) xexpr?) = default-html-tag
This function uses xexpr->string to generate the HTML string. This function will blindly escape characters inside <script> and <style> tags, which may introduce errors. For HTML output that is friendlier and more correct, consider using the html-printer package in concert with doc->html-xexpr.
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? (listof (list/c symbol? string?)) (listof xexpr?) 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 (list/c symbol? string?)) elements : (listof xexpr?)
> (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 (list/c symbol? string?)) (listof xexpr?) xexpr?) = (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"