Browse Source

Přejmenování zkratkovitě pojmenovaných proměnných a fix změn z fe63458a51 aby to actually fungovalo. #13

pull/28/head
Jan Černohorský 1 year ago
parent
commit
bec8100786
  1. 22
      src/formatitko/html_generator.py
  2. 6
      src/formatitko/latex_generator.py
  3. 54
      src/formatitko/output_generator.py

22
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? # text = text.replace(" ", " ") # Don't replace no-break spaces with HTML escapes, because we trust unicode?
return text 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] words = [tag]
for key, value in attributes.items(): for key, value in attributes.items():
words.append(f"{key}=\"{self.escape_special_chars(value)}\"") words.append(f"{key}=\"{self.escape_special_chars(value)}\"")
return "<" + " ".join(words) + ">" 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 "</" + 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 self.stag(tag, attributes) return self.start_tag(tag, attributes)
def tagname(self, e) -> str: def tagname(self, e) -> str:
if isinstance(e, Header): if isinstance(e, Header):
@ -187,7 +187,7 @@ class HTMLGenerator(OutputGenerator):
else: else:
attributes["src"] = url attributes["src"] = url
img = RawInline(self.ntag("img", attributes)) img = RawInline(self.single_tag("img", attributes))
link = Link(img, url=url) link = Link(img, url=url)
self.generate(link) self.generate(link)
@ -210,15 +210,15 @@ class HTMLGenerator(OutputGenerator):
inline = inlinify(e) inline = inlinify(e)
tag = self.tagname(e) tag = self.tagname(e)
if inline is not None: if inline is not None:
self.write(self.stag(tag)+" (") self.write(self.start_tag(tag)+" (")
self.generate(inline) self.generate(inline)
self.write(") "+self.etag(tag)) self.write(") "+self.end_tag(tag))
else: else:
self.writeln(self.stag(tag) + "(") self.writeln(self.start_tag(tag) + "(")
self.iup() self.indent_more()
self.generate(e.content) self.generate(e.content)
self.ido() self.indent_less()
self.writeln(self.etag(tag) + ")") self.writeln(self.end_tag(tag) + ")")
def generate_Math(self, e: Math): def generate_Math(self, e: Math):
formats = { formats = {

6
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 text = text.replace(" ", "~") # We use unicode no-break spaces to force nbsp in output
return text 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 + "{" return "\\" + tag + "{"
def etag(self, tag: str, attributes: Dict[str,str]={}) -> str: def end_tag(self, tag: str, attributes: Dict[str,str]={}) -> str:
return "}" return "}"
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 + "{}"

54
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 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):

Loading…
Cancel
Save