Created
November 9, 2014 15:12
-
-
Save punchagan/8a77b6583340d3920b3d to your computer and use it in GitHub Desktop.
Pygmentize code for Nikola
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; Use pygments highlighting for code | |
(defun pygmentize (lang code) | |
"Use Pygments to highlight the given code and return the output" | |
(with-temp-buffer | |
(insert code) | |
(let ((lang (or (cdr (assoc lang org-pygments-language-alist)) "text"))) | |
;; A crazy hack!! Why src-block have no info?!@!@# | |
(let* ((file-name (format "%s.png" (sha1 code))) | |
(output-path (format "%s" file-name)) | |
(url (format "%s" file-name))) | |
(shell-command-on-region | |
(point-min) (point-max) | |
(format "pygmentize -f png -O font_name=UbuntuMono,line_numbers=False -o %s -l %s" output-path lang)) | |
(format "<img src=\"%s\" >" url))))) | |
(defconst org-pygments-language-alist | |
'( | |
("asymptote" . "asymptote") | |
("awk" . "awk") | |
("C" . "c") | |
("cpp" . "cpp") | |
("clojure" . "clojure") | |
("css" . "css") | |
("D" . "d") | |
("ditaa" . "image") | |
("emacs-lisp" . "scheme") | |
("F90" . "fortran") | |
("gnuplot" . "gnuplot") | |
("groovy" . "groovy") | |
("haskell" . "haskell") | |
("java" . "java") | |
("js" . "js") | |
("julia" . "julia") | |
("latex" . "latex") | |
("lisp" . "lisp") | |
("makefile" . "makefile") | |
("matlab" . "matlab") | |
("mscgen" . "mscgen") | |
("ocaml" . "ocaml") | |
("octave" . "octave") | |
("perl" . "perl") | |
("picolisp" . "scheme") | |
("python" . "python") | |
("R" . "r") | |
("ruby" . "ruby") | |
("sass" . "sass") | |
("scala" . "scala") | |
("scheme" . "scheme") | |
("sh" . "sh") | |
("sql" . "sql") | |
("sqlite" . "sqlite3") | |
("tcl" . "tcl") | |
) | |
"Alist between org-babel languages and Pygments lexers. | |
See: http://orgmode.org/worg/org-contrib/babel/languages.html and | |
http://pygments.org/docs/lexers/ for adding new languages to the | |
mapping. ") | |
;; Override the html export function to use pygments | |
(defun org-html-src-block (src-block contents info) | |
"Transcode a SRC-BLOCK element from Org to HTML. | |
CONTENTS holds the contents of the item. INFO is a plist holding | |
contextual information." | |
(if (org-export-read-attribute :attr_html src-block :textarea) | |
(org-html--textarea-block src-block) | |
(let ((lang (org-element-property :language src-block)) | |
(code (car (org-export-unravel-code element)))) | |
(pygmentize lang code)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment