]> git.scottworley.com Git - paperdoorknob/blame - glowfic.py
Learning TeX: Keep icon image size in TeX
[paperdoorknob] / glowfic.py
CommitLineData
e6adf6ce
SW
1# paperdoorknob: Print glowfic
2#
3# This program is free software: you can redistribute it and/or modify it
4# under the terms of the GNU General Public License as published by the
5# Free Software Foundation, version 3.
6
7
d2a41ff4 8from abc import ABC, abstractmethod
aa060d9b 9from dataclasses import dataclass
e6adf6ce 10import itertools
1452f8d3 11from urllib.parse import parse_qsl, urlencode, urlparse, urlunparse
e6adf6ce
SW
12
13from typing import Iterable
14
15from bs4 import BeautifulSoup
16from bs4.element import Tag
17
aa060d9b 18from images import ImageStore
d2a41ff4 19from texify import Texifier
aa060d9b
SW
20
21
1452f8d3
SW
22def _removeViewFromURL(url: str) -> str:
23 u = urlparse(url)
24 old_qs = parse_qsl(u.query)
25 new_qs = [(k, v) for k, v in old_qs if k != 'view']
26 return urlunparse(u._replace(query=urlencode(new_qs)))
27
28
29def nonFlatURL(url: str) -> str:
30 return _removeViewFromURL(url)
31
32
33def flatURL(url: str) -> str:
34 u = urlparse(_removeViewFromURL(url))
35 qs = parse_qsl(u.query) + [('view', 'flat')]
36 return urlunparse(u._replace(query=urlencode(qs)))
37
38
aa060d9b
SW
39@dataclass(frozen=True)
40class Chunk:
41 icon: str | None
37c47bc2
SW
42 character: Tag | None
43 screen_name: Tag | None
44 author: Tag | None
aa060d9b
SW
45 content: Tag
46
e6adf6ce
SW
47# We avoid the name "post" because the Glowfic community uses the term
48# inconsistently:
49# * The Glowfic software sometimes uses "post" to refer to a whole thread
aa060d9b
SW
50# (in the URL), sometimes uses "post" to refer to chunks (in the CSS),
51# but mostly uses "post" to refer to just the first chunk in a thread
52# (in the HTML and UI). The non-first chunks are "replies".
e6adf6ce
SW
53# * Readers and this software don't need to distinguish first-chunks and
54# non-first-chunks.
aa060d9b 55# * Humans in the community tend to use "posts" to mean chunks.
e6adf6ce
SW
56
57
58def chunkDOMs(html: BeautifulSoup) -> Iterable[Tag]:
59 def text() -> Tag:
60 body = html.body
61 assert body
62 text = body.find_next("div", class_="post-post")
63 assert isinstance(text, Tag)
64 return text
65
66 def the_replies() -> Iterable[Tag]:
67 rs = html.find_all("div", class_="post-reply")
68 assert all(isinstance(r, Tag) for r in rs)
69 return rs
70
71 return itertools.chain([text()], the_replies())
aa060d9b
SW
72
73
74def makeChunk(chunk_dom: Tag, image_store: ImageStore) -> Chunk:
75
76 def getIcon() -> str | None:
551bb1c9 77 icon_div = chunk_dom.findChild('div', class_='post-icon')
aa060d9b
SW
78 if icon_div is None:
79 return None
551bb1c9
SW
80 assert isinstance(icon_div, Tag)
81 icon_img = icon_div.findChild('img')
aa060d9b
SW
82 if icon_img is None:
83 return None
84 assert isinstance(icon_img, Tag)
85 return image_store.get_image(icon_img.attrs['src'])
86
37c47bc2 87 def getByClass(css_class: str) -> Tag | None:
551bb1c9 88 tag = chunk_dom.findChild('div', class_=css_class)
37c47bc2
SW
89 assert tag is None or isinstance(tag, Tag)
90 return tag
aa060d9b 91
62043b2b
SW
92 def stripHREF(tag: Tag) -> None:
93 for c in tag.findChildren("a"):
94 if "href" in c.attrs:
95 del c.attrs["href"]
96
97 def getMeta(css_class: str) -> Tag | None:
98 tag = getByClass(css_class)
99 if tag is None:
100 return None
101 stripHREF(tag)
102 return tag
103
551bb1c9 104 content = chunk_dom.findChild('div', class_='post-content')
aa060d9b
SW
105 assert isinstance(content, Tag)
106
107 return Chunk(getIcon(),
62043b2b
SW
108 getMeta('post-character'),
109 getMeta('post-screenname'),
110 getMeta('post-author'),
aa060d9b 111 content)
d2a41ff4
SW
112
113
9afdb32a 114def renderIcon(icon_path: str | None) -> bytes | None:
39db9a10
SW
115 if icon_path is None:
116 return None
9afdb32a
SW
117 return (
118 br'\includegraphics[width=\glowiconsize,height=\glowiconsize,keepaspectratio]{%s}' %
119 icon_path.encode('UTF-8'))
d2a41ff4
SW
120
121
122class Layout(ABC):
123
124 @abstractmethod
125 def renderChunk(self, chunk: Chunk) -> bytes:
126 raise NotImplementedError()
127
128
129class ContentOnlyLayout(Layout):
130
131 def __init__(self, texifier: Texifier) -> None:
132 self._texifier = texifier
133
134 def renderChunk(self, chunk: Chunk) -> bytes:
23dabdf5 135 return self._texifier.texify(chunk.content) + b'\n'
d2a41ff4
SW
136
137
138class BelowIconLayout(Layout):
139
9afdb32a 140 def __init__(self, texifier: Texifier) -> None:
d2a41ff4
SW
141 self._texifier = texifier
142
143 def renderChunk(self, chunk: Chunk) -> bytes:
9afdb32a 144 icon = renderIcon(chunk.icon)
39db9a10 145 meta = [icon] if icon else []
37c47bc2
SW
146 meta += [self._texifier.texify(x)
147 for x in [chunk.character, chunk.screen_name, chunk.author]
148 if x is not None]
23dabdf5 149
67612898
SW
150 return br'''\wrapstuffclear
151\begin{wrapstuff}[l]
152\fbox{
153\begin{varwidth}{0.5\textwidth}
154 \smash{\parbox[t][0pt]{0pt}{
155 \setlength{\fboxrule}{0.2pt}
156 \setlength{\fboxsep}{0pt}
157 \vspace{-3.4pt}
158 \fbox{\hspace{107mm}}
159 }\\*}
160 \vspace{-1em}
161\begin{center}
d2a41ff4 162%s
67612898
SW
163\end{center}
164\end{varwidth}
23dabdf5 165}
67612898 166\end{wrapstuff}
23dabdf5 167
67612898 168\strut
16385131 169
67612898 170\noindent %s
d2a41ff4 171''' % (
67612898 172 br'\\*'.join(meta),
d2a41ff4 173 self._texifier.texify(chunk.content))
f75c1629
SW
174
175
176class BesideIconLayout(Layout):
177
9afdb32a 178 def __init__(self, texifier: Texifier) -> None:
f75c1629 179 self._texifier = texifier
f75c1629
SW
180
181 def renderChunk(self, chunk: Chunk) -> bytes:
9afdb32a 182 icon = renderIcon(chunk.icon)
f75c1629
SW
183 meta = [
184 chunk.character,
185 chunk.screen_name,
186 chunk.author,
187 ]
188
189 # Why is \textwidth not the width of the text?
190 # Why is the width of the text .765\textwidth?
67612898 191 return br'''\noindent\fbox{
f75c1629 192%s
67612898
SW
193\parbox[b]{.765\textwidth}{
194\begin{center}
f75c1629 195%s
67612898 196\end{center}
f75c1629 197}
67612898
SW
198}\\*
199\vspace{-0.75em}\\*
200\noindent %s
f75c1629 201
67612898 202\strut
f75c1629
SW
203
204''' % (
39db9a10 205 icon if icon else b'',
67612898 206 br'\\*'.join(self._texifier.texify(x) for x in meta if x is not None),
f75c1629 207 self._texifier.texify(chunk.content))