Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Pygmentize code for Nikola
;; 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
You can’t perform that action at this time.