|
@ -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 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 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 panflute import TableRow, TableCell, Caption, Doc |
|
|
from typing import Union, Dict |
|
|
from typing import Union, Dict, List |
|
|
|
|
|
|
|
|
from .whitespace import NBSP |
|
|
from .whitespace import NBSP |
|
|
from .transform import FQuoted |
|
|
from .transform import FQuoted |
|
@ -21,8 +21,11 @@ class OutputGenerator: |
|
|
self.indent_level = initial_indent_level |
|
|
self.indent_level = initial_indent_level |
|
|
self._at_start_of_line = True |
|
|
self._at_start_of_line = True |
|
|
|
|
|
|
|
|
def generate(self, e: Union[Element, ListContainer]): |
|
|
def generate(self, e: Union[Element, ListContainer, List[Union[Element, ListContainer]]]): |
|
|
if isinstance(e, ListContainer): |
|
|
if isinstance(e, List): |
|
|
|
|
|
for el in e: |
|
|
|
|
|
self.generate(el) |
|
|
|
|
|
elif isinstance(e, ListContainer): |
|
|
self.generate_ListContainer(e) |
|
|
self.generate_ListContainer(e) |
|
|
elif isinstance(e, Inline): |
|
|
elif isinstance(e, Inline): |
|
|
self.generate_Inline(e) |
|
|
self.generate_Inline(e) |
|
@ -47,10 +50,10 @@ class OutputGenerator: |
|
|
def indent(self) -> str: |
|
|
def indent(self) -> str: |
|
|
return self.indent_str*self.indent_level |
|
|
return self.indent_str*self.indent_level |
|
|
|
|
|
|
|
|
def iup(self): |
|
|
def indent_more(self): |
|
|
self.indent_level += 1 |
|
|
self.indent_level += 1 |
|
|
|
|
|
|
|
|
def ido(self): |
|
|
def indent_less(self): |
|
|
self.indent_level -= 1 |
|
|
self.indent_level -= 1 |
|
|
|
|
|
|
|
|
def write(self, text: str): |
|
|
def write(self, text: str): |
|
@ -77,13 +80,13 @@ class OutputGenerator: |
|
|
self.output_file.write("\n") |
|
|
self.output_file.write("\n") |
|
|
self._at_start_of_line = True |
|
|
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 |
|
|
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 |
|
|
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 + "/" |
|
|
return "/" + tag + "/" |
|
|
|
|
|
|
|
|
def tagname(self, e) -> str: |
|
|
def tagname(self, e) -> str: |
|
@ -92,14 +95,14 @@ class OutputGenerator: |
|
|
def common_attributes(self, e: Element) -> Dict[str,str]: |
|
|
def common_attributes(self, e: Element) -> Dict[str,str]: |
|
|
return {} |
|
|
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: |
|
|
if not tag and e: |
|
|
tag = self.tagname(e) |
|
|
tag = self.tagname(e) |
|
|
if attributes is None and e: |
|
|
if attributes is None and e: |
|
|
attributes = self.common_attributes(e) |
|
|
attributes = self.common_attributes(e) |
|
|
else: |
|
|
else: |
|
|
attributes = {} |
|
|
attributes = {} |
|
|
if content is None and e and e.content: |
|
|
if content is None and e and hasattr(e, "content"): |
|
|
content = e.content |
|
|
content = e.content |
|
|
if content is None and e and hasattr(e, "text"): |
|
|
if content is None and e and hasattr(e, "text"): |
|
|
content = e.text |
|
|
content = e.text |
|
@ -108,6 +111,7 @@ class OutputGenerator: |
|
|
|
|
|
|
|
|
if content is None: |
|
|
if content is None: |
|
|
self.generate_empty_block_tag(tag, attributes) |
|
|
self.generate_empty_block_tag(tag, attributes) |
|
|
|
|
|
return |
|
|
|
|
|
|
|
|
if inline: |
|
|
if inline: |
|
|
if isinstance(content, str): |
|
|
if isinstance(content, str): |
|
@ -120,30 +124,30 @@ class OutputGenerator: |
|
|
else: |
|
|
else: |
|
|
self.generate_simple_block_tag(tag, content, attributes) |
|
|
self.generate_simple_block_tag(tag, content, attributes) |
|
|
|
|
|
|
|
|
def generate_simple_inline_tag(self, tag: str, content: Union[ListContainer, Element], attributes: Dict[str,str]={}): |
|
|
def generate_simple_inline_tag(self, tag: str, content: Union[ListContainer, Element, List[Union[Element, ListContainer]]], attributes: Dict[str,str]={}): |
|
|
self.write(self.stag(tag, attributes)) |
|
|
self.write(self.start_tag(tag, attributes)) |
|
|
self.generate(content) |
|
|
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]={}): |
|
|
def generate_simple_block_tag(self, tag: str, content: Union[ListContainer, Element, List[Union[Element, ListContainer]]], attributes: Dict[str,str]={}): |
|
|
self.writeln(self.stag(tag, attributes)) |
|
|
self.writeln(self.start_tag(tag, attributes)) |
|
|
self.iup() |
|
|
self.indent_more() |
|
|
self.generate(content) |
|
|
self.generate(content) |
|
|
self.ido() |
|
|
self.indent_less() |
|
|
self.writeln(self.etag(tag)) |
|
|
self.writeln(self.end_tag(tag)) |
|
|
|
|
|
|
|
|
def generate_raw_inline_tag(self, tag: str, text: str, attributes: Dict[str,str]={}): |
|
|
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(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]={}): |
|
|
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.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]={}): |
|
|
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): |
|
|
def generate_ListContainer(self, e: ListContainer): |
|
|
for child in e: |
|
|
for child in e: |
|
@ -393,10 +397,10 @@ class OutputGenerator: |
|
|
|
|
|
|
|
|
# Special elements with more contents |
|
|
# Special elements with more contents |
|
|
def generate_Table(self, e: Table): |
|
|
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): |
|
|
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 |
|
|
# Emtpy tags |
|
|
def generate_Null(self, e: Null): |
|
|
def generate_Null(self, e: Null): |
|
|