easyBBCode solution for .Net

Last post 08-24-2008 5:40 PM by Westnyorai. 0 replies.

Sort Posts:

  • easyBBCode solution for .Net

    08-24-2008, 5:40 PM
    • Member
      202 point Member
    • Westnyorai
    • Member since 08-08-2007, 12:13 PM
    • Posts 97

    Well I saw a few ways of using BBCode online, and I thought to myself, I know I can do this a lot easier with half the code.

    The following is a very simple solution up for grabs that uses a regular expression along side with a xml document.

    VB.Net function to be included in codebehind or App_Code
     

    1            Function easyBBCode(ByVal InputString As String) As String
    2                'Notes: Current version only supports closed tags (i.e. [tag]text[/tag])
    3                'Version 1.0, Please visit http://sanzon.wordpress.com for more .Net examples and solutions
    4    
    5                'Collects XML Tag information (change location as needed)
    6                Dim doc As XmlDocument = New XmlDocument()
    7                doc.Load(Server.MapPath("/documents/bbcode.xml"))
    8                Dim root As XmlElement = doc.DocumentElement
    9    
    10               'Replaces all closed BBCode Tags i.e. [tag]text[/tag]
    11               Dim RegexPattern As String = "\[\s*(?<1>[^/\s""\]=]+)=?\s*""?(?<2>[^\]\[""]*)""?\s*\](?<3>.*?)\[\s*/\s*\1\s*\]"
    12               Dim m As Match = Regex.Match(InputString, RegexPattern, RegexOptions.IgnoreCase Or RegexOptions.Compiled)
    13   
    14               Dim ItemAttribute As String = ""
    15               Dim ItemInnerText As String = ""
    16               Dim NewString As String = ""
    17               Dim StartPlace As Integer = 1
    18   
    19               Do While m.Success
    20                   If root.SelectSingleNode("tag[name=""" & LCase(m.Groups(1).Value) & """ and code]/name") IsNot Nothing Then
    21   
    22                       'Sets Values
    23                       ItemAttribute = m.Groups(2).Value
    24                       ItemInnerText = m.Groups(3).Value
    25   
    26                       'Checks for attributecode tag, and reformats attribute value
    27                       If root.SelectSingleNode("tag[name=""" & LCase(m.Groups(1).Value) & """]/attributeCode") IsNot Nothing And ItemAttribute <> "" Then
    28                           ItemAttribute = String.Format(root.SelectSingleNode("tag[name=""" & LCase(m.Groups(1).ToString) & """ and code]/attributeCode").InnerText, LCase(ItemAttribute))
    29                       End If
    30   
    31                       'Checks for Inner As Attribute Tag
    32                       If root.SelectSingleNode("tag[name=""" & LCase(m.Groups(1).Value) & """]/innerasattribute") IsNot Nothing And ItemAttribute = "" Then
    33                           If root.SelectSingleNode("tag[name=""" & LCase(m.Groups(1).Value) & """]/innerasattribute").InnerText = "True" Then
    34                               ItemAttribute = ItemInnerText
    35                           End If
    36                       End If
    37   
    38                       'Converts inner text for bbcode formating
    39                       ItemInnerText = BBCodeConvert(m.Groups(3).Value)
    40   
    41                       'Appends Values to Code String
    42                       NewString += Mid(InputString, StartPlace, m.Groups(1).Index - StartPlace)
    43                       NewString += String.Format(root.SelectSingleNode("tag[name=""" & LCase(m.Groups(1).ToString) & """ and code]/code").InnerText, LCase(ItemInnerText), LCase(ItemAttribute))
    44   
    45                       'Sets New StartPlace for next string
    46                       StartPlace = m.Groups(3).Index + m.Groups(3).Length + m.Groups(1).Length + 4
    47                   End If
    48                   m = m.NextMatch()
    49               Loop
    50   
    51               NewString += Mid(InputString, StartPlace)
    52   
    53               Return NewString
    54           End Function
    55   
    
    Now as for the BBCode.xml file the format should be as follows:
     
    1     
    2    <bbcode>
    3      <tag>
    4        <name>url</name>
    5        <innerasattribute>True</innerasattribute>
    6        <code>
    7          ![CDATA[<a href="{1}" mce_href="{1}" target="_blank">{0}</a>]]>
    8        </code>
    9      </tag>
    10     <tag>
    11       <name>img</name>
    12       <code>
    13         ![CDATA[<img src="{0}" mce_src="{0}" alt="{0}" />]]>
    14       </code>
    15     </tag>
    16     <tag>
    17       <name>quote</name>
    18       <attributeCode>
    19         ![CDATA[<span style="font-size:10px;"><b><i>Quoted: "{0}"</i></b></span><br />]]>
    20       </attributeCode>
    21       <code>
    22         ![CDATA[<div style="border:1px solid black;padding:3px;">{1}{0}</div>]]>
    23       </code>
    24   	</tag>
    25     <tag>
    26   </bbcode>
    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
    <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.
     If you want a download of the XML file you can get it on my blog at: 
    http://sanzon.wordpress.com/2008/08/24/easybbcode-script-for-vbnet/
    Please remember to mark the post(s) that provided your answer. Also, please don't paste insane amounts of code and use short paragraphs. That and remember it may take a few days to get a response.
Page 1 of 1 (1 items)