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