]> git.scottworley.com Git - paperdoorknob/blame_incremental - paperdoorknob.py
Show name of thread being processed
[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 chunkDOMs, flatURL, makeChunk, renderChunk
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 spec.log('Counting chunks...\r')
61 num_chunks = ilen(chunkDOMs(dom))
62 title = get_title(dom) or "chunk"
63 for i, r in enumerate(chunkDOMs(dom)):
64 percent = 100.0 * i / num_chunks
65 spec.log(f'Processing {title} {i} of {num_chunks} ({percent:.1f}%)\r')
66 spec.domfilter(r)
67 chunk = makeChunk(r, spec.images)
68 spec.texout.write(spec.texfilter(renderChunk(spec.texifier, chunk)))
69 spec.log('')
70 spec.texout.write(b'\\end{document}\n')
71
72
73def main() -> None:
74 with spec_from_commandline_args() as spec:
75 process(spec)
76
77
78if __name__ == '__main__':
79 main()