Skip to main content

flm_core/stdlib/backends/html/
mod.rs

1//! The `html` backend: writes a document as an HTML page for a browser.
2//!
3//! A backend is the registration bundle for one output format: the elements its
4//! emitter writes natively, a lowering stage, an emit stage and a final stage
5//! ([`BackendDef`](crate::core::registry::BackendDef); see [backends, lowering,
6//! and outputs](crate::guide::concepts_overview#backends-lowering-and-outputs)).
7//! This module is the HTML backend. Its feature, [`HtmlBackend`], registers the
8//! backend, the `html` output and the `html` workflow; the feature, the backend,
9//! the output and the workflow are all named `html`. A package that lists the
10//! feature ([`feature()`]) must also list the `lowering`, `pieces`
11//! and `check` features.
12//!
13//! A run of the `html` workflow produces two things a caller can use:
14//!
15//! - the **page**: the entry artifact of the table `flm.output.html.document`
16//!   ([`DOCUMENT`]), one HTML file that carries, or loads from an address, the
17//!   stylesheets and the scripts the document requires;
18//! - the **HTML of each document tree**, without a page around it, in the table
19//!   `flm.output.html` ([`HtmlFragments`]), with the identifiers of the style
20//!   and script definitions each tree requires (the stylesheets and scripts its
21//!   output depends on; see [style and script
22//!   definitions](crate::guide::concepts_overview#style-and-script-definitions)).
23//!   A host that embeds the content in a page of its own reads this table, the
24//!   table `flm.output.html.pieces` ([`HtmlPieces`]) of the pieces (document-level
25//!   values a backend places beside the content, such as the title;
26//!   [`Piece`](crate::core::piece::Piece)), and the definitions table
27//!   `flm.html.definitions` ([`DEFINITIONS_TABLE`]).
28//!
29//! The page is assembled from output regions ([`Regions`](crate::core::region::Regions)):
30//! named parts of the page (the content, each piece, the stylesheets, the
31//! scripts, the title and the language) that the stage `flm.html.regions` writes
32//! once. The backend's final stage assembles the page from them in a layout of
33//! its own; a host may assemble the same regions through a template of its own
34//! instead (see [output regions and the final
35//! stage](crate::guide::concepts_overview#output-regions-and-the-final-stage)).
36//!
37//! The [HTML backend chapter](crate::guide::rendering#the-html-backend) of the
38//! rendering guide describes the output element by element.
39//!
40//! # Writing HTML: escaping and the trust guarantee
41//!
42//! Every byte of HTML this library writes goes through the modules below, and
43//! they guarantee one thing: **text that comes from a document reaches the
44//! page escaped, and by no other way**. Bytes are written unescaped only as a
45//! [`Markup`] value, or as the content of a `<style>` or a `<script>` through
46//! [`HtmlSink::raw_text_element`]; the documentation of each states what the
47//! caller must ensure.
48//!
49//! The guarantee covers every byte outside mathematics. Inside a formula that
50//! the feature of the package
51//! [`math-passthrough`](crate::math_passthrough) writes,
52//! nothing is inspected and the author is trusted: what an author writes there
53//! reaches the reader's formula renderer as typed.
54//! [`mod@mathjax`] says what that means for a host and what such a host
55//! configures for itself.
56//!
57//! - [`escape`] — the escaping functions: one for text between tags, two for an
58//!   attribute value.
59//! - [`markup`] — [`Markup`], the value that carries HTML written without
60//!   escaping.
61//! - [`sink`] — [`HtmlSink`], the only interface through which HTML bytes are
62//!   written: separate entry points for document text, for a tag and its
63//!   attributes, for a markup value, and for the content of a `<style>` or a
64//!   `<script>`; it also writes the line breaks and the indentation between
65//!   block-level tags, and offers no way to read back what was written.
66//! - [`names`] — how the names of presentation elements, roles and payload
67//!   fields become the class and attribute names of the [HTML
68//!   contract](CONVENTIONS) (the classes and attributes a stylesheet or a script
69//!   may rely on in the output), and the checks a tag name, an attribute name,
70//!   a role or a kind passes before it is written.
71//! - [`address`] — which addresses a link and an image may carry, the render
72//!   option [`HtmlAllowedUrlSchemes`] that decides which URL schemes are
73//!   allowed, and what happens to the other addresses.
74//! - [`construct`] — the construct element [`construct::TAG`]
75//!   (`flm.pres.html.tag`): one HTML tag as a node of the lowered tree, for a
76//!   lowering rule that must put HTML in the page that no shared presentation
77//!   element carries.
78//!
79//! # The backend's stages, tables and conventions
80//!
81//! - [`conventions`] — the HTML contract as data: one row per presentation
82//!   element, saying which tag, classes and attributes the output carries for
83//!   it, which payload fields it cannot express, and which style definition
84//!   gives it its default look.
85//! - [`mod@emit`] — the emitter: [`emit_tree`] writes the HTML of one lowered
86//!   tree; the conditions `flm.html.invalid-name`, `flm.html.refused-address`,
87//!   `flm.html.refused-in-math`, `flm.html.nesting-too-deep` and
88//!   `flm.html.worker-thread-unavailable`.
89//! - [`mod@stages`] — the emit stages [`HtmlEmitStage`] and
90//!   [`HtmlEmitPiecesStage`], the regions stage [`HtmlRegionsStage`], the final
91//!   stage [`HtmlFinalStage`], the tables they write and read, and the
92//!   conditions `flm.html.stage-failed`, `flm.html.missing-subtree`,
93//!   `flm.html.subtree-cycle` and `flm.html.piece-placement`.
94//! - [`mod@defs`] — the stylesheets this backend ships, the stage
95//!   [`ResolveDefinitionsStage`] that decides how each definition a document
96//!   requires reaches the page, the table `flm.html.definitions` it writes, and
97//!   the conditions `flm.html.unknown-requirement`,
98//!   `flm.html.unknown-definition` and `flm.html.definition-not-inlinable`.
99//! - [`mod@mathjax`] — the formula renderer's script definition
100//!   `flm.html.mathjax`, what its shipped configuration does, and the
101//!   configuration a host writes for itself when it renders mathematics written
102//!   by authors it does not trust.
103//! - [`mod@page`] — how the regions are made from the tables
104//!   ([`page::regions`]) and how the final stage assembles the page from them
105//!   ([`page::assemble`]).
106//! - [`mod@feature`] — [`HtmlBackend`], the native element set
107//!   ([`native_elements`]), the render options [`HtmlRenderOptions`], and the
108//!   four names [`FEATURE_NAME`], [`BACKEND`], [`OUTPUT`] and [`WORKFLOW`].
109//!
110//! **Not yet implemented:** a multi-file output, which would be a second final
111//! stage of this same backend.
112
113pub mod address;
114pub mod construct;
115pub mod conventions;
116pub mod defs;
117pub mod emit;
118pub mod escape;
119pub mod feature;
120pub mod markup;
121pub mod mathjax;
122pub mod names;
123pub mod page;
124pub mod sink;
125pub mod stages;
126
127pub use address::{
128    AddressUse, DEFAULT_ALLOWED_URL_SCHEMES, HtmlAllowedUrlSchemes, InvalidUrlScheme,
129    RefusedAddress, UrlScheme, check_address,
130};
131pub use construct::{HtmlTagPayload, TAG, TagBody};
132pub use defs::{
133    DEFINITIONS_TABLE, DefinitionKind, DefinitionNotInlinable, DefinitionPartMissing,
134    NotInlinablePart, RESOLVE_STAGE,
135    ResolveDefinitionsStage, ResolvedDefinition, ResolvedDefinitions, STYLE_PAGE,
136    UnknownDefinition, UnknownRequirement,
137};
138pub use conventions::{
139    AUXILIARY_CLASSES, CONTENT_CLASS, CONVENTIONS, Convention, LIST_LABEL_CLASS, TagRule,
140    UNKNOWN_MARKER_CLASS, markdown_table,
141};
142pub use emit::{
143    AddressRefused, EmittedTree, InvalidName, NestingTooDeep,
144    NoWorkerThread, RefusedInMath, emit_tree,
145};
146pub use escape::{escape_attr_into, escape_attr_single_quoted_into, escape_text_into};
147pub use feature::{
148    BACKEND, DEFAULT_TITLE, DefinitionOverride, FEATURE_NAME, FLOW_THROUGH, HtmlBackend,
149    HtmlRenderOptions, OUTPUT, WORKFLOW, feature, native_elements,
150};
151pub use markup::Markup;
152pub use mathjax::{MATHJAX_LOADER, mathjax_script};
153pub use names::{
154    class_of_element, class_of_role, data_attribute, is_valid_attribute_name, is_valid_tag_name,
155    is_valid_token, style_property,
156};
157pub use page::HtmlRegions;
158pub use sink::{HtmlSink, RawTextRefused, TagBuilder};
159pub use stages::{
160    ARTIFACT_NAME, ARTIFACT_OWNER, ARTIFACTS, DOCUMENT, EMIT_PIECES_STAGE, EMIT_STAGE, EXTENSION,
161    FINAL_STAGE, HtmlEmitPiecesStage, HtmlEmitStage, HtmlFinalStage, HtmlFragment, HtmlFragments,
162    HtmlPiece, HtmlPieces, HtmlRegionsStage, HtmlSegment, MEDIA_TYPE, MissingSubtree, OUTPUT_TABLE,
163    PIECES_TABLE, PiecePlacement, REGIONS, REGIONS_STAGE, SINGLEFILE, StageFailed, SubtreeCycle, stages,
164};