For this blog and a few other uses, I regularly need some piece of source code nicely formatted in HTML. Emacs already provides a toolchain for that with the htmlfontify
package. Sadly, that package both only works on whole buffers, and always adds the same kind of huge header and footer. I need neither. I already have the CSS around, and I want to add the HTML into some other page.
So, the following command will create a temporary buffer, and insert pre
-wrapped HTML of the current region (if active) or the whole buffer (if not) there.
Simple, useful.
(defun fc-htmlfontify-buffer-or-region () "Show the current buffer or region if active as HTML in a temporary buffer. This uses `htmlfontify'." (interactive) (let ((hfy-page-footer (lambda (filename) "")) (hfy-page-header (lambda (filename stylesheet) ""))) (if (region-active-p) (let ((text (buffer-substring (region-beginning) (region-end)))) (with-temp-buffer (insert text) (switch-to-buffer (htmlfontify-buffer)))) (switch-to-buffer (htmlfontify-buffer)))))