org-table.el (orgtbl-to-generic): Fix docstring.
[org-mode.git] / lisp / org-exp-blocks.el
blobb46f743468076eeeabfa6c2066bb34ef4f9b72f3
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 'find-func)
76 (require 'org-compat)
78 (declare-function org-split-string "org" (string &optional separators))
79 (declare-function org-remove-indentation "org" (code &optional n))
81 (defvar org-protecting-blocks nil) ; From org.el
83 (defun org-export-blocks-set (var value)
84 "Set the value of `org-export-blocks' and install fontification."
85 (set var value)
86 (mapc (lambda (spec)
87 (if (nth 2 spec)
88 (setq org-protecting-blocks
89 (delete (symbol-name (car spec))
90 org-protecting-blocks))
91 (add-to-list 'org-protecting-blocks
92 (symbol-name (car spec)))))
93 value))
95 (defcustom org-export-blocks
96 '((export-comment org-export-blocks-format-comment t)
97 (ditaa org-export-blocks-format-ditaa nil)
98 (dot org-export-blocks-format-dot nil))
99 "Use this alist to associate block types with block exporting functions.
100 The type of a block is determined by the text immediately
101 following the '#+BEGIN_' portion of the block header. Each block
102 export function should accept three arguments."
103 :group 'org-export-general
104 :type '(repeat
105 (list
106 (symbol :tag "Block name")
107 (function :tag "Block formatter")
108 (boolean :tag "Fontify content as Org syntax")))
109 :set 'org-export-blocks-set)
111 (defun org-export-blocks-add-block (block-spec)
112 "Add a new block type to `org-export-blocks'.
113 BLOCK-SPEC should be a three element list the first element of
114 which should indicate the name of the block, the second element
115 should be the formatting function called by
116 `org-export-blocks-preprocess' and the third element a flag
117 indicating whether these types of blocks should be fontified in
118 org-mode buffers (see `org-protecting-blocks'). For example the
119 BLOCK-SPEC for ditaa blocks is as follows.
121 (ditaa org-export-blocks-format-ditaa nil)"
122 (unless (member block-spec org-export-blocks)
123 (setq org-export-blocks (cons block-spec org-export-blocks))
124 (org-export-blocks-set 'org-export-blocks org-export-blocks)))
126 (defcustom org-export-interblocks
128 "Use this a-list to associate block types with block exporting functions.
129 The type of a block is determined by the text immediately
130 following the '#+BEGIN_' portion of the block header. Each block
131 export function should accept three arguments."
132 :group 'org-export-general
133 :type 'alist)
135 (defcustom org-export-blocks-witheld
136 '(hidden)
137 "List of block types (see `org-export-blocks') which should not be exported."
138 :group 'org-export-general
139 :type 'list)
141 (defcustom org-export-blocks-postblock-hook nil
142 "Run after blocks have been processed with `org-export-blocks-preprocess'."
143 :group 'org-export-general
144 :version "24.1"
145 :type 'hook)
147 (defun org-export-blocks-html-quote (body &optional open close)
148 "Protect BODY from org html export.
149 The optional OPEN and CLOSE tags will be inserted around BODY."
150 (concat
151 "\n#+BEGIN_HTML\n"
152 (or open "")
153 body (if (string-match "\n$" body) "" "\n")
154 (or close "")
155 "#+END_HTML\n"))
157 (defun org-export-blocks-latex-quote (body &optional open close)
158 "Protect BODY from org latex export.
159 The optional OPEN and CLOSE tags will be inserted around BODY."
160 (concat
161 "\n#+BEGIN_LaTeX\n"
162 (or open "")
163 body (if (string-match "\n$" body) "" "\n")
164 (or close "")
165 "#+END_LaTeX\n"))
167 (defvar org-src-preserve-indentation) ; From org-src.el
168 (defun org-export-blocks-preprocess ()
169 "Export all blocks according to the `org-export-blocks' block export alist.
170 Does not export block types specified in specified in BLOCKS
171 which defaults to the value of `org-export-blocks-witheld'."
172 (interactive)
173 (save-window-excursion
174 (let ((case-fold-search t)
175 (types '())
176 matched indentation type func
177 start end body headers preserve-indent progress-marker)
178 (org-flet ((interblock (start end)
179 (mapcar (lambda (pair) (funcall (second pair) start end))
180 org-export-interblocks)))
181 (goto-char (point-min))
182 (setq start (point))
183 (let ((beg-re "^\\([ \t]*\\)#\\+begin_\\(\\S-+\\)[ \t]*\\(.*\\)?[\r\n]"))
184 (while (re-search-forward beg-re nil t)
185 (let* ((match-start (copy-marker (match-beginning 0)))
186 (body-start (copy-marker (match-end 0)))
187 (indentation (length (match-string 1)))
188 (inner-re (format "^[ \t]*#\\+\\(begin\\|end\\)_%s"
189 (regexp-quote (downcase (match-string 2)))))
190 (type (intern (downcase (match-string 2))))
191 (headers (save-match-data
192 (org-split-string (match-string 3) "[ \t]+")))
193 (balanced 1)
194 (preserve-indent (or org-src-preserve-indentation
195 (member "-i" headers)))
196 match-end)
197 (while (and (not (zerop balanced))
198 (re-search-forward inner-re nil t))
199 (if (string= (downcase (match-string 1)) "end")
200 (decf balanced)
201 (incf balanced)))
202 (when (not (zerop balanced))
203 (error "unbalanced begin/end_%s blocks with %S"
204 type (buffer-substring match-start (point))))
205 (setq match-end (copy-marker (match-end 0)))
206 (unless preserve-indent
207 (setq body (save-match-data (org-remove-indentation
208 (buffer-substring
209 body-start (match-beginning 0))))))
210 (unless (memq type types) (setq types (cons type types)))
211 (save-match-data (interblock start match-start))
212 (when (setq func (cadr (assoc type org-export-blocks)))
213 (let ((replacement (save-match-data
214 (if (memq type org-export-blocks-witheld) ""
215 (apply func body headers)))))
216 ;; ;; un-comment this code after the org-element merge
217 ;; (save-match-data
218 ;; (when (and replacement (string= replacement ""))
219 ;; (delete-region
220 ;; (car (org-element-collect-affiliated-keyword))
221 ;; match-start)))
222 (when replacement
223 (delete-region match-start match-end)
224 (goto-char match-start) (insert replacement)
225 (if preserve-indent
226 ;; indent only the code block markers
227 (save-excursion
228 (indent-line-to indentation) ; indent end_block
229 (goto-char match-start)
230 (indent-line-to indentation)) ; indent begin_block
231 ;; indent everything
232 (indent-code-rigidly match-start (point) indentation)))))
233 ;; cleanup markers
234 (set-marker match-start nil)
235 (set-marker body-start nil)
236 (set-marker match-end nil))
237 (setq start (point))))
238 (interblock start (point-max))
239 (run-hooks 'org-export-blocks-postblock-hook)))))
241 ;;================================================================================
242 ;; type specific functions
244 ;;--------------------------------------------------------------------------------
245 ;; ditaa: create images from ASCII art using the ditaa utility
246 (defcustom org-ditaa-jar-path (expand-file-name
247 "ditaa.jar"
248 (file-name-as-directory
249 (expand-file-name
250 "scripts"
251 (file-name-as-directory
252 (expand-file-name
253 "../contrib"
254 (file-name-directory (org-find-library-dir "org")))))))
255 "Path to the ditaa jar executable."
256 :group 'org-babel
257 :type 'string)
259 (defvar org-export-current-backend) ; dynamically bound in org-exp.el
260 (defun org-export-blocks-format-ditaa (body &rest headers)
261 "DEPRECATED: use begin_src ditaa code blocks
263 Pass block BODY to the ditaa utility creating an image.
264 Specify the path at which the image should be saved as the first
265 element of headers, any additional elements of headers will be
266 passed to the ditaa utility as command line arguments."
267 (message "begin_ditaa blocks are DEPRECATED, use begin_src blocks")
268 (let* ((args (if (cdr headers) (mapconcat 'identity (cdr headers) " ")))
269 (data-file (make-temp-file "org-ditaa"))
270 (hash (progn
271 (set-text-properties 0 (length body) nil body)
272 (sha1 (prin1-to-string (list body args)))))
273 (raw-out-file (if headers (car headers)))
274 (out-file-parts (if (string-match "\\(.+\\)\\.\\([^\\.]+\\)$" raw-out-file)
275 (cons (match-string 1 raw-out-file)
276 (match-string 2 raw-out-file))
277 (cons raw-out-file "png")))
278 (out-file (concat (car out-file-parts) "_" hash "." (cdr out-file-parts))))
279 (unless (file-exists-p org-ditaa-jar-path)
280 (error (format "Could not find ditaa.jar at %s" org-ditaa-jar-path)))
281 (setq body (if (string-match "^\\([^:\\|:[^ ]\\)" body)
282 body
283 (mapconcat (lambda (x) (substring x (if (> (length x) 1) 2 1)))
284 (org-split-string body "\n")
285 "\n")))
286 (prog1
287 (cond
288 ((member org-export-current-backend '(html latex docbook))
289 (unless (file-exists-p out-file)
290 (mapc ;; remove old hashed versions of this file
291 (lambda (file)
292 (when (and (string-match (concat (regexp-quote (car out-file-parts))
293 "_\\([[:alnum:]]+\\)\\."
294 (regexp-quote (cdr out-file-parts)))
295 file)
296 (= (length (match-string 1 out-file)) 40))
297 (delete-file (expand-file-name file
298 (file-name-directory out-file)))))
299 (directory-files (or (file-name-directory out-file)
300 default-directory)))
301 (with-temp-file data-file (insert body))
302 (message (concat "java -jar " org-ditaa-jar-path " " args " " data-file " " out-file))
303 (shell-command (concat "java -jar " org-ditaa-jar-path " " args " " data-file " " out-file)))
304 (format "\n[[file:%s]]\n" out-file))
305 (t (concat
306 "\n#+BEGIN_EXAMPLE\n"
307 body (if (string-match "\n$" body) "" "\n")
308 "#+END_EXAMPLE\n")))
309 (message "begin_ditaa blocks are DEPRECATED, use begin_src blocks"))))
311 ;;--------------------------------------------------------------------------------
312 ;; dot: create graphs using the dot graphing language
313 ;; (require the dot executable to be in your path)
314 (defun org-export-blocks-format-dot (body &rest headers)
315 "DEPRECATED: use \"#+begin_src dot\" code blocks
317 Pass block BODY to the dot graphing utility creating an image.
318 Specify the path at which the image should be saved as the first
319 element of headers, any additional elements of headers will be
320 passed to the dot utility as command line arguments. Don't
321 forget to specify the output type for the dot command, so if you
322 are exporting to a file with a name like 'image.png' you should
323 include a '-Tpng' argument, and your block should look like the
324 following.
326 #+begin_dot models.png -Tpng
327 digraph data_relationships {
328 \"data_requirement\" [shape=Mrecord, label=\"{DataRequirement|description\lformat\l}\"]
329 \"data_product\" [shape=Mrecord, label=\"{DataProduct|name\lversion\lpoc\lformat\l}\"]
330 \"data_requirement\" -> \"data_product\"
332 #+end_dot"
333 (message "begin_dot blocks are DEPRECATED, use begin_src blocks")
334 (let* ((args (if (cdr headers) (mapconcat 'identity (cdr headers) " ")))
335 (data-file (make-temp-file "org-ditaa"))
336 (hash (progn
337 (set-text-properties 0 (length body) nil body)
338 (sha1 (prin1-to-string (list body args)))))
339 (raw-out-file (if headers (car headers)))
340 (out-file-parts (if (string-match "\\(.+\\)\\.\\([^\\.]+\\)$" raw-out-file)
341 (cons (match-string 1 raw-out-file)
342 (match-string 2 raw-out-file))
343 (cons raw-out-file "png")))
344 (out-file (concat (car out-file-parts) "_" hash "." (cdr out-file-parts))))
345 (prog1
346 (cond
347 ((member org-export-current-backend '(html latex docbook))
348 (unless (file-exists-p out-file)
349 (mapc ;; remove old hashed versions of this file
350 (lambda (file)
351 (when (and (string-match (concat (regexp-quote (car out-file-parts))
352 "_\\([[:alnum:]]+\\)\\."
353 (regexp-quote (cdr out-file-parts)))
354 file)
355 (= (length (match-string 1 out-file)) 40))
356 (delete-file (expand-file-name file
357 (file-name-directory out-file)))))
358 (directory-files (or (file-name-directory out-file)
359 default-directory)))
360 (with-temp-file data-file (insert body))
361 (message (concat "dot " data-file " " args " -o " out-file))
362 (shell-command (concat "dot " data-file " " args " -o " out-file)))
363 (format "\n[[file:%s]]\n" out-file))
364 (t (concat
365 "\n#+BEGIN_EXAMPLE\n"
366 body (if (string-match "\n$" body) "" "\n")
367 "#+END_EXAMPLE\n")))
368 (message "begin_dot blocks are DEPRECATED, use begin_src blocks"))))
370 ;;--------------------------------------------------------------------------------
371 ;; comment: export comments in author-specific css-stylable divs
372 (defun org-export-blocks-format-comment (body &rest headers)
373 "Format comment BODY by OWNER and return it formatted for export.
374 Currently, this only does something for HTML export, for all
375 other backends, it converts the comment into an EXAMPLE segment."
376 (let ((owner (if headers (car headers)))
377 (title (if (cdr headers) (mapconcat 'identity (cdr headers) " "))))
378 (cond
379 ((eq org-export-current-backend 'html) ;; We are exporting to HTML
380 (concat "#+BEGIN_HTML\n"
381 "<div class=\"org-comment\""
382 (if owner (format " id=\"org-comment-%s\" " owner))
383 ">\n"
384 (if owner (concat "<b>" owner "</b> ") "")
385 (if (and title (> (length title) 0)) (concat " -- " title "<br/>\n") "<br/>\n")
386 "<p>\n"
387 "#+END_HTML\n"
388 body
389 "\n#+BEGIN_HTML\n"
390 "</p>\n"
391 "</div>\n"
392 "#+END_HTML\n"))
393 (t ;; This is not HTML, so just make it an example.
394 (concat "#+BEGIN_EXAMPLE\n"
395 (if title (concat "Title:" title "\n") "")
396 (if owner (concat "By:" owner "\n") "")
397 body
398 (if (string-match "\n\\'" body) "" "\n")
399 "#+END_EXAMPLE\n")))))
401 (provide 'org-exp-blocks)
403 ;;; org-exp-blocks.el ends here