]> git.scottworley.com Git - paperdoorknob/blame - paperdoorknob.py
Show name of thread being processed
[paperdoorknob] / paperdoorknob.py
CommitLineData
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 7from typing import Any, Iterable
92b11a10 8
136277e3 9from bs4 import BeautifulSoup
7b4b6812 10from bs4.element import Tag
23f31879
SW
11
12from args import spec_from_commandline_args
1fac41bf 13from glowfic import chunkDOMs, flatURL, makeChunk, renderChunk
23f31879 14from spec import Spec
92b11a10
SW
15
16
bf06f467
SW
17def parse(content: bytes) -> BeautifulSoup:
18 return BeautifulSoup(content, 'html.parser')
b25a2f90
SW
19
20
81557fab
SW
21def ilen(it: Iterable[Any]) -> int:
22 return sum(1 for _ in it)
23
24
7b4b6812
SW
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
23f31879 32def 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))
60 spec.log('Counting chunks...\r')
606c97c5 61 num_chunks = ilen(chunkDOMs(dom))
7b4b6812 62 title = get_title(dom) or "chunk"
606c97c5 63 for i, r in enumerate(chunkDOMs(dom)):
81557fab 64 percent = 100.0 * i / num_chunks
7b4b6812 65 spec.log(f'Processing {title} {i} of {num_chunks} ({percent:.1f}%)\r')
8be20b9d 66 spec.domfilter(r)
d2a41ff4 67 chunk = makeChunk(r, spec.images)
1fac41bf 68 spec.texout.write(spec.texfilter(renderChunk(spec.texifier, chunk)))
c594c8bf 69 spec.log('')
23f31879 70 spec.texout.write(b'\\end{document}\n')
47cfa3cd
SW
71
72
92b11a10 73def main() -> None:
23f31879
SW
74 with spec_from_commandline_args() as spec:
75 process(spec)
92b11a10
SW
76
77
78if __name__ == '__main__':
79 main()