Codekicker.BBCode is a stable and fast BBCode-Parser for .NET. It can transform any BBCode into HTML or into an in-memory syntax tree that can be analyzed or translated. All tags are fully customizable, nothing is hardcoded.
<script>
under any circumstancesBBCode.ToHtml("[url=http://example.com/]example.com[url]")
via NuGet
PM > Install-Package PoLaKoSz.CodeKicker.BBCode
There is no built-in parser rule but You can make your own in 10 sec, so let's start:
To make a parsing rule You will need to initialize a new BBTag
class.
For instance if You have simple tag like [b]
[b]My bold text.[/b]
and You want to parse it to this
<strong>My bold text.</strong>
You only need to write
new BBTag("b", "<strong>", "</strong>")
But what if You need to extract data to a specific attribute into the HTML tag?
Given this BBCode input
[url=https://bbcode.codeplex.com class=jsCanModifyThis]Official CodeKicker parser link[/url]
which should be parsed to this HTML code
<a href="https://bbcode.codeplex.com" class="jsCanModifyThis">Official CodeKicker parser link</a>
You need to pass BBAttribute
objects into the BBTag
constructor like this
var attrs = new BBAttribute[]
{
new BBAttribute("url", ""),
new BBAttribute("className", "class")
};
new BBTag("url", "<a href=\"${url}\" class=\"${className}\">", "</a>", attrs),
to extract the link's URL and the class name from it.
Now that You can build custom parsing rules You only need to know how to perform an actual BBCode to HTML parsing. First make a parser rule collection
var bbTags = new List<BBTag>()
{
new BBTag("b", "<strong>", "</strong>"),
new BBTag("i", "<em>", "</em>"),
new BBTag("u", "<span style=\"text-decoration: line-through\">", "</span>"),
new BBTag("list", "<ul>", "</ul>") { SuppressFirstNewlineAfter = true },
new BBTag("li", "<li>", "</li>", true, false),
new BBTag("url", "<a href=\"${url}\">", "</a>",
new BBAttribute("url", "")),
new BBTag("code", "<pre class=\"prettyprint\">", "</pre>")
{
StopProcessing = true,
SuppressFirstNewlineAfter = true
},
};
Initialize the parser
var parser = new BBCodeParser(bbTags);
And execute the parser on a BBCode
string output = parser.ToHtml(
"[i]Not to be confused with [url=https://en.wikipedia.org/wiki/Text_formatting]Text formatting[/url]." +
"\"Rich text\" redirects here. For text/richtext, see [url=https://en.wikipedia.org/wiki/Enriched_text]Enriched text.[/url][/i]" +
"[list]" +
"[li]Capitalization: I am NOT making this up.[/li]" +
"[li]Surrounding with underscores: I am _not_ making this up.[/li]" +
"[li]Surrounding with asterisks: I am *not* making this up.[/li]" +
"[li]Spacing: I am n o t making this up.[/li]" +
"[/list]");
And the output should be this (in one line)
<em>Not to be confused with <a href=\"https://en.wikipedia.org/wiki/Text_formatting\">Text formatting</a>.
""Rich text" redirects here. For text/richtext, see <a href=\"https://en.wikipedia.org/wiki/Enriched_text\">Enriched text.</a></em>
<ul>
<li>Capitalization: I am NOT making this up.</li>
<li>Surrounding with underscores: I am _not_ making this up.</li>
<li>Surrounding with asterisks: I am *not* making this up.</li>
<li>Spacing: I am n o t making this up.</li>
</ul>
For more info check out the Documentation.