From bec8100786ebde15a01132dda68a296df5e13f67 Mon Sep 17 00:00:00 2001 From: Greenscreener Date: Thu, 20 Jul 2023 23:05:00 +0200 Subject: [PATCH] =?UTF-8?q?P=C5=99ejmenov=C3=A1n=C3=AD=20zkratkovit=C4=9B?= =?UTF-8?q?=20pojmenovan=C3=BDch=20prom=C4=9Bnn=C3=BDch=20a=20fix=20zm?= =?UTF-8?q?=C4=9Bn=20z=20fe63458a51=20aby=20to=20actually=20fungovalo.=20#?= =?UTF-8?q?13?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/formatitko/html_generator.py | 22 ++++++------ src/formatitko/latex_generator.py | 6 ++-- src/formatitko/output_generator.py | 54 ++++++++++++++++-------------- 3 files changed, 43 insertions(+), 39 deletions(-) diff --git a/src/formatitko/html_generator.py b/src/formatitko/html_generator.py index 1c2ba68..5e6af2d 100644 --- a/src/formatitko/html_generator.py +++ b/src/formatitko/html_generator.py @@ -40,17 +40,17 @@ class HTMLGenerator(OutputGenerator): # text = text.replace(" ", " ") # Don't replace no-break spaces with HTML escapes, because we trust unicode? return text - def stag(self, tag: str, attributes: Dict[str,str]={}) -> str: + def start_tag(self, tag: str, attributes: Dict[str,str]={}) -> str: words = [tag] for key, value in attributes.items(): words.append(f"{key}=\"{self.escape_special_chars(value)}\"") return "<" + " ".join(words) + ">" - def etag(self, tag: str, attributes: Dict[str,str]={}) -> str: + def end_tag(self, tag: str, attributes: Dict[str,str]={}) -> str: return "" - def ntag(self, tag: str, attributes: Dict[str,str]={}) -> str: - return self.stag(tag, attributes) + def single_tag(self, tag: str, attributes: Dict[str,str]={}) -> str: + return self.start_tag(tag, attributes) def tagname(self, e) -> str: if isinstance(e, Header): @@ -187,7 +187,7 @@ class HTMLGenerator(OutputGenerator): else: attributes["src"] = url - img = RawInline(self.ntag("img", attributes)) + img = RawInline(self.single_tag("img", attributes)) link = Link(img, url=url) self.generate(link) @@ -210,15 +210,15 @@ class HTMLGenerator(OutputGenerator): inline = inlinify(e) tag = self.tagname(e) if inline is not None: - self.write(self.stag(tag)+" (") + self.write(self.start_tag(tag)+" (") self.generate(inline) - self.write(") "+self.etag(tag)) + self.write(") "+self.end_tag(tag)) else: - self.writeln(self.stag(tag) + "(") - self.iup() + self.writeln(self.start_tag(tag) + "(") + self.indent_more() self.generate(e.content) - self.ido() - self.writeln(self.etag(tag) + ")") + self.indent_less() + self.writeln(self.end_tag(tag) + ")") def generate_Math(self, e: Math): formats = { diff --git a/src/formatitko/latex_generator.py b/src/formatitko/latex_generator.py index 026ef5e..202c636 100644 --- a/src/formatitko/latex_generator.py +++ b/src/formatitko/latex_generator.py @@ -34,13 +34,13 @@ class LaTeXGenerator(OutputGenerator): text = text.replace(" ", "~") # We use unicode no-break spaces to force nbsp in output return text - 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 "}" - def ntag(self, tag: str, attributes: Dict[str,str]={}) -> str: + def single_tag(self, tag: str, attributes: Dict[str,str]={}) -> str: return "\\" + tag + "{}" diff --git a/src/formatitko/output_generator.py b/src/formatitko/output_generator.py index 6f51d72..80bfdb1 100644 --- a/src/formatitko/output_generator.py +++ b/src/formatitko/output_generator.py @@ -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):