Codeparser – implenting custom BB Codes the easy way

*** NOTE – The author discontinued this tool ***

BBCodes are well known through forum communities and is the abbreviation for Bulletin Board Code. The tags are usually indicated by rectangular brackets surrounding a keyword, the use of it is to make text formatting possible without having to write HTML. It’s safer, comfortable and easy to implent.

Every tag is being translated into markup language that web browsers understand, this is where Codeparser.NET joins the game:

codeparser.net is a free .NET based and highly configurable parser for BBCode. Since it is a component, it can be integrated with Windows as well as with web applications and web services.

It’s pretty easy to implent and you will find some pre-made examples for parsing Smilies, Lists, Links, Code and much more. Alternative replacements, syntax highlighting, invalid tag configuration, expression replacements, things you don’t want to miss when dealing with BBCodes. I recently used it for two ASP.NET Web Applications, it’s pretty straight forward. Define some rules, save them as parser.xml, create the parser object and just starting parsing:

parser.Parse(txt);

If you use the dll for communities there’s just one thing to look after, people forget closing specific tags sometimes which will lead to an exception at the moment. One of the developer, Golo Roden, already told me that they are working on a better solution for this in a future version, for now just use a try/catch block if you deal with user input and do not validate the proper tag closings yourself:

    protected string ParseText(string txt)
    {
        try
        {
            return parser.Parse(txt);
        }
        catch
        {
            return txt;
        }
    }

Here is a example of a parser.xml which is parsing Links and text formattings:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <parser>
    <throwExceptionOnInvalidTag value="False" />
  </parser>
  <tags>
    <tag name="url" replacement="&lt;a href=&quot;{1}&quot;&gt;{0}&lt;/a&gt;"
     alternativeReplacement="&lt;a href=&quot;{0}&quot;&gt;{0}&lt;/a&gt;" />
    <tag name="mail" replacement="&lt;a href=&quot;mailto:{1}&quot;&gt;{0}&lt;/a&gt;" />
    <tag name="email" replacement="&lt;a href=&quot;mailto:{1}&quot;&gt;{0}&lt;/a&gt;" />
    <tag name="b" replacement="&lt;span style=&quot;font-weight:bold;&quot;&gt;{0}&lt;/span&gt;" />
    <tag name="i" replacement="&lt;span style=&quot;font-style:italic;&quot;&gt;{0}&lt;/span&gt;" />
    <tag name="u" replacement="&lt;span style=&quot;text-decoration:underline;&quot;&gt;{0}&lt;/span&gt;" />
    <tag name="quote" replacement="&lt;table border=&quot;0&quot; cellpadding=&quot;8&quot; cellspacing=&quot;0&quot;&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td class=&quot;alt2&quot; style=&quot;border: 1px inset ;&quot;&gt;{0}&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;" />
  </tags>
</configuration>

I didn’t face any other problems after running it for about one month now, good job and I’m happy to recommend it to you guys! Head over to the -removed- homepage for more.

5 comments ↓

#1 Paul on 01.05.08 at 11:52 am

Hi Andreas,

Could you also post the parser.xml?

Cheers,

Paul

#2 andreas.kraus on 01.05.08 at 1:29 pm

Hi Paul,

I just posted an example, you can find more on the Codeparser Homepage!

Cheers,
Andreas

#3 Björn on 01.18.08 at 8:08 am

Hi Andreas,

Thanks for the tip! I’ve tried it out and works perfect, except for one little thing. I’m trying to use the Text.Code extension but receive this error:

Exception of type ‘codeparser.net.Exceptions.InvalidTagNestingException’ was thrown.

Any ideas?

Regards,
Björn

#4 Golo Roden on 01.18.08 at 9:04 am

Hi Björn,

if you want you may send your not-working example to me, and I’ll have a look at it … perhaps there’s a bug in codeparser.net we do not know of yet, perhaps it’s simply a configuration issue …

Just contact me via email webmaster@goloroden.de

Cheerio,

Golo
(Developer of codeparser.net)

#5 Björn on 02.11.08 at 10:42 am

Hi Golo,

I managed to find the error. I was using a class to truncate the text to 100 words and when the post exceeded this and I was using BBCode the exception was thrown since it did not have an ending tag.

The issue is now fixed.

Regards,
Bjorn

Leave a Comment