LaTeX-Stundenplan/generate.py

113 lines
2.9 KiB
Python
Raw Normal View History

2024-06-18 12:22:32 +02:00
import json
import math
import os
import sys
from jinja2 import Template
# Konstanten zum Einsetzen
heading = {
'de': 'Ophasenstundenplan',
'en': 'Ophase Timetable'
}
dayNames = [
{
'de': 'Montag',
'en': 'Monday'
},
{
'de': 'Dienstag',
'en': 'Tuesday'
},
{
'de': 'Mittwoch',
'en': 'Wednesday'
},
{
'de': 'Donnerstag',
'en': 'Thursday'
},
{
'de': 'Freitag',
'en': 'Friday'
}
]
def main(argv):
if len(sys.argv) < 5:
raise Exception('Zu wenige Argumente gegeben ({} <language> <input-json> <output-pdf> <template>)'.format(sys.argv[0]))
language = sys.argv[1]
input_path = sys.argv[2]
output_path = sys.argv[3]
template_path = sys.argv[4]
if output_path.endswith('.pdf'):
output_path = output_path[0:output_path.rfind('.pdf')]
with open(input_path, 'r') as f:
data = json.load(f)
# Kategorien der Veranstaltungen, z.B. "Zentrale Veranstaltung"
categories = ''
for category in data['categories']:
name = category["name"][language]
name = name.replace('&', '\\&').replace('_', '\\_')
categories += '\\farbe{{{}}}{{{}}}{{{}}}\n'.format(category["id"], name, category["colour"][1:])
# Randanmerkungen
notes = ''
for note in data['notes']:
notes += '\\notiz{{{}}}'.format(note[language])
# Einzelne Zeitfenster der Veranstaltungen
time_min = 24
time_max = 0
events = ''
for day in data['days']:
events += '\\begin{{tag}}{{{}}}\n'.format(dayNames[day['dayOfWeek']-1][language])
for slot in day['slots']:
start = slot['from']
end = slot['to']
time_min = min(time_min, math.floor(float(start.replace(':', '.'))))
time_max = max(time_max, math.ceil (float( end.replace(':', '.'))))
name = slot['name'][language]
name = name.replace('&', '\\&').replace('_', '\\_').replace('\n', '\\\\')
room = slot['room']
room = room.replace('&', '\\&').replace('_', '\\_')
# Referenz auf Randanmerkungen
if 'notes' in slot:
for i in slot['notes']:
name += '\\textsuperscript{{{})}}'.format(i)
events += '\t\\slot{{{}}}{{{}}}{{{}}}{{{}}}{{{}}}\n'.format(start, end, name, room, slot['category'])
events += '\\end{tag}\n'
# Spalte für Stunden
time = ''
time += '\\begin{tag}{Zeit}\n'
for h in range(time_min, time_max, 1):
time += '\t\zeit{{{:02d}:00}}{{{:02d}:00}}'.format(h, h+1)
time += '\\end{tag}\n'
with open(template_path, 'r') as f:
template = f.read()
template = template.replace('{{SCALING}}', str(data['scaling']))
template = template.replace('{{HEADING}}', heading[language])
template = template.replace('{{STUDIENGANG}}', data['study'])
template = template.replace('{{KATEGORIEN}}', categories)
template = template.replace('{{ZEITEN}}', time)
template = template.replace('{{VERANSTALTUNGEN}}', events)
template = template.replace('{{NOTIZEN}}', notes)
with open('stundenplan-working-copy.tex', 'w') as f:
f.write(template)
os.system('xelatex --jobname={} stundenplan-working-copy.tex'.format(output_path))
2024-06-18 12:22:32 +02:00
main(sys.argv)