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