| 1 | <!doctype html> |
| 2 | <title>Railroad Diagram Generator</title> |
| 3 | <link href="railroad.css" rel=stylesheet> |
| 4 | <style> |
| 5 | /* latin-ext */ |
| 6 | @font-face { |
| 7 | font-family: mono; |
| 8 | font-style: normal; |
| 9 | font-display: swap; |
| 10 | src: url("https://www.xanthir.com/utils/fonts/droidsansmono.otf"); |
| 11 | } |
| 12 | textarea { |
| 13 | font-family: mono, monospace; |
| 14 | font-optical-sizing: auto; |
| 15 | } |
| 16 | </style> |
| 17 | |
| 18 | <textarea class='input'>Diagram( |
| 19 | Optional('+', 'skip'), |
| 20 | Choice(0, |
| 21 | NonTerminal('name-start char'), |
| 22 | NonTerminal('escape')), |
| 23 | ZeroOrMore( |
| 24 | Choice(0, |
| 25 | NonTerminal('name char'), |
| 26 | NonTerminal('escape'))))</textarea> |
| 27 | <div class='output-image'></div> |
| 28 | <textarea class='output-text' readonly></textarea> |
| 29 | <ul class=info> |
| 30 | <li><label><input type=number min=1 value=8 width=3 id=option-vs> Minimum vertical separation between lines (in px)?</label> |
| 31 | <li><label><input type=number min=2 value=10 id=option-ar> Arc radius for corners (in px)?</label> |
| 32 | <li><label><input type=number min=1 value=8.5 step=.5 id=option-char-width> Average width of characters in "normal" (Terminal/NonTerminal) text (in px)?</label> |
| 33 | <li><label><input type=number min=1 value=7 step=.5 id=option-comment-width> Average width of characters in Comment text (in px)?</label> |
| 34 | <li><label> |
| 35 | <select id=option-alignment> |
| 36 | <option>center</option> |
| 37 | <option>left</option> |
| 38 | <option>right</option> |
| 39 | </select> |
| 40 | How should items be aligned when there's more space than they need?</label> |
| 41 | <li><label><input type=checkbox id=option-odd-stroke checked> Is the stroke width an odd number of px? (Yes, in the default styles.) |
| 42 | <li><label>Output type? |
| 43 | <select id=option-type> |
| 44 | <option value=svg>SVG |
| 45 | <option value=standalone>Standalone SVG |
| 46 | <option value=text>Text |
| 47 | </select></label> |
| 48 | </ul> |
| 49 | |
| 50 | <script type=module> |
| 51 | import rr, * as rrClass from "./railroad.js"; |
| 52 | Object.assign(window, rr); |
| 53 | window.rrOptions = rrClass.Options; |
| 54 | console.log(rr); |
| 55 | |
| 56 | const find = document.querySelector.bind(document); |
| 57 | |
| 58 | find('.input').addEventListener('input', e=>process(e.target.value), false); |
| 59 | document.addEventListener('DOMContentLoaded', initialProcess, false); |
| 60 | window.addEventListener('hashchange', hashProcess, false); |
| 61 | find('.info').addEventListener('input', _=>process(), false); |
| 62 | |
| 63 | function initialProcess() { |
| 64 | if(location.hash && location.hash.length > 1) { |
| 65 | const code = decodeURIComponent(location.hash.substr(1)); |
| 66 | const input = find('.input'); |
| 67 | input.textContent = code; |
| 68 | } |
| 69 | process(); |
| 70 | } |
| 71 | |
| 72 | function hashProcess() { |
| 73 | if(location.hash && location.hash.length > 1) { |
| 74 | const code = decodeURIComponent(location.hash.substr(1)); |
| 75 | const input = find('.input'); |
| 76 | if(input.value != code) { |
| 77 | input.textContent = code; |
| 78 | process(code); |
| 79 | } |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | function process(input) { |
| 84 | if(!input) input = find('.input').value; |
| 85 | const outputType = find('#option-type').value; |
| 86 | rrOptions.VS = parseInt(find("#option-vs").value, 10); |
| 87 | rrOptions.AR = parseInt(find("#option-ar").value, 10); |
| 88 | rrOptions.STROKE_ODD_PIXEL_LENGTH = find("#option-odd-stroke").checked; |
| 89 | rrOptions.CHAR_WIDTH = parseInt(find("#option-char-width").value, 10); |
| 90 | rrOptions.COMMENT_CHAR_WIDTH = parseInt(find("#option-comment-width").value, 10); |
| 91 | rrOptions.INTERNAL_ALIGNMENT = find("#option-alignment").value; |
| 92 | try { |
| 93 | var result = eval(input).format(); |
| 94 | location.hash = "#" + encodeURIComponent(input); |
| 95 | } catch (e) { |
| 96 | find('.output-text').textContent = "Invalid input.\n" + e |
| 97 | throw e; |
| 98 | } |
| 99 | if(outputType == "svg" || outputType == "standalone") { |
| 100 | find('.output-image').innerHTML = ''; |
| 101 | result.addTo(find('.output-image')); |
| 102 | find('.output-text').textContent = outputType == "standalone" ? result.toStandalone() : result.toString(); |
| 103 | } else if(outputType == "text") { |
| 104 | find('.output-text').textContent = result.toText(); |
| 105 | find('.output-image').innerHTML = ''; |
| 106 | } else { |
| 107 | console.error(`Unknown output type '${outputType}'.`); |
| 108 | } |
| 109 | console.log(result); |
| 110 | } |
| 111 | </script> |
| 112 | |
| 113 | <style> |
| 114 | @media all and (min-width: 400px) { |
| 115 | html, body { margin: 0; padding: 0; height: 100%; } |
| 116 | body { |
| 117 | display: grid; |
| 118 | grid-template: |
| 119 | "input code" 1fr |
| 120 | "output info" 1fr |
| 121 | / auto minmax(15em, auto); |
| 122 | max-width: 100%; |
| 123 | } |
| 124 | } |
| 125 | .input { |
| 126 | grid-area: input; |
| 127 | } |
| 128 | .output-text { |
| 129 | grid-area: code; |
| 130 | } |
| 131 | .output-image { |
| 132 | grid-area: output; |
| 133 | overflow: auto; |
| 134 | } |
| 135 | .info { |
| 136 | grid-area: info; |
| 137 | } |
| 138 | .info input[type=number], .info select { |
| 139 | width: 60px; |
| 140 | box-sizing: border-box; |
| 141 | } |
| 142 | </style> |