tags:

views:

1971

answers:

5

I'm building a website that requires very basic markup capabilities. I can't use any 3rd party plugins, so I just need a simple way to convert markup to HTML. I might have a total of 3 tags that I'll allow.

What is the best way to convert "==Heading=="" to "< h2>Heading< / h2>", or "--bold--" to "< b> bold < /b>"? Can this be done simply with Regex, or does somebody have a simple function?

I'm writing this in C#, but examples from other languages would probably work.

thanks,

Jim

Follow up: This is such a small part of my website that I liked the simplicity of using a simple Regex replace. I made this work in C# with the following code:

string html = Regex.Replace("==This will be inside h2==", "==([^=]*)==", "< h2>$1< /h2>")

.NET uses $1 notation instead of \1 notation that might be in other languages. (Note--I added spaces in the html tags to make it display in the markdown)

+3  A: 

It's not really a simple problem, because if you're going to display things back to the user, you'll need to also sanitise the input to ensure you don't create any cross site scripting vulnerabilities.

That said, you could probably do something pretty simple as you describe most easily with a regular expression replacement.

For example

replace the pattern ==([^=]*)== with <h2>\1</h2>
Matt Sheppard
+1  A: 

I use Markdown (the same lightweight markup language used on this site). For C# there is a very good bit of source code available here. It fully supports Markdown, although it doesn't appear to be maintained. But for the time being it works really well and it's free open source.

The best part is all the work is done for you if you include this source with your project. It's very small; basically a single method call to transform a chunk of text into HTML.

Joseph Daigle
a python equivalent also exists: http://www.freewisdom.org/projects/python-markdown/
TokenMacGuy
A: 

This really depends on the Wiki syntax you're using as there are several different ones. Obviously the wiki software has this functionality somewhere; if you can't find a software package that does this for you, you could start looking for the relevant code in your wiki software.

pix0r
A: 

Probably overkill for your 3 tags, but if it blows up into a fully-fledged markup language, and the regexp's are beginning to look scary, then you might want to consider antlr

toolkit
+2  A: 

There is also a perl module and a php project to do this. The source code to either could be useful in developing your own solution.

Paul Wicks