Browse Source

Změněn interface OutputGeneratoru. Viz poslední komentář v #13.

pull/28/head
Jan Černohorský 1 year ago
parent
commit
fe63458a51
  1. 14
      src/formatitko/html_generator.py
  2. 19
      src/formatitko/latex_generator.py
  3. 238
      src/formatitko/output_generator.py

14
src/formatitko/html_generator.py

@ -99,10 +99,8 @@ class HTMLGenerator(OutputGenerator):
def generate_Doc(self, e: Doc): def generate_Doc(self, e: Doc):
formatter = HtmlFormatter(style=e.get_metadata("highlight-style") if e.get_metadata("highlight-style") is not None else "default") formatter = HtmlFormatter(style=e.get_metadata("highlight-style") if e.get_metadata("highlight-style") is not None else "default")
self.writeln("<style>") self.generate_simple_tag(tag="style", attributes={}, content=formatter.get_style_defs(".highlight"))
self.writeraw(formatter.get_style_defs(".highlight")) self.generate_simple_tag(e, tag="main")
self.writeln("</style>")
self.generate_simple_block_tag(e, "main", self.common_attributes(e))
def generate_CodeBlock(self, e: CodeBlock): def generate_CodeBlock(self, e: CodeBlock):
lexer = None lexer = None
@ -122,7 +120,7 @@ class HTMLGenerator(OutputGenerator):
result = highlight(e.text, lexer, formatter) result = highlight(e.text, lexer, formatter)
self.writeraw(result) self.writeraw(result)
else: else:
self.generate_raw_block_tag(e, "pre", self.common_attributes(e)) self.generate_simple_tag(e, tag="pre")
def generate_Image(self, e: Image): def generate_Image(self, e: Image):
url = e.url url = e.url
@ -243,7 +241,7 @@ class HTMLGenerator(OutputGenerator):
attributes["href"] = e.url attributes["href"] = e.url
if e.title: if e.title:
attributes["title"] = e.title attributes["title"] = e.title
self.generate_simple_inline_tag(e, self.tagname(e), self.common_attributes(e) | attributes) self.generate_simple_tag(e, attributes=self.common_attributes(e) | attributes)
def generate_OrderedList(self, e: OrderedList): def generate_OrderedList(self, e: OrderedList):
attributes = {} attributes = {}
@ -259,7 +257,7 @@ class HTMLGenerator(OutputGenerator):
if e.style and e.style != "DefaultStyle": if e.style and e.style != "DefaultStyle":
attributes["type"] = html_styles[e.style] attributes["type"] = html_styles[e.style]
# FIXME: Delimeter styles: 1. 1) (1) # FIXME: Delimeter styles: 1. 1) (1)
self.generate_simple_block_tag(e, self.tagname(e), self.common_attributes(e) | attributes) self.generate_simple_tag(e, attributes=self.common_attributes(e) | attributes)
def generate_TableCell(self, e: TableCell): def generate_TableCell(self, e: TableCell):
attributes = self.common_attributes(e) attributes = self.common_attributes(e)
@ -274,7 +272,7 @@ class HTMLGenerator(OutputGenerator):
} }
if e.alignment and e.alignment != "AlignDefault": if e.alignment and e.alignment != "AlignDefault":
attributes["style"] = attributes.get("style", "")+f"text-align: {aligns[e.alignment]};" attributes["style"] = attributes.get("style", "")+f"text-align: {aligns[e.alignment]};"
self.generate_simple_block_tag(e, self.tagname(e), attributes) self.generate_simple_tag(e, attributes=attributes)
# These are also disabled in pandoc so they shouldn't appear in the AST at all. # These are also disabled in pandoc so they shouldn't appear in the AST at all.
def generate_Citation(self, e: Citation): def generate_Citation(self, e: Citation):

19
src/formatitko/latex_generator.py

@ -15,7 +15,7 @@ import re
class LaTeXGenerator(OutputGenerator): class LaTeXGenerator(OutputGenerator):
def __init__(self, output_file, imageProcessor: ImageProcessor, *args, **kwargs): def __init__(self, output_file, imageProcessor: ImageProcessor, *args, **kwargs):
self.imageProcessor = imageProcessor self.imageProcessor = imageProcessor
super().__init__(outpout_file, *args, **kwargs) super().__init__(output_file, *args, **kwargs)
def generate(self, e: Union[Element, ListContainer]): def generate(self, e: Union[Element, ListContainer]):
if hasattr(e, "attributes") and "only" in e.attributes and e.attributes["only"] != "tex": if hasattr(e, "attributes") and "only" in e.attributes and e.attributes["only"] != "tex":
@ -47,8 +47,6 @@ class LaTeXGenerator(OutputGenerator):
def generate_NBSP(self, e: NBSP): def generate_NBSP(self, e: NBSP):
self.write("~") self.write("~")
@ -61,7 +59,13 @@ class LaTeXGenerator(OutputGenerator):
def generate_Para(self, e: Para): def generate_Para(self, e: Para):
self.generate(e) self.generate(e)
self.endln() self.writeln("") # This ensures an empty line
def generate_Plain(self, e: Plain):
self.generate(e.content)
def generate_Span(self, e: Plain):
self.generate(e.content)
def generate_Header(self, e: Header): def generate_Header(self, e: Header):
tag = { tag = {
@ -72,11 +76,16 @@ class LaTeXGenerator(OutputGenerator):
5: "subparagraph", 5: "subparagraph",
6: "textbf" 6: "textbf"
} }
self.generate_simple_block_tag(e, tag[e.level])
self.generate_simple_tag(e, tag=tag[e.level])
def generate_HorizontalRule(self, e: HorizontalRule): def generate_HorizontalRule(self, e: HorizontalRule):
self.writeln("\\begin{center}\\rule{0.5\\linewidth}{0.5pt}\\end{center}") self.writeln("\\begin{center}\\rule{0.5\\linewidth}{0.5pt}\\end{center}")
# These are also disabled in pandoc so they shouldn't appear in the AST at all. # These are also disabled in pandoc so they shouldn't appear in the AST at all.
def generate_Citation(self, e: Citation): def generate_Citation(self, e: Citation):
self.writeln("% FIXME: Citations not implemented") self.writeln("% FIXME: Citations not implemented")

238
src/formatitko/output_generator.py

@ -21,6 +21,26 @@ 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]):
if isinstance(e, ListContainer):
self.generate_ListContainer(e)
elif isinstance(e, Inline):
self.generate_Inline(e)
elif isinstance(e, Block):
self.generate_Block(e)
else:
try:
{
TableRow: self.generate_TableRow,
TableCell: self.generate_TableCell,
Caption: self.generate_Caption,
Doc: self.generate_Doc,
LineItem: self.generate_LineItem,
ListItem: self.generate_ListItem
}[type(e)](e)
except KeyError:
raise UnknownElementError(type(e))
def escape_special_chars(self, text: str) -> str: def escape_special_chars(self, text: str) -> str:
return text return text
@ -69,28 +89,61 @@ class OutputGenerator:
def tagname(self, e) -> str: def tagname(self, e) -> str:
return type(e).__name__ return type(e).__name__
def common_attributes(self, e) -> Dict[str,str]: def common_attributes(self, e: Element) -> Dict[str,str]:
return {} return {}
def generate(self, e: Union[Element, ListContainer]): 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):
if isinstance(e, ListContainer): if not tag and e:
self.generate_ListContainer(e) tag = self.tagname(e)
elif isinstance(e, Inline): if attributes is None and e:
self.generate_Inline(e) attributes = self.common_attributes(e)
elif isinstance(e, Block):
self.generate_Block(e)
else: else:
try: attributes = {}
{ if content is None and e and e.content:
TableRow: self.generate_TableRow, content = e.content
TableCell: self.generate_TableCell, if content is None and e and hasattr(e, "text"):
Caption: self.generate_Caption, content = e.text
Doc: self.generate_Doc, if inline is None and e:
LineItem: self.generate_LineItem, inline = isinstance(e, Inline)
ListItem: self.generate_ListItem
}[type(e)](e) if content is None:
except KeyError: self.generate_empty_block_tag(tag, attributes)
raise UnknownElementError(type(e))
if inline:
if isinstance(content, str):
self.generate_raw_inline_tag(tag, content, attributes)
else:
self.generate_simple_inline_tag(tag, content, attributes)
else:
if isinstance(content, str):
self.generate_raw_block_tag(tag, content, attributes)
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))
self.generate(content)
self.write(self.etag(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()
self.generate(content)
self.ido()
self.writeln(self.etag(tag))
def generate_raw_inline_tag(self, tag: str, text: str, attributes: Dict[str,str]={}):
self.write(self.stag(tag, attributes))
self.write(text)
self.write(self.etag(tag))
def generate_raw_block_tag(self, tag: str, text: str, attributes: Dict[str,str]={}):
self.writeln(self.stag(tag, attributes))
self.writeraw(text)
self.writeln(self.etag(tag))
def generate_empty_block_tag(self, tag: str, attributes: Dict[str,str]={}):
self.writeln(self.ntag(tag, attributes))
def generate_ListContainer(self, e: ListContainer): def generate_ListContainer(self, e: ListContainer):
for child in e: for child in e:
@ -167,63 +220,58 @@ class OutputGenerator:
self.generate(e.content) self.generate(e.content)
self.write("\"") self.write("\"")
# Inline Elements
def generate_Cite(self, e: Cite): def generate_Cite(self, e: Cite):
self.generate_simple_inline_tag(e, self.tagname(e), self.common_attributes(e)) self.generate_simple_tag(e)
def generate_Emph(self, e: Emph): def generate_Emph(self, e: Emph):
self.generate_simple_inline_tag(e, self.tagname(e), self.common_attributes(e)) self.generate_simple_tag(e)
def generate_Image(self, e: Image): def generate_Image(self, e: Image):
self.generate_simple_inline_tag(e, self.tagname(e), self.common_attributes(e)) self.generate_simple_tag(e)
def generate_Link(self, e: Link): def generate_Link(self, e: Link):
self.generate_simple_inline_tag(e, self.tagname(e), self.common_attributes(e)) self.generate_simple_tag(e)
def generate_Note(self, e: Note): def generate_Note(self, e: Note):
self.generate_simple_inline_tag(e, self.tagname(e), self.common_attributes(e)) self.generate_simple_tag(e)
def generate_Quoted(self, e: Quoted): def generate_Quoted(self, e: Quoted):
self.generate_simple_inline_tag(e, self.tagname(e), self.common_attributes(e)) self.generate_simple_tag(e)
def generate_SmallCaps(self, e: SmallCaps): def generate_SmallCaps(self, e: SmallCaps):
self.generate_simple_inline_tag(e, self.tagname(e), self.common_attributes(e)) self.generate_simple_tag(e)
def generate_Span(self, e: Span): def generate_Span(self, e: Span):
self.generate_simple_inline_tag(e, self.tagname(e), self.common_attributes(e)) self.generate_simple_tag(e)
def generate_Strikeout(self, e: Strikeout): def generate_Strikeout(self, e: Strikeout):
self.generate_simple_inline_tag(e, self.tagname(e), self.common_attributes(e)) self.generate_simple_tag(e)
def generate_Strong(self, e: Strong): def generate_Strong(self, e: Strong):
self.generate_simple_inline_tag(e, self.tagname(e), self.common_attributes(e)) self.generate_simple_tag(e)
def generate_Subscript(self, e: Subscript): def generate_Subscript(self, e: Subscript):
self.generate_simple_inline_tag(e, self.tagname(e), self.common_attributes(e)) self.generate_simple_tag(e)
def generate_Superscript(self, e: Superscript): def generate_Superscript(self, e: Superscript):
self.generate_simple_inline_tag(e, self.tagname(e), self.common_attributes(e)) self.generate_simple_tag(e)
def generate_Underline(self, e: Underline): def generate_Underline(self, e: Underline):
self.generate_simple_inline_tag(e, self.tagname(e), self.common_attributes(e)) self.generate_simple_tag(e)
def generate_simple_inline_tag(self, e, tag: str, attributes: Dict[str,str]={}):
self.write(self.stag(tag, attributes))
self.generate(e.content)
self.write(self.etag(tag))
# Raw Inline elements
def generate_Math(self, e: Math): def generate_Math(self, e: Math):
self.generate_raw_inline_tag(e, self.tagname(e), self.common_attributes(e)) self.generate_simple_tag(e)
def generate_Code(self, e: Code): def generate_Code(self, e: Code):
self.generate_raw_inline_tag(e, self.tagname(e), self.common_attributes(e)) self.generate_simple_tag(e)
def generate_RawInline(self, e: RawInline): def generate_RawInline(self, e: RawInline):
self.generate_raw_inline_tag(e, self.tagname(e), self.common_attributes(e)) self.generate_simple_tag(e)
def generate_raw_inline_tag(self, e, tag: str, attributes: Dict[str,str]={}):
self.write(self.stag(tag, attributes))
self.write(e.text)
self.write(self.etag(tag))
def generate_Block(self, e: Block): def generate_Block(self, e: Block):
{ {
@ -257,139 +305,117 @@ class OutputGenerator:
Group: self.generate_Group Group: self.generate_Group
}[type(e)](e) }[type(e)](e)
# Block elements
def generate_BlockQuote(self, e: BlockQuote): def generate_BlockQuote(self, e: BlockQuote):
self.generate_simple_block_tag(e, self.tagname(e), self.common_attributes(e)) self.generate_simple_tag(e)
def generate_BulletList(self, e: BulletList): def generate_BulletList(self, e: BulletList):
self.generate_simple_block_tag(e, self.tagname(e), self.common_attributes(e)) self.generate_simple_tag(e)
def generate_Citation(self, e: Citation): def generate_Citation(self, e: Citation):
self.generate_simple_block_tag(e, self.tagname(e), self.common_attributes(e)) self.generate_simple_tag(e)
def generate_Definition(self, e: Definition): def generate_Definition(self, e: Definition):
self.generate_simple_block_tag(e, self.tagname(e), self.common_attributes(e)) self.generate_simple_tag(e)
def generate_DefinitionItem(self, e: DefinitionItem): def generate_DefinitionItem(self, e: DefinitionItem):
self.generate_simple_block_tag(e, self.tagname(e), self.common_attributes(e)) self.generate_simple_tag(e)
def generate_DefinitionList(self, e: DefinitionList): def generate_DefinitionList(self, e: DefinitionList):
self.generate_simple_block_tag(e, self.tagname(e), self.common_attributes(e)) self.generate_simple_tag(e)
def generate_Div(self, e: Div): def generate_Div(self, e: Div):
self.generate_simple_block_tag(e, self.tagname(e), self.common_attributes(e)) self.generate_simple_tag(e)
def generate_Header(self, e: Header): def generate_Header(self, e: Header):
self.generate_simple_block_tag(e, self.tagname(e), self.common_attributes(e)) self.generate_simple_tag(e)
def generate_LineBlock(self, e: LineBlock): def generate_LineBlock(self, e: LineBlock):
self.generate_simple_block_tag(e, self.tagname(e), self.common_attributes(e)) self.generate_simple_tag(e)
def generate_LineItem(self, e: LineItem): def generate_LineItem(self, e: LineItem):
self.generate_simple_block_tag(e, self.tagname(e), self.common_attributes(e)) self.generate_simple_tag(e)
def generate_ListItem(self, e: ListItem): def generate_ListItem(self, e: ListItem):
self.generate_simple_block_tag(e, self.tagname(e), self.common_attributes(e)) self.generate_simple_tag(e)
def generate_MetaBlocks(self, e: MetaBlocks): def generate_MetaBlocks(self, e: MetaBlocks):
self.generate_simple_block_tag(e, self.tagname(e), self.common_attributes(e)) self.generate_simple_tag(e)
def generate_MetaBool(self, e: MetaBool): def generate_MetaBool(self, e: MetaBool):
self.generate_simple_block_tag(e, self.tagname(e), self.common_attributes(e)) self.generate_simple_tag(e)
def generate_MetaInlines(self, e: MetaInlines): def generate_MetaInlines(self, e: MetaInlines):
self.generate_simple_block_tag(e, self.tagname(e), self.common_attributes(e)) self.generate_simple_tag(e)
def generate_MetaList(self, e: MetaList): def generate_MetaList(self, e: MetaList):
self.generate_simple_block_tag(e, self.tagname(e), self.common_attributes(e)) self.generate_simple_tag(e)
def generate_MetaMap(self, e: MetaMap): def generate_MetaMap(self, e: MetaMap):
self.generate_simple_block_tag(e, self.tagname(e), self.common_attributes(e)) self.generate_simple_tag(e)
def generate_MetaString(self, e: MetaString): def generate_MetaString(self, e: MetaString):
self.generate_simple_block_tag(e, self.tagname(e), self.common_attributes(e)) self.generate_simple_tag(e)
def generate_OrderedList(self, e: OrderedList): def generate_OrderedList(self, e: OrderedList):
self.generate_simple_block_tag(e, self.tagname(e), self.common_attributes(e)) self.generate_simple_tag(e)
def generate_Para(self, e: Para): def generate_Para(self, e: Para):
self.generate_simple_block_tag(e, self.tagname(e), self.common_attributes(e)) self.generate_simple_tag(e)
def generate_Plain(self, e: Plain): def generate_Plain(self, e: Plain):
self.generate_simple_block_tag(e, self.tagname(e), self.common_attributes(e)) self.generate_simple_tag(e)
def generate_Caption(self, e: Caption): def generate_Caption(self, e: Caption):
self.generate_simple_block_tag(e, self.tagname(e), self.common_attributes(e)) self.generate_simple_tag(e)
def generate_TableBody(self, e: TableBody): def generate_TableBody(self, e: TableBody):
self.generate_simple_block_tag(e, self.tagname(e), self.common_attributes(e)) self.generate_simple_tag(e)
def generate_TableCell(self, e: TableCell): def generate_TableCell(self, e: TableCell):
self.generate_simple_block_tag(e, self.tagname(e), self.common_attributes(e)) self.generate_simple_tag(e)
def generate_TableFoot(self, e: TableFoot): def generate_TableFoot(self, e: TableFoot):
self.generate_simple_block_tag(e, self.tagname(e), self.common_attributes(e)) self.generate_simple_tag(e)
def generate_TableHead(self, e: TableHead): def generate_TableHead(self, e: TableHead):
self.generate_simple_block_tag(e, self.tagname(e), self.common_attributes(e)) self.generate_simple_tag(e)
def generate_TableRow(self, e: TableRow): def generate_TableRow(self, e: TableRow):
self.generate_simple_block_tag(e, self.tagname(e), self.common_attributes(e)) self.generate_simple_tag(e)
def generate_Doc(self, e: Doc): def generate_Doc(self, e: Doc):
self.generate_simple_block_tag(e, self.tagname(e), self.common_attributes(e)) self.generate_simple_tag(e)
def generate_Group(self, e: Group): def generate_Group(self, e: Group):
self.generate_simple_block_tag(e, self.tagname(e), self.common_attributes(e)) self.generate_simple_tag(e)
def generate_simple_block_tag(self, e, tag: str, attributes: Dict[str,str]={}):
self.writeln(self.stag(tag, attributes))
self.iup()
self.generate(e.content)
self.ido()
self.writeln(self.etag(tag))
# Special elements with more contents
def generate_Table(self, e: Table): def generate_Table(self, e: Table):
tag = self.tagname(e) self.generate_simple_tag(e, content=ListContainer(e.head, e.content, e.foot))
self.writeln(self.stag(tag, self.common_attributes(e)))
self.iup()
self.generate(e.head)
self.generate(e.content)
self.generate(e.foot)
self.ido()
self.writeln(self.etag(tag))
def generate_Figure(self, e: Figure): def generate_Figure(self, e: Figure):
tag = self.tagname(e) self.generate_simple_tag(e, content=ListContainer(e.content, e.caption))
self.writeln(self.stag(tag, self.common_attributes(e)))
self.iup()
self.generate(e.content)
self.generate(e.caption)
self.ido()
self.writeln(self.etag(tag))
# Emtpy tags
def generate_Null(self, e: Null): def generate_Null(self, e: Null):
self.generate_empty_block_tag(e, self.tagname(e), self.common_attributes(e)) self.generate_simple_tag(e)
def generate_HorizontalRule(self, e: HorizontalRule): def generate_HorizontalRule(self, e: HorizontalRule):
self.generate_empty_block_tag(e, self.tagname(e), self.common_attributes(e)) self.generate_simple_tag(e)
def generate_LineBreak(self, e: LineBreak): def generate_LineBreak(self, e: LineBreak):
self.generate_empty_block_tag(e, self.tagname(e), self.common_attributes(e)) self.generate_simple_tag(e)
def generate_empty_block_tag(self, e, tag: str, attributes: Dict[str,str]={}):
self.writeln(self.ntag(tag, attributes))
# Raw Block tags
def generate_CodeBlock(self, e: CodeBlock): def generate_CodeBlock(self, e: CodeBlock):
self.generate_raw_block_tag(e, self.tagname(e), self.common_attributes(e)) self.generate_simple_tag(e)
def generate_RawBlock(self, e: RawBlock): def generate_RawBlock(self, e: RawBlock):
self.generate_raw_block_tag(e, self.tagname(e), self.common_attributes(e)) self.generate_simple_tag(e)
def generate_raw_block_tag(self, e, tag: str, attributes: Dict[str,str]={}):
self.writeln(self.stag(tag, attributes))
self.writeraw(e.text)
self.writeln(self.etag(tag))
# Maybe move this to ImageProcessor?
def get_image_processor_args(self, attributes:Dict[str,str]) -> Dict: def get_image_processor_args(self, attributes:Dict[str,str]) -> Dict:
# Attributes → image processor args # Attributes → image processor args
additional_args = {} additional_args = {}

Loading…
Cancel
Save