|
|
@ -2,7 +2,7 @@ from panflute import Element, ListContainer, Inline, Block |
|
|
|
from panflute import Cite, Code, Emph, Image, LineBreak, Link, Math, Note, Quoted, RawInline, SmallCaps, SoftBreak, Space, Span, Str, Strikeout, Strong, Subscript, Superscript, Underline |
|
|
|
from panflute import BlockQuote, BulletList, Citation, CodeBlock, Definition, DefinitionItem, DefinitionList, Div, Figure, Header, HorizontalRule, LineBlock, LineItem, ListItem, MetaBlocks, MetaBool, MetaInlines, MetaList, MetaMap, MetaString, Null, OrderedList, Para, Plain, RawBlock, Table, TableBody, TableFoot, TableHead |
|
|
|
from panflute import TableRow, TableCell, Caption, Doc |
|
|
|
from typing import Union, Dict |
|
|
|
from typing import Union, Dict, List |
|
|
|
|
|
|
|
from .whitespace import NBSP |
|
|
|
from .transform import FQuoted |
|
|
@ -21,8 +21,11 @@ class OutputGenerator: |
|
|
|
self.indent_level = initial_indent_level |
|
|
|
self._at_start_of_line = True |
|
|
|
|
|
|
|
def generate(self, e: Union[Element, ListContainer]): |
|
|
|
if isinstance(e, ListContainer): |
|
|
|
def generate(self, e: Union[Element, ListContainer, List[Union[Element, ListContainer]]]): |
|
|
|
if isinstance(e, List): |
|
|
|
for el in e: |
|
|
|
self.generate(el) |
|
|
|
elif isinstance(e, ListContainer): |
|
|
|
self.generate_ListContainer(e) |
|
|
|
elif isinstance(e, Inline): |
|
|
|
self.generate_Inline(e) |
|
|
@ -47,10 +50,10 @@ class OutputGenerator: |
|
|
|
def indent(self) -> str: |
|
|
|
return self.indent_str*self.indent_level |
|
|
|
|
|
|
|
def iup(self): |
|
|
|
def indent_more(self): |
|
|
|
self.indent_level += 1 |
|
|
|
|
|
|
|
def ido(self): |
|
|
|
def indent_less(self): |
|
|
|
self.indent_level -= 1 |
|
|
|
|
|
|
|
def write(self, text: str): |
|
|
@ -77,13 +80,13 @@ class OutputGenerator: |
|
|
|
self.output_file.write("\n") |
|
|
|
self._at_start_of_line = True |
|
|
|
|
|
|
|
def stag(self, tag: str, attributes: Dict[str,str]={}) -> str: |
|
|
|
def start_tag(self, tag: str, attributes: Dict[str,str]={}) -> str: |
|
|
|
return tag |
|
|
|
|
|
|
|
def etag(self, tag: str, attributes: Dict[str,str]={}) -> str: |
|
|
|
def end_tag(self, tag: str, attributes: Dict[str,str]={}) -> str: |
|
|
|
return "/" + tag |
|
|
|
|
|
|
|
def ntag(self, tag: str, attributes: Dict[str,str]={}) -> str: |
|
|
|
def single_tag(self, tag: str, attributes: Dict[str,str]={}) -> str: |
|
|
|
return "/" + tag + "/" |
|
|
|
|
|
|
|
def tagname(self, e) -> str: |
|
|
@ -92,14 +95,14 @@ class OutputGenerator: |
|
|
|
def common_attributes(self, e: Element) -> Dict[str,str]: |
|
|
|
return {} |
|
|
|
|
|
|
|
def generate_simple_tag(self, e: Union[Element, None]=None, tag: str="", attributes: Union[Dict[str,str],None]=None, content: Union[ListContainer, Element, str, None]=None, inline: Union[bool, None]=None): |
|
|
|
def generate_simple_tag(self, e: Union[Element, None]=None, tag: str="", attributes: Union[Dict[str,str],None]=None, content: Union[ListContainer, Element, List[Union[Element, ListContainer]], str, None]=None, inline: Union[bool, None]=None): |
|
|
|
if not tag and e: |
|
|
|
tag = self.tagname(e) |
|
|
|
if attributes is None and e: |
|
|
|
attributes = self.common_attributes(e) |
|
|
|
else: |
|
|
|
attributes = {} |
|
|
|
if content is None and e and e.content: |
|
|
|
if content is None and e and hasattr(e, "content"): |
|
|
|
content = e.content |
|
|
|
if content is None and e and hasattr(e, "text"): |
|
|
|
content = e.text |
|
|
@ -108,6 +111,7 @@ class OutputGenerator: |
|
|
|
|
|
|
|
if content is None: |
|
|
|
self.generate_empty_block_tag(tag, attributes) |
|
|
|
return |
|
|
|
|
|
|
|
if inline: |
|
|
|
if isinstance(content, str): |
|
|
@ -120,30 +124,30 @@ class OutputGenerator: |
|
|
|
else: |
|
|
|
self.generate_simple_block_tag(tag, content, attributes) |
|
|
|
|
|
|
|
def generate_simple_inline_tag(self, tag: str, content: Union[ListContainer, Element], attributes: Dict[str,str]={}): |
|
|
|
self.write(self.stag(tag, attributes)) |
|
|
|
def generate_simple_inline_tag(self, tag: str, content: Union[ListContainer, Element, List[Union[Element, ListContainer]]], attributes: Dict[str,str]={}): |
|
|
|
self.write(self.start_tag(tag, attributes)) |
|
|
|
self.generate(content) |
|
|
|
self.write(self.etag(tag)) |
|
|
|
self.write(self.end_tag(tag)) |
|
|
|
|
|
|
|
def generate_simple_block_tag(self, tag: str, content: Union[ListContainer, Element], attributes: Dict[str,str]={}): |
|
|
|
self.writeln(self.stag(tag, attributes)) |
|
|
|
self.iup() |
|
|
|
def generate_simple_block_tag(self, tag: str, content: Union[ListContainer, Element, List[Union[Element, ListContainer]]], attributes: Dict[str,str]={}): |
|
|
|
self.writeln(self.start_tag(tag, attributes)) |
|
|
|
self.indent_more() |
|
|
|
self.generate(content) |
|
|
|
self.ido() |
|
|
|
self.writeln(self.etag(tag)) |
|
|
|
self.indent_less() |
|
|
|
self.writeln(self.end_tag(tag)) |
|
|
|
|
|
|
|
def generate_raw_inline_tag(self, tag: str, text: str, attributes: Dict[str,str]={}): |
|
|
|
self.write(self.stag(tag, attributes)) |
|
|
|
self.write(self.start_tag(tag, attributes)) |
|
|
|
self.write(text) |
|
|
|
self.write(self.etag(tag)) |
|
|
|
self.write(self.end_tag(tag)) |
|
|
|
|
|
|
|
def generate_raw_block_tag(self, tag: str, text: str, attributes: Dict[str,str]={}): |
|
|
|
self.writeln(self.stag(tag, attributes)) |
|
|
|
self.writeln(self.start_tag(tag, attributes)) |
|
|
|
self.writeraw(text) |
|
|
|
self.writeln(self.etag(tag)) |
|
|
|
self.writeln(self.end_tag(tag)) |
|
|
|
|
|
|
|
def generate_empty_block_tag(self, tag: str, attributes: Dict[str,str]={}): |
|
|
|
self.writeln(self.ntag(tag, attributes)) |
|
|
|
self.writeln(self.single_tag(tag, attributes)) |
|
|
|
|
|
|
|
def generate_ListContainer(self, e: ListContainer): |
|
|
|
for child in e: |
|
|
@ -393,10 +397,10 @@ class OutputGenerator: |
|
|
|
|
|
|
|
# Special elements with more contents |
|
|
|
def generate_Table(self, e: Table): |
|
|
|
self.generate_simple_tag(e, content=ListContainer(e.head, e.content, e.foot)) |
|
|
|
self.generate_simple_tag(e, content=[e.head, e.content, e.foot]) |
|
|
|
|
|
|
|
def generate_Figure(self, e: Figure): |
|
|
|
self.generate_simple_tag(e, content=ListContainer(e.content, e.caption)) |
|
|
|
self.generate_simple_tag(e, content=[e.content, e.caption]) |
|
|
|
|
|
|
|
# Emtpy tags |
|
|
|
def generate_Null(self, e: Null): |
|
|
|