]>
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 |
7b4b6812 | 10 | from bs4.element import Tag |
23f31879 SW |
11 | |
12 | from args import spec_from_commandline_args | |
94027099 | 13 | from glowfic import flatURL, makeChunk, renderChunk, Thread |
23f31879 | 14 | from spec import Spec |
92b11a10 SW |
15 | |
16 | ||
bf06f467 SW |
17 | def parse(content: bytes) -> BeautifulSoup: |
18 | return BeautifulSoup(content, 'html.parser') | |
b25a2f90 SW |
19 | |
20 | ||
81557fab SW |
21 | def ilen(it: Iterable[Any]) -> int: |
22 | return sum(1 for _ in it) | |
23 | ||
24 | ||
7b4b6812 SW |
25 | def get_title(dom: BeautifulSoup) -> str | None: |
26 | span = dom.findChild("span", id="post-title") | |
27 | if not isinstance(span, Tag): | |
28 | return None | |
29 | return span.text | |
30 | ||
31 | ||
23f31879 | 32 | def process(spec: Spec) -> None: |
67612898 SW |
33 | spec.texout.write(br'''\documentclass{article} |
34 | \usepackage{booktabs} | |
35 | \usepackage{graphicx} | |
36 | \usepackage{longtable} | |
37 | \usepackage{soul} | |
38 | \usepackage{varwidth} | |
39 | \usepackage{wrapstuff} | |
d2a41ff4 | 40 | ''') |
e10b5b6f | 41 | if spec.geometry is not None: |
67612898 | 42 | spec.texout.write(br'\usepackage[' + |
e10b5b6f SW |
43 | spec.geometry.encode('UTF-8') + |
44 | b']{geometry}\n') | |
67612898 SW |
45 | spec.texout.write(br'''\begin{document} |
46 | \newcommand{\href}[2]{#2\footnote{\detokenize{#1}}} | |
9afdb32a | 47 | \def\glowiconsize{%fmm} |
5f230208 SW |
48 | \newcommand{\glowicon}[1]{\includegraphics[ |
49 | width=\glowiconsize,height=\glowiconsize,keepaspectratio | |
50 | ]{#1}} | |
1fac41bf SW |
51 | \newcommand{\ifnotempty}[2]{\expandafter\ifx\expandafter\relax |
52 | \detokenize{#1}\relax\else #2\fi} | |
53 | %s | |
54 | ''' % (spec.icon_size, spec.layout)) | |
1452f8d3 | 55 | url = flatURL(spec.url) |
d70acaef SW |
56 | spec.log('Fetching HTML...\r') |
57 | html = spec.fetcher.fetch(url) | |
58 | spec.log('Parsing HTML...\r') | |
59 | dom = parse(spec.htmlfilter(html)) | |
94027099 | 60 | thread = Thread(dom) |
d70acaef | 61 | spec.log('Counting chunks...\r') |
94027099 | 62 | num_chunks = ilen(thread.chunkDOMs()) |
7b4b6812 | 63 | title = get_title(dom) or "chunk" |
94027099 | 64 | for i, r in enumerate(thread.chunkDOMs()): |
81557fab | 65 | percent = 100.0 * i / num_chunks |
7b4b6812 | 66 | spec.log(f'Processing {title} {i} of {num_chunks} ({percent:.1f}%)\r') |
8be20b9d | 67 | spec.domfilter(r) |
d2a41ff4 | 68 | chunk = makeChunk(r, spec.images) |
1fac41bf | 69 | spec.texout.write(spec.texfilter(renderChunk(spec.texifier, chunk))) |
c594c8bf | 70 | spec.log('') |
23f31879 | 71 | spec.texout.write(b'\\end{document}\n') |
47cfa3cd SW |
72 | |
73 | ||
92b11a10 | 74 | def main() -> None: |
23f31879 SW |
75 | with spec_from_commandline_args() as spec: |
76 | process(spec) | |
92b11a10 SW |
77 | |
78 | ||
79 | if __name__ == '__main__': | |
80 | main() |