Customizing Emacs Muse – Part 3
This is part 3 in the customizing emacs muse series.
In part 1 (Creating Squidoo Lenses With Emacs Muse) we talked about adding hooks to transform the generated HTML to cope with the impoverished tag set that Squidoo provides.
In part 2 (Updating Elements Within Association Lists) we talked about adding a function that can replace values in association lists to manipulate muse-html-markup-strings.
In this post, we will talk about how to select between different output styles if you’re writing for, say, a wordpress.com blog, an article directory and squidoo.
Note: muse already allows for different styles, but I’m not aware of a sub-style mechanism which is what I am really describing here.
Let’s get started. First of all, it is handy to have a mechanism for getting back to the default state. I provide a variable to store a function that will undo the changes that made the current sub-style. Returning to the default is just a matter of calling this function if it is defined.
(defvar muse-style-remove-fn nil) (defun muse-style-default () (when muse-style-remove-fn (funcall muse-style-remove-fn)) (setq muse-style-remove-fn nil))
muse-style-select enables me to choose between the various styles using ido. You may recognise this selection mechanism from my directory aliases post.
(defconst muse-style-mapping (list (cons "squidoo" 'muse-style-squidoo-enable) (cons "curious" 'muse-style-curious-enable) (cons "default" 'muse-style-default))) (defconst muse-style-aliases (mapcar (lambda (e) (car e)) muse-style-mapping)) (defun muse-style-select (&optional style) (interactive) (unless style (setq style (ido-completing-read "Muse Style: " muse-style-aliases nil t))) (if (and (stringp style) (> (length style) 0 )) (let ((pair (assoc style muse-style-mapping))) (if pair (funcall (cdr pair)) (error "Invalid style %s" style))) (error "Invalid style %s" style)))
I have a helper function to replace the keys within muse-html-markup-strings.
(defun muse-strings-replace (mapping) (setq muse-html-markup-strings (assoc-replace muse-html-markup-strings mapping)))
The disable function resets begin-literal and end-literal to the original state. Possibly I should have copied the entire original associative list instead. It also removes the hooks which have been added.
(defun muse-style-squidoo-disable () (interactive) (muse-strings-replace (list (kv 'begin-literal "<code>") (kv 'end-literal "</code>"))) (remove-hook 'muse-html-after-htmlize-hook 'squidoo-htmlize) (remove-hook 'muse-after-publish-hook 'squidoo-fix-para))
The enable function returns us to the default style first, just in case there is already a sub-style selected. It then overwrites begin-literal and end-literal and adds the hooks.
(defun muse-style-squidoo-enable () (interactive) (muse-style-default) (when muse-style-remove-fn (funcall muse-style-remove-fn)) (setq muse-style-remove-fn 'muse-style-squidoo-disable) (muse-strings-replace (list (kv 'begin-literal sq-code-prefix) (kv 'end-literal "</b>"))) (add-hook 'muse-html-after-htmlize-hook 'squidoo-htmlize) (add-hook 'muse-after-publish-hook 'squidoo-fix-para))
Muse does allow you to derive new styles from existing ones. In this case, your new style inherits all the variables and functions from the parent style, and you can override them for customization.
I use this technique all the time, e.g. different styles use different LaTeX headers and footers. It also very clean, a new style with a different LaTeX header is only 1 line of elisp.
Hi Peter,
Would you like to share that 1 line of elisp with my readers and I?
Thanks
Here is how I create a new LaTeX based style, but with a different header than the default:
(muse-derive-style “pdf-logo-article” “pdf”
:header “/path/to/file”)
I do the same trick for HTML:
(muse-derive-style “printable-xhtml” “xhtml”
:header ‘pmade-muse-html-header)
Sorry I didn’t include this in my first comment.
No worries Peter
Since your comment I’ve been taking a look through the documentation and examples. Is there a definitive list of keyword parameters that muse-derive-style takes?
Yes, take a look at the Muse manual, see section 10.5 “Parameters used for defining styles”.
Oops, looks like I need to learn to read properly. Cheers Peter