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