Friday, January 4, 2008

Emacs Tip #8 : markdown

I saw a post for auto-generating HTML and thought, neat trick. My preferred email client is Gnus, which already mimics that (changing *text* into text and so forth).

But how cool would it be to have both my regular text show up, as well as an HTML version (when useful) - I already write using the syntax of markdown.

Mr. O'Connor happened to choose to do integration using Python (preferred programming language). Of course I don't grok Python, so getting it to work isn't quite as easy as running the Perl version.

So I coded up similar functionality, only I also tweaked things such that only the text before the signature line is converted into HTML.


(defun mimedown ()
(interactive)
(save-excursion
(message-goto-body)
(let* ((sig-point (save-excursion (message-goto-signature) (forward-line -1) (point)))
(orig-txt (buffer-substring-no-properties (point) sig-point)))
(shell-command-on-region (point) sig-point "Markdown.pl" nil t)
(insert "<#multipart type=alternative>\n")
(insert orig-txt)
(insert "<#part type=text/html>\n< html>\n< head>\n< title> HTML version of email</title>\n</head>\n< body>")
(exchange-point-and-mark)
(insert "\n</body>\n</html>\n<#/multipart>\n"))))

3 comments:

Edwin said...

The code sample is not complete in the view on my browser (Firefox). Why don't you post it to emacswiki.org. They have markdown mode page at http://www.emacswiki.org/cgi-bin/wiki/MarkdownMode.

Thanks

a said...

Actually, the code is complete - blogger just isn't displaying it properly. If you select the code and paste it into Emacs, it'll show up just fine.

I'm not sure what I want to do about showing code in blogger because it doesn't format well. I have toyed with the idea of putting it on emacswiki.org.

Thanks for the feedback.

a said...

I just updated the template blogger uses for this blog, which doesn't restrict the main portion to be a fixed width. This seems to help with the code display.