Are you making an application where you want to allow users to chose between either the most awesome textile markup language or an RTE for content editing? Are you building said applicaiton in C#? A little while ago, I needed to do this as well. I approached it the same way as a few folks before me. I decided to always save the content in the DB as textile then convert back and forth as necessary. For an RTE to work in this environment, you will need a fancy-pants html-to-textile parser to convert the HTML before database insertion.
There are textile to HTML converter libraries for just about every language including C#. Much to my disappointment, however, there seems to be no C#/.net library going the other way: html-2-textile. I was able to find libraries in various states for other languages such as ruby, python, and php, but no love from the .netters. After I found SGMLReader, I decided to write my own.
What I ended up with will hopefully serve as a framework for those six people who need this functionality in the .net world. I say framework because HTML2Markup is built with extension in mind. By writing a new class defining your markup language (markdown, wiki syntax, etc) and plugging it in to HTML2Markup, you can make this library work for your markup language of choice.
HTML2Markup Textile Conversion Features
- Handles i, em, b, strong, s, del, strike, sub, sup, cite, code, h#, p, blockquote, a, img, and br tags.
- Handles lists including nested lists
- Handles tables
- Handles style attributes
- Ignores style attributes like float, position, attributes with expression, etc.
- Ignores all tags it doesn’t recognize which is nice for sanitization
- Gets the newlines right producing very elegant, readable textile. Yeah!
Ok, so how do you use it? It is pretty simple. To use our textile definition:
using HTML2Markup;
void SomeFunct(string html)
{
string textile = MarkupConverter.HTML2Textile(html);
}
Or using the object:
using HTML2Markup;
void SomeFunct(string html)
{
Parser p = new TextileParser();
string textile = p.HTML2Markup(html);
}
Extending HTML2Markup
You can use HTML2Markup for any HTML-to-something-else conversion. For this post, I will just tell you to checkout the TextileParser.cs file and mod as necessary. If there is a little interest, I will write a more in-depth post on extending HTML2Markup.
HTML2Markup is under the MIT license.
Download
The project is on GitHub: http://github.com/benogle/html2markup. It contains three Visual Studio 2008 projects: HTML2Markup, HTML2Markup.Test, and SGMLReader 1.8.2.
You will need NUnit to run the tests.
Form there, you can download just the binaries if you’d like.





