# Creating a language -- HTML + a few special tags

**URL:** <https://www.ruby-forum.com/t/creating-a-language-html-a-few-special-tags/109063>\
**Category:** Ruby\
**Created:** [July 11, 2007, 5:59am UTC](https://www.ruby-forum.com/t/creating-a-language-html-a-few-special-tags/109063 "2007-07-11T05:59:35Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![rhalff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/8e8cbc/32.png) [@rhalff](https://www.ruby-forum.com/u/rhalff)\
**Post date:** [July 11, 2007, 5:59am UTC](https://www.ruby-forum.com/t/creating-a-language-html-a-few-special-tags/109063/1 "2007-07-11T05:59:35Z")

</div>

Hi all,

I’m looking to implement a “plug-in” language which is essentially just  
HTML plus a few custom tags that I want to support. For example, some  
source code in this language might look like

- something
- something else

where FOO and BAR are special tags that have some meaning that I define.  
Say, for this example, that FOO strongs text inside a

tag, and BAR  
emphasizes text inside a blink tag. So the “compiled down” source would  
look like

- **something**

- _something else_

Is there some open-source software that already does something like  
this? Absent that, it seems like the best way is to just read in the  
HTML with something like XmlSimple, write a simple interpreter to grovel  
over the expression, evaluating the custom tags as they come up, and the  
spitting out the resulting HTML.

Thanks in advance for any advice!

–rob

---

<div class="post-metadata">

**Author:** ![rhalff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/8e8cbc/32.png) [@rhalff](https://www.ruby-forum.com/u/rhalff)\
**Post date:** [July 11, 2007, 6:23am UTC](https://www.ruby-forum.com/t/creating-a-language-html-a-few-special-tags/109063/2 "2007-07-11T06:23:48Z")

</div>

Rob H. wrote:

> Is there some open-source software that already does something like  
> this? Absent that, it seems like the best way is to just read in the  
> HTML with something like XmlSimple, write a simple interpreter to grovel  
> over the expression, evaluating the custom tags as they come up, and the  
> spitting out the resulting HTML.
> 
> Thanks in advance for any advice!
> 
> –rob

That sounds an awful lot like XML with XSL stylesheets… I don’t know  
much about these, but I would start here  
[http://www.w3schools.com/xsl/xsl\_languages.asp](http://www.w3schools.com/xsl/xsl_languages.asp)  
if I wanted to learn.

-Dan

---

<div class="post-metadata">

**Author:** ![rhalff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/8e8cbc/32.png) [@rhalff](https://www.ruby-forum.com/u/rhalff)\
**Post date:** [July 11, 2007, 10:59am UTC](https://www.ruby-forum.com/t/creating-a-language-html-a-few-special-tags/109063/3 "2007-07-11T10:59:13Z")

</div>

Dan Wrote:

> Rob H. wrote:
> 
> > Hi all,
> > 
> > I’m looking to implement a “plug-in” language which is essentially  
> > just  
> > HTML plus a few custom tags that I want to support. For example, some  
> > source code in this language might look like  
> > _snip_  
> > That sounds an awful lot like XML with XSL stylesheets… I don’t know  
> > much about these, but I would start here  
> > [http://www.w3schools.com/xsl/xsl\_languages.asp](http://www.w3schools.com/xsl/xsl_languages.asp)  
> > if I wanted to learn.

Template engines also sound like a good fit. There are many for Ruby  
(and I know I’ve seen pages that describe their various benefits). You  
could start with looking at erb though, which is in the Ruby standard  
library:

[http://www.ruby-doc.org/stdlib/libdoc/erb/rdoc/](http://www.ruby-doc.org/stdlib/libdoc/erb/rdoc/)

Also, see this email:

[http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/238970](http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/238970)

Cheers,  
B

---

<div class="post-metadata">

**Author:** ![rhalff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/8e8cbc/32.png) [@rhalff](https://www.ruby-forum.com/u/rhalff)\
**Post date:** [July 11, 2007, 5:08pm UTC](https://www.ruby-forum.com/t/creating-a-language-html-a-few-special-tags/109063/4 "2007-07-11T17:08:01Z")

</div>

This is great! Thank you both so much for your replies.

–rob

---

<div class="post-metadata">

**Author:** ![rhalff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/8e8cbc/32.png) [@rhalff](https://www.ruby-forum.com/u/rhalff)\
**Post date:** [July 11, 2007, 11:08am UTC](https://www.ruby-forum.com/t/creating-a-language-html-a-few-special-tags/109063/5 "2007-07-11T11:08:17Z")

</div>

This is classic XSLT. For example given this data file:

# foobar.xml

- something
- something else

And this xslt…

# foobar.xsl
\<?xml version="1.0"?\>

\<xsl:stylesheet xmlns:xsl=“[XSLT Namespace](http://www.w3.org/1999/XSL/Transform)”  
version=“1.0”\>  
\<xsl:output  
method=“xml”  
version=“1.0”  
encoding=“utf8”  
indent=“yes”/\>

\<xsl:template match=“/”\>  
xsl:apply-templates/  
\</xsl:template\>

\<xsl:template match=“FOO”\>

**\<xsl:apply-templates /\>**

  
\</xsl:template\>

\<xsl:template match=“BAR”\>  
_\<xsl:apply-templates /\>_  
\</xsl:template\>

\<xsl:template match=“_“\>  
\<xsl:element name=”{name()}“\>  
\<xsl:for-each select=”@_”\>  
\<xsl:attribute name=“{name()}”\>  
\<xsl:value-of select=“.”/\>  
\</xsl:attribute\>  
\</xsl:for-each\>  
xsl:apply-templates/  
\</xsl:element\>  
\</xsl:template\>  
\</xsl:stylesheet\>

and we run it like this

peter@radish:~$ xsltproc foobar.xsl foobar.xml

\<?xml version="1.0" encoding="utf8"?\>
- **something**

- _something else_

You can see how to add all the new elements you want.

For this problem the correct tool is XSLT.
