11.2 Decode
(require pollen/decode) | package: pollen |
The doc export of a Pollen markup file is a simple X-expression. Decoding refers to any post-processing of this X-expression. The pollen/decode module provides tools for creating decoders.
The decode step can happen separately from the compilation of the file. But you can also attach a decoder to the markup file’s root node, so the decoding happens automatically when the markup is compiled, and thus automatically incorporated into doc. (Following this approach, you could also attach multiple decoders to different tags within doc.)
You can, of course, embed function calls within Pollen markup. But since markup is optimized for authors, decoding is useful for operations that can or should be moved out of the authoring layer.
One example is presentation and layout. For instance, detect-paragraphs is a decoder function that lets authors mark paragraphs in their source simply by using two carriage returns.
Another example is conversion of output into a particular data format. Most Pollen functions are optimized for HTML output, but one could write a decoder that targets another format.
procedure
(decode tagged-xexpr [ #:txexpr-tag-proc txexpr-tag-proc #:txexpr-attrs-proc txexpr-attrs-proc #:txexpr-elements-proc txexpr-elements-proc #:block-txexpr-proc block-txexpr-proc #:inline-txexpr-proc inline-txexpr-proc #:string-proc string-proc #:symbol-proc symbol-proc #:valid-char-proc valid-char-proc #:cdata-proc cdata-proc #:exclude-tags tags-to-exclude]) → txexpr? tagged-xexpr : txexpr?
txexpr-tag-proc : (txexpr-tag? . -> . txexpr-tag?) = (λ(tag) tag)
txexpr-attrs-proc : (txexpr-attrs? . -> . txexpr-attrs?) = (λ(attrs) attrs)
txexpr-elements-proc : (txexpr-elements? . -> . txexpr-elements?) = (λ(elements) elements) block-txexpr-proc : (block-txexpr? . -> . xexpr?) = (λ(tx) tx) inline-txexpr-proc : (txexpr? . -> . xexpr?) = (λ(tx) tx) string-proc : (string? . -> . xexpr?) = (λ(str) str) symbol-proc : (symbol? . -> . xexpr?) = (λ(sym) sym) valid-char-proc : (valid-char? . -> . xexpr?) = (λ(vc) vc) cdata-proc : (cdata? . -> . xexpr?) = (λ(cdata) cdata) tags-to-exclude : (listof symbol?) = null
This function doesn’t do much on its own. Rather, it provides the hooks upon which harder-working functions can be hung.
Recall from [future link: Pollen mechanics] that any tag can have a function attached to it. By default, the tagged-xexpr from a source file is tagged with root. So the typical way to use decode is to attach your decoding functions to it, and then define root to invoke your decode function. Then it will be automatically applied to every doc during compile.
For instance, here’s how decode is attached to root in Butterick’s Practical Typography. There’s not much to it —
(define (root . items) (decode (make-txexpr 'root '() items) #:txexpr-elements-proc detect-paragraphs #:block-txexpr-proc (compose1 hyphenate wrap-hanging-quotes) #:string-proc (compose1 smart-quotes smart-dashes) #:exclude-tags '(style script)))
The hyphenate function is not part of Pollen, but rather the hyphenate package, which you can install separately.
This illustrates another important point: even though decode presents an imposing list of arguments, you’re unlikely to use all of them at once. These represent possibilities, not requirements. For instance, let’s see what happens when decode is invoked without any of its optional arguments.
Examples: | |||||
|
Right — nothing. That’s because the default value for the decoding arguments is the identity function, (λ (x) x). So all the input gets passed through intact unless another action is specified.
The *-proc arguments of decode take procedures that are applied to specific categories of elements within txexpr.
The txexpr-tag-proc argument is a procedure that handles X-expression tags.
Examples: | ||||||
|
The txexpr-attrs-proc argument is a procedure that handles lists of X-expression attributes. (The txexpr module, included at no extra charge with Pollen, includes useful helper functions for dealing with these attribute lists.)
Examples: | ||||||
|
Note that txexpr-attrs-proc will change the attributes of every tagged X-expression, even those that don’t have attributes. This is useful, because sometimes you want to add attributes where none existed before. But be careful, because the behavior may make your processing function overinclusive.
Examples: | |||||||||||||||||
|
The txexpr-elements-proc argument is a procedure that operates on the list of elements that represents the content of each tagged X-expression. Note that each element of an X-expression is subject to two passes through the decoder: once now, as a member of the list of elements, and also later, through its type-specific decoder (i.e., string-proc, symbol-proc, and so on).
Examples: | |||||||||||
|
So why do you need txexpr-elements-proc? Because some types of element decoding depend on context, thus it’s necessary to handle the elements as a group. For instance, the doubling function above, though useless, requires handling the element list as a whole, because elements are being added.
A more useful example: paragraph detection. The behavior is not merely a map across each element:
Examples: | ||||||||||||
|
The block-txexpr-proc argument and the inline-txexpr-proc arguments are procedures that operate on tagged X-expressions. If the X-expression meets the block-txexpr? test, it’s processed by block-txexpr-proc. Otherwise, it’s inline, so it’s processed by inline-txexpr-proc. (Careful, however — these aren’t mutually exclusive, because block-txexpr-proc operates on all the elements of a block, including other tagged X-expressions within.)
Of course, if you want block and inline elements to be handled the same way, you can set block-txexpr-proc and inline-txexpr-proc to be the same procedure.
Examples: | |||||||||||||||||||
|
The string-proc, symbol-proc, valid-char-proc, and cdata-proc arguments are procedures that operate on X-expressions that are strings, symbols, valid-chars, and CDATA, respectively. Deliberately, the output contracts for these procedures accept any kind of X-expression (meaning, the procedure can change the X-expression type).
Examples: | ||||||||||||||||||||||||
|
Finally, the tags-to-exclude argument is a list of tags that will be exempted from decoding. Though you could get the same result by testing the input within the individual decoding functions, that’s tedious and potentially slower.
Examples: | |||||||
|
The tags-to-exclude argument is useful if you’re decoding source that’s destined to become HTML. According to the HTML spec, material within a <style> or <script> block needs to be preserved literally. In this example, if the CSS and JavaScript blocks are capitalized, they won’t work. So exclude '(style script), and problem solved.
Examples: | ||||||||||||||||||||
|
procedure
(decode-elements elements [ #:txexpr-tag-proc txexpr-tag-proc #:txexpr-attrs-proc txexpr-attrs-proc #:txexpr-elements-proc txexpr-elements-proc #:block-txexpr-proc block-txexpr-proc #:inline-txexpr-proc inline-txexpr-proc #:string-proc string-proc #:symbol-proc symbol-proc #:valid-char-proc valid-char-proc #:cdata-proc cdata-proc #:exclude-tags tags-to-exclude]) → txexpr-elements? elements : txexpr-elements?
txexpr-tag-proc : (txexpr-tag? . -> . txexpr-tag?) = (λ(tag) tag)
txexpr-attrs-proc : (txexpr-attrs? . -> . txexpr-attrs?) = (λ(attrs) attrs)
txexpr-elements-proc : (txexpr-elements? . -> . txexpr-elements?) = (λ(elements) elements) block-txexpr-proc : (block-txexpr? . -> . xexpr?) = (λ(tx) tx) inline-txexpr-proc : (txexpr? . -> . xexpr?) = (λ(tx) tx) string-proc : (string? . -> . xexpr?) = (λ(str) str) symbol-proc : (symbol? . -> . xexpr?) = (λ(sym) sym) valid-char-proc : (valid-char? . -> . xexpr?) = (λ(vc) vc) cdata-proc : (cdata? . -> . xexpr?) = (λ(cdata) cdata) tags-to-exclude : (listof symbol?) = null
11.2.1 Block
Because it’s convenient, Pollen puts tagged X-expressions into two categories: block and inline. Why is it convenient? When using decode, you often want to treat the two categories differently. Not that you have to. But this is how you can.
parameter
(project-block-tags block-tags) → void? block-tags : (listof txexpr-tag?)
(address article aside audio blockquote body canvas dd div dl fieldset figcaption figure footer form h1 h2 h3 h4 h5 h6 header hgroup noscript ol output p pre section table tfoot ul video)
procedure
(register-block-tag tag) → void?
tag : txexpr-tag?
Pollen tries to do the right thing without being told. But this is the rare case where you have to be explicit. If you introduce a tag into your markup that you want treated as a block, you must use this function to identify it, or you will get spooky behavior later on.
For instance, detect-paragraphs knows that block elements in the markup shouldn’t be wrapped in a p tag. So if you introduce a new block element called bloq without registering it as a block, misbehavior will follow:
Examples: | ||||||
|
But once you register bloq as a block, order is restored:
Examples: | |||||||||
|
If you find the idea of registering block tags unbearable, good news. The project-block-tags include the standard HTML block tags by default. So if you just want to use things like div and p and h1–h6, you’ll get the right behavior for free.
Examples: | |||||
|
procedure
(block-txexpr? v) → boolean?
v : any/c
11.2.2 Typography
An assortment of typography & layout functions, designed to be used with decode. These aren’t hard to write. So if you like these, use them. If not, make your own.
procedure
(whitespace? v) → boolean?
v : any/c
Examples: | |||||||||||||
|
procedure
(whitespace/nbsp? v) → boolean?
v : any/c
Examples: | |||||||||||||
|
procedure
(smart-quotes str) → string?
str : string?
Examples: | |||||||||||||
|
procedure
(smart-dashes str) → string?
str : string?
Examples: | ||||||||||||
|
procedure
(detect-linebreaks tagged-xexpr-elements [ #:separator linebreak-sep #:insert linebreak]) → txexpr-elements? tagged-xexpr-elements : txexpr-elements? linebreak-sep : string? = world:linebreak-separator linebreak : xexpr? = '(br)
Examples: | ||||
|
procedure
(detect-paragraphs elements [ #:separator paragraph-sep #:tag paragraph-tag #:linebreak-proc linebreak-proc]) → txexpr-elements? elements : txexpr-elements? paragraph-sep : string? = world:paragraph-separator paragraph-tag : symbol? = 'p
linebreak-proc : (txexpr-elements? . -> . txexpr-elements?) = detect-linebreaks
If element is already a block-txexpr?, it will not be wrapped as a paragraph (because in that case, the wrapping would be superfluous). Thus, as a consequence, if paragraph-sep occurs between two blocks, it will be ignored (as in the example below using two sequential 'div blocks.)
The paragraph-tag argument sets the tag used to wrap paragraphs.
The linebreak-proc argument allows you to use a different linebreaking procedure other than the usual detect-linebreaks.
Examples: | ||||||||||||||
|
procedure
(wrap-hanging-quotes tx [ #:single-preprend single-preprender #:double-preprend double-preprender]) → txexpr? tx : txexpr? single-preprender : txexpr-tag? = 'squo double-preprender : txexpr-tag? = 'dquo
Examples: | ||||
|
In pro typography, quotation marks at the beginning of a line or paragraph are often shifted into the margin slightly to make them appear more optically aligned with the left edge of the text. With a reflowable layout model like HTML, you don’t know where your line breaks will be.
This function will simply insert the 'squo and 'dquo tags, which provide hooks that let you do the actual hanging via CSS, like so (actual measurement can be refined to taste):
squo {margin-left: -0.25em;} |
dquo {margin-left: -0.50em;} |
Be warned: there are many edge cases this function does not handle well.
Examples: | |||
|