summaryrefslogtreecommitdiff
path: root/secateurs.py
blob: 249651c3178abd3761549aeb23e82807aa1a3a86 (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
import plantuml
import sys
import time
import os

f = open(sys.argv[1], 'r').readlines()
p = plantuml.PlantUML(url="http://www.plantuml.com/plantuml/svg/")

preamble = []
exported = set()


def add_preamble(offset):
    for line in range(offset, len(f)):
        if "namespace" in f[line]:
            return line - 1
        else:
            preamble.append(f[line])


def generate_svg(text):
    name = text.split(' ')
    name = name[name.index('namespace') + 1]
    if name in exported:
        return
    exported.add(name)
    print(f"generating: {text}")
    try:
        res = p.processes(plantuml_text=('\n'.join(preamble) + text))
    except Exception as e:
        print(e)
        print(p.get_url(plantuml_text=('\n'.join(preamble) + text)))
        return

    name = 'out/' + name
    open(name + '.svg', 'wb').write(res)
    os.system(f"inkscape {name + '.svg'} -o {name + '.pdf'}")
    time.sleep(1)


def parse_namespace(offset):
    print(f"offset: {offset}")
    depth = 1
    contains_subspaces = False
    for i in range(offset + 1, len(f)):
        #print(f[i])
        depth += f[i].count("{") - f[i].count("}")
        if "namespace" in f[i]:
            print("parsing subnamespace")
            contains_subspaces = True
            parse_namespace(i)
        if depth == 0:
            if not contains_subspaces or True:
                lines = f[offset:i + 1]
                #lines = filter(lambda x: "hidden" not in x, lines)
                generate_svg(''.join(lines))
            return i


offset = 0
offset = add_preamble(offset)
while offset < len(f):
    #offset = add_preamble(offset)
    offset = parse_namespace(offset)