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