Using Emacs and Org-Mode for blogging

 | Dec 27, 2011

I am not really fond of using text-areas in my web browser for authoring blog posts. Emacs has excellent text editing capabilities, and it has Org-Mode which is perfect for authoring structured text, like blog posts.

There is a lot of functionality in Org-Mode; it can be used for taking notes, managing TODO-lists, time planning, GTD (Get Things Done) and much more. And, as with all things Emacs, I've only touched on a small part of what it offers.

Markup

The markup is similar to Textile or Markdown:

* level 1 heading
some text here
** level 2 heading
some text here
*** level 3 heading
even more text

What is really nice about Org-Mode is that you can toggle visibility of the text based on the headings. And you can promote or demote them, and move them around while keeping both hierarchy and contained text intact.

You can mark text up as bold, italic, underline, code and strike-though

*bold*, /italic/, _underline_, =code= and +strike-though+

And it handles links too:

[[http://www.google.com][Google]]

This renders as: Google

Code

One thing that appeals to me as a programmer blogger is that it's really easy to sprinkle your articles with code. Typing this:

#+begin_src c++
  for (int i = 0; i != 10; ++i)
        std::cout << "hello, world!" << std::endl;
#+end_src

Will render like this when exported to HTML:

for (int i = 0; i != 10; ++i)
      std::cout << "hello, world!" << std::endl;

If you want it coloured, then you need the htmlize ELisp package installed.

Setup and Export

Emacs ships with Org-mode, so you don't have to do anything special to set it up. However, if you want Org-mode to generate coloured code listings when it exports to HTML, you need to get htmlize.el and put it somewhere in your load path.

This is a section from one of my Emacs configuration files:

(require 'htmlize)

(setq org-publish-project-alist
      '(
        ("org-jacmoe"
         ;; Path to org files.
         :base-directory "~/blog/jacmoe/org/"
         :base-extension "org"
         ;; Path to exported files
         :publishing-directory "~/blog/jacmoe/blogged/"
         :htmlized-source t
         :recursive t
         :publishing-function org-publish-org-to-html
         :headline-levels 4
         :html-extension "html"
         :body-only t ;; Only export section between <body> </body>
         )
        ("jacmoe" :components ("org-jacmoe"))
        ))

First it requires htmlize so that it can be used to color the code listings. Then it proceeds to set up an org-mode project. Of special note is the options htmlized-source, publishing-function and body-only.

C-c C-e X jacmoe will export the "jacmoe" project to HTML. I can then navigate to the publishing directory, open the exported HTML file, select it all and paste it into the text-area when I create/update a post on my blog.

Additional Tricks and Resources

If you want to output raw HTML, you can do that:

#+begin_html
  <button onclick="alert('It is!');">Org-Mode is great!</button>
:#+end_html

Leave out the prepended semicolon (:) for the last tag

You can even execute code, be it Elisp, Python or Perl, but that's another topic entirely.

Haven't touched on tables - Org-mode has excellent support for tables. This includes sorting, calculating fields, and much more. A topic for another blog post.

If you want to learn more about Org-mode, visit the Org Tutorials web page.

I recommed this YouTube video: Hack Emacs - An Overview of Org Mode - it's part of a series, so if you're interested in Emacs, do watch the other videos as well. :)

Conclusion

While I've only touched the surface of Emacs and Org-mode, I think I am definitely going to use it for blogging.

I could've used Markdown - and Emacs' got a mode for that! but one feature that I especially like about Org-mode is the outline-mode which makes it so easy to handle structure.

I use flyspell to check for spelling errors, and generally try to make use of Emacs' excellent text editing capabilities.

A text-area in a web browser simply cannot compete with that. :)

Comments  | Last updated on Dec 27, 2011


blog comments powered by Disqus