diff options
author | Hugo Hörnquist <hugo@lysator.liu.se> | 2020-08-31 19:41:42 +0200 |
---|---|---|
committer | Hugo Hörnquist <hugo@lysator.liu.se> | 2020-08-31 19:41:42 +0200 |
commit | e1d5b85e64786675560b0db3a4dc4d9669b330a0 (patch) | |
tree | 8620d8ea2a9f55028d0449ba014e2509b5030102 | |
download | vimwiki-scripts-e1d5b85e64786675560b0db3a4dc4d9669b330a0.tar.gz vimwiki-scripts-e1d5b85e64786675560b0db3a4dc4d9669b330a0.tar.xz |
initial commit.
-rwxr-xr-x | do.py | 108 |
1 files changed, 108 insertions, 0 deletions
@@ -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}') |