]> git.scottworley.com Git - paperdoorknob/blame - paperdoorknob.py
Move get_title() to Thread
[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
23f31879
SW
10
11from args import spec_from_commandline_args
94027099 12from glowfic import flatURL, makeChunk, renderChunk, Thread
23f31879 13from spec import Spec
92b11a10
SW
14
15
bf06f467
SW
16def parse(content: bytes) -> BeautifulSoup:
17 return BeautifulSoup(content, 'html.parser')
b25a2f90
SW
18
19
81557fab
SW
20def ilen(it: Iterable[Any]) -> int:
21 return sum(1 for _ in it)
22
23
23f31879 24def 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 39\def\glowiconsize{%fmm}
5f230208
SW
40\newcommand{\glowicon}[1]{\includegraphics[
41 width=\glowiconsize,height=\glowiconsize,keepaspectratio
42]{#1}}
1fac41bf
SW
43\newcommand{\ifnotempty}[2]{\expandafter\ifx\expandafter\relax
44 \detokenize{#1}\relax\else #2\fi}
45%s
46''' % (spec.icon_size, spec.layout))
1452f8d3 47 url = flatURL(spec.url)
d70acaef
SW
48 spec.log('Fetching HTML...\r')
49 html = spec.fetcher.fetch(url)
50 spec.log('Parsing HTML...\r')
51 dom = parse(spec.htmlfilter(html))
94027099 52 thread = Thread(dom)
d70acaef 53 spec.log('Counting chunks...\r')
94027099 54 num_chunks = ilen(thread.chunkDOMs())
21e82200 55 title = thread.title() or "chunk"
94027099 56 for i, r in enumerate(thread.chunkDOMs()):
81557fab 57 percent = 100.0 * i / num_chunks
7b4b6812 58 spec.log(f'Processing {title} {i} of {num_chunks} ({percent:.1f}%)\r')
8be20b9d 59 spec.domfilter(r)
d2a41ff4 60 chunk = makeChunk(r, spec.images)
1fac41bf 61 spec.texout.write(spec.texfilter(renderChunk(spec.texifier, chunk)))
c594c8bf 62 spec.log('')
23f31879 63 spec.texout.write(b'\\end{document}\n')
47cfa3cd
SW
64
65
92b11a10 66def main() -> None:
23f31879
SW
67 with spec_from_commandline_args() as spec:
68 process(spec)
92b11a10
SW
69
70
71if __name__ == '__main__':
72 main()