Converting BBCode into HTML using C#

Although the dynamic content in the Cyotek website is written using Markdown syntax using the MarkdownSharp library, we decided to use the more commonly used BBCode tags for the forums.

Some of the source code on this site is also preformatted using the CSharpFormat library, and we wanted to provide access to this via forum tags too.

A quick Google search brought up a post by Mike343 which had a BBCode parser that more or less worked, but didn't cover everything we wanted.

You can download below an updated version of this parser which has been modified to correct some problems with the original implementation and add some missing BBCode tags, including a set of custom tags for providing the syntax highlighting offered by CSharpFormat. Using the provided formatter classes you can easily create additional tags to suit the needs of your application.

To transform a block of BBCode into HTML, call the static Format method of the BbCodeProcessor class, for example:

string exampleBbcCode = "[b]this text is bold[/b]\n[i]this text is italic[/i]\n[u]this text is underlined[/u]";
string html = BbCodeProcessor.Format(exampleBbcCode);

is transformed into

<p>
<strong>this text is bold</strong><br>
<em>this text is italic</em><br>
<u>this text is underlined</u>
</p>

Much of the formatting is also customisable via CSS - several of the BBCode tags such as [code], [quote], [list] etc are assigned a class which you can configure in your style sheets. Listed below are the default rules used by the Cyotek site as a starting point for your own:

.bbc-codetitle, .bbc-quotetitle { margin: 1em 1.5em 0; padding: 2px 4px; background-color: #A0B3CA; font-weight: bold; }
.bbc-codecontent, .bbc-quotecontent { margin: 0 1.5em 1em; padding: 5px; border: solid 1px #A0B3CA; background-color: #fff; }
.bbc-codecontent pre { margin: 0; padding: 0; }
.bbc-highlight { background-color: #FFFF00; color: #333399; }
.bbc-spoiler { color: #C0C0C0; background-color: #C0C0C0; }
.bbc-indent { padding: 0 1em; }
.bbc-list { margin: 1em; }

Finally, if you are using MVC, you may find the following HTML Helper useful for transforming code from within your views.

public static string FormatBbCode(this HtmlHelper helper, string text)
{
   return BbCodeProcessor.Format(helper.Encode(text));
}

If you create any additional formatting codes for use with this library, please let us know via either comments or the Contact Us link, and we'll integrate them into the library for others to use.

Downloads

Filename Description Version Release Date
cyotek.web.bbcodeformatter.zip
  • md5: e33d27a1760bd48f92ea2556322c392f

A simple to use class library for converting text containing BBCode used by popular forum systems into HTML using C#.

18/03/2010 Download

About The Author

Gravatar

The founder of Cyotek, Richard enjoys creating new blog content for the site. Much more though, he likes to develop programs, and can often found writing reams of code. A long term gamer, he has aspirations in one day creating an epic video game. Until that time, he is mostly content with adding new bugs to WebCopy and the other Cyotek products.

Leave a Comment

While we appreciate comments from our users, please follow our posting guidelines. Have you tried the Cyotek Forums for support from Cyotek and the community?

Styling with Markdown is supported