Thomas’ Developer Blog

August 24, 2008

easyBBCode script for VB.Net

Filed under: .Net Framework 3.5, Asp.Net Framework 2.0, CSS, HTML, vb.net, vbscript, xml — Tags: , , , , — sanzon @ 9:33 pm

After looking up some examples of BBCode online. Sadly I didn’t like anything I saw and I knew I could easily do it myself with a lot less work and make it far easier to edit in the future by using a simple XML document to do all the work for me.

So, without further adieu I present to you easyBBCode v1.0

Function easyBBCode(ByVal InputString As String) As String
'Notes: Current version only supports closed tags (i.e. [tag]text[/tag])
'easyBBCode v1.0, Please visit https://sanzon.wordpress.com for more .Net examples and solutions

'Collects XML Tag information (change location as needed)
Dim doc As XmlDocument = New XmlDocument()
doc.Load(Server.MapPath("/documents/easybbcode.xml"))
Dim root As XmlElement = doc.DocumentElement

'Replaces all closed BBCode Tags i.e. [tag]text[/tag]
Dim RegexPattern As String = "\[\s*(?<1>[^/\s""\]=]+)=?\s*""?(?<2>[^\]\[""]*)""?\s*\](?<3>.*?)\[\s*/\s*\1\s*\]"
Dim m As Match = Regex.Match(InputString, RegexPattern, RegexOptions.IgnoreCase Or RegexOptions.Compiled)

Dim ItemAttribute As String = ""
Dim ItemInnerText As String = ""
Dim NewString As String = ""
Dim StartPlace As Integer = 1

Do While m.Success
    If root.SelectSingleNode("tag[name=""" & LCase(m.Groups(1).Value) & """ and code]/name") IsNot Nothing Then

        'Sets Values
        ItemAttribute = m.Groups(2).Value
        ItemInnerText = m.Groups(3).Value

        'Checks for attributecode tag, and reformats attribute value
        If root.SelectSingleNode("tag[name=""" & LCase(m.Groups(1).Value) & """]/attributeCode") IsNot Nothing And ItemAttribute <> "" Then
            ItemAttribute = String.Format(root.SelectSingleNode("tag[name=""" & LCase(m.Groups(1).ToString) & """ and code]/attributeCode").InnerText, LCase(ItemAttribute))
        End If

        'Checks for Inner As Attribute Tag
        If root.SelectSingleNode("tag[name=""" & LCase(m.Groups(1).Value) & """]/innerasattribute") IsNot Nothing And ItemAttribute = "" Then
            If root.SelectSingleNode("tag[name=""" & LCase(m.Groups(1).Value) & """]/innerasattribute").InnerText = "True" Then
                ItemAttribute = ItemInnerText
            End If
        End If

        'Converts inner text for bbcode formating
        ItemInnerText = BBCodeConvert(m.Groups(3).Value)

        'Appends Values to Code String
        NewString += Mid(InputString, StartPlace, m.Groups(1).Index - StartPlace)
        NewString += String.Format(root.SelectSingleNode("tag[name=""" & LCase(m.Groups(1).ToString) & """ and code]/code").InnerText, LCase(ItemInnerText), LCase(ItemAttribute))

        'Sets New StartPlace for next string
        StartPlace = m.Groups(3).Index + m.Groups(3).Length + m.Groups(1).Length + 4
    End If
    m = m.NextMatch()
Loop

NewString += Mid(InputString, StartPlace)

Return NewString
End Function

An example for the easyBBCode.xml is shown here:

<bbcode>
    <tag>
        <name>url</name>
        <innerasattribute>True</innerasattribute>
        <code>
            ![CDATA[<a href="{1}" target="_blank">{0}</a>]]>
        </code>
    </tag>
    <tag>
        <name>img</name>
        <code>
            ![CDATA[<img src="{0}" alt="{0}" />]]>
        </code>
    </tag>
    <tag>
        <name>quote</name>
        <attributeCode>
            ![CDATA[<span style="font-size:10px;"><b><i>Quoted: "{0}"</i></b></span><br />]]>
        </attributeCode>
        <code>
            ![CDATA[<div style="border:1px solid black;padding:3px;">{1}{0}</div>]]>
        </code>
    </tag>
    <tag>
</bbcode>      

Note: For the full file visit http://www.otakuelite.com/documents/easybbcode.xml

The current easyBBCode function only supports closed tags such as [tag]text[/tag] along with child tags as well.

The xml file uses two optional tags: <attributecode> and <innerasattribute>

<attributecode> is used in cases such as with the Quote tag where if a user includes the name for the person being quoted it will be shown as Quoted by “User” instead of the person having to type in [Quote=Quoted By User]Text[/Quote]

<innerasattribute> is used in cases as with the URL tag where a user may have either [url]http://www.site.com[/url] or [url=http://www.site.com]Text[/url]

The above code also supports white space and child tags. Not too shabby for two days work and my first practical use of XML.

Please visit https://sanzon.wordpress.com for more examples in the future.

PS: Sorry about the pre tag not wrapping. WordPress doesn’t allow custom CSS, and for some reason editing the style in the page doesn’t work either. So for now we’ll just have to deal with it until I get the blog page done on my server.

Advertisements

6 Comments »

  1. […] public links >> bbcode easyBBCode script for VB.Net Saved by rskaje on Sun 02-11-2008 bb-ruby, a small BBCode to HTML parser in Ruby Saved by […]

    Pingback by Recent Links Tagged With "bbcode" - JabberTags — November 3, 2008 @ 9:34 am

  2. I am sure this piece of writing has touched all the
    internet visitors, its really really nice paragraph on building up new webpage.

    Comment by Eileen — January 6, 2014 @ 11:08 pm

  3. Wow! This blog looks just like my old one! It’s on a entirely different subject but it has pretty much the same layout and design.
    Excellent choice of colors!

    Comment by http://batmanarkhamoriginscheatsz.blogspot.com — January 19, 2014 @ 12:04 am

  4. Hi there I am so excited I found your site, I really found you by mistake, while I was searching on Bing for something else, Anyhow I am here now and would just
    like to say thanks a lot for a marvelous post and
    a all round enjoyable blog (I also love the theme/design), I don’t have
    time to read through it all at the moment but I have saved it and also added in your RSS feeds, so when I have
    time I will be back to read a great deal more, Please do keep up the
    superb jo.

    Comment by Mark — January 20, 2014 @ 5:24 am

  5. I feel that is among the such a lot significant info for me.
    And i am satisfied reading your article. But should observation on few basic issues,
    The web site style is great, the articles is in reality excellent :
    D. Excellent task, cheers

    Comment by path of exile hack download — March 6, 2014 @ 5:24 pm

  6. If you wish for to increase your know-how simply keep visiting this website and be updated with the hottest gossip posted
    here.

    Comment by mobile car window repair in atlanta — September 20, 2014 @ 4:09 am


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: