Railroad-Diagram Generator, JS Version ======================================= This is a small library for generating railroad diagrams (like what [JSON.org](http://json.org) uses) using SVG or Unicode text, with both JS and Python ports. [Here's an online dingus for you to play with and get SVG code from!](https://tabatkins.github.io/railroad-diagrams/generator.html) (This is the README for the JS port; see the [main README](https://github.com/tabatkins/railroad-diagrams) for other ports, and for more non-JS-specific information.) Diagrams -------- To use the library, include `railroad.css` in your page, and import the `railroad.js` module in your script, then call the Diagram() function. Its arguments are the components of the diagram (Diagram is a special form of Sequence). The constructors for each node are named exports in the module; the default export is an object of same-named functions that just call the constructors, so you can construct diagrams without having to spam `new` all over the place: ```js // Use the constructors import {Diagram, Choice} from "./railroad.js"; const d = new Diagram("foo", new Choice(0, "bar", "baz")); // Or use the functions that call the constructors for you import rr from "./railroad.js"; const d = rr.Diagram("foo", rr.Choice(0, "bar", "baz")); // Then output as inline SVG... container.appendChild(d.toSVG()); // Or output SVG source code... container.textContent = ""+d; // Or write out a standalone SVG as source... console.log(""+d.toStandalone()) // Or instead render it as ASCII art... container.textContent = d.toText(); ``` Alternately, you can construct a ComplexDiagram(); it's identical to Diagram(), but has slightly different start/end shapes, same as what JSON.org does to distinguish between "leaf" types like number (ordinary Diagram()) and "container" types like Array (ComplexDiagram()). The Diagram class also has a few methods: * `.walk(cb)` calls the cb function on the diagram, then on its child nodes, recursing down the tree. This is a "pre-order depth-first" traversal, if you're into that sort of thing - the first child's children are visited before the diagram's second child. (In other words, the same order you encounter their constructors in the code that created the diagram.) Use this if you want to, say, sanitize things in the diagram. * `.format(...paddings)` "formats" the Diagram to make it ready for output. Pass it 0-4 paddings, interpreted just like the CSS `padding` property, to give it some "breathing room" around its box; these default to `20` if not specified. This is automatically called by the output functions if you don't do so yourself, so if the default paddings suffice, there's no need to worry about this. * `.toString()` outputs the SVG of the diagram as a string, ready to be put into your HTML. This is *not* a standalone SVG file; it's intended to be embedded into HTML. * `.toStandalone()` outputs the SVG of the diagram as a string, but this *is* a standalone SVG file. * `.toSVG()` outputs the diagram as an actual `` DOM element, ready for appending into a document. * `.toText()` outputs a text-mode diagram as a string, ready to be put into a file like your source code. This is *not* an SVG diagram; it's intended to be readable in a text environment. * `.addTo(parent?)` directly appends the diagram, as an `` element, to the specified parent element. If you omit the parent element, it instead appends to the script element it's being called from, so you can easily insert a diagram into your document by just dropping a tiny inline `