summaryrefslogtreecommitdiff
path: root/do.py
blob: 5f5a438e1e4d2fb32eda38144a7578099cc822cc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
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}')