org.texi (Custom Table Styles - An Illustration): New node
[org-mode/org-kjn.git] / lisp / org-exp-blocks.el
blob940068aee0e8e0ebe8cefec5033c419cb72cd2e5
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 (copy-marker (match-beginning 0)))
179 (body-start (copy-marker (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 (copy-marker (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 ;; cleanup markers
215 (set-marker match-start nil)
216 (set-marker body-start nil)
217 (set-marker match-end nil))
218 (setq start (point))))
219 (interblock start (point-max))
220 (run-hooks 'org-export-blocks-postblock-hook)))))
222 ;;================================================================================
223 ;; type specific functions
225 ;;--------------------------------------------------------------------------------
226 ;; ditaa: create images from ASCII art using the ditaa utility
227 (defvar org-ditaa-jar-path (expand-file-name
228 "ditaa.jar"
229 (file-name-as-directory
230 (expand-file-name
231 "scripts"
232 (file-name-as-directory
233 (expand-file-name
234 "../contrib"
235 (file-name-directory (or load-file-name buffer-file-name)))))))
236 "Path to the ditaa jar executable.")
238 (defvar org-export-current-backend) ; dynamically bound in org-exp.el
239 (defun org-export-blocks-format-ditaa (body &rest headers)
240 "DEPRECATED: use begin_src ditaa code blocks
242 Pass block BODY to the ditaa utility creating an image.
243 Specify the path at which the image should be saved as the first
244 element of headers, any additional elements of headers will be
245 passed to the ditaa utility as command line arguments."
246 (message "begin_ditaa blocks are DEPRECATED, use begin_src blocks")
247 (let* ((args (if (cdr headers) (mapconcat 'identity (cdr headers) " ")))
248 (data-file (make-temp-file "org-ditaa"))
249 (hash (progn
250 (set-text-properties 0 (length body) nil body)
251 (sha1 (prin1-to-string (list body args)))))
252 (raw-out-file (if headers (car headers)))
253 (out-file-parts (if (string-match "\\(.+\\)\\.\\([^\\.]+\\)$" raw-out-file)
254 (cons (match-string 1 raw-out-file)
255 (match-string 2 raw-out-file))
256 (cons raw-out-file "png")))
257 (out-file (concat (car out-file-parts) "_" hash "." (cdr out-file-parts))))
258 (unless (file-exists-p org-ditaa-jar-path)
259 (error (format "Could not find ditaa.jar at %s" org-ditaa-jar-path)))
260 (setq body (if (string-match "^\\([^:\\|:[^ ]\\)" body)
261 body
262 (mapconcat (lambda (x) (substring x (if (> (length x) 1) 2 1)))
263 (org-split-string body "\n")
264 "\n")))
265 (prog1
266 (cond
267 ((member org-export-current-backend '(html latex docbook))
268 (unless (file-exists-p out-file)
269 (mapc ;; remove old hashed versions of this file
270 (lambda (file)
271 (when (and (string-match (concat (regexp-quote (car out-file-parts))
272 "_\\([[:alnum:]]+\\)\\."
273 (regexp-quote (cdr out-file-parts)))
274 file)
275 (= (length (match-string 1 out-file)) 40))
276 (delete-file (expand-file-name file
277 (file-name-directory out-file)))))
278 (directory-files (or (file-name-directory out-file)
279 default-directory)))
280 (with-temp-file data-file (insert body))
281 (message (concat "java -jar " org-ditaa-jar-path " " args " " data-file " " out-file))
282 (shell-command (concat "java -jar " org-ditaa-jar-path " " args " " data-file " " out-file)))
283 (format "\n[[file:%s]]\n" out-file))
284 (t (concat
285 "\n#+BEGIN_EXAMPLE\n"
286 body (if (string-match "\n$" body) "" "\n")
287 "#+END_EXAMPLE\n")))
288 (message "begin_ditaa blocks are DEPRECATED, use begin_src blocks"))))
290 ;;--------------------------------------------------------------------------------
291 ;; dot: create graphs using the dot graphing language
292 ;; (require the dot executable to be in your path)
293 (defun org-export-blocks-format-dot (body &rest headers)
294 "DEPRECATED: use \"#+begin_src dot\" code blocks
296 Pass block BODY to the dot graphing utility creating an image.
297 Specify the path at which the image should be saved as the first
298 element of headers, any additional elements of headers will be
299 passed to the dot utility as command line arguments. Don't
300 forget to specify the output type for the dot command, so if you
301 are exporting to a file with a name like 'image.png' you should
302 include a '-Tpng' argument, and your block should look like the
303 following.
305 #+begin_dot models.png -Tpng
306 digraph data_relationships {
307 \"data_requirement\" [shape=Mrecord, label=\"{DataRequirement|description\lformat\l}\"]
308 \"data_product\" [shape=Mrecord, label=\"{DataProduct|name\lversion\lpoc\lformat\l}\"]
309 \"data_requirement\" -> \"data_product\"
311 #+end_dot"
312 (message "begin_dot blocks are DEPRECATED, use begin_src blocks")
313 (let* ((args (if (cdr headers) (mapconcat 'identity (cdr headers) " ")))
314 (data-file (make-temp-file "org-ditaa"))
315 (hash (progn
316 (set-text-properties 0 (length body) nil body)
317 (sha1 (prin1-to-string (list body args)))))
318 (raw-out-file (if headers (car headers)))
319 (out-file-parts (if (string-match "\\(.+\\)\\.\\([^\\.]+\\)$" raw-out-file)
320 (cons (match-string 1 raw-out-file)
321 (match-string 2 raw-out-file))
322 (cons raw-out-file "png")))
323 (out-file (concat (car out-file-parts) "_" hash "." (cdr out-file-parts))))
324 (prog1
325 (cond
326 ((member org-export-current-backend '(html latex docbook))
327 (unless (file-exists-p out-file)
328 (mapc ;; remove old hashed versions of this file
329 (lambda (file)
330 (when (and (string-match (concat (regexp-quote (car out-file-parts))
331 "_\\([[:alnum:]]+\\)\\."
332 (regexp-quote (cdr out-file-parts)))
333 file)
334 (= (length (match-string 1 out-file)) 40))
335 (delete-file (expand-file-name file
336 (file-name-directory out-file)))))
337 (directory-files (or (file-name-directory out-file)
338 default-directory)))
339 (with-temp-file data-file (insert body))
340 (message (concat "dot " data-file " " args " -o " out-file))
341 (shell-command (concat "dot " data-file " " args " -o " out-file)))
342 (format "\n[[file:%s]]\n" out-file))
343 (t (concat
344 "\n#+BEGIN_EXAMPLE\n"
345 body (if (string-match "\n$" body) "" "\n")
346 "#+END_EXAMPLE\n")))
347 (message "begin_dot blocks are DEPRECATED, use begin_src blocks"))))
349 ;;--------------------------------------------------------------------------------
350 ;; comment: export comments in author-specific css-stylable divs
351 (defun org-export-blocks-format-comment (body &rest headers)
352 "Format comment BODY by OWNER and return it formatted for export.
353 Currently, this only does something for HTML export, for all
354 other backends, it converts the comment into an EXAMPLE segment."
355 (let ((owner (if headers (car headers)))
356 (title (if (cdr headers) (mapconcat 'identity (cdr headers) " "))))
357 (cond
358 ((eq org-export-current-backend 'html) ;; We are exporting to HTML
359 (concat "#+BEGIN_HTML\n"
360 "<div class=\"org-comment\""
361 (if owner (format " id=\"org-comment-%s\" " owner))
362 ">\n"
363 (if owner (concat "<b>" owner "</b> ") "")
364 (if (and title (> (length title) 0)) (concat " -- " title "<br/>\n") "<br/>\n")
365 "<p>\n"
366 "#+END_HTML\n"
367 body
368 "\n#+BEGIN_HTML\n"
369 "</p>\n"
370 "</div>\n"
371 "#+END_HTML\n"))
372 (t ;; This is not HTML, so just make it an example.
373 (concat "#+BEGIN_EXAMPLE\n"
374 (if title (concat "Title:" title "\n") "")
375 (if owner (concat "By:" owner "\n") "")
376 body
377 (if (string-match "\n\\'" body) "" "\n")
378 "#+END_EXAMPLE\n")))))
380 (provide 'org-exp-blocks)
382 ;;; org-exp-blocks.el ends here