org.el: minor fix: delete trailing whitespaces.
[org-mode.git] / lisp / org-exp-blocks.el
blobc1c9cd1a09ba2b4926b6183192373dfdd6b29185
1 ;;; org-exp-blocks.el --- pre-process blocks when exporting org files
3 ;; Copyright (C) 2009-2011 Free Software Foundation, Inc.
5 ;; Author: Eric Schulte
6 ;; Version: 7.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.
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.
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/>.
23 ;;; Commentary:
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.
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_=.
38 ;; #+begin_ditaa blue.png -r -S
39 ;; +---------+
40 ;; | cBLU |
41 ;; | |
42 ;; | +----+
43 ;; | |cPNK|
44 ;; | | |
45 ;; +----+----+
46 ;; #+end_ditaa
48 ;;; Currently Implemented Block Types
50 ;; ditaa :: (DEPRECATED--use "#+begin_src ditaa" code blocks) Convert
51 ;; ascii pictures to actual images using ditaa
52 ;; http://ditaa.sourceforge.net/. To use this set
53 ;; `org-ditaa-jar-path' to the path to ditaa.jar on your
54 ;; system (should be set automatically in most cases) .
56 ;; dot :: (DEPRECATED--use "#+begin_src dot" code blocks) Convert
57 ;; graphs defined using the dot graphing language to images
58 ;; using the dot utility. For information on dot see
59 ;; http://www.graphviz.org/
61 ;; comment :: Wrap comments with titles and author information, in
62 ;; their own divs with author-specific ids allowing for css
63 ;; coloring of comments based on the author.
65 ;;; Adding new blocks
67 ;; When adding a new block type first define a formatting function
68 ;; along the same lines as `org-export-blocks-format-dot' and then use
69 ;; `org-export-blocks-add-block' to add your block type to
70 ;; `org-export-blocks'.
72 ;;; Code:
74 (eval-when-compile
75 (require 'cl))
76 (require 'org)
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 '((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 :type 'hook)
141 (defun org-export-blocks-html-quote (body &optional open close)
142 "Protect BODY from org html export.
143 The optional OPEN and CLOSE tags will be inserted around BODY."
145 (concat
146 "\n#+BEGIN_HTML\n"
147 (or open "")
148 body (if (string-match "\n$" body) "" "\n")
149 (or close "")
150 "#+END_HTML\n"))
152 (defun org-export-blocks-latex-quote (body &optional open close)
153 "Protect BODY from org latex export.
154 The optional OPEN and CLOSE tags will be inserted around BODY."
155 (concat
156 "\n#+BEGIN_LaTeX\n"
157 (or open "")
158 body (if (string-match "\n$" body) "" "\n")
159 (or close "")
160 "#+END_LaTeX\n"))
162 (defun org-export-blocks-preprocess ()
163 "Export all blocks according to the `org-export-blocks' block export alist.
164 Does not export block types specified in specified in BLOCKS
165 which defaults to the value of `org-export-blocks-witheld'."
166 (interactive)
167 (save-window-excursion
168 (let ((case-fold-search t)
169 (types '())
170 matched indentation type func
171 start end body headers preserve-indent progress-marker)
172 (flet ((interblock (start end)
173 (mapcar (lambda (pair) (funcall (second pair) start end))
174 org-export-interblocks)))
175 (goto-char (point-min))
176 (setq start (point))
177 (let ((beg-re "^\\([ \t]*\\)#\\+begin_\\(\\S-+\\)[ \t]*\\(.*\\)?[\r\n]"))
178 (while (re-search-forward beg-re nil t)
179 (let* ((match-start (match-beginning 0))
180 (body-start (match-end 0))
181 (indentation (length (match-string 1)))
182 (inner-re (format "[\r\n]*[ \t]*#\\+\\(begin\\|end\\)_%s"
183 (regexp-quote (downcase (match-string 2)))))
184 (type (intern (downcase (match-string 2))))
185 (headers (save-match-data
186 (org-split-string (match-string 3) "[ \t]+")))
187 (balanced 1)
188 (preserve-indent (or org-src-preserve-indentation
189 (member "-i" headers)))
190 match-end)
191 (while (and (not (zerop balanced))
192 (re-search-forward inner-re nil t))
193 (if (string= (downcase (match-string 1)) "end")
194 (decf balanced)
195 (incf balanced)))
196 (when (not (zerop balanced))
197 (error "unbalanced begin/end_%s blocks with %S"
198 type (buffer-substring match-start (point))))
199 (setq match-end (match-end 0))
200 (unless preserve-indent
201 (setq body (save-match-data (org-remove-indentation
202 (buffer-substring
203 body-start (match-beginning 0))))))
204 (unless (memq type types) (setq types (cons type types)))
205 (save-match-data (interblock start match-start))
206 (when (setq func (cadr (assoc type org-export-blocks)))
207 (let ((replacement (save-match-data
208 (if (memq type org-export-blocks-witheld) ""
209 (apply func body headers)))))
210 (when replacement
211 (delete-region match-start match-end)
212 (goto-char match-start) (insert replacement)
213 (unless preserve-indent
214 (indent-code-rigidly match-start (point) indentation))))))
215 (setq start (point))))
216 (interblock start (point-max))
217 (run-hooks 'org-export-blocks-postblock-hook)))))
219 ;;================================================================================
220 ;; type specific functions
222 ;;--------------------------------------------------------------------------------
223 ;; ditaa: create images from ASCII art using the ditaa utility
224 (defvar org-ditaa-jar-path (expand-file-name
225 "ditaa.jar"
226 (file-name-as-directory
227 (expand-file-name
228 "scripts"
229 (file-name-as-directory
230 (expand-file-name
231 "../contrib"
232 (file-name-directory (or load-file-name buffer-file-name)))))))
233 "Path to the ditaa jar executable.")
235 (defvar org-export-current-backend) ; dynamically bound in org-exp.el
236 (defun org-export-blocks-format-ditaa (body &rest headers)
237 "DEPRECATED: use begin_src ditaa code blocks
239 Pass block BODY to the ditaa utility creating an image.
240 Specify the path at which the image should be saved as the first
241 element of headers, any additional elements of headers will be
242 passed to the ditaa utility as command line arguments."
243 (message "begin_ditaa blocks are DEPRECATED, use begin_src blocks")
244 (let* ((args (if (cdr headers) (mapconcat 'identity (cdr headers) " ")))
245 (data-file (make-temp-file "org-ditaa"))
246 (hash (progn
247 (set-text-properties 0 (length body) nil body)
248 (sha1 (prin1-to-string (list body args)))))
249 (raw-out-file (if headers (car headers)))
250 (out-file-parts (if (string-match "\\(.+\\)\\.\\([^\\.]+\\)$" raw-out-file)
251 (cons (match-string 1 raw-out-file)
252 (match-string 2 raw-out-file))
253 (cons raw-out-file "png")))
254 (out-file (concat (car out-file-parts) "_" hash "." (cdr out-file-parts))))
255 (unless (file-exists-p org-ditaa-jar-path)
256 (error (format "Could not find ditaa.jar at %s" org-ditaa-jar-path)))
257 (setq body (if (string-match "^\\([^:\\|:[^ ]\\)" body)
258 body
259 (mapconcat (lambda (x) (substring x (if (> (length x) 1) 2 1)))
260 (org-split-string body "\n")
261 "\n")))
262 (prog1
263 (cond
264 ((member org-export-current-backend '(html latex docbook))
265 (unless (file-exists-p out-file)
266 (mapc ;; remove old hashed versions of this file
267 (lambda (file)
268 (when (and (string-match (concat (regexp-quote (car out-file-parts))
269 "_\\([[:alnum:]]+\\)\\."
270 (regexp-quote (cdr out-file-parts)))
271 file)
272 (= (length (match-string 1 out-file)) 40))
273 (delete-file (expand-file-name file
274 (file-name-directory out-file)))))
275 (directory-files (or (file-name-directory out-file)
276 default-directory)))
277 (with-temp-file data-file (insert body))
278 (message (concat "java -jar " org-ditaa-jar-path " " args " " data-file " " out-file))
279 (shell-command (concat "java -jar " org-ditaa-jar-path " " args " " data-file " " out-file)))
280 (format "\n[[file:%s]]\n" out-file))
281 (t (concat
282 "\n#+BEGIN_EXAMPLE\n"
283 body (if (string-match "\n$" body) "" "\n")
284 "#+END_EXAMPLE\n")))
285 (message "begin_ditaa blocks are DEPRECATED, use begin_src blocks"))))
287 ;;--------------------------------------------------------------------------------
288 ;; dot: create graphs using the dot graphing language
289 ;; (require the dot executable to be in your path)
290 (defun org-export-blocks-format-dot (body &rest headers)
291 "DEPRECATED: use \"#+begin_src dot\" code blocks
293 Pass block BODY to the dot graphing utility creating an image.
294 Specify the path at which the image should be saved as the first
295 element of headers, any additional elements of headers will be
296 passed to the dot utility as command line arguments. Don't
297 forget to specify the output type for the dot command, so if you
298 are exporting to a file with a name like 'image.png' you should
299 include a '-Tpng' argument, and your block should look like the
300 following.
302 #+begin_dot models.png -Tpng
303 digraph data_relationships {
304 \"data_requirement\" [shape=Mrecord, label=\"{DataRequirement|description\lformat\l}\"]
305 \"data_product\" [shape=Mrecord, label=\"{DataProduct|name\lversion\lpoc\lformat\l}\"]
306 \"data_requirement\" -> \"data_product\"
308 #+end_dot"
309 (message "begin_dot blocks are DEPRECATED, use begin_src blocks")
310 (let* ((args (if (cdr headers) (mapconcat 'identity (cdr headers) " ")))
311 (data-file (make-temp-file "org-ditaa"))
312 (hash (progn
313 (set-text-properties 0 (length body) nil body)
314 (sha1 (prin1-to-string (list body args)))))
315 (raw-out-file (if headers (car headers)))
316 (out-file-parts (if (string-match "\\(.+\\)\\.\\([^\\.]+\\)$" raw-out-file)
317 (cons (match-string 1 raw-out-file)
318 (match-string 2 raw-out-file))
319 (cons raw-out-file "png")))
320 (out-file (concat (car out-file-parts) "_" hash "." (cdr out-file-parts))))
321 (prog1
322 (cond
323 ((member org-export-current-backend '(html latex docbook))
324 (unless (file-exists-p out-file)
325 (mapc ;; remove old hashed versions of this file
326 (lambda (file)
327 (when (and (string-match (concat (regexp-quote (car out-file-parts))
328 "_\\([[:alnum:]]+\\)\\."
329 (regexp-quote (cdr out-file-parts)))
330 file)
331 (= (length (match-string 1 out-file)) 40))
332 (delete-file (expand-file-name file
333 (file-name-directory out-file)))))
334 (directory-files (or (file-name-directory out-file)
335 default-directory)))
336 (with-temp-file data-file (insert body))
337 (message (concat "dot " data-file " " args " -o " out-file))
338 (shell-command (concat "dot " data-file " " args " -o " out-file)))
339 (format "\n[[file:%s]]\n" out-file))
340 (t (concat
341 "\n#+BEGIN_EXAMPLE\n"
342 body (if (string-match "\n$" body) "" "\n")
343 "#+END_EXAMPLE\n")))
344 (message "begin_dot blocks are DEPRECATED, use begin_src blocks"))))
346 ;;--------------------------------------------------------------------------------
347 ;; comment: export comments in author-specific css-stylable divs
348 (defun org-export-blocks-format-comment (body &rest headers)
349 "Format comment BODY by OWNER and return it formatted for export.
350 Currently, this only does something for HTML export, for all
351 other backends, it converts the comment into an EXAMPLE segment."
352 (let ((owner (if headers (car headers)))
353 (title (if (cdr headers) (mapconcat 'identity (cdr headers) " "))))
354 (cond
355 ((eq org-export-current-backend 'html) ;; We are exporting to HTML
356 (concat "#+BEGIN_HTML\n"
357 "<div class=\"org-comment\""
358 (if owner (format " id=\"org-comment-%s\" " owner))
359 ">\n"
360 (if owner (concat "<b>" owner "</b> ") "")
361 (if (and title (> (length title) 0)) (concat " -- " title "<br/>\n") "<br/>\n")
362 "<p>\n"
363 "#+END_HTML\n"
364 body
365 "\n#+BEGIN_HTML\n"
366 "</p>\n"
367 "</div>\n"
368 "#+END_HTML\n"))
369 (t ;; This is not HTML, so just make it an example.
370 (concat "#+BEGIN_EXAMPLE\n"
371 (if title (concat "Title:" title "\n") "")
372 (if owner (concat "By:" owner "\n") "")
373 body
374 (if (string-match "\n\\'" body) "" "\n")
375 "#+END_EXAMPLE\n")))))
377 (provide 'org-exp-blocks)
380 ;;; org-exp-blocks.el ends here