Click here to Skip to main content
5,785,816 members and growing! (18,884 online)
Email Password   helpLost your password?
Platforms, Frameworks & Libraries » Windows Presentation Foundation » Controls     Intermediate License: The Code Project Open License (CPOL)

BBCode Parser for WPF

By m0sa

A customizable BBCode parser for WPF
XML, C# 3.0, C#.NET, .NET 3.5, XAML, WPF, Design

Posted: 27 Nov 2008
Updated: 27 Nov 2008
Views: 2,339
Bookmarked: 13 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
9 votes for this Article.
Popularity: 4.29 Rating: 4.50 out of 5
0 votes, 0.0%
1
0 votes, 0.0%
2
1 vote, 11.1%
3
1 vote, 11.1%
4
7 votes, 77.8%
5

Introduction

This article is about a custom WPF control that enables the definition and usage of BBCode in your XAML application.

Background

I was trying to create an ASP.NET backoffice for basic CMS operations on a Silverlight page. Since XAML/XML is hard to edit without some kind of code completion, I decided to give BBCode a shot, since it's easy and the square brackets look a lot nicer to the user. :)

Parser Design

cd.png

The design of the control is fairly simple. It has a BBCode dependency property that stores the BBCode currently on display, a collection of Tags that are used during the parsing, a DefaultElementName that is used for elements without any tag, and a RootElementName, that is used as a root for the parsed XAML output.

The Parse method utilizes the tags to transform the BBCode into XAML and then uses the XamlReader.Load method to set the UserControl.Content property of the Parser control. 

Explanation of the Tag Objects

  • SimpleTag: [;)]
  • EnclosingTag: [i]text[/i]
  • ValueTag: [url]http://m0sa.net[/url]
  • ParameterValueTag: [url=http://m0sa.net]m0sa.net[url]
  • ParametrizedTag: [img width=100 height=200]image.jpg[/img]

You can add your own tags if you wish. Just implement the ITag interface, and you can use it in Parser.Tags in code or XAML! 

Using the Code

Here is a example of how one can define custom BBCodes for the Parser control:

BBCode definition - Click to enlarge

And this is the demo app: 

example.png

Since the BBCode and Tags are dependency properties, you can use bindings on them. In the demo app, I have bound the Textbox.Text to the Parser.BBCode property. And you have a (very) simple editor!

Points of Interest  

My first attempt to write this control was with regular expressions, but I decided to write the parsing manually because I had problems with nested BBCodes. 

I had problems with special Slovenian characters (š, c, ž) using the XamlReader.Parse(string) method. I had to use... 

XamlReader.Load(new MemoryStream(Encoding.UTF8.GetBytes(xaml)), pc);  

... to get it to work properly. Seems like the Parse method uses Encoding.ASCI to get the bytes from the input string.

History

  • 27-11-2008: First release

License

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

About the Author

m0sa


m0sa is a student at FERI
and a freelance website developer


Occupation: Web Developer
Company: m0sa.net
Location: Slovenia Slovenia

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 4 of 4 (Total in Forum: 4) (Refresh)FirstPrevNext
QuestionNested Tags???membersibercyberia21:22 27 Nov '08  
AnswerRe: Nested Tags???memberSteve Hansen22:24 27 Nov '08  
GeneralRe: Nested Tags???memberm0sa1:27 28 Nov '08  
GeneralRe: Nested Tags???memberRoberto Collina1:36 28 Nov '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 27 Nov 2008
Editor: Deeksha Shenoy
Copyright 2008 by m0sa
Everything else Copyright © CodeProject, 1999-2009
Web20 | Advertise on the Code Project