Skip to main content

flm_core/stdlib/backends/text/
mod.rs

1//! The text backend: it prints a document as plain text, wrapped to a line
2//! width, through the [layout engine](crate::layout).
3//!
4//! A backend is the registration bundle for one output format: the elements
5//! its emitter writes natively, a lowering stage, an emit stage and a final
6//! stage ([`BackendDef`](crate::core::registry::BackendDef); see [backends,
7//! lowering, and outputs](crate::guide::concepts_overview#backends-lowering-and-outputs)).
8//! The text backend is registered by the `text` feature ([`TextBackend`],
9//! [`feature()`]), which also registers the `text` output and the `text`
10//! workflow ([`WORKFLOW`]). The standard features
11//! ([`stdlib::add_features`](crate::stdlib::add_features)) include that feature
12//! together with the features it needs (see [`feature()`]).
13//!
14//! # Using the backend
15//!
16//! A caller builds a plan for the `text` workflow
17//! ([`Plan::for_workflow`](crate::core::plan::Plan::for_workflow) with
18//! [`WORKFLOW`]), runs it over a document, and reads one of two tables:
19//!
20//! - [`TEXT_OUTPUT`] (`flm.output.text`), a [`TextOutput`]: the printed text,
21//!   its position marks, which give for each printed part the presentation
22//!   node it was printed from ([`TextMark`]), and its style marks, which give
23//!   the printed ranges whose content carries a property plain text cannot
24//!   show, such as bold weight or a link's address ([`TextStyleMark`]);
25//! - [`TEXT_DOCUMENT`] (`flm.output.text.document`), the `text` output's
26//!   artifact set: one entry artifact `text:document` of media type
27//!   `text/plain`.
28//!
29//! The render options ([`TextRenderOptions`]) set the line width, the width
30//! of a tab, the stop after a run-in heading's title, whether links print
31//! their address, the heading level printed as a first-level heading, how a
32//! table is drawn ([`TableFrame`]) and the characters of the rules under the
33//! headings ([`HeadingRules`]).
34//! A program that writes the text to a medium able to show what plain text
35//! cannot reads the style marks; the extension `profile-tty`, which is
36//! compiled into the `flm` program, paints the printed text from them for a
37//! terminal, under the same render options.
38//! [The plain-text backend](crate::guide::rendering#the-plain-text-backend), in
39//! the rendering guide, shows a complete run.
40//!
41//! # The stages
42//!
43//! The text backend writes natively every element of the [presentation
44//! vocabulary](crate::stdlib::backends::pres), its two [construct
45//! elements](construct) (`flm.pres.text.flush-right`, content set at the right
46//! edge of its line, and `flm.pres.text.ruled`, blocks set between rule
47//! lines), and the six [structural
48//! elements](crate::lang::structural::StructuralElements) of [`FLOW_THROUGH`]
49//! (see [`native_elements`]). A plan of the `text`
50//! workflow runs, in order:
51//!
52//! 1. the lowering stage `flm.text.lower`: the shared
53//!    [`LoweringStage`](crate::stdlib::backends::lowering::LoweringStage) for
54//!    this backend, which writes the lowered trees into `flm.lowered.text`;
55//! 2. the pieces stage `flm.text.pieces`: the shared
56//!    [`PiecesStage`](crate::stdlib::backends::pieces::PiecesStage) for this
57//!    backend, which lowers the [pieces](crate::core::piece) the render places
58//!    (document-level values such as a list of notes) into
59//!    `flm.lowered.text.pieces`;
60//! 3. the emit stage `flm.text.emit` ([`TextEmitStage`]), which prints the
61//!    pieces placed before the content, the lowered trees in document order
62//!    and the pieces placed after the content into one [`TextOutput`];
63//! 4. the regions stage `flm.text.regions` ([`TextRegionsStage`]), which
64//!    writes that text as the output's one [output
65//!    region](crate::core::region::Regions), its content;
66//! 5. the backend's final stage `flm.text.final` ([`TextFinalStage`]), which
67//!    writes the content as the artifact set of [`TEXT_DOCUMENT`].
68//!
69//! # Items of this module
70//!
71//! - [`mod@construct`]: the construct elements [`FLUSH_RIGHT`]
72//!   (`flm.pres.text.flush-right`) and [`RULED`] (`flm.pres.text.ruled`), their
73//!   payloads [`FlushRightPayload`] and [`RuledPayload`], the gap
74//!   [`FLUSH_RIGHT_GAP`], and their payload readers
75//!   ([`construct::payload_readers`]), for a lowering rule that sets content
76//!   at the right edge of a line or between rule lines.
77//! - [`mod@emit`]: the emitter [`emit()`], the output table [`TextOutput`]
78//!   with its position marks ([`TextMark`]) and its style marks
79//!   ([`TextStyleMark`], [`TextStyle`]), and the conditions
80//!   `flm.text.flush-right-lines` ([`FlushRightLines`]),
81//!   `flm.text.missing-subtree` ([`MissingSubtree`]),
82//!   `flm.text.subtree-cycle` ([`SubtreeCycle`]), `flm.text.piece-placement`
83//!   ([`PiecePlacement`]), `flm.text.nesting-too-deep` ([`NestingTooDeep`])
84//!   and `flm.text.worker-thread-unavailable` ([`NoWorkerThread`]).
85//! - [`mod@stages`]: the names of the backend, its stages and tables,
86//!   [`TextEmitStage`], [`TextRegionsStage`], [`TextFinalStage`],
87//!   [`TextRenderOptions`], and the condition `flm.text.stage-failed`
88//!   ([`StageFailed`]).
89//! - [`mod@feature`]: [`TextBackend`], the feature that registers the backend
90//!   and its construct elements, and its native element set.
91//! - [`mod@superscript`]: the Unicode superscript and subscript forms with
92//!   which a superscript or subscript span is printed.
93
94pub mod construct;
95pub mod emit;
96pub mod feature;
97pub mod stages;
98pub mod superscript;
99
100pub use construct::{FLUSH_RIGHT, FLUSH_RIGHT_GAP, FlushRightPayload, RULED, RuledPayload};
101pub use emit::{
102    Emitted, FlushRightLines, MissingSubtree, NestingTooDeep, NoWorkerThread, PiecePlacement,
103    SubtreeCycle, TEXT_OUTPUT, TextMark, TextOutput, TextStyle, TextStyleMark, emit,
104};
105pub use feature::{FEATURE_NAME, FLOW_THROUGH, TextBackend, feature, native_elements};
106pub use stages::{
107    ARTIFACT_NAME, ARTIFACT_OWNER, BACKEND, EMIT_STAGE, EXTENSION, FINAL_STAGE, HeadingRules,
108    MEDIA_TYPE, REGIONS_STAGE, StageFailed, TEXT_DOCUMENT, TEXT_REGIONS, TableFrame,
109    TextEmitStage, TextFinalStage, TextRegionsStage, TextRenderOptions, WORKFLOW, stages,
110};