]>
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 dataclasses
import dataclass
10 from urllib
.parse
import parse_qsl
, urlencode
, urljoin
, urlparse
, urlunparse
12 from typing
import Iterable
14 from bs4
import BeautifulSoup
15 from bs4
.element
import Tag
17 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
: Tag |
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.
60 def __init__(self
, spec
: Spec
) -> None:
61 def find_next_thread(dom
: BeautifulSoup
) -> str |
None:
62 for c
in dom
.findChildren('div', class_
='post-navheader'):
63 for a
in c
.findChildren('a'):
64 if 'Next Post' in a
.text
and 'href' in a
.attrs
and isinstance(
65 a
.attrs
['href'], str):
66 return urljoin(spec
.url
, a
.attrs
['href'])
69 spec
.log('Fetching HTML...\r')
70 html
= spec
.fetcher
.fetch(spec
.url
)
71 flat_html
= spec
.fetcher
.fetch(flatURL(spec
.url
))
72 spec
.log('Parsing HTML...\r')
73 self
._next
_thread
= find_next_thread(
74 BeautifulSoup(spec
.htmlfilter(html
), 'html.parser'))
75 self
._dom
= BeautifulSoup(spec
.htmlfilter(flat_html
), 'html.parser')
77 def title(self
) -> str |
None:
78 span
= self
._dom
.findChild("span", id="post-title")
79 if not isinstance(span
, Tag
):
81 return span
.text
.strip()
83 def next_thread(self
) -> str |
None:
84 return self
._next
_thread
86 def chunkDOMs(self
) -> Iterable
[Tag
]:
90 text
= body
.find_next("div", class_
="post-post")
91 assert isinstance(text
, Tag
)
94 def the_replies() -> Iterable
[Tag
]:
95 rs
= self
._dom
.find_all("div", class_
="post-reply")
96 assert all(isinstance(r
, Tag
) for r
in rs
)
99 return itertools
.chain([text()], the_replies())
102 def makeChunk(chunk_dom
: Tag
, image_store
: ImageStore
) -> Chunk
:
104 def getIcon() -> str |
None:
105 icon_div
= chunk_dom
.findChild('div', class_
='post-icon')
108 assert isinstance(icon_div
, Tag
)
109 icon_img
= icon_div
.findChild('img')
112 assert isinstance(icon_img
, Tag
)
113 return image_store
.get_image(icon_img
.attrs
['src'])
115 def getByClass(css_class
: str) -> Tag |
None:
116 tag
= chunk_dom
.findChild('div', class_
=css_class
)
117 assert tag
is None or isinstance(tag
, Tag
)
120 def stripHREF(tag
: Tag
) -> None:
121 for c
in tag
.findChildren("a"):
122 if "href" in c
.attrs
:
125 def getMeta(css_class
: str) -> Tag |
None:
126 tag
= getByClass(css_class
)
132 content
= chunk_dom
.findChild('div', class_
='post-content')
133 assert isinstance(content
, Tag
)
135 return Chunk(getIcon(),
136 getMeta('post-character'),
137 getMeta('post-screenname'),
138 getMeta('post-author'),
142 def renderChunk(texifier
: Texifier
, chunk
: Chunk
) -> bytes:
145 br
'\glowicon{%s}' % chunk
.icon
.encode('UTF-8') if chunk
.icon
else b
'',
147 texifier
.texify(chunk
.character
) if chunk
.character
else b
'',
149 texifier
.texify(chunk
.screen_name
) if chunk
.screen_name
else b
'',
151 texifier
.texify(chunk
.author
) if chunk
.author
else b
'',
153 texifier
.texify(chunk
.content
)])
156 ContentOnlyLayout
= br
'''
157 \newcommand{\glowhead}[4]{}
161 BelowIconLayout
= br
'''
162 \newcommand{\glowhead}[4]{\wrapstuffclear
165 \begin{varwidth}{0.5\textwidth}
166 \smash{\parbox[t][0pt]{0pt}{
167 \setlength{\fboxrule}{0.2pt}
168 \setlength{\fboxsep}{0pt}
170 \fbox{\hspace{107mm}}
175 {#1}{\\*}#2\ifnotempty
176 {#2}{\\*}#3\ifnotempty
188 # Why is \textwidth not the width of the text?
189 # Why is the width of the text .765\textwidth?
190 BesideIconLayout
= br
'''
191 \newcommand{\glowhead}[4]{
197 \parbox[b]{.765\textwidth}{
200 {#2}{\\*}#3\ifnotempty