Using Emacs and Muse to publish OCaml code

In Uncategorized on November 10, 2009 at 12:55 pm

I think I’ve got it now. In this post, I’ll look at using Emacs and Muse to publish blog entries containing OCaml soure code.

Much of the following Emacs configuration is derived from this blog post.

First, include Muse:

(require 'muse-mode)
(require 'muse-html)
(require 'muse-colors)

Next, do some Muse configuration:

;; Needed??
(add-to-list 'auto-mode-alist '("\\.muse$" . muse-mode))
(add-to-list 'file-coding-system-alist (cons "\\.muse$" 'utf-8))

(defun ali/muse-mode-hook ()
  (longlines-mode 1)
  (filladapt-mode 0)
  (flyspell-mode 1))

(add-hook 'muse-mode-hook 'ali/muse-mode-hook)

I did some Muse tweaking to make the published source code look pretty.

(defun ali-regex-replace (regex string)
  (goto-char (point-min))
  (while (re-search-forward regex nil t)
    (replace-match string)))

;; The current WordPress theme has the <pre> font size set too small,
;; hence the change to font-size below.

(defconst ali-wordpress-src-prefix
  (concat "<pre style=\"font-size: medium; border: 1px solid #bbb; "
          "background: #eee; overflow: auto; "
          "margin: 15px 5px; padding: 5px;\">"))

(defconst ali-wordpress-example-prefix

(defconst ali-wordpress-code-prefix
  (concat "<code style=\"font-size: medium; "
          "background: #eee; padding: 3px;\">"))

;; I have to unquote `ali-wordpress-example-prefix' below, otherwise I
;; get a Muse error.
(defconst ali-muse-wordpress-strings
  `((begin-literal   . ,ali-wordpress-code-prefix)
    (end-literal     . "</code>")
    (begin-example   . ,ali-wordpress-example-prefix)
    (end-example     . "</pre>")
    (wordpress-more  . "<!--more-->")))

;; <more>
(defun ali-muse-wordpress-publish-more-tag (beg end)
  (delete-region beg end)
  (muse-insert-markup (muse-markup-text 'wordpress-more)))

;; Extra tags I use with WordPress.
;; See muse.el, search for "br" to find how it is specified.
(defconst ali-muse-wordpress-tags
  '(("more" nil nil nil ali-muse-wordpress-publish-more-tag)))

;; No direct access to how code is marked up. So instead of using
;; `ali-muse-wordpress-strings', we have to do a regex replace here.
(defun ali-muse-wordpress-htmlize ()
  (ali-regex-replace "<pre class=\"src\">" ali-wordpress-src-prefix))

(muse-derive-style "html-wordpress" "html"
                   :strings 'ali-muse-wordpress-strings
                   :after 'ali-muse-wordpress-htmlize
                   :tags 'ali-muse-wordpress-tags)

(defun ali-muse-publish ()
  (muse-publish-file (buffer-file-name) "html-wordpress")
  (muse-browse-result "html-wordpress"))

(define-key muse-mode-map [f7] 'ali-muse-publish)

With all that set up, I can begin publishing some OCaml code as such:

The following code shows the =hello= function, which prints the string ="Hello, World!"= to standard output.

<src lang="tuareg">
let hello () =
  print_endline "Hello, World!"

The following code shows the hello function, which prints the string "Hello, World!" to standard output.

let hello () =
  print_endline "Hello, World!"

I had to use tuareg above instead of ocaml because of a Muse oddity. It will expect to call ocaml-mode to fontify the source code, but I use Tuareg.

To preview a post, I use the ali-muse-publish function, bound to the F7 key. When I’m confident that I want to post to WordPress, I visit the file in Emacs, copy the HTML, and paste in the WordPress HTML editor.

In the future, I may create an alias that copies HTML directly to the clipboard, or better yet, I could use weblogger.el to post directly. But for now, I’m happy with my setup.

%d bloggers like this: