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