]> git.scottworley.com Git - paperdoorknob/blame - args.py
Project Lawful start URL in --help
[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
23f31879
SW
19from spec import Spec
20from texify import PandocTexifier
21
22
23def _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"))
8be20b9d
SW
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))
929db576
SW
34 parser.add_argument(
35 '--htmlfilters',
36 help='Which HTML filters to use (default: %(default)s)',
37 default=','.join(f[0] for f in HTMLFilters))
23f31879
SW
38 parser.add_argument(
39 '--out',
40 help='The filename stem at which to write output ' +
41 '(eg: "%(default)s" produces %(default)s.tex, %(default)s.pdf, etc.)',
42 default='book')
43 parser.add_argument('--pandoc', help='Location of the pandoc executable')
44 parser.add_argument(
45 '--timeout',
46 help='How long to wait for HTTP requests, in seconds',
47 default=30)
c04b2e78
SW
48 parser.add_argument(
49 'url',
50 help='URL to retrieve (example: https://www.projectlawful.com/posts/4582 )')
23f31879
SW
51 return parser
52
53
54@contextmanager
55def spec_from_commandline_args() -> Iterator[Spec]:
56 args = _command_line_parser().parse_args()
57 with CachingFetcher(args.cache_path, args.timeout) as fetcher:
58 with open(args.out + '.tex', 'wb') as texout:
929db576
SW
59 yield Spec(
60 args.url,
61 fetcher,
62 lambda x: ApplyHTMLFilters(args.htmlfilters, x),
8be20b9d 63 lambda x: ApplyDOMFilters(args.domfilters, x),
929db576
SW
64 PandocTexifier(args.pandoc or 'pandoc'),
65 texout)