Browse Source

Highlighting je nyní vždy inline, ať tam není třeba dělat taková obří magie s zjišťováním, jestli používáme defaultní styl nebo ne. Možná by bylo fajn to nějak umět overridovat. Menší fixy.

pull/28/head
Jan Černohorský 1 year ago
parent
commit
24f877565d
  1. 6
      src/formatitko/formatitko.py
  2. 8
      src/formatitko/html_generator.py
  3. 8
      src/formatitko/output_generator.py
  4. 6
      src/formatitko/transform_processor.py

6
src/formatitko/formatitko.py

@ -61,9 +61,9 @@ def main():
# Generate HTML and TeX out of the transformed document # Generate HTML and TeX out of the transformed document
#open(args.output_html, "w").write(html(doc, katexClient, imageProcessor)) #open(args.output_html, "w").write(html(doc, katexClient, imageProcessor))
#open(args.output_tex, "w").write(tex(doc, imageProcessor)) #open(args.output_tex, "w").write(tex(doc, imageProcessor))
# HTMLGenerator(sys.stdout, katexClient, imageProcessor).generate(doc) HTMLGenerator(sys.stdout, katexClient, imageProcessor).generate(doc2)
OutputGenerator(open("/tmp/doc1", "w")).generate(doc1) #OutputGenerator(open("/tmp/doc1", "w")).generate(doc1)
OutputGenerator(open("/tmp/doc2", "w")).generate(doc2) #OutputGenerator(open("/tmp/doc2", "w")).generate(doc2)
if args.debug: if args.debug:
print(show(doc)) print(show(doc))

8
src/formatitko/html_generator.py

@ -97,8 +97,8 @@ class HTMLGenerator(OutputGenerator):
pass pass
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.generate_simple_tag(tag="style", attributes={}, content=formatter.get_style_defs(".highlight")) # self.generate_simple_tag(tag="style", attributes={}, content=formatter.get_style_defs(".highlight"))
self.generate_simple_tag(e, tag="main") self.generate_simple_tag(e, tag="main")
def generate_CodeBlock(self, e: CodeBlock): def generate_CodeBlock(self, e: CodeBlock):
@ -115,7 +115,7 @@ class HTMLGenerator(OutputGenerator):
warnings.warn(f"Syntax highligher does not have lexer for element with these classes: {e.classes}", UserWarning) warnings.warn(f"Syntax highligher does not have lexer for element with these classes: {e.classes}", UserWarning)
if lexer: if lexer:
formatter = HtmlFormatter(style=e.attributes["style"]) formatter = HtmlFormatter(style=e.attributes["style"], noclasses=True)
result = highlight(e.text, lexer, formatter) result = highlight(e.text, lexer, formatter)
self.writeraw(result) self.writeraw(result)
else: else:
@ -127,7 +127,7 @@ class HTMLGenerator(OutputGenerator):
additional_args = self.get_image_processor_args(e.attributes) additional_args = self.get_image_processor_args(e.attributes)
# The directory of the current file, will also look for images there. # The directory of the current file, will also look for images there.
source_dir = e.attributes["source_dir"] source_dir = self.context.dir
_, ext = os.path.splitext(url) _, ext = os.path.splitext(url)
ext = ext[1:] ext = ext[1:]

8
src/formatitko/output_generator.py

@ -20,7 +20,7 @@ class OutputGenerator:
self.indent_str = indent_str self.indent_str = indent_str
self.indent_level = initial_indent_level self.indent_level = initial_indent_level
self._at_start_of_line = True self._at_start_of_line = True
self.current_context = None self.context = None
self.TYPE_DICT_MISC = { self.TYPE_DICT_MISC = {
TableRow: self.generate_TableRow, TableRow: self.generate_TableRow,
@ -89,8 +89,8 @@ class OutputGenerator:
def generate(self, e: Union[Element, ListContainer, list[Union[Element, ListContainer]]]): def generate(self, e: Union[Element, ListContainer, list[Union[Element, ListContainer]]]):
if isinstance(e, Group): if isinstance(e, Group):
old_context = self.current_context old_context = self.context
self.current_context = e.context self.context = e.context
if isinstance(e, list): if isinstance(e, list):
self.generate_list(e) self.generate_list(e)
elif isinstance(e, ListContainer): elif isinstance(e, ListContainer):
@ -105,7 +105,7 @@ class OutputGenerator:
except KeyError: except KeyError:
raise UnknownElementError(type(e)) raise UnknownElementError(type(e))
if isinstance(e, Group): if isinstance(e, Group):
self.current_context = old_context self.context = old_context
def escape_special_chars(self, text: str) -> str: def escape_special_chars(self, text: str) -> str:
return text return text

6
src/formatitko/transform_processor.py

@ -31,6 +31,7 @@ class TransformProcessor:
def __init__(self, root_file_path: str): def __init__(self, root_file_path: str):
self.context: Context = None self.context: Context = None
self.root_file_path = root_file_path self.root_file_path = root_file_path
self.root_highlight_style = "default"
self.TYPE_DICT = { self.TYPE_DICT = {
TableRow: self.transform_TableRow, TableRow: self.transform_TableRow,
@ -325,7 +326,6 @@ class TransformProcessor:
"sk": "cs", "sk": "cs",
None: None None: None
} }
print(self.context.get_metadata("language"))
return FQuoted(*e.content, quote_type=e.quote_type, style=quote_styles[self.context.get_metadata("language")]) return FQuoted(*e.content, quote_type=e.quote_type, style=quote_styles[self.context.get_metadata("language")])
def transform_Image(self, e: Image) -> Image: def transform_Image(self, e: Image) -> Image:
@ -458,10 +458,6 @@ class TransformProcessor:
e.attributes["highlight"] = self.context.get_metadata("highlight") if self.context.get_metadata("highlight") is not None else True e.attributes["highlight"] = self.context.get_metadata("highlight") if self.context.get_metadata("highlight") is not None else True
if not "style" in e.attributes: if not "style" in e.attributes:
e.attributes["style"] = self.context.get_metadata("highlight-style") if self.context.get_metadata("highlight-style") is not None else "default" e.attributes["style"] = self.context.get_metadata("highlight-style") if self.context.get_metadata("highlight-style") is not None else "default"
e.attributes["noclasses"] = False
# I think this is supposed to enable inline styles for highlighting when the style differs from the document, but it clearly doesn't work. a) HTML_generator never accesses it and b) Only the top-level document contains a style so you have to ask the top level context, not the current context.
else:
e.attributes["noclasses"] = True
return e return e
def transform_Command(self, e: Command) -> Union[Div, Span]: def transform_Command(self, e: Command) -> Union[Div, Span]:

Loading…
Cancel
Save