Creating a language -- HTML + a few special tags

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

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
if I wanted to learn.

-Dan

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
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/

Also, see this email:

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

Cheers,
B

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

–rob

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
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.