summaryrefslogtreecommitdiff
path: root/do.py
blob: b1e57221676e9de5d5c5759d7a810021618c3419 (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
109
110
111
112
113
114
115
116
117
118
119
120
121
#!/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, heading, *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{{{heading}}}
\\date\\today
\\author{{{name}}}
\\begin{{document}}
\\maketitle
''')

# '\\verbatiminput

page1 = get_heading(heading, data)
# bilagor = get_heading('Bilagor', page1).strip().split('\n')
r = "\\[\\[([^|#\\]]*)(?:#([^|\\]]*))?(?:[|]([^\\]]*))?\\]\\]"
bilagor = re.findall(r, page1)

# bilagor = [m[0] for m in re.findall(r, data)]

output.write('\\begin{verbatim}\n')
output.write(page1 + '\n')
output.write('\\end{verbatim}\n\\appendix\n')

print (bilagor)

for (address, frag, comment) in bilagor:
    print(address,frag,comment)
    # m = re.search('^[0-9]+', item)
    # if m:
    #     print(f'Bilaga {m.group(0)}')
    #     output.write(f'\\section*{{Bilaga {m.group(0)}}}\n')

    if comment:
        output.write(f"\\section{{{comment}}}\n")
    else:
        output.write(f"\\section{{{address}}}\n")

    try:
        proto, addr = address.split(":", 1)
    except ValueError:
        proto = ''
        addr = address
    print(proto, addr)
    if proto == 'mail':
        msg_id = addr
        mailfile = subprocess.run(f"mu find -u 'i:{msg_id}' --fields 'l'", shell=True, stdout=PIPE).stdout.decode('UTF-8').strip()
        mail = subprocess.run(f"mu view {mailfile}", shell=True, stdout=PIPE).stdout.decode('UTF-8')
        output.write('\\begin{verbatim}\n')
        if frag == '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')

    elif proto == 'help':
        pass
    else:
        if addr == '':
            page = get_heading(frag, data)
            output.write('\\begin{verbatim}\n')
            output.write(page + '\n')
            output.write('\\end{verbatim}\n')
        else:
            print(addr)
            print("Vimwiki intra-page links not yet supported")


output.write('\\end{document}')