]> git.scottworley.com Git - paperdoorknob/blob - paperdoorknob.py
d545f6184514f0386068871a247b6a30fe1aad18
[paperdoorknob] / paperdoorknob.py
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
7 from typing import Any, Iterable
8
9 from args import spec_from_commandline_args
10 from glowfic import makeChunk, renderChunk, Thread
11 from spec import Spec
12
13
14 def ilen(it: Iterable[Any]) -> int:
15 return sum(1 for _ in it)
16
17
18 def process(spec: Spec) -> None:
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}
26 ''')
27 if spec.geometry is not None:
28 spec.texout.write(br'\usepackage[' +
29 spec.geometry.encode('UTF-8') +
30 b']{geometry}\n')
31 spec.texout.write(br'''\begin{document}
32 \newcommand{\href}[2]{#2\footnote{\detokenize{#1}}}
33 \def\glowiconsize{%fmm}
34 \newcommand{\glowicon}[1]{\includegraphics[
35 width=\glowiconsize,height=\glowiconsize,keepaspectratio
36 ]{#1}}
37 \newcommand{\ifnotempty}[2]{\expandafter\ifx\expandafter\relax
38 \detokenize{#1}\relax\else #2\fi}
39 %s
40 ''' % (spec.icon_size, spec.layout))
41 thread = Thread(spec)
42 spec.log('Counting chunks...\r')
43 num_chunks = ilen(thread.chunkDOMs())
44 title = thread.title() or "chunk"
45 for i, r in enumerate(thread.chunkDOMs()):
46 percent = 100.0 * i / num_chunks
47 spec.log(f'Processing {title} {i} of {num_chunks} ({percent:.1f}%)\r')
48 spec.domfilter(r)
49 chunk = makeChunk(r, spec.images)
50 spec.texout.write(spec.texfilter(renderChunk(spec.texifier, chunk)))
51 spec.log('')
52 spec.texout.write(b'\\end{document}\n')
53
54
55 def main() -> None:
56 with spec_from_commandline_args() as spec:
57 process(spec)
58
59
60 if __name__ == '__main__':
61 main()