views:

71

answers:

1

Does anyone know of a lightly-marked-up-text to styled-text formatting library (ie. something like Markdown# or Textile.NET), but which produces a native XAML document (or rather, a FlowDocument model or similar that can be displayed directly in a WPF app), to avoid the use of a WebBrowser?

Bonus points for something lightweight. I'm hoping for something that will tolerate very frequent updates in the source text.

Alternatively, is there a lightweight HTML rendering control that can be used in WPF? (I don't consider the standard WebBrowser to be lightweight.)

+1  A: 

I don't know of such a library pre-built, but I do have some thoughts for you that may be helpful.

The first big question in my mind is why you want to use something primitive like Markdown when you could be using RichTextBox. Markdown is required for StackOverflow and similar sites because of the limitations of the browser. But if your app is WPF this is not an issue.

One guess as to why you might want to do this is that you want your documents to be editable both in WPF and in a lowest-common-denominator web application. In that case you will need an engine that renders the markdown to HTML anyway, so why not leverage that same engine to convert the markdown to XAML?

Converting arbitrary HTML to XAML is very difficult, but converting the sort of HTML that a Markdown converter would spit out is another matter entirely. Most Markdown-style converters spit out only a few simple HTML tags, all of which are trivially convertible to equivalent XAML.

If you use an Markdown-to-HTML converter it will have done all of the really heavy lifting for you (parsing the text, etc) and left you with an XML-like document (HTML to be precise) that is relatively easy to parse. Also, if you are using the Markdown-to-HTML converter elsewhere you will have confidence that your Markdown parser will parse your Markdown syntax exactly the same for both HTML and XAML use because it will be the same parser in each case.

So basically what I am thinking is:

string html = MarkdownEngine.MarkdownToHtml(markdown);
string xaml = MarkdownHtmlToXamlTranslator.HtmlToXaml(html);

Where you design your implementation of MarkdownHtmlToXamlTranslator around whatever the markdown engine actually spits out. It could be a very simple XSLT, or you could use LINQ to XML along with XDocument construction techniques. Either way it should be a very small bit of code.

Ray Burns
Apologies for the delay; I was distracted by another project.The main reason for wanting to use Markdown or similar is that the source data basically arrives as a big string (no chance to use the RichTextBox object model for formatting), and I'd like it to be fairly readable (which RTF often isn't), although that isn't a top-priority requirement.In terms of supported features, at present it's mostly just minor text style changes (including colour), plus bullet lists (to arbitrary nesting level) and tables.
Miral
Given those requirements, I think you should seriously consider using a XAML FlowDocument directly: 1. FlowDocument is just a big string, 2. It is dramatically more readable than RTF, and 3. No conversion is required to use it with RichTextBox. Unless I'm still misunderstanding one of your requirements, this seems the obvious way to go. For example, your "big string" could be `"This is <Bold>very</Bold> easy!"`, which RichTextBox will handle directly. If the extra shortcuts such as `**` used in markdown are important, they could be added using a simple preprocessor.
Ray Burns