]> git.scottworley.com Git - paperdoorknob/blame - paperdoorknob.py
--quiet flag to suppress progress messages
[paperdoorknob] / paperdoorknob.py
CommitLineData
92b11a10
SW
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
81557fab 7from typing import Any, Iterable
92b11a10 8
136277e3 9from bs4 import BeautifulSoup
23f31879
SW
10
11from args import spec_from_commandline_args
1452f8d3 12from glowfic import chunkDOMs, flatURL, makeChunk
23f31879 13from spec import Spec
92b11a10
SW
14
15
bf06f467
SW
16def parse(content: bytes) -> BeautifulSoup:
17 return BeautifulSoup(content, 'html.parser')
b25a2f90
SW
18
19
81557fab
SW
20def ilen(it: Iterable[Any]) -> int:
21 return sum(1 for _ in it)
22
23
23f31879 24def process(spec: Spec) -> None:
d2a41ff4 25 spec.texout.write(b'''\\documentclass{article}
a5f4539c 26\\usepackage{booktabs}
d2a41ff4 27\\usepackage{graphicx}
a5f4539c 28\\usepackage{longtable}
24e86634 29\\usepackage{soul}
23dabdf5 30\\usepackage{varwidth}
357f37be 31\\usepackage{wrapstuff}
d2a41ff4 32''')
e10b5b6f
SW
33 if spec.geometry is not None:
34 spec.texout.write(b'\\usepackage[' +
35 spec.geometry.encode('UTF-8') +
36 b']{geometry}\n')
37 spec.texout.write(b'\\begin{document}\n')
1452f8d3
SW
38 url = flatURL(spec.url)
39 html = parse(spec.htmlfilter(spec.fetcher.fetch(url)))
81557fab
SW
40 num_chunks = ilen(chunkDOMs(html))
41 for i, r in enumerate(chunkDOMs(html)):
42 percent = 100.0 * i / num_chunks
c594c8bf 43 spec.log(f'Processing chunk {i} of {num_chunks} ({percent:.1f}%)\r')
8be20b9d 44 spec.domfilter(r)
d2a41ff4 45 chunk = makeChunk(r, spec.images)
131deef1 46 spec.texout.write(spec.texfilter(spec.layout.renderChunk(chunk)))
c594c8bf 47 spec.log('')
23f31879 48 spec.texout.write(b'\\end{document}\n')
47cfa3cd
SW
49
50
92b11a10 51def main() -> None:
23f31879
SW
52 with spec_from_commandline_args() as spec:
53 process(spec)
92b11a10
SW
54
55
56if __name__ == '__main__':
57 main()