Transforms
The transform system provides a powerful pipeline for modifying documents during conversion. Transforms operate on the AST (Abstract Syntax Tree) and can add, remove, or modify nodes.
Using Transforms
Transforms can be applied during conversion by passing them to the main API functions:
from all2md import to_markdown
from all2md.transforms import RemoveImagesTransform, HeadingOffsetTransform
markdown = to_markdown(
'document.pdf',
transforms=[
RemoveImagesTransform(),
HeadingOffsetTransform(offset=1)
]
)
Transforms can also be specified by name:
markdown = to_markdown('document.pdf', transforms=['remove-images', 'heading-offset'])
Transform Pipeline
The transform pipeline coordinates transform execution and rendering:
Pipeline for transforming and rendering AST documents. |
|
Render document with transforms and hooks using specified renderer. |
|
Apply transforms and hooks to document without rendering. |
Transform Registry
The registry manages transform discovery and instantiation. Custom transforms can be registered via Python entry points:
Registry for managing AST transforms. |
Built-in Transforms
Commonly used transforms included with all2md:
Remove all Image nodes from the AST. |
|
Remove nodes of specified types from the AST. |
|
Shift heading levels by a specified offset. |
|
Promote a leading H1 to a document title and shift subsequent headings. |
|
Generate a table of contents from document headings. |
|
Remove paragraphs matching common boilerplate patterns. |
|
Add footnote definitions for attachment references. |
|
Generate and add unique IDs to heading nodes. |
|
Rewrite link URLs using regex pattern matching. |
|
Find and replace text in Text nodes. |
|
Add conversion timestamp to document metadata. |
|
Calculate word and character counts and add to metadata. |
For the complete list, see all2md.transforms.builtin.
Transform Hooks
Hooks allow custom code execution at specific points in the pipeline:
Manager for registering and executing hooks. |
|
Context passed to hook functions. |
Transform Options
Options for configuring transform behavior:
Options for RemoveNodesTransform. |
|
Options for HeadingOffsetTransform. |
|
Options for GenerateTocTransform. |