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_specinfo object provides a method named by recompose_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 override recompose_specinfo_method and rx_escape_chars_text to 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:

str

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_text are backslash-escaped, except when:

Parameters:
  • chars (str) – Raw character content from a LatexCharsNode.

  • parsing_state – The node’s parsing state.

Returns:

The (possibly escaped) string.

Return type:

str

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:

str

recompose_chars(chars, n)

Recompose a character-content node.

Converts chars to a string (handling None), then applies escape_chars() using the node’s parsing state.

Parameters:
  • chars – The raw character content (may be None).

  • n – The LatexCharsNode that owns this content.

Returns:

The escaped character string.

Return type:

str

node_standard_process_macro(node)

Process a macro node. If the node’s flm_specinfo provides a recompose method (named by recompose_specinfo_method), that method is used; otherwise falls back to the base-class recomposition.

Parameters:

node – A LatexMacroNode.

Returns:

Recomposed string.

Return type:

str

node_standard_process_environment(node)

Process an environment node. If the node’s flm_specinfo provides a recompose method (named by recompose_specinfo_method), that method is used; otherwise falls back to the base-class recomposition.

Parameters:

node – A LatexEnvironmentNode.

Returns:

Recomposed string.

Return type:

str

node_standard_process_specials(node)

Process a specials node. If the node’s flm_specinfo provides a recompose method (named by recompose_specinfo_method), that method is used; otherwise falls back to the base-class recomposition.

Parameters:

node – A LatexSpecialsNode.

Returns:

Recomposed string.

Return type:

str

visit_unknown_node(node, **kwargs)

Fallback visitor for node types not handled by the standard processing methods. Attempts the flm_specinfo recompose method first; if unavailable, delegates to the base class.

Parameters:

node – An unrecognized node.

Returns:

Recomposed string.

Return type:

str

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’s flm_specinfo is expected to provide a recompose_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 options and retrieved by feature code via get_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:

dict

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')).

Parameters:

key (str) – The feature key to look up in options.

Returns:

A dict of options for the given feature, or an empty dict if the key is absent.

Return type:

dict

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, a ValueError is 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'