Merge from emacs-23; up to 2010-06-15T03:34:12Z!rgm@gnu.org.
[bpt/emacs.git] / lisp / org / org-exp-blocks.el
1 ;;; org-exp-blocks.el --- pre-process blocks when exporting org files
2
3 ;; Copyright (C) 2009-2011 Free Software Foundation, Inc.
4
5 ;; Author: Eric Schulte
6 ;; Version: 7.4
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 ;;
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 ;;; Code:
71
72 (eval-when-compile
73 (require 'cl))
74 (require 'org)
75
76 (defvar htmlp)
77 (defvar latexp)
78 (defvar docbookp)
79 (defvar asciip)
80
81 (defun org-export-blocks-set (var value)
82 "Set the value of `org-export-blocks' and install fontification."
83 (set var value)
84 (mapc (lambda (spec)
85 (if (nth 2 spec)
86 (setq org-protecting-blocks
87 (delete (symbol-name (car spec))
88 org-protecting-blocks))
89 (add-to-list 'org-protecting-blocks
90 (symbol-name (car spec)))))
91 value))
92
93 (defcustom org-export-blocks
94 '((comment org-export-blocks-format-comment t)
95 (ditaa org-export-blocks-format-ditaa nil)
96 (dot org-export-blocks-format-dot nil))
97 "Use this alist to associate block types with block exporting functions.
98 The type of a block is determined by the text immediately
99 following the '#+BEGIN_' portion of the block header. Each block
100 export function should accept three arguments."
101 :group 'org-export-general
102 :type '(repeat
103 (list
104 (symbol :tag "Block name")
105 (function :tag "Block formatter")
106 (boolean :tag "Fontify content as Org syntax")))
107 :set 'org-export-blocks-set)
108
109 (defun org-export-blocks-add-block (block-spec)
110 "Add a new block type to `org-export-blocks'.
111 BLOCK-SPEC should be a three element list the first element of
112 which should indicate the name of the block, the second element
113 should be the formatting function called by
114 `org-export-blocks-preprocess' and the third element a flag
115 indicating whether these types of blocks should be fontified in
116 org-mode buffers (see `org-protecting-blocks'). For example the
117 BLOCK-SPEC for ditaa blocks is as follows.
118
119 (ditaa org-export-blocks-format-ditaa nil)"
120 (unless (member block-spec org-export-blocks)
121 (setq org-export-blocks (cons block-spec org-export-blocks))
122 (org-export-blocks-set 'org-export-blocks org-export-blocks)))
123
124 (defcustom org-export-interblocks
125 '()
126 "Use this a-list to associate block types with block exporting functions.
127 The type of a block is determined by the text immediately
128 following the '#+BEGIN_' portion of the block header. Each block
129 export function should accept three arguments."
130 :group 'org-export-general
131 :type 'alist)
132
133 (defcustom org-export-blocks-witheld
134 '(hidden)
135 "List of block types (see `org-export-blocks') which should not be exported."
136 :group 'org-export-general
137 :type 'list)
138
139 (defcustom org-export-blocks-postblock-hook nil
140 "Run after blocks have been processed with `org-export-blocks-preprocess'."
141 :group 'org-export-general
142 :type 'hook)
143
144 (defun org-export-blocks-html-quote (body &optional open close)
145 "Protect BODY from org html export.
146 The optional OPEN and CLOSE tags will be inserted around BODY."
147
148 (concat
149 "\n#+BEGIN_HTML\n"
150 (or open "")
151 body (if (string-match "\n$" body) "" "\n")
152 (or close "")
153 "#+END_HTML\n"))
154
155 (defun org-export-blocks-latex-quote (body &optional open close)
156 "Protect BODY from org latex export.
157 The optional OPEN and CLOSE tags will be inserted around BODY."
158 (concat
159 "\n#+BEGIN_LaTeX\n"
160 (or open "")
161 body (if (string-match "\n$" body) "" "\n")
162 (or close "")
163 "#+END_LaTeX\n"))
164
165 (defun org-export-blocks-preprocess ()
166 "Export all blocks according to the `org-export-blocks' block export alist.
167 Does not export block types specified in specified in BLOCKS
168 which defaults to the value of `org-export-blocks-witheld'."
169 (interactive)
170 (save-window-excursion
171 (let ((case-fold-search t)
172 (types '())
173 indentation type func start body headers preserve-indent progress-marker)
174 (flet ((interblock (start end)
175 (mapcar (lambda (pair) (funcall (second pair) start end))
176 org-export-interblocks)))
177 (goto-char (point-min))
178 (setq start (point))
179 (while (re-search-forward
180 "^\\([ \t]*\\)#\\+begin_\\(\\S-+\\)[ \t]*\\(.*\\)?[\r\n]\\([^\000]*?\\)[\r\n][ \t]*#\\+end_\\S-+.*[\r\n]?" nil t)
181 (setq indentation (length (match-string 1)))
182 (setq type (intern (downcase (match-string 2))))
183 (setq headers (save-match-data (org-split-string (match-string 3) "[ \t]+")))
184 (setq body (match-string 4))
185 (setq preserve-indent (or org-src-preserve-indentation (member "-i" headers)))
186 (unless preserve-indent
187 (setq body (save-match-data (org-remove-indentation body))))
188 (unless (memq type types) (setq types (cons type types)))
189 (save-match-data (interblock start (match-beginning 0)))
190 (when (setq func (cadr (assoc type org-export-blocks)))
191 (let ((replacement (save-match-data
192 (if (memq type org-export-blocks-witheld) ""
193 (apply func body headers)))))
194 (when replacement
195 (replace-match replacement t t)
196 (unless preserve-indent
197 (indent-code-rigidly
198 (match-beginning 0) (match-end 0) indentation)))))
199 (setq start (match-end 0)))
200 (interblock start (point-max))
201 (run-hooks 'org-export-blocks-postblock-hook)))))
202
203 ;;================================================================================
204 ;; type specific functions
205
206 ;;--------------------------------------------------------------------------------
207 ;; ditaa: create images from ASCII art using the ditaa utility
208 (defvar org-ditaa-jar-path (expand-file-name
209 "ditaa.jar"
210 (file-name-as-directory
211 (expand-file-name
212 "scripts"
213 (file-name-as-directory
214 (expand-file-name
215 "../contrib"
216 (file-name-directory (or load-file-name buffer-file-name)))))))
217 "Path to the ditaa jar executable.")
218
219 (defun org-export-blocks-format-ditaa (body &rest headers)
220 "Pass block BODY to the ditaa utility creating an image.
221 Specify the path at which the image should be saved as the first
222 element of headers, any additional elements of headers will be
223 passed to the ditaa utility as command line arguments."
224 (message "ditaa-formatting...")
225 (let* ((args (if (cdr headers) (mapconcat 'identity (cdr headers) " ")))
226 (data-file (make-temp-file "org-ditaa"))
227 (hash (progn
228 (set-text-properties 0 (length body) nil body)
229 (sha1 (prin1-to-string (list body args)))))
230 (raw-out-file (if headers (car headers)))
231 (out-file-parts (if (string-match "\\(.+\\)\\.\\([^\\.]+\\)$" raw-out-file)
232 (cons (match-string 1 raw-out-file)
233 (match-string 2 raw-out-file))
234 (cons raw-out-file "png")))
235 (out-file (concat (car out-file-parts) "_" hash "." (cdr out-file-parts))))
236 (unless (file-exists-p org-ditaa-jar-path)
237 (error (format "Could not find ditaa.jar at %s" org-ditaa-jar-path)))
238 (setq body (if (string-match "^\\([^:\\|:[^ ]\\)" body)
239 body
240 (mapconcat (lambda (x) (substring x (if (> (length x) 1) 2 1)))
241 (org-split-string body "\n")
242 "\n")))
243 (cond
244 ((or htmlp latexp docbookp)
245 (unless (file-exists-p out-file)
246 (mapc ;; remove old hashed versions of this file
247 (lambda (file)
248 (when (and (string-match (concat (regexp-quote (car out-file-parts))
249 "_\\([[:alnum:]]+\\)\\."
250 (regexp-quote (cdr out-file-parts)))
251 file)
252 (= (length (match-string 1 out-file)) 40))
253 (delete-file (expand-file-name file
254 (file-name-directory out-file)))))
255 (directory-files (or (file-name-directory out-file)
256 default-directory)))
257 (with-temp-file data-file (insert body))
258 (message (concat "java -jar " org-ditaa-jar-path " " args " " data-file " " out-file))
259 (shell-command (concat "java -jar " org-ditaa-jar-path " " args " " data-file " " out-file)))
260 (format "\n[[file:%s]]\n" out-file))
261 (t (concat
262 "\n#+BEGIN_EXAMPLE\n"
263 body (if (string-match "\n$" body) "" "\n")
264 "#+END_EXAMPLE\n")))))
265
266 ;;--------------------------------------------------------------------------------
267 ;; dot: create graphs using the dot graphing language
268 ;; (require the dot executable to be in your path)
269 (defun org-export-blocks-format-dot (body &rest headers)
270 "Pass block BODY to the dot graphing utility creating an image.
271 Specify the path at which the image should be saved as the first
272 element of headers, any additional elements of headers will be
273 passed to the dot utility as command line arguments. Don't
274 forget to specify the output type for the dot command, so if you
275 are exporting to a file with a name like 'image.png' you should
276 include a '-Tpng' argument, and your block should look like the
277 following.
278
279 #+begin_dot models.png -Tpng
280 digraph data_relationships {
281 \"data_requirement\" [shape=Mrecord, label=\"{DataRequirement|description\lformat\l}\"]
282 \"data_product\" [shape=Mrecord, label=\"{DataProduct|name\lversion\lpoc\lformat\l}\"]
283 \"data_requirement\" -> \"data_product\"
284 }
285 #+end_dot"
286 (message "dot-formatting...")
287 (let* ((args (if (cdr headers) (mapconcat 'identity (cdr headers) " ")))
288 (data-file (make-temp-file "org-ditaa"))
289 (hash (progn
290 (set-text-properties 0 (length body) nil body)
291 (sha1 (prin1-to-string (list body args)))))
292 (raw-out-file (if headers (car headers)))
293 (out-file-parts (if (string-match "\\(.+\\)\\.\\([^\\.]+\\)$" raw-out-file)
294 (cons (match-string 1 raw-out-file)
295 (match-string 2 raw-out-file))
296 (cons raw-out-file "png")))
297 (out-file (concat (car out-file-parts) "_" hash "." (cdr out-file-parts))))
298 (cond
299 ((or htmlp latexp docbookp)
300 (unless (file-exists-p out-file)
301 (mapc ;; remove old hashed versions of this file
302 (lambda (file)
303 (when (and (string-match (concat (regexp-quote (car out-file-parts))
304 "_\\([[:alnum:]]+\\)\\."
305 (regexp-quote (cdr out-file-parts)))
306 file)
307 (= (length (match-string 1 out-file)) 40))
308 (delete-file (expand-file-name file
309 (file-name-directory out-file)))))
310 (directory-files (or (file-name-directory out-file)
311 default-directory)))
312 (with-temp-file data-file (insert body))
313 (message (concat "dot " data-file " " args " -o " out-file))
314 (shell-command (concat "dot " data-file " " args " -o " out-file)))
315 (format "\n[[file:%s]]\n" out-file))
316 (t (concat
317 "\n#+BEGIN_EXAMPLE\n"
318 body (if (string-match "\n$" body) "" "\n")
319 "#+END_EXAMPLE\n")))))
320
321 ;;--------------------------------------------------------------------------------
322 ;; comment: export comments in author-specific css-stylable divs
323 (defun org-export-blocks-format-comment (body &rest headers)
324 "Format comment BODY by OWNER and return it formatted for export.
325 Currently, this only does something for HTML export, for all
326 other backends, it converts the comment into an EXAMPLE segment."
327 (let ((owner (if headers (car headers)))
328 (title (if (cdr headers) (mapconcat 'identity (cdr headers) " "))))
329 (cond
330 (htmlp ;; We are exporting to HTML
331 (concat "#+BEGIN_HTML\n"
332 "<div class=\"org-comment\""
333 (if owner (format " id=\"org-comment-%s\" " owner))
334 ">\n"
335 (if owner (concat "<b>" owner "</b> ") "")
336 (if (and title (> (length title) 0)) (concat " -- " title "</br>\n") "</br>\n")
337 "<p>\n"
338 "#+END_HTML\n"
339 body
340 "#+BEGIN_HTML\n"
341 "</p>\n"
342 "</div>\n"
343 "#+END_HTML\n"))
344 (t ;; This is not HTML, so just make it an example.
345 (concat "#+BEGIN_EXAMPLE\n"
346 (if title (concat "Title:" title "\n") "")
347 (if owner (concat "By:" owner "\n") "")
348 body
349 (if (string-match "\n\\'" body) "" "\n")
350 "#+END_EXAMPLE\n")))))
351
352 (provide 'org-exp-blocks)
353
354 ;;; org-exp-blocks.el ends here