Expand description
The HTML emitter: writes the HTML of one lowered tree through the HTML sink.
An emitter is a backend’s emit stage: it walks the lowered trees and writes
the output’s bytes into a table, from which the backend’s final stage makes
the artifact set (see backends, lowering, and
outputs).
On this backend, emit_tree writes one tree; the emit stage
(HtmlEmitStage) calls it once per
document tree, and the stage flm.html.emit-pieces once per
piece the render places. It
returns an EmittedTree: the tree’s HTML in segments, the identifiers of
the style and script definitions the tree requires, and the diagnostics
reported along the way.
§What is written for each element
The tag, the classes and the attributes of every presentation element come from that element’s row of the conventions table, the HTML contract written down as data: the emitter reads the row and never spells a class or an attribute name of its own. The row also gives the style definition the element needs, which the emitter requires (records as a requirement of the tree) when it writes the element. The rendering guide prints the table.
A paragraph is a <p>, except a paragraph that holds flow
content: an element this emitter writes with a tag HTML does not allow
inside a <p>, such as the <div> of a block, a list or a table, which a
displayed item may hold. That paragraph is a <div> carrying
role="paragraph", with the same class and the same id. The emitter
decides for each paragraph, from the paragraph’s lowered content, so no
element has to know of HTML’s rule.
A paragraph with nothing to show (its content empty, or only spaces,
source comments and paragraph breaks) is not written at all: the text
backend prints nothing for one, and an empty <p> would only add space on
the page. An element inside it, even an empty one, counts as something to
show. A paragraph carrying an anchor, which a link can lead to, is written
even when it is empty.
Beyond the rows, these elements are written with more than one tag:
- a list writes
<li>per entry, with a rich label as a<span class="flm-pres-list-label">at its start; a description list (<dl>) writes the label as a<dt>carrying the same class and the entry as a<dd>, because those are the only children a<dl>may have. An entry’s anchor is theidof its<li>, or of its<dt>; - a table of cells writes a
<tr>per row of its grid and, in it, a<td>(a<th>carryingscopefor a header cell) per position that no span covers: a cell at its top-left position, withcolspanandrowspanwhere it spans, its alignment, its rules and its custom styles asdata-flm-…attributes and its content as blocks; an empty<td>where no cell is placed; - verbatim content as a block is
<pre><code>…</code></pre>, with the class and the attributes on the<pre>and the content written as text, character for character; - an unattributed macro (one no feature declares) is a
<span>around the text the author wrote; an unattributed environment is a<div>holding a\begin{…}line, the lowered body, and an\end{…}line, each line a<span class="flm-pres-unknown-marker">; - a decoration
of every kind is a
<span>written at the position of its decoration mark, in the flow of the text. A margin decoration is one too, which the core stylesheet (flm.html.style.core) sets beside the text, because a mark can be inside a paragraph and an<aside>inside a<p>is not valid HTML; - a link inside another link’s content is a
<span>carrying the link’s classes and kind but nohref, role or accessible name, because an<a>inside an<a>is not valid HTML; the outer link is the one a reader follows. Nothing is reported, since a document may legitimately nest links this way (a term link inside the text of an\href).
§Mathematics
Passthrough mathematics (a formula whose source the
math-passthrough feature
carries unread, as a flm.pres.math-passthrough node) is not translated
into HTML: it is handed, as the LaTeX the author wrote, to a formula
renderer, a script in the reader’s browser that reads
LaTeX out of the page and typesets it. FLM does not inspect what a formula
says (super::mathjax states what that means for a host). Every
formula, inline or displayed, is a <span> carrying the class, the display
(data-flm-display) and the environment’s name; the stylesheet
of passthrough mathematics (flm.html.style.math-passthrough) gives a
displayed formula the appearance of a block. One tag serves both
because a displayed formula can be inside a paragraph, and HTML does not
allow a block element there. The emitter writes the delimiters (\(…\),
\[…\] or \begin{…}…\end{…}), and the document’s own source between
them as text. It requires the script definition
flm.html.mathjax at the first
passthrough formula it writes in a tree; a page that does not carry that
script shows
the LaTeX as characters.
Content inside a formula (the text of \text{…}, a link, a point a
reference can lead to) is a flm.pres.math-text node among the formula’s
source. The emitter writes it as HTML inside the formula, through the
formula renderer’s <tex-html> element:
<tex-html><span class="flm-pres-math--text" …>…</span></tex-html>.
The renderer searches the whole formula for its closing delimiter and for
that element, so the emitter breaks up any such sequence in the text it
writes inside a formula; an attribute value holding one is left out and
reported as RefusedInMath.
The repair of the text is about the <tex-html> element, not about who
staged it: the text written inside a flm.pres.html.tag construct whose
tag is tex-html (which a rule stages to put HTML inside a formula it
writes itself, as a processing feature’s rule does) is repaired too, for
every closing delimiter (\), \], \end{…}) and the two spellings of
the tag, since the emitter does not know which formula the rule wrote
around it.
§The trust guarantee
Every byte goes through the sink: document text through its text entry point, every attribute value through its attribute entry points, each escaped where it is written. This module builds no HTML by formatting strings and writes no unescaped byte.
Two kinds of value are checked before they are written, because escaping does not make them safe:
- an address (a link’s
href, an image’ssrcand the candidates of itssrcset) must pass the address policy, under the render optionHtmlAllowedUrlSchemes. A refused address is not written and is reported as the Errorflm.html.refused-address(AddressRefused); the link keeps its text and the image its alternative text, because content is never dropped. - a tag or attribute name of a construct node,
which a lowering rule wrote, must pass the name check. A
node whose name fails it is written as the unsupported stand-in (an empty
<span class="flm-pres-unsupported">) and reported asflm.html.invalid-name(InvalidName).
A third check applies to the two names a rule writes into a node of a
presentation element: a role, written as one class, and a kind, written
as one token of data-flm-kind (see decorations, roles and
kinds). Both
attributes separate their tokens by whitespace, so a name holding
whitespace would be read as several; such a name is left out (that one
class, that one token) and reported as flm.html.invalid-name, and the rest
of the node is written as usual.
A construct node is outside the HTML contract and outside the address
policy: a lowering rule that stages an HTML tag node (builds it into the
lowered tree; see Lowerer) is
responsible for the attributes it writes.
§Trees placed in other trees
A flm.pres.subtree node
(SUBTREE, SubtreePayload)
places another document tree at its position. This emitter sees one tree
only, so it ends the segment it is writing and
records the placed tree (HtmlSegment::Tree); the regions stage, which
sees every tree, writes the placed tree’s HTML there.
§Limits on nesting
The walk recurses as deeply as the tree nests, so it must run on a worker
thread, a thread that declares its stack size
(on_worker_thread; see the
recursion guard and worker
threads).
emit_tree runs it on one. The walk also refuses a tree that nests deeper
than its depth limit, writing nothing for it and reporting
flm.html.nesting-too-deep (NestingTooDeep).
The depth limit is read once, when the walk starts, from the stack that
remains on the worker thread for guarded operations: that stack divided by
an estimate of the stack one level of the walk uses (12 KiB). At the top of
a thread of the library, which declares
WORKER_STACK_SIZE, the limit is
just under 4096 levels; a thread that declares a smaller stack gives a
smaller limit. A tree that came from a parsed document never comes near the
limit on a thread of the library, since the parse refuses far shallower
nesting. A lowered tree can, when its lowering rules add levels of their
own.
Structs§
- Address
Refused - Condition
flm.html.refused-address(Error): a link’s or an image’s address has a scheme that the address policy does not allow under the render optionHtmlAllowedUrlSchemes, such asjavascript. - Emitted
Tree - The result of
emit_tree: one tree’s HTML, its requirements, where it places other trees, and the diagnostics reported. - Invalid
Name - Condition
flm.html.invalid-name(Error): a name the emitter would write into the page is not a valid name of its kind, and writing it unchanged could produce markup nobody asked for. - Nesting
TooDeep - Condition
flm.html.nesting-too-deep(Error): a lowered tree nests deeper than the emitter’s depth limit, which the walk reads when it starts from the stack that remains on its thread (see the module documentation). The detail gives the limit, in levels. - NoWorker
Thread - Condition
flm.html.worker-thread-unavailable(Error): the emitter could not run, because the calling thread is not a worker thread and the operating system refused to create one. - Refused
InMath - Condition
flm.html.refused-in-math(Error): an attribute value the emitter would write inside a formula contains a sequence that would break the formula.
Functions§
- emit_
tree - Writes one lowered tree as HTML under the render options
optionsand the valueurl_schemesof the render optionHtmlAllowedUrlSchemes, and returns the HTML with the tree’s requirements and diagnostics (EmittedTree).