1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
//! `flm-core` is the Rust library that implements FLM ("flexible latexlike
//! markup"), a markup language for documents. FLM borrows its syntax from
//! LaTeX: commands begin with a backslash, environments are written as
//! `\begin{…}…\end{…}`, and a blank line separates two paragraphs. Each
//! construct of FLM expresses the meaning of a part of the document, not its
//! appearance, so that one source can be rendered in several output formats.
//!
//! The library parses FLM source, processes the document, and renders it in
//! four output formats: plain text, HTML, *rendered LaTeX* and *recomposed
//! LaTeX*. Rendered LaTeX is LaTeX for LuaLaTeX in which FLM has already
//! resolved the numbering, the cross-references and the other computed
//! content. Recomposed LaTeX is LaTeX source close to what an author would
//! type; it leaves the numbering and the cross-references to LaTeX.
//!
//! The library is built on [techy], a parser toolkit for LaTeX-like
//! languages, which provides the tokenizer, the parser and the node tree. The
//! library `flm-host`, which finds and reads the files and the settings of a
//! document and writes its outputs, uses this library, and so does the
//! command-line program `flm` (crate `flm`).
//!
//! The processing reads and writes no files. A program that uses the library
//! provides the source text and receives the output as
//! [artifacts](core::artifact::Artifact): each artifact is a value that
//! contains the content of one output file. The program decides where to
//! write the artifacts. When a document includes other files with `\input`,
//! the program provides an [input resolver](lang::InputResolver), which finds
//! and reads these files.
//!
//! # Using the library
//!
//! A program that renders an FLM document takes the following five steps. The
//! [Introduction](guide::introduction#embedding-the-library) shows them in a
//! complete program, and the chapter [Embedding flm-core](guide::embedding)
//! describes each of them in full.
//!
//! 1. **Build a registry.** A [registry](core::registry) contains the
//! definitions used to parse and process documents: the commands and
//! environments that an author can write, the processing stages, the tables
//! of computed data, the backends, the workflows, and so on. These
//! definitions are made in *features*: a
//! [feature](core::registry::FlmFeature) is a Rust value that contributes a
//! group of related definitions to a registry. Build the registry with a
//! [`RegistryBuilder`](core::registry::RegistryBuilder); most programs add
//! to it a package that holds the standard features of [`stdlib`]
//! ([`stdlib::add_features`]). The
//! result, an [`FlmRegistry`](core::registry::FlmRegistry), cannot be
//! changed after it is built. For documents that contain mathematics, the
//! registry also needs a feature that processes mathematics, such as the
//! feature of the package [`math_passthrough`]. Without such a feature,
//! each formula is reported as an error when the document is rendered.
//! 2. **Parse the source and place it in a document.**
//! [`parse_fragment`](lang::parse_fragment) parses one source text into a
//! [fragment](model::doc::Fragment), and the program places the fragment in
//! a [document](model::doc::Doc). To parse a file together with the files
//! that it includes, use [`assemble`](model::assemble::assemble).
//! 3. **Build a plan.** A [plan](core::plan::Plan) is the list of
//! [stages](core::stage::Stage) that a run executes, in the order of their
//! dependencies. A stage is one unit of processing work. The program states
//! in a [plan request](core::plan::PlanRequest) which *capabilities* it
//! wants. A [capability](core::registry::CapabilityDef) is a named state of
//! processing, such as a rendered output. The library then selects the
//! stages that provide these capabilities and orders them. Usually, the
//! program uses a [workflow](core::registry::WorkflowDef), a complete plan
//! request stored in the registry under a name such as `html`, and builds
//! the plan with [`Plan::for_workflow`](core::plan::Plan::for_workflow).
//! 4. **Execute a run.** A [run](core::run::Run) is one execution of a plan
//! over a document. Create the run with [`Run::new`](core::run::Run::new)
//! and execute it with [`Run::execute`](core::run::Run::execute).
//! 5. **Read the results.** A run stores what it computes in typed
//! [tables](core::data::table::Table), which
//! [`Run::table`](core::run::Run::table) returns. The files of an output,
//! such as the HTML page, form an [artifact
//! set](core::artifact::ArtifactSet), which is stored in the table
//! declared for that output ([`OutputDef`](core::registry::OutputDef)).
//! The problems found in the document are
//! [diagnostics](guide::concepts_overview#diagnostics), which
//! [`Run::diagnostics`](core::run::Run::diagnostics) returns.
//!
//! The registry and the plan do not depend on a document. A program therefore
//! builds them once and uses them for every document that it processes.
//!
//! A program that adds commands, checks or output formats of its own defines
//! them in features, and adds these features to the registry beside the
//! standard features. The standard features register in the same way. The
//! chapter [Extending FLM](guide::extending) describes this work.
//!
//! # How a document is processed
//!
//! Three design decisions shape the library. The [Concepts
//! overview](guide::concepts_overview) explains each of them.
//!
//! 1. **The parsed node trees are the only representation of a document.** A
//! document consists of the node trees that techy's parser produces. Each
//! node stores an [element](model::element::ElementSpec), a value that
//! describes what the node means, such as a heading or emphasis. The
//! library does not convert the trees into another data structure. See [the
//! document tree](guide::concepts_overview#the-document-tree).
//! 2. **The trees are frozen early, and computed data is stored in tables.**
//! The *rewrite stages* run first, and they are the only stages that change
//! the trees. Among the standard features, they insert the content of included
//! files, expand user-defined macros and environments, and group the
//! content into paragraphs and blocks. After the rewrite stages, the trees
//! are *frozen*, and no later stage changes them. Each later stage stores
//! what it computes in tables, such as the numbers of the headings or the
//! output text, or it checks the document and reports diagnostics. [Frozen
//! trees and computed
//! data](guide::concepts_overview#frozen-trees-and-computed-data) explains
//! the reasons for this design.
//! 3. **The library derives the processing from what a program requests.**
//! Each stage is declared with the capabilities that it provides and the
//! capabilities that it requires. A program *demands* capabilities in its
//! plan request, and the library builds the plan that provides them. A
//! feature can therefore add a stage without any change to the other
//! stages: the stage runs in every plan that needs a capability that it
//! provides. See [stages, capabilities, and
//! plans](guide::concepts_overview#stages-capabilities-and-plans).
//!
//! # Modules
//!
//! - [`model`]: the document model. It defines documents and fragments, the
//! elements that the nodes store and their aspects, the addresses of trees
//! and nodes, dotted names, and the option values set on a document.
//! - [`lang`]: the FLM language on top of techy. It contains the parse
//! functions, the construction of techy's command specifications from the
//! elements defined in the features ([`lang::mint`]), the recursion
//! guard and the worker threads ([`lang::guard`]), and the rewrite passes
//! that insert included files, expand user definitions and group content
//! into blocks ([`lang::rewrites`]).
//! - [`core`]: the processing framework. It contains the registry, stages and
//! the tables of computed data, plans and runs, lowering rules, artifacts,
//! services and settings.
//! - [`stdlib`]: the standard features. They provide the commands and
//! environments of the language, the stages that structure and check a
//! document, and the four backends `text`, `html`, `latex-rendered` and
//! `latex-recomposed`. [`stdlib::add_features`] adds them to a package that
//! a program declares.
//! - [`math_passthrough`]: the package `math-passthrough`. Its feature
//! processes mathematics by copying the source of each formula to the output
//! as the author wrote it.
//! - [`layout`]: the text layout engine, which wraps and indents text to a
//! given line width. The plain-text backend uses it.
//! - [`progress`]: the progress sheet of a build: the rows of the work under
//! way and how far each is, the request to stop a build, and the sink of the
//! log lines that the work writes.
//! - [`dump`]: tools to inspect and test what the library holds in memory:
//! projections of trees, tables and diagnostics that tests compare, golden
//! files, assertions on diagnostics, and the serialization of parses that
//! the command `flm dump` prints.
//! - [`guide`]: the chapters of the guide (see [Guide](#guide) below).
//!
//! # Cargo features
//!
//! The crate has two Cargo features, and both are enabled by default.
//!
//! - `parallel`: A [run](core::run::Run) executes independent work in
//! parallel, on a thread pool of the `rayon` crate. For instance, a stage
//! that runs once per tree runs on all the trees of a document at the same
//! time. The output is the same as without the feature. To run all the work
//! on one thread in a build with this feature, pass
//! [`RunOptions::sequential`](core::run::RunOptions::sequential) to
//! [`Run::execute`](core::run::Run::execute).
//! - `docs`: The library stores the documentation texts given with the
//! definitions of a registry: the texts of the commands, of their
//! arguments, of the features, of the stages, of the tables, of the
//! settings, and so on. The command `flm info` shows these texts. A program
//! that never shows them, such as a WebAssembly build, can disable the
//! feature; the library then stores no documentation text.
//!
//! When the feature is disabled, every builder method and constructor that
//! takes a documentation text, such as
//! [`ElementDef::docs`](lang::mint::ElementDef::docs), stores an empty text.
//! This also applies to the texts read from settings data, such as the
//! description of a user definition or of a workflow, because they pass
//! through the same builder methods. A text that a program assigns directly
//! to a public `docs` field is kept.
//!
//! Disabling the feature changes neither the rendered output of a document
//! nor the [registry digest](core::registry::FlmRegistry::digest). The JSON
//! Schema of a feature's settings
//! ([`SettingsDecl::json_schema`](core::settings::SettingsDecl::json_schema))
//! then has no top-level description, but it keeps the descriptions of the
//! individual settings.
//!
//! # Module layering
//!
//! The top-level modules form layers, and each module uses only the modules of
//! its own layer and of the layers below it:
//!
//! ```text
//! {model, lang} < core < dump < stdlib < math_passthrough layout, progress standalone
//! ```
//!
//! The modules [`model`] and [`lang`] form one layer because each uses the
//! other. The parser defined in [`lang`] stores an element in each node, and
//! elements are defined in [`model`]. A fragment, defined in [`model`],
//! contains a tree of the FLM language defined in [`lang`]. The [`layout`]
//! module uses no other module of this crate, and [`stdlib`] uses it in
//! addition to the modules of the lower layers. The [`progress`] module uses
//! no other module of this crate either, and [`core`] and [`stdlib`] use it.
//!
//! # Guide
//!
//! The [`guide`] module contains the narrative documentation of FLM, and the
//! other modules form the API reference. Start with the
//! [Introduction](guide::introduction). The [Concepts
//! overview](guide::concepts_overview) defines the main concepts of the
//! library, one per section. The [guide's landing page](guide) lists all
//! chapters: the chapters on the language and on the `flm` program for
//! authors, the chapters on the library for programmers, and condensed
//! chapters for AI assistants.
//!
//! [techy]: https://github.com/phfaist/techy
/// Re-exports the parser toolkit techy, on which FLM is built. The re-exported
/// crate is the version of techy that `flm-core` is compiled against.
///
/// A program or an extension that uses FLM works with techy's types, such as
/// node trees, diagnostics and serialization sessions. Use these types through
/// `flm_core::techy` rather than through a dependency on techy in your own
/// crate. If a build contains two versions of techy, the types of one version
/// are distinct from the types of the other, and the functions of one version
/// do not accept the values of the other. The re-export ensures that your code
/// uses the same version of techy as `flm-core`.
pub use techy;
/// Re-exports the Unicode-to-LaTeX encoder `untechxt`. The re-exported crate is
/// the version of `untechxt` that `flm-core` is compiled against.
///
/// `untechxt` converts text into LaTeX source. It replaces each character that
/// LaTeX does not accept, or treats specially, with LaTeX code that prints the
/// character, and it records what this code needs in the preamble of a LaTeX
/// document, such as a package or a font encoding. The LaTeX backends of
/// `flm-core` take the LaTeX code for each non-ASCII character of a document
/// from `untechxt` ([`stdlib::backends::latex::escape`]), and the interface of
/// that module contains types of `untechxt`, such as
/// [`PreambleNeeds`](untechxt::preamble::PreambleNeeds). Use `untechxt` through
/// `flm_core::untechxt` rather than through a dependency in your own crate, for
/// the reason given in the documentation of [`techy`].
pub use untechxt;
// The cargo feature `docs` is tested in this one function, which each builder
// calls in its own body, never where a builder is called: those calls number
// in the hundreds, and every one added would have to remember the test.
/// Returns the documentation text `docs` when the Cargo feature `docs` is
/// enabled, and an empty string when it is disabled.
///
/// Every builder method and constructor that stores a documentation text
/// passes the text through this function, so that a build without the feature
/// stores no documentation text. The code that calls these builder methods,
/// including the code that reads definitions from settings data, therefore
/// does not test the feature itself. Library code that sets a documentation
/// text without a builder method calls this function directly. For instance,
/// the shared LaTeX module ([`stdlib::backends::latex`]) calls it for the
/// LaTeX definitions that it derives from the Unicode-to-LaTeX table of
/// [`untechxt`].
pub
// The module layering stated in the crate documentation above is not something
// the compiler enforces; `tests/module_layering.rs` reads the source tree and
// fails on any `crate::` path pointing upward, and its ALLOWED table has to be
// updated whenever a top-level module is added or renamed.
// Narrative documentation: markdown pages in the workspace-level `docs/` rendered as
// doc-only modules. They are read through the symbolic link `flm-core/docs` to `../docs`,
// so that the crate's package holds them. `cfg(doc)` keeps them out of compiled code;
// rustdoc (including doctest collection) builds with `--cfg doc`, so code blocks in these
// pages still run as doctests. The docs sidebar pins these pages in a "Guide" section
// (docs/rustdoc-header.html, wired up in .cargo/config.toml). A new chapter takes four
// steps: its file `docs/NAME.md`, its module in the block below, its entry in
// GUIDE_PAGES of docs/rustdoc-header.html, and its line in the chapter index of
// docs/guide.md.