forked from tabatkins/railroad-diagrams
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerator-no-module.html
82 lines (76 loc) · 1.9 KB
/
generator-no-module.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
<!doctype html>
<title>Railroad Diagram Generator</title>
<link href="railroad-diagrams.css" rel=stylesheet>
<textarea class='input'>Diagram(
Optional('+', 'skip'),
Choice(0,
NonTerminal('name-start char'),
NonTerminal('escape')),
ZeroOrMore(
Choice(0,
NonTerminal('name char'),
NonTerminal('escape'))))</textarea>
<div class='output-image'></div>
<textarea class='output-text' readonly></textarea>
<script src="railroad-diagrams.js"></script>
<script>
const find = document.querySelector.bind(document);
find('.input').addEventListener('input', e=>process(e.target.value), false);
document.addEventListener('DOMContentLoaded', initialProcess, false);
window.addEventListener('hashchange', hashProcess, false);
function initialProcess() {
if(location.hash && location.hash.length > 1) {
const code = decodeURIComponent(location.hash.substr(1));
const input = find('.input');
input.textContent = code;
}
process();
}
function hashProcess() {
if(location.hash && location.hash.length > 1) {
const code = decodeURIComponent(location.hash.substr(1));
const input = find('.input');
if(input.value != code) {
input.textContent = code;
process(code);
}
}
}
function process(input) {
if(!input) input = find('.input').value;
try {
var result = eval(input).format();
location.hash = "#" + encodeURIComponent(input);
} catch (e) {
find('.output-text').textContent = "Invalid input.\n" + e
throw e;
}
find('.output-image').innerHTML = '';
result.addTo(find('.output-image'));
find('.output-text').textContent = result;
}
</script>
<style>
@media all and (min-width: 400px) {
html, body { margin: 0; padding: 0; height: 100%; }
body {
display: grid;
grid-template:
"input code" 1fr
"output expl" 1fr
/ 1fr 1fr;
}
}
.input {
grid-area: input;
}
.output-text {
grid-area: code;
}
.output-image {
grid-area: output;
}
.explanation {
grid-area: expl;
}
</style>