]> git.scottworley.com Git - paperdoorknob/blob - paperdoorknob.py
0fb3fe18c1f5c8c34e9cf8ace6052b15a030995e
[paperdoorknob] / paperdoorknob.py
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 from typing import Any, Iterable
8
9 from bs4 import BeautifulSoup
10
11 from args import spec_from_commandline_args
12 from glowfic import chunkDOMs, flatURL, makeChunk
13 from spec import Spec
14
15
16 def parse(content: bytes) -> BeautifulSoup:
17 return BeautifulSoup(content, 'html.parser')
18
19
20 def ilen(it: Iterable[Any]) -> int:
21 return sum(1 for _ in it)
22
23
24 def process(spec: Spec) -> None:
25 spec.texout.write(br'''\documentclass{article}
26 \usepackage{booktabs}
27 \usepackage{graphicx}
28 \usepackage{longtable}
29 \usepackage{soul}
30 \usepackage{varwidth}
31 \usepackage{wrapstuff}
32 ''')
33 if spec.geometry is not None:
34 spec.texout.write(br'\usepackage[' +
35 spec.geometry.encode('UTF-8') +
36 b']{geometry}\n')
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
42 ]{#1}}
43 ''' % spec.icon_size)
44 url = flatURL(spec.url)
45 spec.log('Fetching HTML...\r')
46 html = spec.fetcher.fetch(url)
47 spec.log('Parsing HTML...\r')
48 dom = parse(spec.htmlfilter(html))
49 spec.log('Counting chunks...\r')
50 num_chunks = ilen(chunkDOMs(dom))
51 for i, r in enumerate(chunkDOMs(dom)):
52 percent = 100.0 * i / num_chunks
53 spec.log(f'Processing chunk {i} of {num_chunks} ({percent:.1f}%)\r')
54 spec.domfilter(r)
55 chunk = makeChunk(r, spec.images)
56 spec.texout.write(spec.texfilter(spec.layout.renderChunk(chunk)))
57 spec.log('')
58 spec.texout.write(b'\\end{document}\n')
59
60
61 def main() -> None:
62 with spec_from_commandline_args() as spec:
63 process(spec)
64
65
66 if __name__ == '__main__':
67 main()