all2md.renderers.epub
EPUB rendering from AST.
This module provides the EpubRenderer class which converts AST nodes to EPUB format. The renderer uses ebooklib to generate EPUB3 packages with proper metadata, chapters, and navigation.
The rendering process splits the AST into chapters using configurable strategies (separator-based or heading-based), converts each chapter to XHTML, and assembles a complete EPUB package.
- class all2md.renderers.epub.EpubRenderer
Bases:
BaseRendererRender AST nodes to EPUB format.
This class converts an AST document into an EPUB3 package using ebooklib. It splits the document into chapters based on configured strategy and generates proper EPUB metadata and navigation.
- Parameters:
options (EpubRendererOptions or None, default = None) – EPUB rendering options
Examples
Basic usage:
>>> from all2md.options.epub import EpubRendererOptions >>> from all2md.ast import Document, Heading, Paragraph, Text >>> from all2md.renderers.epub import EpubRenderer >>> doc = Document(children=[ ... Heading(level=1, content=[Text(content="Chapter 1")]), ... Paragraph(content=[Text(content="Content here")]) ... ]) >>> options = EpubRendererOptions(title="My Book") >>> renderer = EpubRenderer(options) >>> renderer.render(doc, "output.epub")
Initialize the EPUB renderer with options.
- __init__(options: EpubRendererOptions | None = None)
Initialize the EPUB renderer with options.
- render(doc: Document, output: str | Path | IO[bytes]) None
Render the AST to an EPUB file.
- Parameters:
doc (Document) – AST Document node to render
output (str, Path, or IO[bytes]) – Output destination (file path or file-like object)
- Raises:
DependencyError – If ebooklib is not installed
RenderingError – If EPUB generation fails
Notes
The original AST document is preserved and not mutated during rendering. Image URL rewriting is performed on a deep copy of the document to ensure the input AST can be safely reused for multiple renderings.
- render_to_bytes(doc: Document) bytes
Render the AST to EPUB bytes.
- Returns:
EPUB file content as bytes
- Return type:
bytes
- visit_comment(node: Comment) None
Render a Comment node (no-op, handled by HTML renderer).
- Parameters:
node (Comment) – Comment to render
Notes
Comments are handled by the HtmlRenderer which is used to render EPUB chapter content. This method exists only to satisfy the visitor pattern but is never called during normal rendering flow.
- visit_comment_inline(node: CommentInline) None
Render a CommentInline node (no-op, handled by HTML renderer).
- Parameters:
node (CommentInline) – Inline comment to render
Notes
Inline comments are handled by the HtmlRenderer which is used to render EPUB chapter content. This method exists only to satisfy the visitor pattern but is never called during normal rendering flow.