Converting Markdown in Emacs via Pandoc

Sorry for the lack of updates, as I’ve been ill lately. I’ve been meaning to write about what it’s like programming with the specific disease I deal with, but that’ll be for another day. Instead tonight I want to share a simple Emacs tip.

I like to write documents in Markdown, which I’ve said multiple times on this site. This whole blog is in Markdown. I find it easy to read in plain text, easy to write, and easy to convert to many formats using the great program Pandoc.

For Emacs I use markdown-mode by Jason Blevins. And I wrote this Emacs lisp function to convert a Markdown document to another format using Pandoc:

(defun convert-markdown-to (newtype)
  "Converts the current buffer, assumed to be in Markdown format,
into a new format.  The new format must be one acceptable to
`Pandoc'.  The function opens the new file in a buffer if called
interactively.  If not called interactively then it returns the
name of the new file."
  (interactive "sOutput: ")
  (let ((current-document (buffer-file-name))
        (temp-filename (concat "/tmp/output." newtype)))
    (with-temp-file temp-filename
      (call-process-shell-command (concat "pandoc -f markdown -t " newtype)
                                  nil t nil current-document))
    (cond ((called-interactively-p 'any)
           (with-current-buffer (find-file temp-filename)))
          (t temp-filename))))

Which I bind to a key like so:

(define-key markdown-mode-map (kbd "C-c c") 'convert-markdown-to)

If you are not using Linux then you will probably want to change the path for temp-filename in the code. But if you’re a heavy Markdown user like myself then I hope you find this useful. And check out Pandoc even if you don’t use Markdown, because that is a terrific tool for documents in general.


Add Your Thoughts

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s