]> git.scottworley.com Git - paperdoorknob/blame_incremental - paperdoorknob.py
Rename html → dom
[paperdoorknob] / paperdoorknob.py
... / ...
CommitLineData
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
7from typing import Any, Iterable
8
9from bs4 import BeautifulSoup
10from bs4.element import Tag
11
12from args import spec_from_commandline_args
13from glowfic import flatURL, makeChunk, renderChunk, Thread
14from spec import Spec
15
16
17def parse(content: bytes) -> BeautifulSoup:
18 return BeautifulSoup(content, 'html.parser')
19
20
21def ilen(it: Iterable[Any]) -> int:
22 return sum(1 for _ in it)
23
24
25def 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
32def process(spec: Spec) -> None:
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}
40''')
41 if spec.geometry is not None:
42 spec.texout.write(br'\usepackage[' +
43 spec.geometry.encode('UTF-8') +
44 b']{geometry}\n')
45 spec.texout.write(br'''\begin{document}
46\newcommand{\href}[2]{#2\footnote{\detokenize{#1}}}
47\def\glowiconsize{%fmm}
48\newcommand{\glowicon}[1]{\includegraphics[
49 width=\glowiconsize,height=\glowiconsize,keepaspectratio
50]{#1}}
51\newcommand{\ifnotempty}[2]{\expandafter\ifx\expandafter\relax
52 \detokenize{#1}\relax\else #2\fi}
53%s
54''' % (spec.icon_size, spec.layout))
55 url = flatURL(spec.url)
56 spec.log('Fetching HTML...\r')
57 html = spec.fetcher.fetch(url)
58 spec.log('Parsing HTML...\r')
59 dom = parse(spec.htmlfilter(html))
60 thread = Thread(dom)
61 spec.log('Counting chunks...\r')
62 num_chunks = ilen(thread.chunkDOMs())
63 title = get_title(dom) or "chunk"
64 for i, r in enumerate(thread.chunkDOMs()):
65 percent = 100.0 * i / num_chunks
66 spec.log(f'Processing {title} {i} of {num_chunks} ({percent:.1f}%)\r')
67 spec.domfilter(r)
68 chunk = makeChunk(r, spec.images)
69 spec.texout.write(spec.texfilter(renderChunk(spec.texifier, chunk)))
70 spec.log('')
71 spec.texout.write(b'\\end{document}\n')
72
73
74def main() -> None:
75 with spec_from_commandline_args() as spec:
76 process(spec)
77
78
79if __name__ == '__main__':
80 main()