Recompose FLM text: flm.flmrecomposer¶
This module provides tools to “recompose” FLM text, i.e., to transform a list of parsed FLM nodes back into FLM code. You can even recompose the node tree into standard LaTeX code. The FLM or LaTeX code is put together without any render-level processing (no equation numbering, etc.).
- class flm.flmrecomposer.FLMNodesFlmRecomposer¶
Recompose FLM source text from a parsed node tree.
Traverses a pylatexenc node tree and produces a string representation by visiting each node. By default the output is round-trip FLM markup that closely matches the original source. If a node’s
flm_specinfoobject provides a method named byrecompose_specinfo_method(default'recompose_flm_text'), that method is called to produce the text for the node; otherwise the base-class default recomposition is used.Subclasses (e.g.
FLMPureLatexRecomposer) can overriderecompose_specinfo_methodandrx_escape_chars_textto target a different output dialect.- recompose_specinfo_method = 'recompose_flm_text'¶
- recompose_flm_text(node)¶
Recompose FLM markup from the given node or node list.
This is the main entry point. Internally delegates to
start(), which walks the node tree via the visitor pattern.- Parameters:
node – A pylatexenc node or node list.
- Returns:
The recomposed FLM source text.
- Return type:
- rx_escape_chars_text = None¶
- recompose_escape_chars_if_specials_disabled = False¶
- escape_chars(chars, parsing_state)¶
Escape special characters in a text chunk according to the current parsing state.
Characters matching
rx_escape_chars_textare backslash-escaped, except when:rx_escape_chars_textisNone(no escaping configured),the parsing state is in math mode, or
specials are disabled and
recompose_escape_chars_if_specials_disabledisFalse.
- subrecompose(node)¶
Recompose a child node during a
recompose_*callback.Call this from within an
flm_specinfo.recompose_flm_text()(or analogous) method to recursively recompose a sub-node or node list using the same recomposer instance.- Parameters:
node – A pylatexenc node or node list to recompose.
- Returns:
The recomposed string for the sub-tree.
- Return type:
- recompose_chars(chars, n)¶
Recompose a character-content node.
Converts chars to a string (handling
None), then appliesescape_chars()using the node’s parsing state.- Parameters:
chars – The raw character content (may be
None).n – The
LatexCharsNodethat owns this content.
- Returns:
The escaped character string.
- Return type:
- node_standard_process_macro(node)¶
Process a macro node. If the node’s
flm_specinfoprovides a recompose method (named byrecompose_specinfo_method), that method is used; otherwise falls back to the base-class recomposition.- Parameters:
node – A
LatexMacroNode.- Returns:
Recomposed string.
- Return type:
- node_standard_process_environment(node)¶
Process an environment node. If the node’s
flm_specinfoprovides a recompose method (named byrecompose_specinfo_method), that method is used; otherwise falls back to the base-class recomposition.- Parameters:
node – A
LatexEnvironmentNode.- Returns:
Recomposed string.
- Return type:
- node_standard_process_specials(node)¶
Process a specials node. If the node’s
flm_specinfoprovides a recompose method (named byrecompose_specinfo_method), that method is used; otherwise falls back to the base-class recomposition.- Parameters:
node – A
LatexSpecialsNode.- Returns:
Recomposed string.
- Return type:
Recomposing “pure” (“standard”) LaTeX code¶
- class flm.flmrecomposer.purelatex.FLMPureLatexRecomposer(options)¶
Recompose a parsed FLM node tree into pure (standard) LaTeX code.
Unlike
FLMNodesFlmRecomposer, which round-trips back to FLM markup, this class converts FLM constructs into their standard LaTeX equivalents so the output can be compiled directly by a LaTeX engine. Each FLM feature’sflm_specinfois expected to provide arecompose_pure_latex(node, recomposer)method that emits the appropriate LaTeX.The recomposer tracks which LaTeX packages are required (via
ensure_latex_package()) and manages label safety mapping so that arbitrary FLM reference labels are converted to LaTeX-safe label strings.Per-feature options are stored in
optionsand retrieved by feature code viaget_options().- recompose_pure_latex(node)¶
Recompose pure LaTeX code from the given node or node list.
This is the main entry point for pure-LaTeX recomposition. Walks the node tree and returns a dict containing the generated LaTeX string and the set of required packages.
- Parameters:
node – A pylatexenc node or node list.
- Returns:
A dict with keys
"latex"(the LaTeX string) and"packages"(a dict mapping package names to their options).- Return type:
- rx_escape_chars_text = re.compile('[\\$&#\\^_%]')¶
- get_options(key)¶
Retrieve per-feature options from the recomposer configuration.
Called by feature
recompose_pure_latex()methods to obtain their feature-specific settings (e.g.recomposer.get_options('math')).
- ensure_latex_package(packagename, options=None)¶
Declare that the recomposed LaTeX output requires a given package.
Feature
recompose_pure_latex()methods call this to register package dependencies. If the package was already registered with the same (or no) options, the call is a no-op. If conflicting options are requested, aValueErroris raised.- Parameters:
packagename (str) – LaTeX package name (e.g.
'hyperref').options – Optional package options string, or
None.
- Raises:
ValueError – If the package was already registered with different options.
- make_safe_label(ref_domain, ref_type, ref_label, resource_info)¶
- recompose_delimited_nodelist(delimiters, nodelist, n)¶
- recompose_specinfo_method = 'recompose_pure_latex'¶