views:

123

answers:

3

Is it possible to present Markdown rendered in an Emacs buffer using Emacs' own buffer text formatting capabilities? Emacs in graphical environments has rich text presentation capabilities (font styles, colors, links and even images) so it should be quite possible. Are there any existing implementations?

Note that the idea is to have the rendered Markdown be native Emacs formatted text that can be navigated and operated on as any other text in Emacs. Therefore solutions that render to an image that is embedded in an Emacs buffer are not desirable here.

Also note that this is not about a mode for editing Markdown, but for presenting rendered Markdown in an Emacs buffer. It should preferably be a pure Emacs Lisp solution for portability.

A: 

I guess you can use the source code of latex-preview for inspiration or pretty-lambda(a much simpler piece of software).

Alternatively you can convert the markdown to html in a background process and preview the html.

Everything is possible in Emacs, but not everything is easily achieved :-)

Bozhidar Batsov
Thanks for mentioning latex-preview. I would however not like to render the Markdown as an image embedded in the Emacs buffer, but as native Emacs formatted text that can be navigated and operated on as any other text. Maybe something could be learned from pretty-lambda though.
Seppo Sade
A: 

If it is only about the rendering, go with Bozhidar's suggestion and do a Markdown to HTML conversion, then display the HTML in a W3 buffer. markdown-mode has code to call the external Markdown command with a few goodies.

But if you really want to do everything within Emacs Lisp, you'll have to write a Markdown parser first.

Thomas Kappler
Thank you, but I really want to have it all in Emacs Lisp for portability. Therefore I'm hoping to find an existing Emacs Lisp Markdown parser.
Seppo Sade
+1  A: 

As I don't know about any emacs-based Markdown parser, I'd have to say that you have to code one from scratch. Maybe this SO question can throw some pointers at you.

If you opt to go through the Emacs-only road, then Semantic is an excellent API for that job (it offers you a lexer, parser-generator, and parser; it's been around for more than a decade, and it has documentation!). After having the language parser, you'll have to make some rendering functions for each token type. And this way you can customize everything.

Although this would be an enlightening trip, for sure, I'd still opt for using an existing Markdown->html converter on a separate background process and then use w3(m) for emacs preview (as Bozhidar suggested). It gets the job done and it's much simpler to do. No major performance issues, neither - you should run this tool rather scarcely, so you can easily spare some extra milliseconds).

A mixed solution would be to make the Markdown parser generate HTML directly, and preview it on a w3(m) buffer (it takes away the rendering weight from your shoulders, you only need to transliterate the markdown into html, and that seems pretty straight forward with Semantic).

Edgar
Thank you for the link to the question about parsing Markdown, and for mentioning Semantic.
Seppo Sade