]>
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 | 7 | from typing import Any, Iterable |
92b11a10 | 8 | |
136277e3 | 9 | from bs4 import BeautifulSoup |
23f31879 SW |
10 | |
11 | from args import spec_from_commandline_args | |
1452f8d3 | 12 | from glowfic import chunkDOMs, flatURL, makeChunk |
23f31879 | 13 | from spec import Spec |
92b11a10 SW |
14 | |
15 | ||
bf06f467 SW |
16 | def parse(content: bytes) -> BeautifulSoup: |
17 | return BeautifulSoup(content, 'html.parser') | |
b25a2f90 SW |
18 | |
19 | ||
81557fab SW |
20 | def ilen(it: Iterable[Any]) -> int: |
21 | return sum(1 for _ in it) | |
22 | ||
23 | ||
23f31879 | 24 | def process(spec: Spec) -> None: |
67612898 SW |
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} | |
d2a41ff4 | 32 | ''') |
e10b5b6f | 33 | if spec.geometry is not None: |
67612898 | 34 | spec.texout.write(br'\usepackage[' + |
e10b5b6f SW |
35 | spec.geometry.encode('UTF-8') + |
36 | b']{geometry}\n') | |
67612898 SW |
37 | spec.texout.write(br'''\begin{document} |
38 | \newcommand{\href}[2]{#2\footnote{\detokenize{#1}}} | |
9afdb32a SW |
39 | \def\glowiconsize{%fmm} |
40 | ''' % spec.icon_size) | |
1452f8d3 | 41 | url = flatURL(spec.url) |
d70acaef SW |
42 | spec.log('Fetching HTML...\r') |
43 | html = spec.fetcher.fetch(url) | |
44 | spec.log('Parsing HTML...\r') | |
45 | dom = parse(spec.htmlfilter(html)) | |
46 | spec.log('Counting chunks...\r') | |
606c97c5 SW |
47 | num_chunks = ilen(chunkDOMs(dom)) |
48 | for i, r in enumerate(chunkDOMs(dom)): | |
81557fab | 49 | percent = 100.0 * i / num_chunks |
c594c8bf | 50 | spec.log(f'Processing chunk {i} of {num_chunks} ({percent:.1f}%)\r') |
8be20b9d | 51 | spec.domfilter(r) |
d2a41ff4 | 52 | chunk = makeChunk(r, spec.images) |
131deef1 | 53 | spec.texout.write(spec.texfilter(spec.layout.renderChunk(chunk))) |
c594c8bf | 54 | spec.log('') |
23f31879 | 55 | spec.texout.write(b'\\end{document}\n') |
47cfa3cd SW |
56 | |
57 | ||
92b11a10 | 58 | def main() -> None: |
23f31879 SW |
59 | with spec_from_commandline_args() as spec: |
60 | process(spec) | |
92b11a10 SW |
61 | |
62 | ||
63 | if __name__ == '__main__': | |
64 | main() |