]>
Commit | Line | Data |
---|---|---|
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 SW |
7 | from sys import stderr |
8 | ||
9 | from typing import Any, Iterable | |
92b11a10 | 10 | |
136277e3 | 11 | from bs4 import BeautifulSoup |
23f31879 SW |
12 | |
13 | from args import spec_from_commandline_args | |
1452f8d3 | 14 | from glowfic import chunkDOMs, flatURL, makeChunk |
23f31879 | 15 | from spec import Spec |
92b11a10 SW |
16 | |
17 | ||
bf06f467 SW |
18 | def parse(content: bytes) -> BeautifulSoup: |
19 | return BeautifulSoup(content, 'html.parser') | |
b25a2f90 SW |
20 | |
21 | ||
81557fab SW |
22 | def ilen(it: Iterable[Any]) -> int: |
23 | return sum(1 for _ in it) | |
24 | ||
25 | ||
23f31879 | 26 | def process(spec: Spec) -> None: |
d2a41ff4 | 27 | spec.texout.write(b'''\\documentclass{article} |
a5f4539c | 28 | \\usepackage{booktabs} |
d2a41ff4 | 29 | \\usepackage{graphicx} |
a5f4539c | 30 | \\usepackage{longtable} |
24e86634 | 31 | \\usepackage{soul} |
23dabdf5 | 32 | \\usepackage{varwidth} |
357f37be | 33 | \\usepackage{wrapstuff} |
d2a41ff4 | 34 | ''') |
e10b5b6f SW |
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') | |
1452f8d3 SW |
40 | url = flatURL(spec.url) |
41 | html = parse(spec.htmlfilter(spec.fetcher.fetch(url))) | |
81557fab SW |
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) | |
8be20b9d | 49 | spec.domfilter(r) |
d2a41ff4 | 50 | chunk = makeChunk(r, spec.images) |
131deef1 | 51 | spec.texout.write(spec.texfilter(spec.layout.renderChunk(chunk))) |
23f31879 | 52 | spec.texout.write(b'\\end{document}\n') |
47cfa3cd SW |
53 | |
54 | ||
92b11a10 | 55 | def main() -> None: |
23f31879 SW |
56 | with spec_from_commandline_args() as spec: |
57 | process(spec) | |
92b11a10 SW |
58 | |
59 | ||
60 | if __name__ == '__main__': | |
61 | main() |