-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
281 lines (259 loc) · 7.14 KB
/
index.html
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
<head>
<link rel='stylesheet' href='https://mostlymaths.net/webfonts/monoid.css'>
<title>CMap simplifier</title>
<style>
body {
font-size: 12pt;
font-family: 'monoidregular';
font-weight:100;
margin: 2%;
}
.left {
float: left;
width: 48%;
min-height: 95%;
border-radius: 3px;
caret-color: brown;
box-shadow: 0.0px 0.0px 2.9px 1px brown;
}
.left:focus {
box-shadow: 0.0px 0.0px 2.9px 3px brown;
outline: none;
}
.right:focus {
box-shadow: 0.0px 0.0px 2.9px 3px darkgreen;
outline: none;
}
.right {
float: right;
width: 48%;
min-height: 95%;
border-radius: 3px;
font-size: 70%;
white-space: pre;
caret-color: darkgreen;
box-shadow: 0.0px 0.0px 2.9px 1px darkgreen;
}
.hid {
display: none;
}
</style>
<script>
const $ = {
cel: (s) => document.createElement(s),
ctn: (s) => document.createTextNode(s),
byId: (s) => document.getElementById(s),
qs: (s) => document.querySelector(s),
}
const hook = () => {
$.qs(".left").addEventListener("keyup", function(event) {
const k = event.key
const text = $.qs(".left").innerText
const normalize = str => str.normalize('NFKD')
const cmapped = convert(normalize(text))
const header = headerT.split("\n").join("<br/>")
$.qs(".right").innerHTML = header + cmapped + "<br/>}"
navigator.clipboard.writeText($.qs(".right").innerText)
})
}
// Spec
// Node definition
// Node Blah blah node; styledefs
// Arrow? Then it's an edge
// Node1 -> Node2 Blah blah edge; styledefs
// To allow positioning commands and subgraph settings
// foo=bar // With no spaces, has no conversion
// { } are ignored if they are the only character aside from spaces
// Handles compound and subgraphs, automatically creates an invisible
// node in the cluster.
// This runs one single pass, so clusters need to be defined before the
// compound nodes are used
const hasArrow = (text) => {
const flag = text.includes('->')
return flag
}
const hasSubgraph = text => text.includes("subgraph cluster_") // Only supporting clusters, no other
const hasCluster = text => text.trim().startsWith("cluster ")
const hasReplacement = text => /^\s*\$\S+\s*=\s*.*$/.test(text)
const getReplacement = text => /^\s*(\$\S+)\s*=\s*(\S+)\s*$/.exec(text)
const hasURL = text => text.includes("URL=")
const isComment = text => /^\s*\/\/.*/.test(text)
const onlyBraces = text => /^\s*{\s*$/.test(text) || /^\s*}\s*$/.test(text)
const onlyAttrs = text => /^\s*\w+=.*\s*$/.test(text)
const getAttrsArrow = text => /^\s*\S+\s*->\s*\S+\s+(.*)$/.exec(text)
const getAttrsNode = text => /^\s*\S+\s+(.*)$/.exec(text)
const getSubgraphCluster = text => /^\s*subgraph cluster_(\w+).*{.*$/.exec(text)
const getLoneCluster = text => /^\s*cluster\s+(\S+).*{.*$/.exec(text)
const isRGBAHex = text => /^#[0-9a-f]{8}$/.test(text.trim())
const labelBreaker = text => {
const leftAlign = "\\l"
if(text.length>30 && !text.includes("\\n")){
const words = text.split(" ")
let lines = []
let line = ""
for(let word of words){
line += `${word} `
if(line.length>30){
lines.push(line)
line = ""
}
}
lines.push(line)
lines.push("")
return lines.join(leftAlign)
}else{
return text
}
}
const convert = (text) => {
result = []
let replacements = []
const lines = text.split('\n')
const tab = " "
const ttab = tab + tab
let clusters = []
result.push(tab + `label="\\n${lines[0].replace('# ', '')}\\n\\n";`)
for(let line of lines.slice(1)){
if(onlyBraces(line) || onlyAttrs(line) || isComment(line)){
result.push(tab + line)
continue
}
if(hasReplacement(line)){
const key = getReplacement(line)[1]
const value = getReplacement(line)[2]
replacements.push([key, value])
continue
}
for(let replacement of replacements){
let [key, value] = replacement
line = line.replace(key, value)
}
if(hasSubgraph(line) || hasCluster(line)){
let cluster
if(hasSubgraph(line)){
cluster = getSubgraphCluster(line)[1]
} else {
cluster = getLoneCluster(line)[1]
}
clusters.push(cluster)
// Add standard formatting
let fill
for(let word of line.split(" ")){
if(isRGBAHex(word)){
fill = ttab + `fillcolor="${word.trim()}"`
line = line.replace(word.trim(), "")
}
}
result.push(tab + `subgraph cluster_${cluster} {`)
result.push(ttab + `style="filled, rounded, dotted"`)
if(fill !== undefined){
result.push(fill)
}
// Add invisible cluster name node and label
result.push(ttab + `label="${cluster}"`)
result.push(ttab + `${cluster} [style=invis,width=0,label="",fixedsize=true]`)
continue
}
let attrs, src, dst
if(hasArrow(line)){
attrs = getAttrsArrow(line);
const match = /^\s*(\w+)\s*->\s*(\w+).*$/.exec(line)
src = match[1]
dst = match[2]
src = src.trim()
dst = dst.trim()
} else {
attrs = getAttrsNode(line)
}
let addendum = ""
if(src && clusters.includes(src)){
addendum = ` ltail="cluster_${src}"`
}
if(dst && clusters.includes(dst)){
addendum = ` lhead="cluster_${dst}"`
}
if(!attrs || attrs.length == 1){
const compoundEdge = addendum != "" ? ` [${addendum}]` : ""
result.push(tab + line + compoundEdge)
continue
}
const linkUTF = hasURL(attrs[1]) ? " 🔗" : ""
let [label, props] = attrs[1].split(";")
if(hasArrow(line) && label.trim() == "!"){
label = ""
props = props ? props : "" + "style=invis"
}
const labelPropper = (label, props) => `[label="${labelBreaker(label)}${linkUTF}"${props ? " " + props : ""}${addendum}]`
const operation = line.replace(" " + attrs[1], " ")
let converted = `${operation} ${labelPropper(label, props)}`
if(!hasArrow(line) && label.trim() == "="){
converted = `${operation} ${labelPropper(operation.trim(), props)}`
}
result.push(tab + converted)
}
let joined = result.join("<br/>")
return joined
}
/*
Sample
# title
$bluefill = fillcolor="#33339933"
$RED=#99333377
zzz The zs; $bluefill
zzz -> arstd label thingy;color=red
longy This is a very long label to see it break up correctly
subgraph cluster_foo {
label="Not foo"
baz baz;URL="foo"
Thingy
bah =;color=red
baz -> Invisible !
}
// test comment -> hah foo;a
bar -> foo
foo -> arstd ;arrowhead=none
cluster colorful { #33333333
a
}
*/
const headerT = `
digraph G {
layout=dot
margin=0.5
bgcolor="#ffffff00"
rankdir=TB
fontname="Roboto"
nodesep=0.5
overlap=scale
compound=true
node [
fontname = "Roboto"
style="rounded,filled"
labelloc=c
margin="0.3,0.15"
splines=true
shape=rect
fontsize=15
];
edge [
minlen=3
penwidth=2
color="#33333388"
fontname="Roboto"
fontsize=14
arrowhead=normal // Latest papers about cmaps have recovered heads
];
graph [
margin=8
style="rounded,dotted"
];
fontsize=24
labelloc="t";
`
</script>
</head>
<body onload="hook()">
<div contenteditable class="left"></div>
<div contenteditable class="right"></div>
</pre>
</body>