Add 2010 to copyright years.
[bpt/emacs.git] / lisp / org / org-exp-blocks.el
CommitLineData
c8d0cf5c
CD
1;;; org-exp-blocks.el --- pre-process blocks when exporting org files
2
114f9c96 3;; Copyright (C) 2009, 2010
c8d0cf5c
CD
4;; Free Software Foundation, Inc.
5
6;; Author: Eric Schulte
7
8;; This file is part of GNU Emacs.
9;;
10;; GNU Emacs is free software: you can redistribute it and/or modify
11;; it under the terms of the GNU General Public License as published by
12;; the Free Software Foundation, either version 3 of the License, or
13;; (at your option) any later version.
14
15;; GNU Emacs is distributed in the hope that it will be useful,
16;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18;; GNU General Public License for more details.
19
20;; You should have received a copy of the GNU General Public License
21;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
22
23;;; Commentary:
24;;
25;; This is a utility for pre-processing blocks in org files before
26;; export using the `org-export-preprocess-hook'. It can be used for
27;; exporting new types of blocks from org-mode files and also for
28;; changing the default export behavior of existing org-mode blocks.
29;; The `org-export-blocks' and `org-export-interblocks' variables can
30;; be used to control how blocks and the spaces between blocks
31;; respectively are processed upon export.
32;;
33;; The type of a block is defined as the string following =#+begin_=,
34;; so for example the following block would be of type ditaa. Note
35;; that both upper or lower case are allowed in =#+BEGIN_= and
36;; =#+END_=.
37;;
38;; #+begin_ditaa blue.png -r -S
39;; +---------+
40;; | cBLU |
41;; | |
42;; | +----+
43;; | |cPNK|
44;; | | |
45;; +----+----+
46;; #+end_ditaa
47;;
48;;; Currently Implemented Block Types
49;;
50;; ditaa :: Convert ascii pictures to actual images using ditaa
51;; http://ditaa.sourceforge.net/. To use this set
52;; `org-ditaa-jar-path' to the path to ditaa.jar on your
53;; system (should be set automatically in most cases) .
54;;
55;; dot :: Convert graphs defined using the dot graphing language to
56;; images using the dot utility. For information on dot see
57;; http://www.graphviz.org/
58;;
59;; comment :: Wrap comments with titles and author information, in
60;; their own divs with author-specific ids allowing for css
61;; coloring of comments based on the author.
62;;
c8d0cf5c
CD
63;;; Adding new blocks
64;;
65;; When adding a new block type first define a formatting function
66;; along the same lines as `org-export-blocks-format-dot' and then use
67;; `org-export-blocks-add-block' to add your block type to
68;; `org-export-blocks'.
69
70(eval-when-compile
71 (require 'cl))
72(require 'org)
73
c8d0cf5c
CD
74(defvar htmlp)
75(defvar latexp)
76(defvar docbookp)
77(defvar asciip)
78
c8d0cf5c
CD
79(defun org-export-blocks-set (var value)
80 "Set the value of `org-export-blocks' and install fontification."
81 (set var value)
82 (mapc (lambda (spec)
83 (if (nth 2 spec)
84 (setq org-protecting-blocks
85 (delete (symbol-name (car spec))
86 org-protecting-blocks))
87 (add-to-list 'org-protecting-blocks
88 (symbol-name (car spec)))))
89 value))
90
91(defcustom org-export-blocks
92 '((comment org-export-blocks-format-comment t)
93 (ditaa org-export-blocks-format-ditaa nil)
8bfe682a 94 (dot org-export-blocks-format-dot nil))
c8d0cf5c
CD
95 "Use this a-list to associate block types with block exporting
96functions. The type of a block is determined by the text
97immediately following the '#+BEGIN_' portion of the block header.
98Each block export function should accept three argumets..."
99 :group 'org-export-general
100 :type '(repeat
101 (list
102 (symbol :tag "Block name")
103 (function :tag "Block formatter")
104 (boolean :tag "Fontify content as Org syntax")))
105 :set 'org-export-blocks-set)
106
107(defun org-export-blocks-add-block (block-spec)
108 "Add a new block type to `org-export-blocks'. BLOCK-SPEC
109should be a three element list the first element of which should
110indicate the name of the block, the second element should be the
111formatting function called by `org-export-blocks-preprocess' and
112the third element a flag indicating whether these types of blocks
113should be fontified in org-mode buffers (see
114`org-protecting-blocks'). For example the BLOCK-SPEC for ditaa
115blocks is as follows...
116
117 (ditaa org-export-blocks-format-ditaa nil)"
118 (unless (member block-spec org-export-blocks)
119 (setq org-export-blocks (cons block-spec org-export-blocks))
120 (org-export-blocks-set 'org-export-blocks org-export-blocks)))
121
122(defcustom org-export-interblocks
8bfe682a 123 '()
c8d0cf5c
CD
124 "Use this a-list to associate block types with block exporting
125functions. The type of a block is determined by the text
126immediately following the '#+BEGIN_' portion of the block header.
127Each block export function should accept three argumets..."
128 :group 'org-export-general
129 :type 'alist)
130
131(defcustom org-export-blocks-witheld
132 '(hidden)
133 "List of block types (see `org-export-blocks') which should not
134be exported."
135 :group 'org-export-general
136 :type 'list)
137
138(defvar org-export-blocks-postblock-hooks nil "")
139
140(defun org-export-blocks-html-quote (body &optional open close)
141 "Protext BODY from org html export. The optional OPEN and
142CLOSE tags will be inserted around BODY."
143 (concat
144 "\n#+BEGIN_HTML\n"
145 (or open "")
146 body (if (string-match "\n$" body) "" "\n")
147 (or close "")
148 "#+END_HTML\n"))
149
150(defun org-export-blocks-latex-quote (body &optional open close)
151 "Protext BODY from org latex export. The optional OPEN and
152CLOSE tags will be inserted around BODY."
153 (concat
154 "\n#+BEGIN_LaTeX\n"
155 (or open "")
156 body (if (string-match "\n$" body) "" "\n")
157 (or close "")
158 "#+END_LaTeX\n"))
159
160(defun org-export-blocks-preprocess ()
8bfe682a 161 "Export all blocks according to the `org-export-blocks' block
c8d0cf5c
CD
162exportation alist. Does not export block types specified in
163specified in BLOCKS which default to the value of
164`org-export-blocks-witheld'."
165 (interactive)
166 (save-window-excursion
8bfe682a 167 (let ((case-fold-search t)
c8d0cf5c 168 (types '())
8bfe682a
CD
169 indentation type func start body headers preserve-indent)
170 (flet ((interblock (start end)
171 (mapcar (lambda (pair) (funcall (second pair) start end))
172 org-export-interblocks)))
c8d0cf5c 173 (goto-char (point-min))
8bfe682a 174 (setq start (point))
c8d0cf5c
CD
175 (while (re-search-forward
176 "^\\([ \t]*\\)#\\+begin_\\(\\S-+\\)[ \t]*\\(.*\\)?[\r\n]\\([^\000]*?\\)[\r\n][ \t]*#\\+end_\\S-+.*" nil t)
8bfe682a
CD
177 (setq indentation (length (match-string 1)))
178 (setq type (intern (downcase (match-string 2))))
179 (setq headers (save-match-data (org-split-string (match-string 3) "[ \t]+")))
180 (setq body (match-string 4))
181 (setq preserve-indent (or org-src-preserve-indentation (member "-i" headers)))
182 (unless preserve-indent
183 (setq body (save-match-data (org-remove-indentation body))))
184 (unless (memq type types) (setq types (cons type types)))
185 (save-match-data (interblock start (match-beginning 0)))
186 (if (setq func (cadr (assoc type org-export-blocks)))
c8d0cf5c
CD
187 (progn
188 (replace-match (save-match-data
8bfe682a
CD
189 (if (memq type org-export-blocks-witheld) ""
190 (apply func body headers))) t t)
191 (unless preserve-indent
192 (indent-code-rigidly (match-beginning 0) (match-end 0) indentation))))
193 (setq start (match-end 0)))
194 (interblock start (point-max))))))
c8d0cf5c
CD
195
196(add-hook 'org-export-preprocess-hook 'org-export-blocks-preprocess)
197
198;;================================================================================
199;; type specific functions
200
201;;--------------------------------------------------------------------------------
202;; ditaa: create images from ASCII art using the ditaa utility
203(defvar org-ditaa-jar-path (expand-file-name
204 "ditaa.jar"
205 (file-name-as-directory
206 (expand-file-name
207 "scripts"
208 (file-name-as-directory
209 (expand-file-name
210 "../contrib"
211 (file-name-directory (or load-file-name buffer-file-name)))))))
212 "Path to the ditaa jar executable")
213
214(defun org-export-blocks-format-ditaa (body &rest headers)
215 "Pass block BODY to the ditaa utility creating an image.
216Specify the path at which the image should be saved as the first
217element of headers, any additional elements of headers will be
218passed to the ditaa utility as command line arguments."
219 (message "ditaa-formatting...")
5dec9555
CD
220 (let* ((args (if (cdr headers) (mapconcat 'identity (cdr headers) " ")))
221 (data-file (make-temp-file "org-ditaa"))
222 (hash (sha1 (prin1-to-string (list body args))))
223 (raw-out-file (if headers (car headers)))
224 (out-file-parts (if (string-match "\\(.+\\)\\.\\([^\\.]+\\)$" raw-out-file)
225 (cons (match-string 1 raw-out-file)
226 (match-string 2 raw-out-file))
227 (cons raw-out-file "png")))
228 (out-file (concat (car out-file-parts) "_" hash "." (cdr out-file-parts))))
c8d0cf5c
CD
229 (unless (file-exists-p org-ditaa-jar-path)
230 (error (format "Could not find ditaa.jar at %s" org-ditaa-jar-path)))
231 (setq body (if (string-match "^\\([^:\\|:[^ ]\\)" body)
232 body
233 (mapconcat (lambda (x) (substring x (if (> (length x) 1) 2 1)))
234 (org-split-string body "\n")
235 "\n")))
236 (cond
237 ((or htmlp latexp docbookp)
5dec9555
CD
238 (unless (file-exists-p out-file)
239 (mapc ;; remove old hashed versions of this file
240 (lambda (file)
241 (when (and (string-match (concat (regexp-quote (car out-file-parts))
242 "_\\([[:alnum:]]+\\)\\."
243 (regexp-quote (cdr out-file-parts)))
244 file)
245 (= (length (match-string 1 out-file)) 40))
246 (delete-file (expand-file-name file
247 (file-name-directory out-file)))))
248 (directory-files (or (file-name-directory out-file)
249 default-directory)))
250 (with-temp-file data-file (insert body))
251 (message (concat "java -jar " org-ditaa-jar-path " " args " " data-file " " out-file))
252 (shell-command (concat "java -jar " org-ditaa-jar-path " " args " " data-file " " out-file)))
c8d0cf5c
CD
253 (format "\n[[file:%s]]\n" out-file))
254 (t (concat
255 "\n#+BEGIN_EXAMPLE\n"
256 body (if (string-match "\n$" body) "" "\n")
257 "#+END_EXAMPLE\n")))))
258
259;;--------------------------------------------------------------------------------
260;; dot: create graphs using the dot graphing language
261;; (require the dot executable to be in your path)
262(defun org-export-blocks-format-dot (body &rest headers)
263 "Pass block BODY to the dot graphing utility creating an image.
264Specify the path at which the image should be saved as the first
265element of headers, any additional elements of headers will be
266passed to the dot utility as command line arguments. Don't
267forget to specify the output type for the dot command, so if you
268are exporting to a file with a name like 'image.png' you should
269include a '-Tpng' argument, and your block should look like the
270following.
271
272#+begin_dot models.png -Tpng
273digraph data_relationships {
274 \"data_requirement\" [shape=Mrecord, label=\"{DataRequirement|description\lformat\l}\"]
275 \"data_product\" [shape=Mrecord, label=\"{DataProduct|name\lversion\lpoc\lformat\l}\"]
276 \"data_requirement\" -> \"data_product\"
277}
278#+end_dot"
279 (message "dot-formatting...")
5dec9555
CD
280 (let* ((args (if (cdr headers) (mapconcat 'identity (cdr headers) " ")))
281 (data-file (make-temp-file "org-ditaa"))
282 (hash (sha1 (prin1-to-string (list body args))))
283 (raw-out-file (if headers (car headers)))
284 (out-file-parts (if (string-match "\\(.+\\)\\.\\([^\\.]+\\)$" raw-out-file)
285 (cons (match-string 1 raw-out-file)
286 (match-string 2 raw-out-file))
287 (cons raw-out-file "png")))
288 (out-file (concat (car out-file-parts) "_" hash "." (cdr out-file-parts))))
c8d0cf5c
CD
289 (cond
290 ((or htmlp latexp docbookp)
5dec9555
CD
291 (unless (file-exists-p out-file)
292 (mapc ;; remove old hashed versions of this file
293 (lambda (file)
294 (when (and (string-match (concat (regexp-quote (car out-file-parts))
295 "_\\([[:alnum:]]+\\)\\."
296 (regexp-quote (cdr out-file-parts)))
297 file)
298 (= (length (match-string 1 out-file)) 40))
299 (delete-file (expand-file-name file
300 (file-name-directory out-file)))))
301 (directory-files (or (file-name-directory out-file)
302 default-directory)))
303 (with-temp-file data-file (insert body))
304 (message (concat "dot " data-file " " args " -o " out-file))
305 (shell-command (concat "dot " data-file " " args " -o " out-file)))
c8d0cf5c
CD
306 (format "\n[[file:%s]]\n" out-file))
307 (t (concat
308 "\n#+BEGIN_EXAMPLE\n"
309 body (if (string-match "\n$" body) "" "\n")
310 "#+END_EXAMPLE\n")))))
311
312;;--------------------------------------------------------------------------------
313;; comment: export comments in author-specific css-stylable divs
314(defun org-export-blocks-format-comment (body &rest headers)
315 "Format comment BODY by OWNER and return it formatted for export.
316Currently, this only does something for HTML export, for all
317other backends, it converts the comment into an EXAMPLE segment."
318 (let ((owner (if headers (car headers)))
319 (title (if (cdr headers) (mapconcat 'identity (cdr headers) " "))))
320 (cond
321 (htmlp ;; We are exporting to HTML
322 (concat "#+BEGIN_HTML\n"
323 "<div class=\"org-comment\""
324 (if owner (format " id=\"org-comment-%s\" " owner))
325 ">\n"
326 (if owner (concat "<b>" owner "</b> ") "")
327 (if (and title (> (length title) 0)) (concat " -- " title "</br>\n") "</br>\n")
328 "<p>\n"
329 "#+END_HTML\n"
330 body
331 "#+BEGIN_HTML\n"
332 "</p>\n"
333 "</div>\n"
334 "#+END_HTML\n"))
335 (t ;; This is not HTML, so just make it an example.
336 (concat "#+BEGIN_EXAMPLE\n"
337 (if title (concat "Title:" title "\n") "")
338 (if owner (concat "By:" owner "\n") "")
339 body
340 (if (string-match "\n\\'" body) "" "\n")
341 "#+END_EXAMPLE\n")))))
342
c8d0cf5c
CD
343(provide 'org-exp-blocks)
344
2c4c2e22 345;; arch-tag: 1c365fe9-8808-4f72-bb15-0b00f36d8024
c8d0cf5c 346;;; org-exp-blocks.el ends here