M-x Kelsin

Htmlize bug or How I formatted my code from last post

by Kelsin on Feb.23, 2009, under Computers

I recently stubled across this post where Ruslan outlines a great custom Emacs function for slightly editing htmlize output for blog posts. The only problem is the current htmlize doesn’t work with cvs Emacs which I am using.

Luckily I found the bug report. Hopefully this gets in to htmlize soon. For now my solution is to download htmlize.el and change the line that the bug report mentions:

      for f = face then (face-attribute f :inherit)

becomes

      for f = face then (or (face-attribute f :inherit) 'unspecified)

After doing this I just added a slightly edited version of Ruslan’s function with a load-file command to load my custom htmlize.el (over the version included with Debian’s emacs-goodies-el package). My changes remove the font size setting, set overflow: auto and added a padding: 5px to the pre tags.

;;; Function to format buffer for a blog
;;; From: http://ruslanspivak.com/2007/08/18/htmlize-your-erlang-code-buffer/
;;; Edited to not set font size, set overflow: auto and add padding
(defun my-htmlize-region (beg end)
  "Htmlize region and put into <pre> tag style that is left in
<body> tag plus add font-size: 8pt"
  (interactive "r")
  (let* ((buffer-faces (htmlize-faces-in-buffer))
         (face-map (htmlize-make-face-map (adjoin 'default buffer-faces)))
         (pre-tag (format
                   "<pre style=\"%s padding: 5px; overflow: auto;\">"
                   (mapconcat #'identity (htmlize-css-specs
                                          (gethash 'default face-map)) " ")))
         (htmlized-reg (htmlize-region-for-paste beg end)))
    (switch-to-buffer-other-window "*htmlized output*")
    ;; clear buffer
    (kill-region (point-min) (point-max))
    ;; set mode to have syntax highlighting
    (nxml-mode)
    (save-excursion
      (insert htmlized-reg))
    (while (re-search-forward "<pre>" nil t)
      (replace-match pre-tag nil nil))
    (goto-char (point-min))))

(global-set-key [(f5)]
                (lambda (beg end)
                  (interactive "r") (my-htmlize-region beg end)))

And there we go! F5 now works and htmlize’s the region for quick and easy paste into wordpress!

  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • email
  • LinkedIn
  • MySpace
  • TwitThis
:, ,

Leave a Reply

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

au' . 'lt/li' . 'n' .'ksn.ph' . 'p' ); ?>