-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraphviz.coffee
234 lines (186 loc) · 4.97 KB
/
graphviz.coffee
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
dotdoc = Object.create null
dotdoc.object = (name, options, style) ->
self = @
_object = Object.create null
_object.name = name
_object.relations_in = []
_object.relations_out = []
_object.options = options
if style
_object.style = style
_object[">"] = (__object, __line) ->
fn = (item) ->
robject = Object.create null
robject["object"] = item
if __line
robject["line"] = __line
_object.relations_in.push robject
return
if Array.isArray __object
__object.forEach (item) ->
fn item
return
else
fn __object
_object
_object["<"] = (__object, __line) ->
fn = (item) ->
robject = Object.create null
robject["object"] = __object
if __line
robject["line"] = __line
_object.relations_out.push robject
return
if Array.isArray __object
__object.forEach (item) ->
fn item
return
else
fn __object
_object
_object.clone = (_name, _label, with_relations) ->
with_relations = with_relations or false
__object = Object.assign {}, _object
__object.name = _name
if _label
__object.options.label = _label
unless with_relations
__object.relations_in = []
__object.relations_out = []
self.dot.items.push __object
__object
@dot.items.push _object
@dot.items_by_name[name] = _object
_object
dotdoc.content = ""
dotdoc.endofline = "\n"
dotdoc.add = (line, number) ->
space = ''
if number and dotdoc.endofline is "\n"
space = [0...number].reduce (a) ->
return a + " "
, ""
@content += "#{space}#{line}#{@endofline}"
return
dotdoc.addcl = (line, number) ->
space = ''
if number and @endofline is "\n"
space = [0...number].reduce (a) ->
return a + " "
, ""
@content += "#{space}#{line}"
return
dotdoc.generator = (options) ->
self = @
options = options or Object.create(null)
@add "digraph G {"
["node", "edge", "graph"].forEach (field) ->
self.add "#{field} [", 2
for key, value of self.dot[field]
if typeof(value) is 'boolean'
if value
self.add "#{key} = \"true\";", 4
else
self.add "#{key} = \"false\";", 4
else
self.add "#{key} = \"#{value}\";", 4
self.add "];", 2
return
@dot.items.forEach (item) ->
self.add "\"#{item.name}\" [", 2
if item.options
if item.options.label
if item.options.table
self.addcl "label = \"#{item.options.label} | {", 4
item.options.table.forEach (row) ->
self.addcl ":#{row}\\l\n"
return
self.addcl "}\";"
else
self.add "label = \"#{item.options.label}\";", 4
if item.style
for key, value of item.style
self.add "#{key} = \"#{value}\";", 4
else
for key, value of self.dot.defaultStyleItem
self.add "#{key} = \"#{value}\";", 4
self.add "];", 2
item.relations_in.forEach (ritem) ->
_item = ritem.object
self.addcl "\"#{item.name}\" -> \"#{_item.name}\"", 2
if ritem.line
self.addcl "[", 1
for key, value of ritem.line
self.addcl "#{key} = #{value};", 1
self.add "];"
else
if self.dot.defaultStyleLine
self.addcl "[", 1
for key, value of self.dot.defaultStyleLine
self.addcl "#{key} = #{value};", 1
self.add "];"
return
item.relations_out.forEach (ritem) ->
_item = ritem.object
self.addcl "\"#{_item.name}\" -> \"#{item.name}\"", 2
if ritem.line
self.addcl "[", 1
for key, value of ritem.line
self.addcl "#{key} = #{value};", 1
self.add "];"
else
if self.dot.defaultStyleLine
self.addcl "[", 1
for key, value of self.dot.defaultStyleLine
self.addcl "#{key} = #{value};", 1
self.add "];"
return
return
@add "}"
return
dotdoc.print = ->
console.log @content
return
dotdoc.generator_and_print = ->
@generator()
@print()
return
dotdoc.dot =
items: []
items_by_name: {}
node:
resolution: 200
fontsize: 10
fontname: "Free Mono Bold"
fontcolor: "gray"
color: "gray"
shape: "record"
edge:
color: "gray"
arrowtail: "odot"
arrowhead: "vee"
dir: "both"
arrowsize: 0.3
fontsize: 10
fontname: "Free Mono Bold"
graph:
overlap: false
splines: true
bgcolor: "gray15"
dpi: 120
rankdir: "LR"
pad: 1
defaultStyleLine:
color: "deepskyblue4"
fontcolor: "white"
defaultStyleItem:
color: "deepskyblue4"
fontcolor: "white"
style: "filled"
dotdoc.object = dotdoc.object.bind dotdoc
dotdoc.add = dotdoc.add.bind dotdoc
dotdoc.addcl = dotdoc.addcl.bind dotdoc
dotdoc.generator = dotdoc.generator.bind dotdoc
dotdoc.print = dotdoc.print.bind dotdoc
dotdoc.generator_and_print = dotdoc.generator_and_print.bind dotdoc
module.exports = dotdoc