Click here to Skip to main content
15,889,600 members
Articles / Web Development / HTML

Converting BBCode into HTML using C#

Rate me:
Please Sign up or sign in to vote.
5.00/5 (3 votes)
14 Sep 2012CPOL1 min read 28.6K   597   5   6
A brief article into how to use a C# library that converts BBCode used by popular forums into HTML

Introduction

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:

C#
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:

HTML
<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:

CSS
.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.

C#
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)


Written By
Software Developer (Senior)
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionGetting rid of abundant &lt;br/&gt; tags Pin
wout de zeeuw1-Oct-12 5:51
wout de zeeuw1-Oct-12 5:51 
All newlines are converted to <br/> tags, but in case of the newlines around [list] tags this is not desired. So I've adapted the (order of the) regex's a little:

In the BbCodeProcessor static constructor I moved these to the top and adapted to eat up the new lines. Also I added support for the [*] tag, that goes on a single line to denote a line item (has no closing tag). The [*] tag is used in MarkItUp[^], a very nice basic markup editor.

Ok, here's the code change:

C#
static BbCodeProcessor()
{
  string orderedListFormat;

  _formatters = new List<IHtmlFormatter>();

  _formatters.Add(new RegexFormatter(@"<(.|\n)*?>", string.Empty));
  _formatters.Add(new RegexFormatter(@"(?:\r\n)*\[list(?:\s*)\](?:\r\n)*(.*?)\[/list(?:\s*)\](?:\r\n)*", "<ul class=\"bbc-list\">$1</ul>", RegexOptions.Singleline));
  _formatters.Add(new RegexFormatter(@"\[li(?:\s*)\](.*?)\[/li(?:\s*)\]", "<li>$1</li>", RegexOptions.Singleline));
  _formatters.Add(new RegexFormatter(@"\[\*\](.*?)\r\n", "<li>$1</li>", RegexOptions.Multiline));

  // using the below three formatters make a complete mess of preformatted text.
  // I've disabled them and replaced them with a slower version that will replace line breaks only when they are not within one of the specified blocks.

  //_formatters.Add(new SearchReplaceFormatter("\r", ""));
  //_formatters.Add(new SearchReplaceFormatter("\n\n", "</p><p>"));
  //_formatters.Add(new SearchReplaceFormatter("\n", "<br />"));
  _formatters.Add(new LineBreaksFormatter(new string[] { "html", "csharp", "code", "jscript", "sql", "vb", "php" }));


Some of these lines were moved from the bottom to happen before the LineBreaksFormatter is executed.

And also added a new RegexFormatter constructor:

C#
public RegexFormatter(string pattern, string replace, RegexOptions options)
{
  options |= RegexOptions.Compiled;

  _replace = replace;
  _regex = new Regex(pattern, options);
}

(I have to say the CodeProject message editor is awesome!)
Wout


modified 1-Oct-12 18:09pm.

AnswerRe: Getting rid of abundant tags Pin
Richard James Moss1-Oct-12 6:02
professionalRichard James Moss1-Oct-12 6:02 
Question[li] tag not converted correctly. Pin
wout de zeeuw14-Sep-12 4:13
wout de zeeuw14-Sep-12 4:13 
AnswerRe: [li] tag not converted correctly. Pin
Richard James Moss14-Sep-12 5:32
professionalRichard James Moss14-Sep-12 5:32 
GeneralRe: [li] tag not converted correctly. Pin
wout de zeeuw14-Sep-12 5:50
wout de zeeuw14-Sep-12 5:50 
GeneralRe: [li] tag not converted correctly. Pin
Richard James Moss14-Sep-12 6:06
professionalRichard James Moss14-Sep-12 6:06 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.