guidedoc_typst ============== .. rust:crate:: guidedoc_typst :synopsis: The Typst-source renderer of Guidedoc: a validated document becomes one Typst source file. The Typst-source renderer of Guidedoc: a validated document becomes one Typst source file. It writes text, not PDF; ``gd-typst`` compiles it. The output is a fixed, trusted preamble of ``gd-`` helper definitions (:rust:const:`PREAMBLE `) followed by the body. Source text never becomes executable Typst: markup text escapes every ASCII punctuation character, and code, URLs, labels, and paths are string literals. Raw Typst is written only under ``ContentPolicy::Allow``; LaTeX math goes through a bounded adapter (:rust:fn:`translate_math `) whose failures follow the math policy, and MathML through a bounded translator (:rust:fn:`translate_mathml `) whose output is generated markup, written under every policy. .. code-block:: rust use guidedoc::Registry; let registry = Registry { readers: vec![], renderers: vec![guidedoc_typst::RENDERER] }; assert!(registry.find_renderer("typst").is_some()); assert!(guidedoc_typst::PREAMBLE.contains("#let gd-link")); .. rubric:: Structs .. rust:struct:: pub struct MathError<'a> The first construct of a formula the adapter cannot translate: a command such as ``\color``, borrowed from the formula, or a description such as "an unclosed {". .. rubric:: Fields .. rust:field:: pub construct: &'a str The command or the description of the construct. .. rubric:: Trait Implementations .. rust:impl:: impl fmt::Display for MathError<'_> :noindex: :for: MathError<'_> :trait: fmt::Display .. rust:method:: fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result :noindex: .. rust:impl:: impl std::error::Error for MathError<'_> :noindex: :for: MathError<'_> :trait: std::error::Error .. rust:struct:: pub struct MathMlMath What the translator found out about a formula it wrote. .. rubric:: Fields .. rust:field:: pub block: bool The root says ``display="block"``: the formula is meant to stand on its own line. .. rust:struct:: pub struct MathMlUnsupported The first construct of a MathML formula the translator cannot write as Typst math: an element such as ````, or a description such as "more than 4096 elements". .. rubric:: Fields .. rust:field:: pub construct: String The element or the description of the construct. .. rubric:: Trait Implementations .. rust:impl:: impl fmt::Display for MathMlUnsupported :noindex: :for: MathMlUnsupported :trait: fmt::Display .. rust:method:: fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result :noindex: .. rust:impl:: impl std::error::Error for MathMlUnsupported :noindex: :for: MathMlUnsupported :trait: std::error::Error .. rubric:: Functions .. rust:function:: pub fn is_label_safe(name: &str) -> bool Tells whether ``name`` can be written as a Typst label literal ````: non-empty, made of letters, digits, non-ASCII characters, and ``-``, ``_``, ``.``, ``:``. .. rust:function:: pub fn write_markup_text(out: &mut (impl TextSink + ?Sized), text: &str) Writes ``text`` as inert Typst markup. Every ASCII punctuation character is escaped with a backslash, which Typst accepts for all of them, so no markup (``#``, ``$``, ``*``, ``_``, , ``<``, ``@``, ``[``, ``=``, ``-``, ``+``, ``/``, ``~``, quotes, ``//`` comments, list and heading markers, shorthands such as ``--`` or ``...``, URL autolinks) can arise from text. Line breaks are white space; control characters are dropped. It never fails. .. rust:function:: pub fn write_string_literal(out: &mut (impl TextSink + ?Sized), s: &str) Writes ``s`` as a Typst string literal, quotes included. It never fails. .. rust:function:: pub fn translate_math<'a>(latex: &'a str, out: &mut String) -> Result<(), MathError<'a>> Appends the Typst math form of ``latex`` (without ``$`` delimiters) to ``out``. On failure it returns the first unsupported construct; ``out`` then holds an incomplete translation the caller should discard. It never panics, for any input. .. rust:function:: pub fn translate_mathml(mathml: &str, out: &mut String) -> Result Appends the Typst math form of ``mathml`` (one ``math`` element, without ``$`` delimiters) to ``out``. It borrows both for the call. The output may name ``gd-math-font``, as the output of :rust:fn:`crate::translate_math ` does. On failure it returns the first construct it cannot translate, and ``out`` is as it was. It never panics, for any input. .. rust:function:: pub fn renderer() -> Renderer Returns :rust:const:`RENDERER `, the Typst renderer descriptor. .. rust:function:: pub fn emit_preamble(out: &mut Emitter<'_>) Writes :rust:const:`PREAMBLE `. A host composing a book from body-only chapters (``RenderOptions::standalone = false``) writes it once, before the chapters. .. rust:function:: pub fn render(ctx: &mut RenderContext<'_>) -> Result<(), Fail> Writes ``ctx.doc`` as Typst source through ``ctx.out``: the preamble, the document and page settings, and the body when ``ctx.options.standalone``, else the body alone. It borrows the context for the call. Problems go to ``ctx.reports()``; it fails with the first error (raw content the policy refuses, an unresolved reference, untranslatable math under the refuse policy, nesting past the limits), or ``Capacity`` when the per-node marks cannot be allocated. .. rubric:: Constants .. rust:const:: pub const SOURCE_LABEL: &str = "gd-source" The label of the metadata that carries block source positions in a book's Typst (see ``RenderOptions::source_positions``). .. rust:const:: pub const MAX_MATHML_ELEMENTS: usize = 4096 The most elements of one formula the translator reads. .. rust:const:: pub const MAX_MATHML_NESTING: usize = 48 The deepest element nesting the translator follows. Typst nests one call for each level, so the bound is lower than the filter's. .. rust:const:: pub const CITATION_MISSING: Message A citation reference without a citation. .. rust:const:: pub const DEPTH_LIMIT: Message Rendering recursion past the depth bound. .. rust:const:: pub const DIMENSION_IGNORED: Message An image size that is not a length. .. rust:const:: pub const FOOTNOTE_MISSING: Message A footnote reference without a footnote. .. rust:const:: pub const IMAGE_REMOTE: Message A remote image, which Typst cannot embed. .. rust:const:: pub const LANGUAGE_INVALID: Message ``RenderOptions::language`` is not a language tag. .. rust:const:: pub const LINK_UNRESOLVED: Message A reference nothing resolved. .. rust:const:: pub const MATH_MATHML: Message A formula written as MathML that the translator cannot write as Typst math. .. rust:const:: pub const MATH_OMITTED: Message Math the adapter cannot translate, under the omit policy. .. rust:const:: pub const MATH_UNSUPPORTED: Message Math the adapter cannot translate, under the refuse policy. One code, three severities: the conversion policy decides whether the formula fails the conversion, is omitted, or is kept as source. .. rust:const:: pub const MATH_VERBATIM: Message Math the adapter cannot translate, under the allow policy: kept as LaTeX source. .. rust:const:: pub const RAW_FOREIGN: Message Raw content for another format, under the refuse policy. .. rust:const:: pub const RAW_FOREIGN_OMITTED: Message Raw content for another format, under the omit or allow policy: never written. .. rust:const:: pub const RAW_OMITTED: Message Raw Typst under the omit policy. .. rust:const:: pub const RAW_REFUSED: Message Raw Typst under the refuse policy. .. rust:const:: pub const SUBSTITUTION_MISSING: Message A substitution without a definition, or one that refers to itself. .. rust:const:: pub const URL_OMITTED: Message An unsafe link under the omit policy: only its text is kept. .. rust:const:: pub const URL_UNSAFE: Message A ``javascript:``, ``vbscript:``, or non-image ``data:`` link under the refuse policy. .. rust:const:: pub const VERSION: &str = env!("CARGO_PKG_VERSION") The renderer's version: the project version. .. rust:const:: pub const CONFORMANCE: Conformance = Conformance::Partial How completely the renderer covers the document model. .. rust:const:: pub const RENDERER: Renderer The "typst" renderer descriptor, for a ``guidedoc::Registry``. It has no state. .. rust:const:: pub const PREAMBLE: &str = include_str!("preamble.typ") The helper definitions every body relies on: fixed, trusted Typst into which no document data is ever interpolated.