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