I have this custom elisp function which I use to convert a selected region or the whole buffer to syntax fontified HTML.
(let ((dir (concat temporary-file-directory
(getenv "USER") "/.htmlize/"))) ; must end with /
(make-directory dir :parents)
"Output directory for files exported by `modi/htmlize-region-to-file'.")
(defvar modi/htmlize-css-file (concat user-emacs-directory
"CSS file to be embedded in the html file created using the
(defun modi/htmlize-region-to-file (option)
"Export the selected region to an html file. If a region is not
selected, export the whole buffer.
The output file is saved to `modi/htmlize-output-directory' and its fontification
is done using `modi/htmlize-css-file'.
If OPTION is non-nil (for example, using `\\[universal-argument]' prefix), copy
the output file name to kill ring.
If OPTION is \\='(16) (using `\\[universal-argument] \\[universal-argument]' prefix),
do the above and also open the html file in the default browser."
(let ((org-html-htmlize-output-type 'css)
(fname (concat modi/htmlize-output-directory
start end html-string)
(setq start (region-beginning))
(setq end (region-end)))
(setq start (point-min))
(setq end (point-max))))
(setq html-string (org-html-htmlize-region-for-paste start end))
;; Insert the `modi/htmlize-css-file' contents in the temp buffer
(insert-file-contents modi/htmlize-css-file nil nil nil :replace)
;; Go to the beginning of the buffer and insert comments and
;; opening tags for `html', `head' and `style'. These are
;; inserted *above* the earlier inserted css code.
(insert (concat "<!-- This file is generated using the "
(insert "<html>\n<head>\n<style media=\"screen\" type=\"text/css\">\n")
;; Go to the end of the buffer (end of the css code) and
;; insert the closing tags for `style' and `head' and opening
;; tag for `body'.
;; Insert the HTML for fontified text in `html-string'.
;; Close the `body' and `html' tags.
(when (= 16 (car option))
Here are few points about the
- If the region is selected, only that region will be exported to an HTML file in
- If no region is selected, the whole buffer will be exported.
- It requires
org-mode HTML exporter) as it uses
org-html-htmlize-region-for-paste function which uses the
htmlize package behind the scenes and also allows customizing how to associate CSS with the HTML code for fontification.
- It is able to fontify code based on the css file specified by
modi/htmlize-css-file variable. To use this function right-away you can save this custom leuven theme based css file somewhere and set this variable to that file path. If you use that css file, the exported code will always have the Leuven theme regardless of your emacs theme (which was my sole purpose for writing this function).
I would recommend installing the
region-bindings-mode package. With that installed, you simply select the region you want to export and hit H... Voila! Your code will be saved to an HTML file in
(define-key region-bindings-mode-map (kbd "H") #'modi/htmlize-region-to-file))
If you do
C-u H, it will export the code and copy the output file name to the kill-ring.
- If you do
C-u C-u H instead, it will do the above and also open the HTML file it in your default browser using the
Below is what I got in my web browser when I did
C-u C-u H on part of the above code:
NOTE: I need to save the HTML to a file. Saving the HTML annotated code just to the clipboard will not work for me because I don't know how to convert html code in clipboard directly to rendered html when pasting in Outlook/Word.