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
11 from args
import spec_from_commandline_args
12 from glowfic
import flatURL
, makeChunk
, renderChunk
, Thread
16 def parse(content
: bytes) -> BeautifulSoup
:
17 return BeautifulSoup(content
, 'html.parser')
20 def ilen(it
: Iterable
[Any
]) -> int:
21 return sum(1 for _
in it
)
24 def process(spec
: Spec
) -> None:
25 spec
.texout
.write(br
'''\documentclass{article}
28 \usepackage{longtable}
31 \usepackage{wrapstuff}
33 if spec
.geometry
is not None:
34 spec
.texout
.write(br
'\usepackage[' +
35 spec
.geometry
.encode('UTF-8') +
37 spec
.texout
.write(br
'''\begin{document}
38 \newcommand{\href}[2]{#2\footnote{\detokenize{#1}}}
39 \def\glowiconsize{%fmm}
40 \newcommand{\glowicon}[1]{\includegraphics[
41 width=\glowiconsize,height=\glowiconsize,keepaspectratio
43 \newcommand{\ifnotempty}[2]{\expandafter\ifx\expandafter\relax
44 \detokenize{#1}\relax\else #2\fi}
46 ''' % (spec
.icon_size
, spec
.layout
))
47 url
= flatURL(spec
.url
)
48 spec
.log('Fetching HTML...\r')
49 html
= spec
.fetcher
.fetch(url
)
50 spec
.log('Parsing HTML...\r')
51 dom
= parse(spec
.htmlfilter(html
))
53 spec
.log('Counting chunks...\r')
54 num_chunks
= ilen(thread
.chunkDOMs())
55 title
= thread
.title() or "chunk"
56 for i
, r
in enumerate(thread
.chunkDOMs()):
57 percent
= 100.0 * i
/ num_chunks
58 spec
.log(f
'Processing {title} {i} of {num_chunks} ({percent:.1f}%)\r')
60 chunk
= makeChunk(r
, spec
.images
)
61 spec
.texout
.write(spec
.texfilter(renderChunk(spec
.texifier
, chunk
)))
63 spec
.texout
.write(b
'\\end{document}\n')
67 with spec_from_commandline_args() as spec
:
71 if __name__
== '__main__':