From e1d5b85e64786675560b0db3a4dc4d9669b330a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hugo=20H=C3=B6rnquist?= Date: Mon, 31 Aug 2020 19:41:42 +0200 Subject: initial commit. --- do.py | 108 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 108 insertions(+) create mode 100755 do.py diff --git a/do.py b/do.py new file mode 100755 index 0000000..5f5a438 --- /dev/null +++ b/do.py @@ -0,0 +1,108 @@ +#!/usr/bin/env python3 + +import re +import os +import sys +import pwd +import subprocess +from subprocess import PIPE + +def get_heading(name, data): + start_match = re.search(f'(?m)^(=+) {name} =+$', data) + print(start_match) + heading_level = len(start_match.group(1)) + print(start_match.end()) + tail = data[start_match.end():] + pat = f'(?m)^(={{1,{heading_level}}} .*|---*)$' + print(pat) + end_match = re.search(pat, tail) + print(end_match) + if end_match: + return tail[:end_match.start() - 1] + else: + return tail + + +[_, infile, *rest] = sys.argv + +file = open(infile, 'r') +data = file.read() + +output = open('doc.tex', 'w') + +name = pwd.getpwuid(os.getuid()).pw_gecos + +output.write(f''' +\\documentclass[a4paper]{{article}} + +\\usepackage[T1]{{fontenc}} +\\usepackage[utf8]{{inputenc}} +\\usepackage[swedish]{{babel}} +\\usepackage{{verbatim}} +\\usepackage{{fullpage}} + +\\title{{Lysator Möte 1}} +\\date\\today +\\author{{{name}}} +\\begin{{document}} +\\maketitle +''') + +# '\\verbatiminput + +page1 = get_heading('Möte 1', data) +bilagor = get_heading('Bilagor', page1).strip().split('\n') + +output.write('\\begin{verbatim}\n') +output.write(page1 + '\n') +output.write('\\end{verbatim}\n') + +for item in bilagor: + print(item) + m = re.search('^[0-9]+', item) + if m: + print(f'Bilaga {m.group(0)}') + output.write(f'\\section*{{Bilaga {m.group(0)}}}\n') + + else: + # TODO allow other cases + m = re.search('^ *- Message-ID: <(.*)> *(\((.*)\))?$', item) + if m: + msg_id = m.group(1) + print(msg_id) + print(m.group(3)) + mailfile = subprocess.run(f"mu find -u 'i:{msg_id}' --fields 'l'", shell=True, stdout=PIPE).stdout.decode('UTF-8').strip() + print(mailfile) + mail = subprocess.run(f"mu view {mailfile}", shell=True, stdout=PIPE).stdout.decode('UTF-8') + output.write('\\begin{verbatim}\n') + if (m.group(3) == 'short'): + last_arrow = False + out = [] + for line in mail.split('\n'): + if not line: + out += [''] + continue + if line[0] == '>': + arrow = True + if arrow and not last_arrow: + out += ['> [...]'] + last_arrow = True + continue + out += [line] + output.write('\n'.join(out)) + + else: + output.write(mail + '\n') + output.write('\\end{verbatim}\n') + else: + m = re.search('^ *- \\[\\[#(.*)\\]\\]', item) + if m: + page = get_heading(m.group(1), data) + output.write('\\begin{verbatim}\n') + output.write(page + '\n') + output.write('\\end{verbatim}\n') + else: + print("Bilaga saknas") + + +output.write('\\end{document}') -- cgit v1.2.3