Commit e3d5082e authored by PizZaKatZe's avatar PizZaKatZe

Implement PDF generator proof-of-concept

parent b7c4a1ed
include jht_flyergen/template.tex
......@@ -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)
pretty_print(sessions[0])
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:
output.write(generate_flyer(sessions))
else:
stdout.buffer.write(generate_flyer(sessions))
......@@ -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__()
\documentclass[10pt,a4paper,DIV=16]{scrartcl}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[ngerman]{babel}
\usepackage{hyperref}
\usepackage{qrcode}
\setlength{\parindent}{0pt}
\setcounter{secnumdepth}{0}
\newcommand{\thetitle}{Junghackertag: Angebote}
\newcommand{\theauthor}{Chaos macht Schule}
\title{\thetitle}
\author{\theauthor}
\hypersetup{
pdftitle = {\thetitle},
pdfauthor = {\theauthor},
pdfsubject = {Junghackertag},
pdfborder = {0 0 0},
}
\begin{document}
\maketitle
%- 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
Veranstalter:
\begin{itemize}
%- for provider in session.providers
\item \VAR{provider.name | e} %\qrcode{\VAR{provider.url | e}}
%- endfor
\end{itemize}
%- endif
%- if session.starttimes
Startzeit(en):
\begin{itemize}
%- for time in session.starttimes
\item \VAR{time | e}
%- endfor
\end{itemize}
%- endif
%- endfor
\end{document}
......@@ -10,10 +10,12 @@ setup(
url='https://gitlab.hasi.it/herzi/project/jht_flyergen',
license='GPLv3+',
version='0.1.0',
install_requires=['latex'],
packages=find_packages(exclude=['tests']),
entry_points={
'console_scripts':[
'jht_flyergen = jht_flyergen.cli:main',
]
},
include_package_data=True,
)
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment