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.
7 from typing
import Any
, Iterable
9 from bs4
import BeautifulSoup
10 from bs4
.element
import Tag
12 from args
import spec_from_commandline_args
13 from glowfic
import flatURL
, makeChunk
, renderChunk
, Thread
17 def parse(content
: bytes) -> BeautifulSoup
:
18 return BeautifulSoup(content
, 'html.parser')
21 def ilen(it
: Iterable
[Any
]) -> int:
22 return sum(1 for _
in it
)
25 def get_title(dom
: BeautifulSoup
) -> str |
None:
26 span
= dom
.findChild("span", id="post-title")
27 if not isinstance(span
, Tag
):
32 def process(spec
: Spec
) -> None:
33 spec
.texout
.write(br
'''\documentclass{article}
36 \usepackage{longtable}
39 \usepackage{wrapstuff}
41 if spec
.geometry
is not None:
42 spec
.texout
.write(br
'\usepackage[' +
43 spec
.geometry
.encode('UTF-8') +
45 spec
.texout
.write(br
'''\begin{document}
46 \newcommand{\href}[2]{#2\footnote{\detokenize{#1}}}
47 \def\glowiconsize{%fmm}
48 \newcommand{\glowicon}[1]{\includegraphics[
49 width=\glowiconsize,height=\glowiconsize,keepaspectratio
51 \newcommand{\ifnotempty}[2]{\expandafter\ifx\expandafter\relax
52 \detokenize{#1}\relax\else #2\fi}
54 ''' % (spec
.icon_size
, spec
.layout
))
55 url
= flatURL(spec
.url
)
56 spec
.log('Fetching HTML...\r')
57 html
= spec
.fetcher
.fetch(url
)
58 spec
.log('Parsing HTML...\r')
59 dom
= parse(spec
.htmlfilter(html
))
61 spec
.log('Counting chunks...\r')
62 num_chunks
= ilen(thread
.chunkDOMs())
63 title
= get_title(dom
) or "chunk"
64 for i
, r
in enumerate(thread
.chunkDOMs()):
65 percent
= 100.0 * i
/ num_chunks
66 spec
.log(f
'Processing {title} {i} of {num_chunks} ({percent:.1f}%)\r')
68 chunk
= makeChunk(r
, spec
.images
)
69 spec
.texout
.write(spec
.texfilter(renderChunk(spec
.texifier
, chunk
)))
71 spec
.texout
.write(b
'\\end{document}\n')
75 with spec_from_commandline_args() as spec
:
79 if __name__
== '__main__':