2009-06-26

Markdown from Emacs to Html (Blogger)

I use emacs to write blog posts for blogger. I write in the markdown syntax, then convert it to html using markdown (markdown mode for emacs, which invokes the perl markdown command).

However, when writing I have hard breaks and 80 column width to make it readable, markdown converts a paragraph to a paragraph tag, but does not remove the line breaks. When inserted into the blogger ("Edit html") it looks crap, random line breaks all over.

I tried to use pandoc, a great alternative to markdown, and it removes the line breaks as it should, but is then too clever for its own good. It makes tags that are syntactically correct but dont work in blogger, because the end tags have a newline embede in its whitespace. Like this:

<atag>helloo</atag
>

To fix this, I use a regular expression replace in Emacs. It is unfortunate that Emacs doesn't have Perl syntax for its regular expressions, but I was glad it hinted to me what I should do. When I wrote \n it suggested what to do:

"Note: \n here doesn't match a newline, try C-q C-j instead"

In the end I made a simple Emacs command for this and put it in my .emacs file. Run with M-x better-markdown. Happy ending!

;; Makes better formatting in a blog
(defun markdown-better ()
(interactive)
(save-excursion
(let ((buffer "*markdown-better*")
(pandoc-strange-end-of-tag "
*>")) ; This is Newline, space, *, >
(shell-command-on-region (point-min)
(point-max)
"pandoc"
buffer)
(switch-to-buffer buffer)
(replace-regexp pandoc-strange-end-of-tag ">"))))

No comments:

Post a Comment