Commits (2)
include jht_flyergen/template.tex
# Junghackertag flyer generator
A Python module that reads information about self-organized sessions for kids from the congress
wiki and turns it into a PDF using
* Jinja2
* LaTeX
and black magic.
## Requirements
To use this module, you need at least:
* Python 3
* [latex](https://pypi.org/project/latex)
* LaTeX
* [koma-script](https://www.ctan.org/pkg/koma-script)
* [qrcode](https://www.ctan.org/pkg/qrcode)
## CLI
For convenience and testing purposes, a simple CLI is included:
usage: jht_flyergen [-h] [-p PRINT] (-u [URL] | -f PATH) [output]
positional arguments:
output path to output file; if omitted raw PDF data is
written to stdout
optional arguments:
-h, --help show this help message and exit
-p PRINT, --print PRINT
print filtered data of session with index PRINT,
starting at 1; 0 prints all sessions
data sources:
-u [URL], --url [URL]
URL to fetch JSON data from; if URL is omitted, a
hard-coded URL returning test data is used
-f PATH, --file PATH path to file containing JSON data
## Templates
At the moment the module uses a bundled, "hard-coded" [LaTeX template](jht_flyergen/template.tex).
Maybe support for custom templates will be added once the thing works as expected and was tested
successfully at 36C3.
......@@ -20,8 +20,9 @@
from argparse import ArgumentParser
from json import dumps
from sys import argv
from sys import stdout
from jht_flyergen.engine import fetch_data, read_data
from jht_flyergen.engine import fetch_data, filter_session, generate_flyer, read_data
TEST_URL = 'https://events.ccc.de/congress/2018/wiki/index.php/Special:Ask/cl:YzpVT9tKxFAM_KKaouzLQl5ERH1QYb8gp03t2Z5LSVJK_XrTdRcUcplhhjAZqmQyPGstkGKOhofWQZmQUoKRqWdR1LGuoEzSjYkCJ3w7fbxDl0hdq2IUEjf37RqneMOa1wuEXUeo4oeQtIM6DMqGLTSHR69XdfNQxecUez0eTRZuDk9e0Dw87_1Cu6dn7STOFmvBP_hm-pTasSr3bg2bD8_GOaQNq3xRid9X5f9RXUINZ-7s7sqNxHxbzIynnUCmWH6fhlnYbJslFsM9JizFg9DMF_YD' # pylint: disable=line-too-long
......@@ -29,8 +30,11 @@ def parse_arguments(args):
"""Parses command-line arguments.
parser = ArgumentParser()
parser.add_argument('-v', dest='verbose', action='store_true',
help='enable verbose output (useful for debugging)')
parser.add_argument('-p', '--print',
help='print filtered data of session with index PRINT, starting at 1; ' +
'0 prints all sessions')
parser.add_argument('output', nargs='?',
help='path to output file; if omitted raw PDF data is written to stdout')
sources = parser.add_argument_group('data sources').add_mutually_exclusive_group(required=True)
sources.add_argument('-u', '--url', nargs='?', const=TEST_URL,
......@@ -49,9 +53,20 @@ def main():
"""Program entry point.
args = parse_arguments(argv[1:])
sessions = None
if args.url:
sessions = fetch_data(args.url)
elif args.path:
sessions = read_data(args.path)
sessions = list(map(filter_session, sessions))
if args.print:
end = int(args.print)
pretty_print(sessions[0 : end if end else -1])
if args.output:
with open(args.output, 'wb') as output:
......@@ -19,10 +19,15 @@
from datetime import datetime
from json import load
from os.path import dirname
from re import sub
from typing import Dict, List
from urllib.request import urlopen
from jinja2 import FileSystemLoader
from latex import build_pdf # type: ignore; pylint: disable=import-error
from latex.jinja2 import make_env # type: ignore; pylint: disable=import-error
def fetch_data(url: str) -> List[Dict]:
"""Fetches given URL and returns self-organized session data.
......@@ -72,3 +77,11 @@ def read_data(path: str) -> List[Dict]:
with open(path) as data_file:
return load(data_file)
def generate_flyer(sessions: List[Dict]) -> bytes:
"""Generates a PDF from self-organized session data.
env = make_env(loader=FileSystemLoader(dirname(__file__)))
template = env.get_template('template.tex')
pdf = build_pdf(template.render(sessions=sessions))
return pdf.__bytes__()
\newcommand{\thetitle}{Junghackertag: Angebote}
\newcommand{\theauthor}{Chaos macht Schule}
pdftitle = {\thetitle},
pdfauthor = {\theauthor},
pdfsubject = {Junghackertag},
pdfborder = {0 0 0},
%- for session in sessions
\section{\VAR{session.title | e}}
%- if session.description
\VAR{session.description | e}
\ \\[1em]
%- endif
%- if session.url
\qrcode{\VAR{session.url | e}}
\ \\[1em]
%- endif
%- if session.providers
%- for provider in session.providers
\item \VAR{provider.name | e} %\qrcode{\VAR{provider.url | e}}
%- endfor
%- endif
%- if session.starttimes
%- for time in session.starttimes
\item \VAR{time | e}
%- endfor
%- endif
%- endfor
......@@ -10,10 +10,12 @@ setup(
'jht_flyergen = jht_flyergen.cli:main',