From: Scott Worley Date: Sun, 31 Dec 2023 22:27:10 +0000 (-0800) Subject: Learning TeX: Keep icon image size in TeX X-Git-Url: http://git.scottworley.com/paperdoorknob/commitdiff_plain/9afdb32a9385204c13018198a136637ad599df47?ds=sidebyside;hp=7804859f8fe8b8dc94b6a33b2221ac0b68d7ed86 Learning TeX: Keep icon image size in TeX --- diff --git a/args.py b/args.py index fc7a0f7..45c080a 100644 --- a/args.py +++ b/args.py @@ -89,9 +89,9 @@ def spec_from_commandline_args() -> Iterator[Spec]: texifier = PandocTexifier(args.pandoc or 'pandoc') layout: Layout if args.layout == 'below': - layout = BelowIconLayout(texifier, args.image_size) + layout = BelowIconLayout(texifier) elif args.layout == 'beside': - layout = BesideIconLayout(texifier, args.image_size) + layout = BesideIconLayout(texifier) else: raise ValueError(f'Unknown layout: {args.layout}') log = (lambda _: None) if args.quiet else _print_status @@ -104,6 +104,7 @@ def spec_from_commandline_args() -> Iterator[Spec]: lambda x: ApplyHTMLFilters(args.htmlfilters, x), lambda x: ApplyDOMFilters(args.domfilters, x), lambda x: ApplyTexFilters(args.texfilters, x), + args.image_size, layout, args.geometry, texout, diff --git a/glowfic.py b/glowfic.py index bda6d19..4c108b8 100644 --- a/glowfic.py +++ b/glowfic.py @@ -111,11 +111,12 @@ def makeChunk(chunk_dom: Tag, image_store: ImageStore) -> Chunk: content) -def renderIcon(icon_path: str | None, image_size: float) -> bytes | None: +def renderIcon(icon_path: str | None) -> bytes | None: if icon_path is None: return None - return br'\includegraphics[width=%fmm,height=%fmm,keepaspectratio]{%s}' % ( - image_size, image_size, icon_path.encode('UTF-8')) + return ( + br'\includegraphics[width=\glowiconsize,height=\glowiconsize,keepaspectratio]{%s}' % + icon_path.encode('UTF-8')) class Layout(ABC): @@ -136,12 +137,11 @@ class ContentOnlyLayout(Layout): class BelowIconLayout(Layout): - def __init__(self, texifier: Texifier, image_size: float) -> None: + def __init__(self, texifier: Texifier) -> None: self._texifier = texifier - self._image_size = image_size def renderChunk(self, chunk: Chunk) -> bytes: - icon = renderIcon(chunk.icon, self._image_size) + icon = renderIcon(chunk.icon) meta = [icon] if icon else [] meta += [self._texifier.texify(x) for x in [chunk.character, chunk.screen_name, chunk.author] @@ -175,12 +175,11 @@ class BelowIconLayout(Layout): class BesideIconLayout(Layout): - def __init__(self, texifier: Texifier, image_size: float) -> None: + def __init__(self, texifier: Texifier) -> None: self._texifier = texifier - self._image_size = image_size def renderChunk(self, chunk: Chunk) -> bytes: - icon = renderIcon(chunk.icon, self._image_size) + icon = renderIcon(chunk.icon) meta = [ chunk.character, chunk.screen_name, diff --git a/paperdoorknob.py b/paperdoorknob.py index a19045c..8183136 100644 --- a/paperdoorknob.py +++ b/paperdoorknob.py @@ -36,7 +36,8 @@ def process(spec: Spec) -> None: b']{geometry}\n') spec.texout.write(br'''\begin{document} \newcommand{\href}[2]{#2\footnote{\detokenize{#1}}} -''') +\def\glowiconsize{%fmm} +''' % spec.icon_size) url = flatURL(spec.url) spec.log('Fetching HTML...\r') html = spec.fetcher.fetch(url) diff --git a/paperdoorknob_test.py b/paperdoorknob_test.py index 61c8e6a..286d6a9 100644 --- a/paperdoorknob_test.py +++ b/paperdoorknob_test.py @@ -42,6 +42,7 @@ class BaseTestProcess(ABC): lambda x: x, lambda x: None, lambda x: x, + 20, ContentOnlyLayout(PandocTexifier('pandoc')), 'margin=20mm', buf, @@ -50,7 +51,7 @@ class BaseTestProcess(ABC): assert re.match(br'''\\documentclass{article} (\\usepackage{[a-z]+}\n)+\\usepackage\[margin=20mm\]{geometry} \\begin{document} -.* +(.|\n)* This is \\href{https://glowfic.com}{glowfic} You \\emph{sure}\? @@ -71,6 +72,7 @@ Pretty sure. lambda x: x, lambda x: None, lambda x: x, + 20, ContentOnlyLayout(texifier), None, buf, @@ -86,7 +88,8 @@ Pretty sure. lambda x: x, lambda x: None, lambda x: x, - BesideIconLayout(PandocTexifier('pandoc'), 20), + 20, + BesideIconLayout(PandocTexifier('pandoc')), None, out, lambda _: None) diff --git a/spec.py b/spec.py index 2eccbb5..c4975b5 100644 --- a/spec.py +++ b/spec.py @@ -25,6 +25,7 @@ class Spec: htmlfilter: Callable[[bytes], bytes] domfilter: Callable[[Tag], None] texfilter: Callable[[bytes], bytes] + icon_size: float layout: Layout geometry: str | None texout: IO[bytes]