Function: org-html-fontify-code

org-html-fontify-code is a byte-compiled function defined in ox-html.el.gz.

Signature

(org-html-fontify-code CODE LANG)

Documentation

Color CODE with htmlize library.

CODE is a string representing the source code to colorize. LANG is the language used for CODE, as a string, or nil.

Source Code

;; Defined in /usr/src/emacs/lisp/org/ox-html.el.gz
;;;; Src Code

(defun org-html-fontify-code (code lang)
  "Color CODE with htmlize library.
CODE is a string representing the source code to colorize.  LANG
is the language used for CODE, as a string, or nil."
  (when code
    (cond
     ;; No language.  Possibly an example block.
     ((not lang) (org-html-encode-plain-text code))
     ;; Plain text explicitly set.
     ((not org-html-htmlize-output-type) (org-html-encode-plain-text code))
     ;; No htmlize library or an inferior version of htmlize.
     ((not (progn (require 'htmlize nil t)
		(fboundp 'htmlize-region-for-paste)))
      ;; Emit a warning.
      (warn "Cannot fontify source block (htmlize.el >= 1.34 required).  Falling back to plain text.  (see `org-html-htmlize-output-type')")
      (org-html-encode-plain-text code))
     (t
      ;; Map language
      (setq lang (or (assoc-default lang org-src-lang-modes) lang))
      (let* ((lang-mode (and lang (intern (format "%s-mode" lang)))))
	(cond
	 ;; Case 1: Language is not associated with any Emacs mode
	 ((not (functionp lang-mode))
	  (org-html-encode-plain-text code))
	 ;; Case 2: Default.  Fontify code.
	 (t
	  ;; htmlize
	  (setq code
		(let ((output-type org-html-htmlize-output-type)
		      (font-prefix org-html-htmlize-font-prefix)
		      (inhibit-read-only t))
		  (with-temp-buffer
		    ;; Switch to language-specific mode.
		    (funcall lang-mode)
		    (insert code)
		    ;; Fontify buffer.
                    (font-lock-ensure)
		    ;; Remove formatting on newline characters.
		    (save-excursion
		      (let ((beg (point-min))
			    (end (point-max)))
			(goto-char beg)
			(while (progn (end-of-line) (< (point) end))
			  (put-text-property (point) (1+ (point)) 'face nil)
			  (forward-char 1))))
		    (org-src-mode)
		    (set-buffer-modified-p nil)
		    ;; Htmlize region.
		    (let ((org-html-htmlize-output-type output-type)
			  (org-html-htmlize-font-prefix font-prefix))
		      (org-html-htmlize-region-for-paste
		       (point-min) (point-max))))))
	  ;; Strip any enclosing <pre></pre> tags.
	  (let* ((beg (and (string-match "\\`<pre[^>]*>\n?" code) (match-end 0)))
		 (end (and beg (string-match "</pre>\\'" code))))
	    (if (and beg end) (substring code beg end) code)))))))))