Creating a language -- HTML + a few special tags

R

Rob Hunter

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

<ul>
<li><FOO>something</FOO></li>
<li><BAR>something else</BAR></li>
</ul>

where FOO and BAR are special tags that have some meaning that I define.
Say, for this example, that FOO strongs text inside a <p> tag, and BAR
emphasizes text inside a blink tag. So the "compiled down" source would
look like

<ul>
<li><p><strong>something</strong></p></li>
<li><blink><em>something else</em></blink></li>
</ul>

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
 
D

Dan Zwell

Rob said:
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

<ul>
<li><FOO>something</FOO></li>
<li><BAR>something else</BAR></li>
</ul>

where FOO and BAR are special tags that have some meaning that I define.
Say, for this example, that FOO strongs text inside a <p> tag, and BAR
emphasizes text inside a blink tag. So the "compiled down" source would
look like

<ul>
<li><p><strong>something</strong></p></li>
<li><blink><em>something else</em></blink></li>
</ul>

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
 
B

benjohn

Dan said:
*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
 
P

Peter Hickman

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

foobar.xml
=======

<ul>
<li><FOO>something</FOO></li>
<li><BAR>something else</BAR></li>
</ul>

And this xslt...

foobar.xsl
=======

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:eek:utput
method="xml"
version="1.0"
encoding="utf8"
indent="yes"/>

<xsl:template match="/">
<xsl:apply-templates/>
</xsl:template>

<xsl:template match="FOO">
<p><strong><xsl:apply-templates /></strong></p>
</xsl:template>

<xsl:template match="BAR">
<blink><em><xsl:apply-templates /></em></blink>
</xsl:template>

<!-- Otherwise just copy the element -->
<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"?>
<ul>
<li><p><strong>something</strong></p></li>
<li><blink><em>something else</em></blink></li>
</ul>

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

For this problem the correct tool is XSLT.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top