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 http://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 http://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.
[...] 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