]>
git.scottworley.com Git - paperdoorknob/blob - args.py
de6fc01f58d353c26cb245352640a0ad1970cc0b
1 # paperdoorknob: Print glowfic
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.
8 from argparse
import ArgumentParser
9 from contextlib
import contextmanager
11 from sys
import stderr
13 from typing
import Iterator
15 from xdg_base_dirs
import xdg_cache_home
17 from domfilter
import ApplyDOMFilters
, DOMFilters
18 from fetch
import CachingFetcher
19 from glowfic
import BesideIconLayout
, BelowIconLayout
20 from htmlfilter
import ApplyHTMLFilters
, HTMLFilters
21 from texfilter
import ApplyTexFilters
, TexFilters
22 from images
import DiskImageStore
24 from texify
import PandocTexifier
27 def _print_status(msg
: str) -> None:
28 print(msg
, file=stderr
, end
='' if msg
.endswith('\r') else '\n')
31 def _command_line_parser() -> ArgumentParser
:
32 parser
= ArgumentParser(prog
='paperdoorknob', description
='Print glowfic')
36 help='Where to keep the http cache (instead of %(default)s)',
37 default
=os
.path
.join(xdg_cache_home(), "paperdoorknob"))
40 help='Which DOM filters to use (default: %(default)s)',
41 default
=','.join(f
[0] for f
in DOMFilters
))
44 help='''Page size and margin control
45 See 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')
51 help='Which HTML filters to use (default: %(default)s)',
52 default
=','.join(f
[0] for f
in HTMLFilters
))
55 help='How large the icon images are, in mm',
60 help='Whether to put character and author information `beside` or `below` the icon ' +
64 help='The filename stem at which to write output ' +
65 '(eg: "%(default)s" produces %(default)s.tex, %(default)s.pdf, etc.)',
67 parser
.add_argument('--pandoc', help='Location of the pandoc executable')
71 help='Suppress progress messages')
74 help='Which TeX filters to use (default: %(default)s)',
75 default
=','.join(f
[0] for f
in TexFilters
))
78 help='How long to wait for HTTP requests, in seconds',
82 help='URL to retrieve (example: https://www.projectlawful.com/posts/4582 )')
87 def spec_from_commandline_args() -> Iterator
[Spec
]:
88 args
= _command_line_parser().parse_args()
89 texifier
= PandocTexifier(args
.pandoc
or 'pandoc')
91 if args
.layout
== 'below':
92 layout
= BelowIconLayout
93 elif args
.layout
== 'beside':
94 layout
= BesideIconLayout
96 raise ValueError(f
'Unknown layout: {args.layout}')
97 log
= (lambda _
: None) if args
.quiet
else _print_status
98 with CachingFetcher(args
.cache_path
, args
.timeout
, log
) as fetcher
:
99 with open(args
.out
+ '.tex', 'wb') as texout
:
103 DiskImageStore(args
.out
+ '_images', fetcher
),
104 lambda x
: ApplyHTMLFilters(args
.htmlfilters
, x
),
105 lambda x
: ApplyDOMFilters(args
.domfilters
, x
),
107 lambda x
: ApplyTexFilters(args
.texfilters
, x
),