Click here to Skip to main content
Licence CPOL
First Posted 27 Jan 2012
Views 6,271
Bookmarked 4 times

Converting BBCode into HTML using C#

By | 27 Jan 2012 | Technical Blog
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
A Technical Blog article. View original blog here.[^]

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<span class="code-none">: 1em 1.5em 0<span class="code-none">; padding<span class="code-none">: 2px 4px<span class="code-none">; background-color<span class="code-none">: #A0B3CA<span class="code-none">; font-weight<span class="code-none">: bold<span class="code-none">; <span class="code-none">}
.bbc-codecontent, .bbc-quotecontent <span class="code-none">{ margin<span class="code-none">: 0 1.5em 1em<span class="code-none">; padding<span class="code-none">: 5px<span class="code-none">; border<span class="code-none">: solid 1px #A0B3CA<span class="code-none">; background-color<span class="code-none">: #fff<span class="code-none">; <span class="code-none">}
.bbc-codecontent pre <span class="code-none">{ margin<span class="code-none">: 0<span class="code-none">; padding<span class="code-none">: 0<span class="code-none">; <span class="code-none">}
.bbc-highlight <span class="code-none">{ background-color<span class="code-none">: #FFFF00<span class="code-none">; color<span class="code-none">: #333399<span class="code-none">; <span class="code-none">}
.bbc-spoiler <span class="code-none">{ color<span class="code-none">: #C0C0C0<span class="code-none">; background-color<span class="code-none">: #C0C0C0<span class="code-none">; <span class="code-none">}
.bbc-indent <span class="code-none">{ padding<span class="code-none">: 0 1em<span class="code-none">; <span class="code-none">}
.bbc-list <span class="code-none">{ margin<span class="code-none">: 1em<span class="code-none">; <span class="code-none">}
</span></span></span></span></span></span></span></span></span></span></span></span></span></span></

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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Cyotek

Software Developer (Senior)

United Kingdom United Kingdom

Member

Follow on Twitter Follow on Twitter


Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
-- There are no messages in this forum --
Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120517.1 | Last Updated 27 Jan 2012
Article Copyright 2012 by Cyotek
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid