]> git.scottworley.com Git - paperdoorknob/blame - args.py
Remove no-longer-needed DOMFilters NoEdit and NoFooter
[paperdoorknob] / args.py
CommitLineData
23f31879
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
7
8from argparse import ArgumentParser
9from contextlib import contextmanager
10import os.path
11
12from typing import Iterator
13
14from xdg_base_dirs import xdg_cache_home
15
8be20b9d 16from domfilter import ApplyDOMFilters, DOMFilters
23f31879 17from fetch import CachingFetcher
f75c1629 18from glowfic import BesideIconLayout, BelowIconLayout, Layout
929db576 19from htmlfilter import ApplyHTMLFilters, HTMLFilters
131deef1 20from texfilter import ApplyTexFilters, TexFilters
4b7d905e 21from images import DiskImageStore
23f31879
SW
22from spec import Spec
23from texify import PandocTexifier
24
25
26def _command_line_parser() -> ArgumentParser:
27 parser = ArgumentParser(prog='paperdoorknob', description='Print glowfic')
28 parser.add_argument(
29 '--cache_path',
30 metavar='PATH',
31 help='Where to keep the http cache (instead of %(default)s)',
32 default=os.path.join(xdg_cache_home(), "paperdoorknob"))
8be20b9d
SW
33 parser.add_argument(
34 '--domfilters',
35 help='Which DOM filters to use (default: %(default)s)',
36 default=','.join(f[0] for f in DOMFilters))
e10b5b6f
SW
37 parser.add_argument(
38 '--geometry',
39 help='''Page size and margin control
40See https://faculty.bard.edu/bloch/geometry.pdf for details
41(default: %(default)s)''',
42 default='paperwidth=5.5in,paperheight=8.5in,nohead,' +
43 'tmargin=15mm,hmargin=15mm,bmargin=17mm,foot=4mm')
929db576
SW
44 parser.add_argument(
45 '--htmlfilters',
46 help='Which HTML filters to use (default: %(default)s)',
47 default=','.join(f[0] for f in HTMLFilters))
c62e8d40
SW
48 parser.add_argument(
49 '--image_size',
50 help='How large the icon images are, in mm',
51 default=20)
f75c1629
SW
52 parser.add_argument(
53 '--layout',
54 default='below',
55 help='Whether to put character and author information `beside` or `below` the icon ' +
56 '(default: below)')
23f31879
SW
57 parser.add_argument(
58 '--out',
59 help='The filename stem at which to write output ' +
60 '(eg: "%(default)s" produces %(default)s.tex, %(default)s.pdf, etc.)',
61 default='book')
62 parser.add_argument('--pandoc', help='Location of the pandoc executable')
131deef1
SW
63 parser.add_argument(
64 '--texfilters',
65 help='Which TeX filters to use (default: %(default)s)',
66 default=','.join(f[0] for f in TexFilters))
23f31879
SW
67 parser.add_argument(
68 '--timeout',
69 help='How long to wait for HTTP requests, in seconds',
70 default=30)
c04b2e78
SW
71 parser.add_argument(
72 'url',
73 help='URL to retrieve (example: https://www.projectlawful.com/posts/4582 )')
23f31879
SW
74 return parser
75
76
77@contextmanager
78def spec_from_commandline_args() -> Iterator[Spec]:
79 args = _command_line_parser().parse_args()
d2a41ff4 80 texifier = PandocTexifier(args.pandoc or 'pandoc')
f75c1629
SW
81 layout: Layout
82 if args.layout == 'below':
83 layout = BelowIconLayout(texifier, args.image_size)
84 elif args.layout == 'beside':
85 layout = BesideIconLayout(texifier, args.image_size)
86 else:
87 raise ValueError(f'Unknown layout: {args.layout}')
23f31879
SW
88 with CachingFetcher(args.cache_path, args.timeout) as fetcher:
89 with open(args.out + '.tex', 'wb') as texout:
929db576
SW
90 yield Spec(
91 args.url,
92 fetcher,
4b7d905e 93 DiskImageStore(args.out + '_images', fetcher),
929db576 94 lambda x: ApplyHTMLFilters(args.htmlfilters, x),
8be20b9d 95 lambda x: ApplyDOMFilters(args.domfilters, x),
131deef1 96 lambda x: ApplyTexFilters(args.texfilters, x),
f75c1629 97 layout,
e10b5b6f 98 args.geometry,
929db576 99 texout)