65.9K
CodeProject is changing. Read more.
Home

Simple Text Markup Language (STML) Parser

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.90/5 (6 votes)

Dec 21, 2014

MIT
viewsIcon

24975

downloadIcon

138

A simple custom HTML parser for converting STML into HTML

Introduction

This tip shows a simple way to parse text with custom tags into HTML.

Background

Recently, we needed to be able to allow our users to enter formatted text online which will be converted into PDF (via XSL-FO) and used to generate documents and reports.

Note: Allowing raw HTML was out of question, and Markdown was not very extensible, I decided to write this custom parser based partially on a syntax (BBCode) I saw years ago on an online forum.

Sample Usage

All you need to do is call the Parse method of the StmlParser.

//
// Parse a simple line of text
//
const string input = "This is [b]bold[/b], [i]italic[i] and [blue]blue[/blue] for now";
const string expected = "This is <b>bold</b>, <i>italic</i> 
	and <font color=\"blue\">blue</font> for now";
var actual = StmlParser.Parse(input).ToString();

Assert.AreEqual(expected, actual);
...

Supported tags include:

Generic HTML tags

b, i, u, s, sub, sup, em, strong, small, dir, center, big, blockquote, 
pre, code, h1, h2, h3, h4, h5, hr, img

Example:

[img=pic.png]Alt text[/img]

Font Color tags

red, green, blue, navy, fuchsia, orange, yellow, gray, purple

Example:

[red]Red text[/red]

List tags

olist, ulist

Example:

[olist]
    [#]This is line 1
    [#]This is line 2
        [olist=a]
            [#]This is line 2.1
            [#]This is line 2.2
            [#]This is line 2.3
    and it continues here
        [/olist]
[/olist]

Hyperlink tags

url,email

Example:

[url=http://google.com]Google[/url] or [email]me@gmail.com[/email]

Other custom tags

check,check-on

Example: [check] for simple empty square box.