Browse Source

Unifikace stylu názvů proměnných, viz můj komentář v #14.

pull/28/head
Jan Černohorský 1 year ago
parent
commit
1ed1265b19
  1. 6
      src/formatitko/formatitko.py
  2. 34
      src/formatitko/html_generator.py
  3. 6
      src/formatitko/output_generator.py

6
src/formatitko/formatitko.py

@ -11,8 +11,8 @@ from .katex import KatexClient
from .html import html
from .tex import tex
from .images import ImageProcessor
from .output_generator import Output_generator
from .html_generator import HTML_generator
from .output_generator import OutputGenerator
from .html_generator import HTMLGenerator
from .mj_show import show
@ -56,7 +56,7 @@ def main():
# Generate HTML and TeX out of the transformed document
#open(args.output_html, "w").write(html(doc, katexClient, imageProcessor))
#open(args.output_tex, "w").write(tex(doc, imageProcessor))
Output_generator(sys.stdout).generate(doc)
OutputGenerator(sys.stdout).generate(doc)
if args.debug:
print(show(doc))

34
src/formatitko/html_generator.py

@ -15,15 +15,15 @@ from pygments.util import ClassNotFound
from .whitespace import NBSP
from .context import Group
from .output_generator import Output_generator
from .output_generator import OutputGenerator
from .katex import KatexClient
from .images import ImageProcessor
from .util import inlinify
class HTML_generator(Output_generator):
def __init__(self, output_file, katex_client: KatexClient, image_processor:ImageProcessor, *args, **kwargs):
self.katex_client = katex_client
self.image_processor = image_processor
class HTMLGenerator(OutputGenerator):
def __init__(self, output_file, katexClient: KatexClient, imageProcessor: ImageProcessor, *args, **kwargs):
self.katexClient = katexClient
self.imageProcessor = imageProcessor
super().__init__(output_file, *args, **kwargs)
def generate(self, e: Union[Element, ListContainer]):
@ -149,15 +149,15 @@ class HTML_generator(Output_generator):
# Even supported elements have to be 'converted' because the
# processing contains finding and moving them to the output
# directory.
url = self.image_processor.process_image(url, ext, source_dir, **additional_args)
url = self.imageProcessor.process_image(url, ext, source_dir, **additional_args)
elif ext in ["pdf", "epdf"]:
if not "dpi" in additional_args:
additional_args["dpi"] = 300
url = self.image_processor.process_image(url, "png", source_dir, **additional_args)
url = self.imageProcessor.process_image(url, "png", source_dir, **additional_args)
elif ext in ["jpg"]:
url = self.image_processor.process_image(url, "jpeg", source_dir, **additional_args)
url = self.imageProcessor.process_image(url, "jpeg", source_dir, **additional_args)
else:
url = self.image_processor.process_image(url, "png", source_dir, **additional_args)
url = self.imageProcessor.process_image(url, "png", source_dir, **additional_args)
# Srcset generation - multiple alternative sizes of images browsers can
# choose from.
@ -168,16 +168,16 @@ class HTML_generator(Output_generator):
# This is inspired by @vojta001's blogPhoto shortcode he made for
# patek.cz:
# https://gitlab.com/patek-devs/patek.cz/-/blob/master/themes/patek/layouts/shortcodes/blogPhoto.html
width, height = self.image_processor.get_image_size(url, [self.image_processor.public_dir])
width, height = self.imageProcessor.get_image_size(url, [self.imageProcessor.public_dir])
sizes = [(640, 360, 85), (1280, 720, 85), (1920, 1080, 90)] # (widht, height, quality)
for size in sizes:
if width <= size[0] and height <= size[1]:
srcset.append((f'{self.image_processor.web_path}/{url}', f'{width}w'))
srcset.append((f'{self.imageProcessor.web_path}/{url}', f'{width}w'))
break
quality = size[2] if ext == "jpeg" else None
srcset.append((f'{self.image_processor.web_path}/{self.image_processor.process_image(url, ext, self.image_processor.public_dir, width=size[0], height=size[1], quality=quality)}', f'{size[0]}w'))
srcset.append((f'{self.imageProcessor.web_path}/{self.imageProcessor.process_image(url, ext, self.imageProcessor.public_dir, width=size[0], height=size[1], quality=quality)}', f'{size[0]}w'))
url = self.image_processor.web_path + "/" + url
url = self.imageProcessor.web_path + "/" + url
attributes = self.common_attributes(e)
if "width" in e.attributes:
@ -187,7 +187,7 @@ class HTML_generator(Output_generator):
attributes["alt"] = e.title
else:
fake_out = io.StringIO()
HTML_generator(fake_out, self.katex_client, self.image_processor).generate(e.content)
HTMLGenerator(fake_out, self.katexClient, self.imageProcessor).generate(e.content)
attributes["alt"] = fake_out.getvalue()
if len(srcset) != 0:
@ -202,9 +202,9 @@ class HTML_generator(Output_generator):
self.generate(link)
def generate_Group(self, e: Group):
self.katex_client.begingroup()
self.katexClient.begingroup()
self.generate(e.content)
self.katex_client.endgroup()
self.katexClient.endgroup()
def generate_Plain(self, e: Plain):
self.generate(e.content)
@ -234,7 +234,7 @@ class HTML_generator(Output_generator):
"DisplayMath": True,
"InlineMath": False
}
self.writeln(self.katex_client.render(e.text, {"displayMode": formats[e.format]}))
self.writeln(self.katexClient.render(e.text, {"displayMode": formats[e.format]}))
def generate_RawInline(self, e: RawInline):
if e.format == "html":

6
src/formatitko/output_generator.py

@ -8,11 +8,11 @@ from .whitespace import NBSP
from .transform import FQuoted
from .context import Group
class UnknownElementException(Exception):
class UnknownElementError(Exception):
"An unknown Element has been passed to the Output_generator, probably because panflute introduced a new one."
pass
class Output_generator:
class OutputGenerator:
def __init__(self, output_file, indent_str: str="\t", initial_indent_level: int=0):
self.output_file = output_file
self.indent_str = indent_str
@ -85,7 +85,7 @@ class Output_generator:
ListItem: self.generate_ListItem
}[type(e)](e)
except KeyError:
raise UnknownElementException(type(e))
raise UnknownElementError(type(e))
def generate_ListContainer(self, e: ListContainer):
for child in e:

Loading…
Cancel
Save