]>
git.scottworley.com Git - paperdoorknob/blob - glowfic.py
1 # paperdoorknob: Print glowfic
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.
8 from abc
import ABC
, abstractmethod
9 from dataclasses
import dataclass
11 from urllib
.parse
import parse_qsl
, urlencode
, urlparse
, urlunparse
13 from typing
import Iterable
15 from bs4
import BeautifulSoup
16 from bs4
.element
import Tag
18 from images
import ImageStore
19 from texify
import Texifier
22 def _removeViewFromURL(url
: str) -> str:
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
)))
29 def nonFlatURL(url
: str) -> str:
30 return _removeViewFromURL(url
)
33 def 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
)))
39 @dataclass(frozen
=True)
43 screen_name
: str |
None
47 # We avoid the name "post" because the Glowfic community uses the term
49 # * The Glowfic software sometimes uses "post" to refer to a whole thread
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".
53 # * Readers and this software don't need to distinguish first-chunks and
55 # * Humans in the community tend to use "posts" to mean chunks.
58 def chunkDOMs(html
: BeautifulSoup
) -> Iterable
[Tag
]:
62 text
= body
.find_next("div", class_
="post-post")
63 assert isinstance(text
, Tag
)
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
)
71 return itertools
.chain([text()], the_replies())
74 def makeChunk(chunk_dom
: Tag
, image_store
: ImageStore
) -> Chunk
:
76 def getIcon() -> str |
None:
77 icon_div
= chunk_dom
.find_next('div', class_
='post-icon')
80 icon_img
= icon_div
.find_next('img')
83 assert isinstance(icon_img
, Tag
)
84 return image_store
.get_image(icon_img
.attrs
['src'])
86 def getTextByClass(css_class
: str) -> str |
None:
87 div
= chunk_dom
.find_next('div', class_
=css_class
)
90 return div
.text
.strip()
92 content
= chunk_dom
.find_next('div', class_
='post-content')
93 assert isinstance(content
, Tag
)
95 return Chunk(getIcon(),
96 getTextByClass('post-character'),
97 getTextByClass('post-screenname'),
98 getTextByClass('post-author'),
102 def renderIcon(icon_path
: str |
None, image_size
: float) -> str |
None:
103 params
= f
'width={image_size}mm,height={image_size}mm,keepaspectratio'
104 return f
'\\includegraphics[{params}]{{{icon_path}}}' if icon_path
else None
110 def renderChunk(self
, chunk
: Chunk
) -> bytes:
111 raise NotImplementedError()
114 class ContentOnlyLayout(Layout
):
116 def __init__(self
, texifier
: Texifier
) -> None:
117 self
._texifier
= texifier
119 def renderChunk(self
, chunk
: Chunk
) -> bytes:
120 return self
._texifier
.texify(chunk
.content
) + b
'\n'
123 class BelowIconLayout(Layout
):
125 def __init__(self
, texifier
: Texifier
, image_size
: float) -> None:
126 self
._texifier
= texifier
127 self
._image
_size
= image_size
129 def renderChunk(self
, chunk
: Chunk
) -> bytes:
131 renderIcon(chunk
.icon
, self
._image
_size
),
137 return b
'''\\wrapstuffclear
138 \\begin{wrapstuff}[l]
140 \\begin{varwidth}{0.5\\textwidth}
141 \\smash{\\parbox[t][0pt]{0pt}{
142 \\setlength{\\fboxrule}{0.2pt}
143 \\setlength{\\fboxsep}{0pt}
145 \\fbox{\\hspace{107mm}}
159 b
'\\\\*'.join(x
.encode('UTF-8') for x
in meta
if x
is not None),
160 self
._texifier
.texify(chunk
.content
))
163 class BesideIconLayout(Layout
):
165 def __init__(self
, texifier
: Texifier
, image_size
: float) -> None:
166 self
._texifier
= texifier
167 self
._image
_size
= image_size
169 def renderChunk(self
, chunk
: Chunk
) -> bytes:
170 icon
= renderIcon(chunk
.icon
, self
._image
_size
)
177 # Why is \textwidth not the width of the text?
178 # Why is the width of the text .765\textwidth?
179 return b
'''\\noindent\\fbox{
181 \\parbox[b]{.765\\textwidth}{
187 \\vspace{-0.75em}\\\\*
193 icon
.encode('UTF-8') if icon
else b
'',
194 b
'\\\\*'.join(x
.encode('UTF-8') for x
in meta
if x
is not None),
195 self
._texifier
.texify(chunk
.content
))