Skip to main content

Module compose

Module compose 

Source
Expand description

The feature compose: the stages that assemble each output’s entry artifact from the output’s regions through a compose template, a text with named places for those regions, and the service slots of the template engines and of the compose templates.

An output region is one named part of an output’s entry artifact (the document’s content, each piece placed beside it, and on HTML the stylesheets, the scripts, the title and the language), which the backend writes once into the table flm.output.<backend>.regions (Regions). The entry artifact is the artifact that represents the output as a whole: the HTML page, the printed text, the recomposed LaTeX document (ArtifactSet). The backend’s final stage assembles the entry artifact from those regions in a layout of the backend’s own (crate::core::region). The feature compose is a second way to assemble them: through the compose template that the settings choose, so that a program chooses what surrounds the document’s content.

§A host registers the feature

The feature is not among the standard features that stdlib::add_features adds. The composer of an output provides the same capability as the backend’s final stage, and a plan chooses between two stages that provide one capability only by a stage the plan request prefers or by a capability marked as the default; with neither, the plan fails (PlanError::Contested). So a host that registers the feature also prefers the composer of each output it renders (stage_name; Overrides::prefer). A registry without the feature has one stage per output that assembles the entry artifact, the backend’s final stage. Where a host registers the feature but the settings switch it off, the host leaves it out of the registry, and the backend’s final stage assembles each output.

§Template engines and compose templates

The feature declares two service slots, to which any feature of any package may contribute. A service slot is the declaration, by the feature that owns an interaction, of a dotted name, the type of the values the slot takes and its arity (crate::core::services). A feature of another package contributes to them as a peer (FeatureBuilder::peer_slot), so that its registry still builds where compose is switched off.

  • ENGINES_SLOT (flm.compose.engines) takes the template engines, each a TemplateEngine with a name. compose contributes content-only, which places the content and the pieces with nothing around them (engines); a host contributes the engines of the template languages it reads.
  • TEMPLATES_SLOT (flm.compose.templates) takes the compose templates, each a Template: its name, the output it serves, its engine, its text, the pieces it accepts and those it requires, and its own settings with their defaults. Template::from_data makes one from a description given as data, whose keys name and output give the compose template’s name and the output it serves, and whose key main gives the path of the file that holds the compose template’s text, relative to the folder of the description; without main, the text is empty. compose contributes none.

§The settings of a compose template

A compose template’s own settings are written under the render option template_config, at template_config.OUTPUT.NAME (ComposeOptions). The feature lets another feature declare the schema of each of these entries (SettingsDecl::open_entries, EntryDecl): the feature that contributes a compose template declares its settings (declare_settings), from the JSON Schema of the compose template (Template::settings), each of whose properties is one setting with its type, its description, its default and its marks. A source of settings that writes one setting of a compose template leaves the others as the lower sources write them, and a key that the schema lacks is left out with a warning; the defaults are the compose template’s (Template::config).

§The stage of each output

The feature registers one stage per output made of output regions, that is, per output for which a backend contributes a regions output (RegionsOutput) to the service slot flm.output.regions (REGIONS_SLOT; served_output). A regions output is a backend’s statement that its output is made of output regions: which table holds them and which capability its final stage provides. The stages are registered in the feature’s second round of registration, a Finalizer that runs once every feature has registered, so every backend has contributed to that service slot by then.

The stage of an output, flm.compose.<output> (stage_name), is called the composer in this module. It writes the output’s artifact set into the same table as the backend’s final stage and provides the same capability, so a plan can schedule either one; the host prefers the composer (see above).

The composer reads the output’s regions and the files that the output’s stages write beside the entry artifact (the style file of the recomposed LaTeX, the copies of images), applies the compose template that the render option ComposeOptions chooses for the output (chosen_template), and writes the entry artifact and those files as the output’s artifact set (ArtifactSet::insert_entry_with). It reports a problem as the Error TemplateFailed; the output is then not made. It reports each warning of the template engine as the Warning TemplateWarning; the output is still made.

§What a compose template reads

NameWhat it is
regionsevery output region, by name, as text: content; each piece’s name; on HTML styles, scripts, title (the words of the page’s title, escaped for HTML: the words of the document’s title piece, else the title that the render option html.title gives) and lang (the language tag, present only when the render option html.lang gives one); on the recomposed LaTeX style-package, when the output has a style file
piecesthe regions that are pieces, by name, in the order of the output’s regions; each has text, words (its displayed characters as plain text) and place (before-content or after-content)
metadatathe document’s metadata (DocMeta): title, author, date, and every other entry the program gives, each value as it is given. author is a list with one map per author, a name alone included: name, affiliation (a list), email (where the author has one), note (a list) and the author’s other keys. The title, the date and an author’s name, affiliations and notes are the texts the author wrote, FLM source and not plain text (A \emph{fine} paper); a compose template that shows them uses the pieces instead: pieces["flm.metadata.title"].text for the HTML, or .words for plain words
configthe compose template’s own settings: its defaults, with the values that the render option template_config gives for it on the output written over them key by key
artifactsthe path of each file the compose template ships, by the file’s name (below): artifacts.style gives paper-style.css; for a folder it ships, the folder’s path, with no trailing slash: artifacts.icons gives paper-icons, and a page writes {{ artifacts.icons }}/check.svg

A compose template that refers to a value that nothing provides cannot be applied: the value is not taken as an empty text.

§Files a compose template ships

A compose template may ship files of its own, written beside the entry artifact: a stylesheet, a script, a logo. It ships them in two ways: the files its description lists, and the files its template engine ships while it applies it (below).

The files of the description (Template::artifacts, TemplateArtifact; the key artifacts of its description, Template::from_data) each have a name, and the identity of each is compose:<name>. The composer asks the program’s naming of files (the service slot flm.artifacts.namer, ArtifactNamer) for the path of each, and gives the paths to the compose template as the value artifacts. A path is relative to the folder where the program writes the output’s files, which is where the entry file lies under a host’s usual naming. A file marked to be applied (the field apply of TemplateArtifact::File) is applied as a template of the same template engine (TemplateEngine::render_text), with the context of the entry artifact, its value html set to whether the file’s media type is text/html; the result is the file’s content. The composer then adds each file to the output’s artifact set, with its media type and extension, and the entry artifact links to each (Artifact::refs). A compose template that ships files cannot be applied where no package in use provides a naming of files. The composer takes the list from the template engine (TemplateEngine::artifacts), once per application and before naming the files. By default it is the compose template’s own list; an engine whose compose templates choose their files when they are applied gives them there.

A compose template may also ship a whole folder as one file of this list (the key dir of its description in place of file; TemplateArtifact::Folder): every file under the folder, at any depth, by its path relative to the folder. The composer asks the naming of files for the folder’s path (a path with no extension, ArtifactRef::folder), gives that path to the compose template, with no trailing slash, and adds a folder artifact (Artifact::folder) to the output’s artifact set; the program writes each of its files under that folder with its own path. A folder is never applied as a template, and its files’ media types come from their extensions.

§A file shipped while the template is applied

A template engine whose language ships a file, where the compose template names it as it is applied, hands the file to the composer through the handle TemplateShipper that it receives in TemplateEngine::render and TemplateEngine::render_text (ShippedFile: a stem, an extension, a media type and the bytes), and writes the path that the handle returns. The composer makes one handle per application, which serves every call of that application. A file’s identity is compose:<stem>-<digest>: its stem, every character other than an ASCII letter, a digit, - and _ written _, and eight hexadecimal digits of a digest of its bytes and its media type, so that the same file shipped twice is one file, under the one path. Its path is the naming of files’ path for that identity, its media type and its extension. Once the compose template is applied, the composer adds the shipped files to the output’s artifact set, after the files of the description, in the order in which they were first shipped, and the entry artifact links to each. Where no package in use provides a naming of files, the handle refuses the file, and the compose template cannot be applied.

The template engine also reports a warning through the handle (TemplateShipper::warn), which it also receives in TemplateEngine::artifacts, for example the use of a kept copy of a downloaded file that could not be downloaded again. The composer reports each distinct warning once per application as the Warning TemplateWarning, whether the compose template is applied or not.

§Files a compose template includes

A compose template’s text, and the text of a file it ships and applies, may include other files, as its template engine’s language provides: a file applied as a template of the same engine, with the same context, or a file whose text is written as it is. Each text carries its place: the file it was read from, given as a qualified path, a path with a root such as ext+NAME:templates/html/page/page.html or std:templates/html/simple/simple.html (Template::source, the field source of TemplateArtifact::File). The template engine reads an included file when the compose template is applied, so that its name may be computed and need not be listed ahead; a path written without a root is read beside the file of the text that writes it. This crate reads no file of its own: the template engine reads through the program that provides it, which records each read.

§On HTML: one element per region, and the composer’s table

On HTML, the composer writes the content and each piece inside a <div> element whose identifier is derived from the region’s name (region_id: flm-region-content, flm-region-flm.metadata.title), before the compose template places it, whichever compose template is used. The composer also writes the table HTML_REGIONS (flm.compose.html.regions, a ComposedRegions), which holds:

  • the HTML that the page holds under the identifier of the content and of each piece;
  • the words of the page’s title (the region title);
  • the rest of the page, everything outside those regions (the field skeleton): the page with exactly what the table holds left out, and everything else kept, namely the stylesheets, the scripts, the language, the compose template’s own markup, and the words of each piece wherever the compose template writes them.

The table holds what a page can bring up to date by itself: an element found by its identifier, and the page’s title, which has its place in every page. The words of a piece have no such place, so a change of them is a change of the rest of the page. The regions in the table and the rest of the page together make up the whole page, so two builds of one document that differ anywhere differ either in the rest of the page or in one entry of the table. A live preview can compare two builds this way, to tell a change of one region, which it applies in place, from a change of the page.

§Which pieces a render builds

A piece is a document-level value that a backend places before or after the document’s content, such as the title, the list of footnotes or the bibliography (Piece). The pieces that a compose template accepts are the pieces that a render request places on the backend of the compose template’s output: the host sets the library’s selection of pieces for that backend from the chosen compose template (accepted_pieces, select_pieces), so that a piece that the compose template does not place is never built. The pieces that the compose template requires (Template::requires) are placed although a piece may be declared off by default: select_pieces includes them. When the plan is built, the composer declares that it reads each required piece; a plan in which no stage provides a required piece therefore fails to build, with an error that gives the piece’s name.

§Map of the items

Re-exports§

pub use engines::CONTENT_ONLY;
pub use engines::ContentOnlyEngine;

Modules§

engines
The template engine that the feature compose contributes, ContentOnlyEngine (content-only).

Structs§

ComposeFeature
The feature compose: see the module documentation. A host registers it; it is not among the standard features that stdlib::add_features adds.
ComposeOptions
The render options of the feature compose, set per render request: the compose template each output is assembled with, and each compose template’s own settings.
ComposedRegion
One region as the page holds it.
ComposedRegions
The composer’s table on HTML, HTML_REGIONS: each region that the composer placed, and the rest of the page (see the module documentation).
Delimiters
The three pairs of delimiters of a compose template written in a Jinja-like template language (such as the language of a host’s minijinja engine), for a compose template whose output itself uses the usual ones ({% %}, {{ }}, {# #}), such as LaTeX. A template engine that reads no delimiters ignores them.
PieceValue
One piece as a compose template reads it: see the module documentation.
ShippedFile
A file that a template engine ships while it applies a compose template, which the composer writes beside the entry artifact (see the module documentation).
Template
A compose template: a text with named places for an output’s regions, applied by a template engine (see the module documentation).
TemplateContext
What a compose template reads: see the module documentation. The stage hands it to the template engine together with the compose template.
TemplateFailed
Condition flm.compose.template-failed (Error): the chosen compose template cannot be applied. The stage flm.compose.<output> reports it when the settings that choose the compose template cannot be read, when no compose template of the chosen name serves the output or more than one does, when its template engine is not registered, when the output’s regions, the files written beside the entry artifact or the document’s metadata cannot be read, when its template engine cannot give the files it ships (TemplateEngine::artifacts), when it ships files and no package in use provides a naming of files, when its text or the text of a file it applies does not compile or refers to a value that nothing provides, or when the output’s artifact set cannot be written. The output is then not made, with one exception: when a file that the compose template ships cannot be added to the artifact set after the entry artifact was written, the output is made without that file.
TemplateWarning
Condition flm.compose.template-warning (Warning): the template engine reported a warning while it applied the chosen compose template (TemplateShipper::warn). The stage flm.compose.<output> reports each distinct warning once per application, in the order first given; the output is made.

Enums§

Accepts
Which pieces a compose template accepts: every piece, or the pieces listed.
ComposedKind
What one region’s place in the page holds: HTML, or plain words.
TemplateArtifact
One file, or one folder, a compose template ships, written beside the entry artifact (see the module documentation).
TemplateChoice
Which compose template a render uses: one name for every output, or a name per output.

Constants§

DEFAULT_TEMPLATE
The name of the compose template that a render uses when the settings choose none, simple. The feature ships no compose template of that name: a host contributes the compose templates it offers, and where none of them serves an output under the chosen name, the composer of that output reports TemplateFailed.
ENGINES_SLOT
The name of the service slot flm.compose.engines, which takes any number of template engines (TemplateEngine), each with a name. Where several engines have the name a template gives, the engine of the feature placed last in the feature order applies.
FEATURE
The name under which the feature is registered, compose.
HTML_REGIONS
The typed name of the composer’s table on HTML, flm.compose.html.regions (ComposedRegions): the HTML that the page holds under each region’s identifier, the words of the page’s title, and the rest of the page. See the module documentation.
REGION_ID_PREFIX
The prefix of the identifier of the element that holds a region on HTML, flm-region-: see region_id.
TEMPLATES_SLOT
The name of the service slot flm.compose.templates, which takes any number of compose templates (Template), each with a name and the output it serves.

Traits§

TemplateEngine
A template engine: the code that applies a compose template written in its language to a TemplateContext.
TemplateShipper
The handle through which a template engine ships a file, and reports a warning, while it applies a compose template (TemplateEngine::render, TemplateEngine::render_text; TemplateEngine::artifacts reports a warning through it).

Functions§

accepted_pieces
Returns the pieces that the compose template template accepts among those that registry declares, in the order of their declarations: the selection of pieces that a host writes for the output’s backend into PiecesRenderOptions::backends.
chosen_template
Returns the compose template of the registry registry that options chooses for the output output.
declare_settings
Returns decl, the settings declaration of a feature that contributes the compose templates templates, declaring the settings of each one that has a schema (Template::settings) as the entry template_config.OUTPUT.NAME of the feature compose (declare_template_settings).
declare_template_settings
Returns decl, the settings declaration of a feature that contributes the compose template name of the output output, declaring schema, the JSON Schema of the compose template’s settings, as the entry template_config.OUTPUT.NAME of the feature compose (SettingsDecl::entry_data), labeled compose template ‘NAME’ for the output ‘OUTPUT’. A schema without a description gets one that names the compose template. declare_settings declares the compose templates of a list this way; a feature that contributes a compose template known by its name and its output before it is made (a template module of an extension) calls this directly.
region_id
Returns the identifier of the element that holds the region name on HTML: REGION_ID_PREFIX followed by the name as it is written, dots included (a . is allowed in an HTML identifier). Two distinct names therefore never share an identifier, and the name is read back from the identifier by removing the prefix. The identifier is derived from the name alone, so adding a region never changes the identifier of another.
select_pieces
Sets, in pieces, the selection of pieces of the backend of output to the pieces that the compose template template accepts (accepted_pieces), and adds the pieces it requires (Template::requires) to the pieces placed although declared off by default (PiecesRenderOptions::include), so that what the compose template requires is placed.
served_output
Returns what a backend states about the output output of registry when that output is made of output regions (RegionsOutput, contributed to the service slot REGIONS_SLOT), and None otherwise. The composer serves exactly those outputs.
stage_name
Returns the name of the composer’s stage for the output output, flm.compose.<output>.