summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--wikimini/formats/__init__.py148
1 files changed, 148 insertions, 0 deletions
diff --git a/wikimini/formats/__init__.py b/wikimini/formats/__init__.py
new file mode 100644
index 0000000..8d6296c
--- /dev/null
+++ b/wikimini/formats/__init__.py
@@ -0,0 +1,148 @@
+"""The formats are responsible for turning a
+:class:`~wikimini.document.Document` into an output string.
+
+Formats work by being given a file-like buffer as argument, into which the
+output should be written.
+"""
+from typing import TextIO
+
+from ..document import (
+ Document, Block, BlockLink, BlockQuote, Heading, ItemList, LineBreak,
+ Paragraph, Verbatim, Node, InlineLink, Plain, Style,
+)
+
+
+class Format:
+ """:class:`Format` is the base class for all output formats.
+
+ Any output format should inherit from this class and override the specific
+ output methods. Note that by default, no output is generated.
+
+ The methods :meth:`render_document`, :meth:`render_block` and
+ :meth:`render_node` have sensible default implementations that dispatch to
+ the more specific rendering methods.
+
+ Attributes:
+ writer: The file-like object that output should be written to.
+ """
+ writer: TextIO
+
+ def __init__(self, writer: TextIO):
+ self.writer = writer
+
+ def render_document(self, document: Document):
+ """Renders the given document.
+
+ Args:
+ document: The document to render.
+ """
+ for block in document:
+ self.render_block(block)
+
+ def render_block(self, block: Block):
+ """Renders a single block.
+
+ Args:
+ block: The block to render.
+ """
+ if isinstance(block, BlockLink):
+ self.render_block_link(block)
+ elif isinstance(block, BlockQuote):
+ self.render_block_quote(block)
+ elif isinstance(block, Heading):
+ self.render_heading(block)
+ elif isinstance(block, ItemList):
+ self.render_item_list(block)
+ elif isinstance(block, LineBreak):
+ self.render_line_break(block)
+ elif isinstance(block, Paragraph):
+ self.render_paragraph(block)
+ elif isinstance(block, Verbatim):
+ self.render_verbatim(block)
+ else:
+ raise TypeError(f"Unknown Block type given: {type(block)}")
+
+ def render_block_link(self, block_link: BlockLink):
+ """Renders a :class:`~wikimini.document.BlockLink`.
+
+ Args:
+ block_link: The block link to render.
+ """
+
+ def render_block_quote(self, block_quote: BlockQuote):
+ """Renders a :class:`~wikimini.document.BlockQuote`.
+
+ Args:
+ block_quote: The block quote to render.
+ """
+
+ def render_heading(self, heading: Heading):
+ """Renders a :class:`~wikimini.document.Heading`.
+
+ Args:
+ heading: The heading to render.
+ """
+
+ def render_item_list(self, item_list: ItemList):
+ """Renders a :class:`~wikimini.document.ItemList`.
+
+ Args:
+ item_list: The item list to render.
+ """
+
+ def render_line_break(self, line_break: LineBreak):
+ """Renders a :class:`~wikimini.document.LineBreak`.
+
+ Args:
+ line_break: The line break to render.
+ """
+
+ def render_paragraph(self, paragraph: Paragraph):
+ """Renders a :class:`~wikimini.document.Paragraph`.
+
+ Args:
+ paragraph: The paragraph to render.
+ """
+
+ def render_verbatim(self, verbatim: Verbatim):
+ """Renders a :class:`~wikimini.document.Verbatim`.
+
+ Args:
+ verbatim: The verbatim to render.
+ """
+
+ def render_node(self, node: Node):
+ """Renders a single node.
+
+ Args:
+ node: The node to render.
+ """
+ if isinstance(node, InlineLink):
+ self.render_inline_link(node)
+ elif isinstance(node, Plain):
+ self.render_plain(node)
+ elif isinstance(node, Style):
+ self.render_style(node)
+ else:
+ raise TypeError(f"Unknown node type: {type(node)}")
+
+ def render_inline_link(self, inline_link: InlineLink):
+ """Renders a :class:`~wikimini.document.InlineLink`.
+
+ Args:
+ inline_link: The inline link to render.
+ """
+
+ def render_plain(self, plain: Plain):
+ """Renders a :class:`~wikimini.document.Plain`.
+
+ Args:
+ plain: The plain text to render.
+ """
+
+ def render_style(self, style: Style):
+ """Renders a :class:`~wikimini.document.Style`.
+
+ Args:
+ style: The styled text to render.
+ """