-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdot2html.py
executable file
·201 lines (169 loc) · 4.42 KB
/
dot2html.py
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#!/usr/bin/env python3
# Ad-hoc dot to svg+compgraph converter.
# (Only meant for quick demo, ideally users export to compgraph directly
# and provide custom CSS, javascript etc.)
import sys
import re
class Token:
def __init__(self, kind, value=None):
self.kind = kind
self.value = value
def __repr__(self):
if self.value is None:
return f"Token({self.kind!r})"
return f"Token({self.kind!r}, {self.value!r})"
def token(scanner, value):
return Token(value)
def skip(scanner, value):
return None
def ident(keywords, scanner, value):
if value in keywords:
return Token(value)
return Token('ident', value)
keywords = set(["digraph", "size", "rankdir", "label"])
rules = [
(r'[a-zA-Z0-9_]+', lambda s, v: ident(keywords, s, v)),
(r'->', token),
(r'/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*+/', skip),
(r'\s+', skip),
(r'"[^"]*"', lambda _, value: Token('string', value)),
('[\[\];{}=,]', token),
]
def scan(rules, string):
scanner = re.Scanner(rules)
result, rest = scanner.scan(string)
if rest != "":
result.append(Token("Error", rest))
return result
class Vertex:
def __init__(self, name):
self.name = name
self.label = name
class Edge:
def __init__(self, src, dst):
self.src = src
self.dst = dst
vertices = dict()
edges = []
t = Token(None)
tokens = []
had_error = False
def parse_error(msg):
global had_error
had_error = True
sys.stderr.write(f"Error: {msg}\n")
def parse_error_expect(*kinds):
parse_error(f"Expected {' or '.join(kinds)}, got {t.kind}")
EOF = Token("EOF")
def next():
global t
if not tokens:
t = EOF
else:
t = tokens.pop()
def eat(kind):
assert t.kind == kind
next()
def expect(kind):
if t.kind != kind:
parse_error_expect(kind)
next()
def match(kind):
if t.kind != kind:
return False
next()
return True
def parse_name():
if t.kind == 'ident':
result = t.value
next()
elif t.kind == 'string':
result = t.value
next()
elif t.kind in keywords:
result = t.kind
next()
else:
parse_error_expect('ident', 'string')
result = "$invalid$"
return result
def skip_attr():
next()
expect('=')
expect('string')
def parse_attributes():
expect('[')
result = {}
first = True
while not match(']') and t.kind not in (EOF, "Error"):
if not first:
expect(",")
first = False
name = parse_name()
expect('=')
value = parse_name()
result[name] = value
return result
def parse_vertex():
name = parse_name()
vertex = vertices.get(name)
if vertex is None:
vertex = Vertex(name)
vertices[name] = vertex
return vertex
def parse_graph_statement():
if t.kind in ('size', 'rankdir', 'label'):
return skip_attr()
v = parse_vertex()
edge = None
if match('->'):
v2 = parse_vertex()
edge = Edge(v, v2)
edges.append(edge)
if t.kind == '[':
attrs = parse_attributes()
label = attrs.get("label")
if edge is None:
v.label = label
def parse_digraph():
expect('digraph')
name = parse_name()
expect('{')
while not match('}'):
parse_graph_statement()
expect(';')
def parse_top():
result = parse_digraph()
expect('EOF')
return result
def parse_file(string):
global tokens
tokens = scan(rules, string)
tokens.reverse()
next()
return parse_top()
def write_svg(out):
out.write(open("dot2html.header.snippet", "r").read())
idx = 0
for v in vertices.values():
v.id = f"v{idx}"
idx += 1
out.write('<svg class="graphdrawing resizeToBBox">\n')
out.write('<g class="layout">\n')
for v in vertices.values():
out.write(f' <text id="{v.id}">{v.label}</text>\n')
out.write('\n')
for e in edges:
out.write(f' <path class="edge" src="{e.src.id}" dst="{e.dst.id}"/>\n')
out.write('</g>\n')
out.write('</svg>\n')
out.write(open("knobs.snippet", "r").read())
out.write(open("dot2html.footer.snippet", "r").read())
if __name__ == "__main__":
if len(sys.argv) > 1:
with open(sys.argv[1]) as fp:
contents = fp.read()
else:
contents = sys.stdin.read()
parse_file(contents)
write_svg(sys.stdout)