]>
git.scottworley.com Git - paperdoorknob/blob - args.py
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
12 from typing
import Iterator
14 from xdg_base_dirs
import xdg_cache_home
16 from domfilter
import ApplyDOMFilters
, DOMFilters
17 from fetch
import CachingFetcher
18 from glowfic
import BesideIconLayout
, BelowIconLayout
, Layout
19 from htmlfilter
import ApplyHTMLFilters
, HTMLFilters
20 from texfilter
import ApplyTexFilters
, TexFilters
21 from images
import DiskImageStore
23 from texify
import PandocTexifier
26 def _command_line_parser() -> ArgumentParser
:
27 parser
= ArgumentParser(prog
='paperdoorknob', description
='Print glowfic')
31 help='Where to keep the http cache (instead of %(default)s)',
32 default
=os
.path
.join(xdg_cache_home(), "paperdoorknob"))
35 help='Which DOM filters to use (default: %(default)s)',
36 default
=','.join(f
[0] for f
in DOMFilters
))
39 help='''Page size and margin control
40 See https://faculty.bard.edu/bloch/geometry.pdf for details
41 (default: %(default)s)''',
42 default
='paperwidth=5.5in,paperheight=8.5in,nohead,' +
43 'tmargin=15mm,hmargin=15mm,bmargin=17mm,foot=4mm')
46 help='Which HTML filters to use (default: %(default)s)',
47 default
=','.join(f
[0] for f
in HTMLFilters
))
50 help='How large the icon images are, in mm',
55 help='Whether to put character and author information `beside` or `below` the icon ' +
59 help='The filename stem at which to write output ' +
60 '(eg: "%(default)s" produces %(default)s.tex, %(default)s.pdf, etc.)',
62 parser
.add_argument('--pandoc', help='Location of the pandoc executable')
65 help='Which TeX filters to use (default: %(default)s)',
66 default
=','.join(f
[0] for f
in TexFilters
))
69 help='How long to wait for HTTP requests, in seconds',
73 help='URL to retrieve (example: https://www.projectlawful.com/posts/4582 )')
78 def spec_from_commandline_args() -> Iterator
[Spec
]:
79 args
= _command_line_parser().parse_args()
80 texifier
= PandocTexifier(args
.pandoc
or 'pandoc')
82 if args
.layout
== 'below':
83 layout
= BelowIconLayout(texifier
, args
.image_size
)
84 elif args
.layout
== 'beside':
85 layout
= BesideIconLayout(texifier
, args
.image_size
)
87 raise ValueError(f
'Unknown layout: {args.layout}')
88 with CachingFetcher(args
.cache_path
, args
.timeout
) as fetcher
:
89 with open(args
.out
+ '.tex', 'wb') as texout
:
93 DiskImageStore(args
.out
+ '_images', fetcher
),
94 lambda x
: ApplyHTMLFilters(args
.htmlfilters
, x
),
95 lambda x
: ApplyDOMFilters(args
.domfilters
, x
),
96 lambda x
: ApplyTexFilters(args
.texfilters
, x
),