Re: [xsl] UBB Code and XSLT

Subject: Re: [xsl] UBB Code and XSLT
From: "M. David Peterson" <m.david@xxxxxxxxxx>
Date: Sat, 10 Jan 2004 16:10:29 -0700
Ive been working on creating an XSLT parser that can parse random text and
seek out certain words or phrases and recursively apply styles to them at
will.  It's a little rough around the edges but it was fairly simple to
apply the concept to UBB.  Anyway, I didn't go after every element and the
URL one without the [url][/url] tag will take a little effort but it is
possible... I eventually will convert this into a template that looks for
words and phrases that are stored in XML and stylized accordingly using a
couple associated attributes...  But time is short and the work is big so
thatll have to be another day.

Anyway, enjoy... hopes this helps get your party started...

Best regards,

M.

The following UBB wrapped into and XML wrapper (there are many ways to do
that on the fly)

*******************************************
<?xml version="1.0"?>

<content>
 <ubb>
  This is a test of UBB.
  [b]this is test of bold[/b]
  [i]this is a test of italic[/i]
  [email]m.david@xxxxxxxxxx[/email]
  [QUOTE]this is a quote[/QUOTE]
  [url]http://www.thissucks.com[/url]
 </ubb>
</content>

*******************************************

And then transformed by the following XSLT...

********************************************
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
version="1.0">
<xsl:preserve-space elements="*"/>

<xsl:output method="html"/>

<xsl:template match="/">
 <xsl:call-template name="capture">
  <xsl:with-param name="processText" select="content/ubb"/>
 </xsl:call-template>
</xsl:template>

<xsl:template name="capture">
 <xsl:param name="processText"/>
 <xsl:variable name="remainingContent" select="$processText"/>
 <xsl:variable name="remaining_after_end_ubb"
select="substring-after($processText, '[/')"/>
 <xsl:variable name="remainingContent_after_ubb"
select="substring-after($remaining_after_end_ubb, ']')"/>
  <xsl:choose>
   <xsl:when test="not(starts-with($processText, '['))">
    <xsl:value-of select="substring-before($processText, '[')"/>
    <xsl:call-template name="capture">
     <xsl:with-param name="processText" select="concat('[',
substring-after($processText, '['))"/>
    </xsl:call-template>
   </xsl:when>
   <xsl:when test="contains($processText, '[')">
    <xsl:call-template name="processUBB">
     <xsl:with-param name="ubbType"
select="substring-after(substring-before($processText, ']'), '[')"/>
     <xsl:with-param name="content"
select="substring-after(substring-before($processText, '[/'), ']')"/>
     <xsl:with-param name="remainingContent"
select="$remainingContent_after_ubb"/>
    </xsl:call-template>

   </xsl:when>
   <xsl:otherwise>
    <xsl:value-of select="$remainingContent"/>
   </xsl:otherwise>
  </xsl:choose>

</xsl:template>

<xsl:template name="processUBB">

<xsl:param name="ubbType"/>
<xsl:param name="content"/>
<xsl:param name="remainingContent"/>

 <xsl:choose>
  <xsl:when test="$ubbType = 'b'">
   <xsl:call-template name="b">
    <xsl:with-param name="remainingContent" select="$remainingContent"/>
    <xsl:with-param name="content" select="$content"/>
   </xsl:call-template>
  </xsl:when>
  <xsl:when test="$ubbType = 'i'">
   <xsl:call-template name="i">
    <xsl:with-param name="remainingContent" select="$remainingContent"/>
    <xsl:with-param name="content" select="$content"/>
   </xsl:call-template>
  </xsl:when>
  <xsl:when test="$ubbType = 'img'">
   <xsl:call-template name="img">
    <xsl:with-param name="remainingContent" select="$remainingContent"/>
    <xsl:with-param name="content" select="$content"/>
   </xsl:call-template>
  </xsl:when>
  <xsl:when test="$ubbType = 'email'">
   <xsl:call-template name="email">
    <xsl:with-param name="remainingContent" select="$remainingContent"/>
    <xsl:with-param name="content" select="$content"/>
   </xsl:call-template>
  </xsl:when>
  <xsl:when test="$ubbType = 'QUOTE'">
   <xsl:call-template name="QUOTE">
    <xsl:with-param name="remainingContent" select="$remainingContent"/>
    <xsl:with-param name="content" select="$content"/>
   </xsl:call-template>
  </xsl:when>
  <xsl:when test="$ubbType = 'url'">
   <xsl:call-template name="url">
    <xsl:with-param name="remainingContent" select="$remainingContent"/>
    <xsl:with-param name="content" select="$content"/>
   </xsl:call-template>
  </xsl:when>
  <xsl:otherwise>
   <xsl:call-template name="capture">
    <xsl:with-param name="remainingContent" select="$remainingContent"/>
    <xsl:with-param name="content" select="$content"/>
   </xsl:call-template>
  </xsl:otherwise>
 </xsl:choose>

</xsl:template>

<xsl:template name="b">
<xsl:param name="content"/>
<xsl:param name="remainingContent"/>
 <b><xsl:value-of select="$content"/></b>
 <xsl:call-template name="capture">
  <xsl:with-param name="processText" select="$remainingContent"/>
 </xsl:call-template>
</xsl:template>

<xsl:template name="i">
<xsl:param name="content"/>
<xsl:param name="remainingContent"/>
 <i><xsl:value-of select="$content"/></i>
 <xsl:call-template name="capture">
  <xsl:with-param name="processText" select="$remainingContent"/>
 </xsl:call-template>
</xsl:template>

<xsl:template name="email">
<xsl:param name="content"/>
<xsl:param name="remainingContent"/>
 <a href="mailto:{$content}";><xsl:value-of select="$content"/></a>
 <xsl:call-template name="capture">
  <xsl:with-param name="processText" select="$remainingContent"/>
 </xsl:call-template>
</xsl:template>

<xsl:template name="img">
<xsl:param name="content"/>
<xsl:param name="remainingContent"/>
 <img src="{$content}"/>
 <xsl:call-template name="capture">
  <xsl:with-param name="processText" select="$remainingContent"/>
 </xsl:call-template>
</xsl:template>

<xsl:template name="QUOTE">
<xsl:param name="content"/>
<xsl:param name="remainingContent"/>
 "<xsl:value-of select="$content"/>"
 <xsl:call-template name="capture">
  <xsl:with-param name="processText" select="$remainingContent"/>
 </xsl:call-template>
</xsl:template>

<xsl:template name="url">
<xsl:param name="content"/>
<xsl:param name="remainingContent"/>
 <a href="{$content}"><xsl:value-of select="$content"/></a>
 <xsl:call-template name="capture">
  <xsl:with-param name="processText" select="$remainingContent"/>
 </xsl:call-template>
</xsl:template>

</xsl:stylesheet>

********************************************

Will recursively crawl the UBB text of the XML node and produce this...

*******************************************


  This is a test of UBB.
  <b>this is test of bold</b>
  <i>this is a test of italic</i>
  <a href="mailto:m.david@xxxxxxxxxx";>m.david@xxxxxxxxxx</a>

 "this is a quote"

  <a href="http://wwwthissucks.com";>http://www.thissucks.com</a>


**************************************************


Have a good one!

| M. David Peterson mark.peterson@xxxxxxxxxxx |

| XSLT GUI Developer 303.357.3448 |

| Cheap Tickets, part of trip network Fax 303.357.3380 |

| 6560 Greenwood Plaza Blvd., Suite 400 Englewood, CO 80111 |

| Cendant Travel Distribution Services http://www.cheaptickets.com/ |

----- Original Message ----- 
From: "Karl J. Stubsjoen" <karl@xxxxxxxxxxxxx>
To: <xsl-list@xxxxxxxxxxxxxxxxxxxxxx>
Sent: Saturday, January 10, 2004 8:04 AM
Subject: Re: [xsl] UBB Code and XSLT


> All right, how about a simplified HTML set of tags that XSLT will parse...
> UBB for XSLT?  I really see the value in UBB type "tagging", but I'm being
> told I need to write my own parser to parse the dang thing.
> http://www.ozoneasylum.com/Forum12/HTML/001704.html
>
> So far, what I've see on the net are UBB Message Forums.  Mostly PHP, and
> such... I'm a vbscript kind of guy (classic ASP).
>
> Karl
>
> ----- Original Message -----
> From: "J.Pietschmann" <j3322ptm@xxxxxxxx>
> To: <xsl-list@xxxxxxxxxxxxxxxxxxxxxx>
> Sent: Friday, January 09, 2004 1:31 PM
> Subject: Re: [xsl] UBB Code and XSLT
>
>
> > Karl J. Stubsjoen wrote:
> > > I'm considering UBB code for the simple formatting needs that my
clients
> may
> > > use when updating active content on my websites.  Would I write a
> template
> > > that could correctly translate the UBB code into valid HTML tags, or
am
> I
> > > way off on this concept?
> >
> > (for everybody wondering, UBB is one of the many variants of "simplified
> > HTML" used in web based bullentin boards, see for example
> >   http://france.expats.tv/data/ultimatebb.php?ubb=ubb_code_page)
> >
> > The problem is that you can't match UBB code with XSLT templates, you
need
> > to parse a text string. It's not too hard to do a one-to-one translation
> > from UBB into possibly malformed HTML, using d-o-e and the usual
recursive
> > string replace template. But if you want to deal with stray brackets and
> > do some validation, you're probably much better off with another tool
> > than XSLT 1.0. Most likely you'll find ready-to-use Perl, PHP or Java
code
> > in one of the open source BBs and Wikis. Search FreshMeat.
> >
> > J.Pietschmann
> >
> >
> >  XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
> >
> >
> >
>
>
>  XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
>


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread