forked from renard/o-blog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
o-blog-source.el
109 lines (88 loc) · 3.16 KB
/
o-blog-source.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
;;; 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: http://sam.zoy.org/wtfpl/
;; This file is NOT part of GNU Emacs.
;;; Commentary:
;;
;;; Code:
(eval-when-compile
(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> %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
`o-blog-source-header'."
:type 'string
:group 'o-blog)
(defcustom o-blog-source-footer
"</div></div>"
"HTML fragment header to be used when publishing an source
using `o-blog-publish-source' as it. This closes
`o-blog-source-header'."
:type 'string
:group 'o-blog)
;;;###autoload
(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
#+BEGIN_HTML
<div class=\"o-blog-source\">
<div class=\"title\">file</div>
<div style=\"display:none;\" class=\"content\">
#+END_HTML
#+BEGIN_SRC mode
(file content)
#+END_SRC
#+BEGIN_HTML
</div></div>
#+END_HTML
The default replacement text could be changed using variables
`o-blog-source-header' and `o-blog-source-footer'."
(save-match-data
(save-excursion
(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)))
(beginning-of-line)
(delete-region (point) (point-at-eol))
(insert
"#+BEGIN_HTML\n"
(format o-blog-source-header
src-file-safe src-file-name src-file-safe
src-file-name)
(with-temp-buffer
(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)))
(set-auto-mode))
;; Unfortunately rainbow-delimiter-mode does not work fine.
;; See https://github.com/jlr/rainbow-delimiters/issues/5
(font-lock-fontify-buffer)
(htmlize-region-for-paste (point-min) (point-max)))
o-blog-source-footer
"\n#+END_HTML\n")))))))
(add-to-list
'org-structure-template-alist
'("os" "#+o_blog_source ?\n"))
(add-hook 'o-blog-html-plugins-hook 'o-blog-publish-source)
(provide 'o-blog-source)