o-blog / o-blog-source.el

;;; o-blog-source.el --- Publish source in o-blog

;; Copyright © 2012 Sébastien Gross <seb•ɑƬ•chezwam•ɖɵʈ•org>

;; Author: Sébastien Gross <seb•ɑƬ•chezwam•ɖɵʈ•org>
;; Keywords: emacs, 
;; Created: 2012-01-23
;; Last changed: 2012-03-20 13:29:09
;; Licence: WTFPL, grab your copy here:

;; This file is NOT part of GNU Emacs.

;;; Commentary:

;;; Code:

  (require 'org nil t))

(defcustom o-blog-source-header
  (concat "<div class=\"o-blog-source\"><a class=\"btn btn-info\" data-toggle=\"modal\" data-target=\"#%s\" ><i class=\"icon-file icon-white\"></i>&nbsp;%s</a></div>"
	  "<div class=\"modal fade hide\" id=\"%s\"><div class=\"modal-header\"><a class=\"close\" data-dismiss=\"modal\">×</a><h3>%s</h3></div><div class=\"modal-body\">")
  "HTML fragment header to be used when publishing an source
using `o-blog-publish-source' using `format' with source
type as parameter. The source should be closed with
  :type 'string
  :group 'o-blog)

(defcustom o-blog-source-footer
  "HTML fragment header to be used when publishing an source
using `o-blog-publish-source' as it. This closes
  :type 'string
  :group 'o-blog)

(defun o-blog-publish-source ()
  "Publish an sourced file in HTML mode.

A source file is defined using:

    #+O_BLOG_SOURCE: path/to/file [mode]

and is converted to 

    <div class=\"o-blog-source\">
    <div class=\"title\">file</div>
    <div style=\"display:none;\" class=\"content\">
    #+BEGIN_SRC mode
    (file content)

The default replacement text could be changed using variables
`o-blog-source-header' and `o-blog-source-footer'."
      (goto-char (point-min))
      (let ((case-fold-search t))
	(while (re-search-forward
		"^#\\+O_BLOG_SOURCE:?[ \t]+\\(.+?\\)\\([ \t]+\\(.+\\)\\)?$"
		nil t)
	  (let* ((src-file (match-string 1))
		 (src-file-name (file-name-nondirectory src-file))
		 (src-file-safe (ob:sanitize-string src-file-name))
		 (mode (match-string 3)))

	    (delete-region (point) (point-at-eol))

	     (format o-blog-source-header
		     src-file-safe src-file-name src-file-safe
	       (insert-file-contents src-file)
	       (if mode
		   (let ((func (intern (format "%s-mode" mode))))
		     (if (functionp func)
			 (funcall func)
		       (warn (concat "Mode %s not found for %s. "
				     "Consider installing it. "
				     "No syntax highlight would be bone this time.")
			     mode src-file)))
	       ;; Unfortunately rainbow-delimiter-mode does not work fine.
	       ;; See
	       (htmlize-region-for-paste (point-min) (point-max)))

 '("os" "#+o_blog_source ?\n"))
(add-hook 'o-blog-html-plugins-hook 'o-blog-publish-source)

(provide 'o-blog-source)