]> git.scottworley.com Git - paperdoorknob/commitdiff
Learning TeX: Keep icon image size in TeX
authorScott Worley <scottworley@scottworley.com>
Sun, 31 Dec 2023 22:27:10 +0000 (14:27 -0800)
committerScott Worley <scottworley@scottworley.com>
Mon, 1 Jan 2024 06:14:24 +0000 (22:14 -0800)
args.py
glowfic.py
paperdoorknob.py
paperdoorknob_test.py
spec.py

diff --git a/args.py b/args.py
index fc7a0f7af147073450761e06fa262a3eea66992d..45c080a30a7eb34904558d89697f14c6f9d5854d 100644 (file)
--- 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,
index bda6d1941c84eb1b578461439dfa87ada971469a..4c108b8310dc3441e373c08c11854f4ff8405c0d 100644 (file)
@@ -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,
index a19045cbfde8ba813d8f549650592ce27c746bf1..81831360e462a6a9af1b366f3384e566e0adb412 100644 (file)
@@ -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)
index 61c8e6a77b6450054af5dcfc9d313efbc06ef1c1..286d6a9fc3aa65ca4cae3c4934470f27e775e468 100644 (file)
@@ -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 2eccbb5562fd359c2148f0039d8e637f9d10474d..c4975b507e64bf6a3e3622d3e0dbf7fb32901260 100644 (file)
--- 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]