Click here to Skip to main content
15,867,453 members
Articles / Web Development / HTML
Tip/Trick

Simple Text Markup Language (STML) Parser

Rate me:
Please Sign up or sign in to vote.
4.90/5 (6 votes)
21 Dec 2014MIT 24.1K   138   6   6
A simple custom HTML parser for converting STML into HTML

Image 1

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.

C#
//
// 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.

License

This article, along with any associated source code and files, is licensed under The MIT License


Written By
Architect
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionProcessing of code tags Pin
T.v.d.H1-Mar-15 6:42
T.v.d.H1-Mar-15 6:42 
AnswerRe: Processing of code tags Pin
Tawani Anyangwe2-Mar-15 1:43
Tawani Anyangwe2-Mar-15 1:43 
GeneralMy vote of 4 Pin
SZeeshanAli22-Dec-14 2:45
SZeeshanAli22-Dec-14 2:45 
GeneralCan you please explain what you mean? Pin
Tawani Anyangwe24-Dec-14 15:26
Tawani Anyangwe24-Dec-14 15:26 

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.