]>
Commit | Line | Data |
---|---|---|
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 argparse import ArgumentParser | |
9 | from contextlib import contextmanager | |
10 | import os.path | |
11 | ||
12 | from typing import Iterator | |
13 | ||
14 | from xdg_base_dirs import xdg_cache_home | |
15 | ||
16 | from domfilter import ApplyDOMFilters, DOMFilters | |
17 | from fetch import CachingFetcher | |
18 | from glowfic import BelowIconLayout | |
19 | from htmlfilter import ApplyHTMLFilters, HTMLFilters | |
20 | from images import DiskImageStore | |
21 | from spec import Spec | |
22 | from texify import PandocTexifier | |
23 | ||
24 | ||
25 | def _command_line_parser() -> ArgumentParser: | |
26 | parser = ArgumentParser(prog='paperdoorknob', description='Print glowfic') | |
27 | parser.add_argument( | |
28 | '--cache_path', | |
29 | metavar='PATH', | |
30 | help='Where to keep the http cache (instead of %(default)s)', | |
31 | default=os.path.join(xdg_cache_home(), "paperdoorknob")) | |
32 | parser.add_argument( | |
33 | '--domfilters', | |
34 | help='Which DOM filters to use (default: %(default)s)', | |
35 | default=','.join(f[0] for f in DOMFilters)) | |
36 | parser.add_argument( | |
37 | '--geometry', | |
38 | help='''Page size and margin control | |
39 | See https://faculty.bard.edu/bloch/geometry.pdf for details | |
40 | (default: %(default)s)''', | |
41 | default='paperwidth=5.5in,paperheight=8.5in,nohead,' + | |
42 | 'tmargin=15mm,hmargin=15mm,bmargin=17mm,foot=4mm') | |
43 | parser.add_argument( | |
44 | '--htmlfilters', | |
45 | help='Which HTML filters to use (default: %(default)s)', | |
46 | default=','.join(f[0] for f in HTMLFilters)) | |
47 | parser.add_argument( | |
48 | '--image_size', | |
49 | help='How large the icon images are, in mm', | |
50 | default=20) | |
51 | parser.add_argument( | |
52 | '--out', | |
53 | help='The filename stem at which to write output ' + | |
54 | '(eg: "%(default)s" produces %(default)s.tex, %(default)s.pdf, etc.)', | |
55 | default='book') | |
56 | parser.add_argument('--pandoc', help='Location of the pandoc executable') | |
57 | parser.add_argument( | |
58 | '--timeout', | |
59 | help='How long to wait for HTTP requests, in seconds', | |
60 | default=30) | |
61 | parser.add_argument( | |
62 | 'url', | |
63 | help='URL to retrieve (example: https://www.projectlawful.com/posts/4582 )') | |
64 | return parser | |
65 | ||
66 | ||
67 | @contextmanager | |
68 | def spec_from_commandline_args() -> Iterator[Spec]: | |
69 | args = _command_line_parser().parse_args() | |
70 | texifier = PandocTexifier(args.pandoc or 'pandoc') | |
71 | with CachingFetcher(args.cache_path, args.timeout) as fetcher: | |
72 | with open(args.out + '.tex', 'wb') as texout: | |
73 | yield Spec( | |
74 | args.url, | |
75 | fetcher, | |
76 | DiskImageStore(args.out + '_images', fetcher), | |
77 | lambda x: ApplyHTMLFilters(args.htmlfilters, x), | |
78 | lambda x: ApplyDOMFilters(args.domfilters, x), | |
79 | BelowIconLayout(texifier, args.image_size), | |
80 | args.geometry, | |
81 | texout) |