forked from tabatkins/railroad-diagrams
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgenerator.html
104 lines (96 loc) · 2.74 KB
/
generator.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
<!doctype html>
<html lang="en">
<meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover">
<meta name="description" content="Draws railroad syntax diagrams from JavaScript input.">
<title>Railroad Diagram Generator from JavaScript</title>
<link href="railroad-diagrams.css" rel=stylesheet>
<style>
html, body { margin: 0; padding: 0; }
h1 { margin: 1rem; padding: 0; font-size: 1.5rem; }
@media all and (min-width: 768px) {
main {
height: calc(100vh - 5rem);
display: grid;
grid-template:
"input code" 1fr
"output info" 1fr
/ 1fr 1fr;
}
}
.input {
width: 100%;
min-height: 10rem;
grid-area: input;
}
.output-text {
width: 100%;
min-height: 10rem;
grid-area: code;
}
.output-image {
grid-area: output;
}
.info {
grid-area: info;
}
</style>
<h1>Railroad Diagram Generator from JavaScript</h1>
<main>
<textarea class='input' aria-label='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' aria-label='Output' readonly></textarea>
<div class=info>
<p><label><input type=checkbox name=standalone id=standalone> Generate code for standalone SVG?</label>
</div>
</main>
<script type=module>
import rr, * as rrClass from "./lib/index.mjs";
Object.assign(window, rr);
window.rrOptions = rrClass.Options;
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);
find('#standalone').addEventListener('input', _=>process(), 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;
const standalone = find('#standalone').checked;
try {
var result = eval(input).format();
location.hash = "#" + encodeURIComponent(input);
} catch (e) {
find('.output-text').textContent = "Invalid input.\n" + e
return;
}
find('.output-image').innerHTML = '';
result.addTo(find('.output-image'));
find('.output-text').textContent = standalone ? result.toStandalone() : result.toString();
}
</script>
</html>