]> git.scottworley.com Git - paperdoorknob/blame - paperdoorknob.py
next_thread should be an absolute URL
[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
23f31879 9from args import spec_from_commandline_args
70adfbff 10from glowfic import makeChunk, renderChunk, Thread
23f31879 11from spec import Spec
92b11a10
SW
12
13
81557fab
SW
14def ilen(it: Iterable[Any]) -> int:
15 return sum(1 for _ in it)
16
17
23f31879 18def process(spec: Spec) -> None:
67612898
SW
19 spec.texout.write(br'''\documentclass{article}
20\usepackage{booktabs}
21\usepackage{graphicx}
22\usepackage{longtable}
23\usepackage{soul}
24\usepackage{varwidth}
25\usepackage{wrapstuff}
d2a41ff4 26''')
e10b5b6f 27 if spec.geometry is not None:
67612898 28 spec.texout.write(br'\usepackage[' +
e10b5b6f
SW
29 spec.geometry.encode('UTF-8') +
30 b']{geometry}\n')
67612898
SW
31 spec.texout.write(br'''\begin{document}
32\newcommand{\href}[2]{#2\footnote{\detokenize{#1}}}
9afdb32a 33\def\glowiconsize{%fmm}
5f230208
SW
34\newcommand{\glowicon}[1]{\includegraphics[
35 width=\glowiconsize,height=\glowiconsize,keepaspectratio
36]{#1}}
1fac41bf
SW
37\newcommand{\ifnotempty}[2]{\expandafter\ifx\expandafter\relax
38 \detokenize{#1}\relax\else #2\fi}
39%s
40''' % (spec.icon_size, spec.layout))
70adfbff 41 thread = Thread(spec)
d70acaef 42 spec.log('Counting chunks...\r')
94027099 43 num_chunks = ilen(thread.chunkDOMs())
21e82200 44 title = thread.title() or "chunk"
94027099 45 for i, r in enumerate(thread.chunkDOMs()):
81557fab 46 percent = 100.0 * i / num_chunks
7b4b6812 47 spec.log(f'Processing {title} {i} of {num_chunks} ({percent:.1f}%)\r')
8be20b9d 48 spec.domfilter(r)
d2a41ff4 49 chunk = makeChunk(r, spec.images)
1fac41bf 50 spec.texout.write(spec.texfilter(renderChunk(spec.texifier, chunk)))
c594c8bf 51 spec.log('')
23f31879 52 spec.texout.write(b'\\end{document}\n')
47cfa3cd
SW
53
54
92b11a10 55def main() -> None:
23f31879
SW
56 with spec_from_commandline_args() as spec:
57 process(spec)
92b11a10
SW
58
59
60if __name__ == '__main__':
61 main()