]> git.scottworley.com Git - paperdoorknob/blob - paperdoorknob.py
24ccccd19d471482cc37542731c1bc1699bd6b41
[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
8 from bs4 import BeautifulSoup
9
10 from args import spec_from_commandline_args
11 from glowfic import chunkDOMs, makeChunk
12 from spec import Spec
13
14
15 def parse(content: bytes) -> BeautifulSoup:
16 return BeautifulSoup(content, 'html.parser')
17
18
19 def process(spec: Spec) -> None:
20 spec.texout.write(b'''\\documentclass{article}
21 \\usepackage{graphicx}
22 \\usepackage{wrapfig}
23 ''')
24 if spec.geometry is not None:
25 spec.texout.write(b'\\usepackage[' +
26 spec.geometry.encode('UTF-8') +
27 b']{geometry}\n')
28 spec.texout.write(b'\\begin{document}\n')
29 html = parse(spec.htmlfilter(spec.fetcher.fetch(spec.url)))
30 first = True
31 for r in chunkDOMs(html):
32 if first:
33 first = False
34 else:
35 # h/t https://tex.stackexchange.com/questions/309856
36 spec.texout.write(b'''
37 \\vspace{-.5\\ht\\strutbox}\\noindent\\hrulefill
38
39 ''')
40 spec.domfilter(r)
41 chunk = makeChunk(r, spec.images)
42 spec.texout.write(spec.layout.renderChunk(chunk))
43 spec.texout.write(b'\\end{document}\n')
44
45
46 def main() -> None:
47 with spec_from_commandline_args() as spec:
48 process(spec)
49
50
51 if __name__ == '__main__':
52 main()