Template Engine to HTML Converter
Convert Pug (Jade) and HAML templates to clean HTML and back. Real-time parsing with syntax support for tags, classes, IDs, attributes, and nesting.
About
Template engines like Pug and HAML reduce markup verbosity through indentation-based nesting and shorthand syntax for classes (.), IDs (#), and attributes. A misplaced indent or forgotten closing structure can produce malformed HTML that breaks page rendering, accessibility trees, and SEO crawlers. This tool parses the abstract syntax of each engine and emits standards-compliant HTML with proper tag nesting. It also reverses the process: feed it HTML and receive valid Pug or HAML output. The parser handles self-closing void elements (img, br, hr, input), attribute lists, inline text content, pipe-based text blocks, and doctype declarations. Note: advanced features like mixins, includes, extends, filters, and embedded JavaScript evaluation are outside scope. The converter assumes static template content only.
Formulas
Template engine conversion is a lexical and syntactic transformation rather than a mathematical function. The core process follows a deterministic pipeline:
Each source line is decomposed into an AST node with properties:
Indentation depth d determines parent-child relationships. A line at depth d + 1 becomes a child of the nearest preceding line at depth d. The tree is walked depth-first to produce nested HTML with proper opening and closing tags.
For reverse conversion (HTML → Template), the HTML string is parsed via the browser's native DOMParser API into a DOM tree. Each element node is visited recursively, extracting tagName, id, classList, and attributes, then formatting per the target engine's syntax rules at the correct indentation level.
Where tag = element name (e.g., div, p, a), id = element identifier, classes = array of CSS class names, attrs = key-value map of HTML attributes, text = inline text content, depth = nesting level determined by leading whitespace count divided by indent unit size (typically 2).
Reference Data
| Feature | Pug (Jade) Syntax | HAML Syntax | HTML Output |
|---|---|---|---|
| Tag | div | %div | <div></div> |
| Tag with ID | div#main | %div#main | <div id="main"></div> |
| Tag with class | div.container | %div.container | <div class="container"></div> |
| Multiple classes | div.foo.bar | %div.foo.bar | <div class="foo bar"></div> |
| ID + class shorthand | #app.wrapper | #app.wrapper | <div id="app" class="wrapper"></div> |
| Attributes | a(href="/", title="Home") | %a{href: "/", title: "Home"} | <a href="/" title="Home"></a> |
| Inline text | p Hello world | %p Hello world | <p>Hello world</p> |
| Nested children | Indent 2 spaces | Indent 2 spaces | Nested tags |
| Self-closing | img(src="a.png") | %img{src: "a.png"}/ | <img src="a.png" /> |
| Doctype HTML5 | doctype html | !!! 5 | <!DOCTYPE html> |
| Pipe text | | Plain text line | Plain text line | Plain text line |
| HTML comment | // comment | / comment | <!-- comment --> |
| Void elements | br, hr, input | %br, %hr, %input | Self-closing by spec |
| Boolean attributes | input(disabled) | %input{disabled: true} | <input disabled /> |
| Data attributes | div(data-id="5") | %div{"data-id": "5"} | <div data-id="5"></div> |
| Implicit div | .box or #box | .box or #box | <div class="box"></div> |
| Block text (Pug) | p. + indented lines | N/A | <p>text...</p> |
| Unescaped HTML (HAML) | N/A | != <b>bold</b> | <b>bold</b> |