Skip to main content

Crate flm_core

Crate flm_core 

Source
Expand description

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: 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, which finds and reads these files.

§Using the library

A program that renders an FLM document takes the following five steps. The Introduction shows them in a complete program, and the chapter Embedding flm-core describes each of them in full.

  1. Build a registry. A 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 is a Rust value that contributes a group of related definitions to a registry. Build the registry with a RegistryBuilder; most programs add to it a package that holds the standard features of stdlib (stdlib::add_features). The result, an 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 parses one source text into a fragment, and the program places the fragment in a document. To parse a file together with the files that it includes, use assemble.
  3. Build a plan. A plan is the list of stages 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 which capabilities it wants. A capability 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, a complete plan request stored in the registry under a name such as html, and builds the plan with Plan::for_workflow.
  4. Execute a run. A run is one execution of a plan over a document. Create the run with Run::new and execute it with Run::execute.
  5. Read the results. A run stores what it computes in typed tables, which Run::table returns. The files of an output, such as the HTML page, form an artifact set, which is stored in the table declared for that output (OutputDef). The problems found in the document are diagnostics, which 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 describes this work.

§How a document is processed

Three design decisions shape the library. The 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, 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.
  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 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.

§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 below).

§Cargo features

The crate has two Cargo features, and both are enabled by default.

  • parallel: A 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 to 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, 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. The JSON Schema of a feature’s 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:

{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. The Concepts overview defines the main concepts of the library, one per section. The guide’s landing page 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.

Re-exports§

pub use techy;
pub use untechxt;

Modules§

core
The processing framework of the library: the values a feature declares itself with, and the machinery that turns those declarations into a registry, a plan and a run over a document.
dump
Tools for inspecting and testing what the library holds in memory: parsed and processed trees, computed tables, and diagnostics.
guide
Guide
lang
The FLM language: the language type techy’s parser runs with, the parse entry points, and everything a parse needs.
layout
The layout engine: it lays text out in lines of a given width, for any output written as characters in fixed-width columns (plain text, a terminal).
math_passthrough
The package math-passthrough and its one feature, math-passthrough: mathematics carried to the output as the author wrote it, without being read.
model
The document model: the types that describe what a document is made of, how its parts are identified, and the option values set on it.
progress
The progress sheet of a build: what is being done and how far it is, the request to stop, and the log lines of the work.
stdlib
The standard features of flm-core: the features and backends that make the library a working FLM processor, and the function that adds them to a package.