Emmet is a text-editor and IDE plugin for writing HTML code using much fewer keypresses. You might think of it like a code-golfing HTML preprocessor. Its syntax is based on CSS selectors.
Your challenge is to code-golf an implementation of Emmet. Given some Emmet code, output the resulting HTML.
Note that for the purposes of this challenge, you will not have to handle element "shortcuts" like bq
→ blockquote
or a
→ a[href]
. This version of Emmet does not support item numbering. You also do not need to handle the difference between block and inline elements having different spacing.
All elements should have at least one whitespace character (space, tab, newline, whatever) wherever whitespace is shown here, but you don't need to have the indentation shown in the examples.
Syntax
(Paraphrased from Emmet's documentation)
Emmet basically just describes an HTML document using syntax similar to CSS selectors. It uses symbols to describe where each element should be placed in the document.
Structure operators
Child:
>
div>ul>li <div> <ul> <li></li> </ul> </div>
Sibling:
+
div+p+blockquote <div></div> <p></p> <blockquote></blockquote>
Put them together:
div+div>p>span+em <div></div> <div> <p> <span></span> <em></em> </p> </div>
Climb-up:
^
div+div>p>span+em^blockquote <div></div> <div> <p> <span></span> <em></em> </p> <blockquote></blockquote> </div>
div+div>p>span+em^^blockquote <div></div> <div> <p> <span></span> <em></em> </p> </div> <blockquote></blockquote>
Multiplication:
*
ul>li*5 <ul> <li></li> <li></li> <li></li> <li></li> <li></li> </ul>
p>span*2+a <p> <span></span> <span></span> <a></a> </p>`
p>span*2^q <p> <span></span> <span></span> </p> <q></q>
Grouping:
()
div>(header>ul>li*2>a)+footer>p <div> <header> <ul> <li> <a></a> </li> <li> <a></a> </li> </ul> </header> <footer> <p></p> </footer> </div>
Attribute operators
ID
#
and class.
div#header+div.page+div#footer.class1.class2.class3 <div id="header"></div> <div class="page"></div> <div id="footer" class="class1 class2 class3"></div>
Custom attributes
[attr]
td[title="Hello world!" colspan=3] <td title="Hello world!" colspan="3"></td>
td[title=hello colspan=3] <td title="hello" colspan="3"></td>
td[title colspan] <td title="" colspan=""></td>
Text
Put text in curly braces:
p{Hello, World!}
<p>Hello, World!</p>
a.important[href="https://www.youtube.com/watch?v=dQw4w9WgXcQ"]{click}
<a
class="important"
href="https://www.youtube.com/watch?v=dQw4w9WgXcQ"
>
click
</a>
a[href]{click}+b{here}
<a href="">click</a>
<b>here</b>
a[href]>{click}+b{here}
<a href="">
click
<b>
here
</b>
</a>
p>{Click}+button{here}+{to continue}
<p>
Click
<button>
here
</button>
to continue
</p>
p{(a+b)*c>a+b*c}
<p>{(a+b)*c>a+b*c}</p>
Additional test cases
div>p[block]*2>{abc}+span*2{123}+{def}
<div>
<p block="">
abc
<span>123</span>
<span>123</span>
def
</p>
</div>
a[href="https://example.com/index.html?a[]={1}&a[]={2}"]{This is an example usage of array parameters "a[]" in url.}
<a href="https://example.com/index.html?a[]={1}&a[]={2}">
This is an example usage of array parameters "a[]" in url.
</a>
Your program should give proper output for all of the examples outlined above. This is code-golf, so the shortest code per language, measured in bytes, wins!
div+div>p>span+em^^^bq
seems illegal since it has three^
s but only two>
s. \$\endgroup\$"[]{}
used as literal? For example:a[href="https://example.com/index.html?a[]={1}&a[]={2}"]{This is an example usage of array parameters "a[]" in url.}
will not be given as input \$\endgroup\$