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