Htmlize your Erlang code buffer
Ruslan Spivak - - August 19, 2007Recently I’ve begun to make blog posts and now want to have highlighting of Erlang’s code(actually any code , be it Python, Lisp, etc) in my posts. I’m using The One True Editor and it goes without saying that i want to convert my erlang code into html from within emacs to have corresponding syntax coloring. Htmlize package to the rescue!
After you downloaded package and put
(add-to-list 'load-path "path_to_your_htmlize.el") (require 'htmlize)
into your dot emacs – you are ready to use it.
M-x htmlize-buffer or M-x htmlize-region will convert whole buffer or selected region with associated decorations into HTML( See C-h a htmlize RET for more functions). By default htmlize will make HTML output in css mode which is not very suitable for inserting into blog post as output includes also style sheets. Excerpt from htmlize documentation:
;; htmlize supports three types of HTML output, selected by setting ;; `htmlize-output-type': `css', `inline-css', and `font'. In `css' ;; mode, htmlize uses cascading style sheets to specify colors; it ;; generates classes that correspond to Emacs faces and uses <span ;; class=FACE>...</span> to color parts of text. In this mode, the ;; produced HTML is valid under the 4.01 strict DTD, as confirmed by ;; the W3C validator. `inline-css' is like `css', except the CSS is ;; put directly in the STYLE attribute of the SPAN element, making it ;; possible to paste the generated HTML to other documents. In `font' ;; mode, htmlize uses <font color="...">...</font> to colorize HTML, ;; which is not standard-compliant, but works better in older ;; browsers. `css' mode is the default.
inline-css is the type of output i want. This can be achieved by customizing htmlize-output-type variable with M-x customize htmlize RET or directly modifying dot emacs. Converting:
(add-to-list 'load-path "path_to_your_htmlize.el") (require 'htmlize)
with htmlize-region and then applying nxml-mode to produced result gives:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"> <!-- Created by htmlize-1.34 in inline-css mode. --> <html> <head> <title>.emacs</title> </head> <body style="color: #ffffff; background-color: #000000;"> <pre> (add-to-list 'load-path <span style="color: #ffa07a;">"path_to_your_htmlize.el"</span>) (<span style="color: #00ffff;">require</span> '<span style="color: #7fffd4;">htmlize</span>) </pre> </body> </html>
As you see I need to cut contents between <pre> </pre> including
<pre> tags itself to insert that then directly into blog post. After that i need manually copy/paste styles from <body> tag into <pre> tag and also add font-size: 8pt to meet my taste. Being lazy i’ve implemented function my-htmlize-region in elisp to make that job for me every time i press F5 key:
(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 font-size: 8pt">" (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)))
So now selecting:
(add-to-list 'load-path "path_to_your_htmlize.el") (require 'htmlize)
and pressing F5 i get this in another buffer:
<pre style="color: #ffffff; background-color: #000000; font-size: 8pt"> (add-to-list 'load-path <span style="color: #ffa07a;">"path_to_your_htmlize.el"</span>) (<span style="color: #00ffff;">require</span> '<span style="color: #7fffd4;">htmlize</span>) </pre>
As you see it’s just what i want:
<pre> tags with content and <pre> tag contains style from above mentioned <body> tag plus font-size is added too.
Factorial in Erlang with syntax coloring as i have it in my Emacs:
-module(factorial). -export([factorial/1, factorial_rec/1]). %% tail recursion factorial(Num) -> factorial(Num, 1). factorial(1, Acc) -> Acc; factorial(Num, Acc) -> factorial(Num-1, Num * Acc). %% recursion factorial_rec(1) -> 1; factorial_rec(Num) -> Num * factorial_rec(Num-1).
This is a joy of using Emacs.
Categories: Blogs Ruslan Spivak
Erlang on Twitter
» TrainByTweet_VO (TrainByTweet_VO): VOIP: Blocking Probability: Now we can let Erlang tell us the probability of blocking
» soErlang (Katarzyna Kraus): RT @get_cookie: Erlang Factory is coming. http://t.co/cDvHMK0bW8
» KillerAKG (赤木文彦): RT @a2zmax: 「プログラミング初心者です。Erlangやってます」
» JoelJacobson (Joel Jacobson): I’m hosting the Big Data, Big Databases and Next Generation Analytics track at the Erlang user conf in Stockholm - http://t.co/8xoyN1qzTX
» Muh_Erlang (M. Erlangga Pangestu): @ofcPOETRICH liat ftonya :D yg aku ilang ngik ckckck
» David_Kei01 (David Kei): 最近对 #Io 和 #Erlang 比较感兴趣,下一阶段会抽出时间看看。
» ofcPOETRICH (CH): liat2 foto pas tk.. eh ado foto rame2 dgn bu ujik belakang aku si @Muh_Erlang hahaha
» Muh_Erlang (M. Erlangga Pangestu): @DRidanvima aponyo yg bruntung?
» irascibleme (Irascible Me): @SteveGodbold Exponentiial describes time between poisson arrivals. Erlang describes poisson arrivals in a period. #statsfacts
» andriyan_erlang (Andry Erlangga): sepuluh juta buat hubungan…..
Statistics
Number of aggregated posts: 10650
Most recent article: May 20, 2013
Latest comments
» Moraru on This is Why You Spent All that Time Learning to Program: It is true that computer science was a pain in the back at time that i’ve had to learn it…
» Commercial hand dryers on Couchbase Meetup at new HQ: Buy online from here where you will get so much of variety in Commercial hand dryers for people. If you…
» Fort McMurray Homes on Motivated Reasoning and Erlang vs Python vs Node: I don’t really understand why this post is motivational? I don’t even see a post, just a title. Fort McMurray…