I'm using the org-mode reader for Pelican to generate articles for my blog (Export to HTML). Code blocks in Markdown version of the article look perfect but I cannot enable Pygments for org-mode.

Is there any way to process org-mode code blocks using Pygments?

P.S I know about Pandoc but I need a more emacs-way solution to be able to customize export options using Emacs Lisp, not Haskell.

up vote 4 down vote accepted

I've found the solution to my issue and described full explanation in my blog.

In a few words, I've created custom export backend and pass the block contents to an external script.

(require 'org)
(require 'ox)
(require 'ox-html)

;; Path for pygments or command name
(defvar pygments-path "pygmentize")

(defun pygments-org-html-code (code contents info)
  ;; Generating tmp file path.
  ;; Current date and time hash will ideally pass our needs.
  (setq temp-source-file (format "/tmp/pygmentize-%s.txt"(md5 (current-time-string))))
  ;; Writing block contents to the file.
  (with-temp-file temp-source-file (insert (org-element-property :value code)))
  ;; Exectuing the shell-command an reading an output
  (shell-command-to-string (format "%s -l \"%s\" -f html %s"
                   pygments-path
                   (or (org-element-property :language code)
                       "")
                   temp-source-file)))

(org-export-define-derived-backend 'pelican-html 'html
  :translate-alist '((src-block .  pygments-org-html-code)
             (example-block . pygments-org-html-code)))

You can use these org reader variables to load the emacs functions in pelican:

ORG_READER_EMACS_SETTINGS = os.path.abspath('lisp/config.el')
ORG_READER_BACKEND = "'pelican-html"
  • Can you post the code here, please? – Dan May 15 '17 at 14:35

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.