]> git.scottworley.com Git - paperdoorknob/blame - args.py
`beside` layout
[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
4b7d905e 20from images import DiskImageStore
23f31879
SW
21from spec import Spec
22from texify import PandocTexifier
23
24
25def _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"))
8be20b9d
SW
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))
e10b5b6f
SW
36 parser.add_argument(
37 '--geometry',
38 help='''Page size and margin control
39See 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')
929db576
SW
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))
c62e8d40
SW
47 parser.add_argument(
48 '--image_size',
49 help='How large the icon images are, in mm',
50 default=20)
f75c1629
SW
51 parser.add_argument(
52 '--layout',
53 default='below',
54 help='Whether to put character and author information `beside` or `below` the icon ' +
55 '(default: below)')
23f31879
SW
56 parser.add_argument(
57 '--out',
58 help='The filename stem at which to write output ' +
59 '(eg: "%(default)s" produces %(default)s.tex, %(default)s.pdf, etc.)',
60 default='book')
61 parser.add_argument('--pandoc', help='Location of the pandoc executable')
62 parser.add_argument(
63 '--timeout',
64 help='How long to wait for HTTP requests, in seconds',
65 default=30)
c04b2e78
SW
66 parser.add_argument(
67 'url',
68 help='URL to retrieve (example: https://www.projectlawful.com/posts/4582 )')
23f31879
SW
69 return parser
70
71
72@contextmanager
73def spec_from_commandline_args() -> Iterator[Spec]:
74 args = _command_line_parser().parse_args()
d2a41ff4 75 texifier = PandocTexifier(args.pandoc or 'pandoc')
f75c1629
SW
76 layout: Layout
77 if args.layout == 'below':
78 layout = BelowIconLayout(texifier, args.image_size)
79 elif args.layout == 'beside':
80 layout = BesideIconLayout(texifier, args.image_size)
81 else:
82 raise ValueError(f'Unknown layout: {args.layout}')
23f31879
SW
83 with CachingFetcher(args.cache_path, args.timeout) as fetcher:
84 with open(args.out + '.tex', 'wb') as texout:
929db576
SW
85 yield Spec(
86 args.url,
87 fetcher,
4b7d905e 88 DiskImageStore(args.out + '_images', fetcher),
929db576 89 lambda x: ApplyHTMLFilters(args.htmlfilters, x),
8be20b9d 90 lambda x: ApplyDOMFilters(args.domfilters, x),
f75c1629 91 layout,
e10b5b6f 92 args.geometry,
929db576 93 texout)