Release 6.35b
[org-mode/org-tableheadings.git] / lisp / org-exp-blocks.el
blobaf53b60127053b9ad4298b9e1d67d64d52839dcd
1 ;;; org-exp-blocks.el --- pre-process blocks when exporting org files
3 ;; Copyright (C) 2009
4 ;; Free Software Foundation, Inc.
6 ;; Author: Eric Schulte
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 :: Convert 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 :: Convert graphs defined using the dot graphing language to
56 ;; images using the dot utility. For information on dot see
57 ;; http://www.graphviz.org/
59 ;; comment :: Wrap comments with titles and author information, in
60 ;; their own divs with author-specific ids allowing for css
61 ;; coloring of comments based on the author.
63 ;;; Adding new blocks
65 ;; When adding a new block type first define a formatting function
66 ;; along the same lines as `org-export-blocks-format-dot' and then use
67 ;; `org-export-blocks-add-block' to add your block type to
68 ;; `org-export-blocks'.
70 (eval-when-compile
71 (require 'cl))
72 (require 'org)
74 (defvar htmlp)
75 (defvar latexp)
76 (defvar docbookp)
77 (defvar asciip)
79 (defun org-export-blocks-set (var value)
80 "Set the value of `org-export-blocks' and install fontification."
81 (set var value)
82 (mapc (lambda (spec)
83 (if (nth 2 spec)
84 (setq org-protecting-blocks
85 (delete (symbol-name (car spec))
86 org-protecting-blocks))
87 (add-to-list 'org-protecting-blocks
88 (symbol-name (car spec)))))
89 value))
91 (defcustom org-export-blocks
92 '((comment org-export-blocks-format-comment t)
93 (ditaa org-export-blocks-format-ditaa nil)
94 (dot org-export-blocks-format-dot nil))
95 "Use this a-list to associate block types with block exporting
96 functions. The type of a block is determined by the text
97 immediately following the '#+BEGIN_' portion of the block header.
98 Each block export function should accept three argumets..."
99 :group 'org-export-general
100 :type '(repeat
101 (list
102 (symbol :tag "Block name")
103 (function :tag "Block formatter")
104 (boolean :tag "Fontify content as Org syntax")))
105 :set 'org-export-blocks-set)
107 (defun org-export-blocks-add-block (block-spec)
108 "Add a new block type to `org-export-blocks'. BLOCK-SPEC
109 should be a three element list the first element of which should
110 indicate the name of the block, the second element should be the
111 formatting function called by `org-export-blocks-preprocess' and
112 the third element a flag indicating whether these types of blocks
113 should be fontified in org-mode buffers (see
114 `org-protecting-blocks'). For example the BLOCK-SPEC for ditaa
115 blocks is as follows...
117 (ditaa org-export-blocks-format-ditaa nil)"
118 (unless (member block-spec org-export-blocks)
119 (setq org-export-blocks (cons block-spec org-export-blocks))
120 (org-export-blocks-set 'org-export-blocks org-export-blocks)))
122 (defcustom org-export-interblocks
124 "Use this a-list to associate block types with block exporting
125 functions. The type of a block is determined by the text
126 immediately following the '#+BEGIN_' portion of the block header.
127 Each block export function should accept three argumets..."
128 :group 'org-export-general
129 :type 'alist)
131 (defcustom org-export-blocks-witheld
132 '(hidden)
133 "List of block types (see `org-export-blocks') which should not
134 be exported."
135 :group 'org-export-general
136 :type 'list)
138 (defvar org-export-blocks-postblock-hooks nil "")
140 (defun org-export-blocks-html-quote (body &optional open close)
141 "Protext BODY from org html export. The optional OPEN and
142 CLOSE tags will be inserted around BODY."
143 (concat
144 "\n#+BEGIN_HTML\n"
145 (or open "")
146 body (if (string-match "\n$" body) "" "\n")
147 (or close "")
148 "#+END_HTML\n"))
150 (defun org-export-blocks-latex-quote (body &optional open close)
151 "Protext BODY from org latex export. The optional OPEN and
152 CLOSE tags will be inserted around BODY."
153 (concat
154 "\n#+BEGIN_LaTeX\n"
155 (or open "")
156 body (if (string-match "\n$" body) "" "\n")
157 (or close "")
158 "#+END_LaTeX\n"))
160 (defun org-export-blocks-preprocess ()
161 "Export all blocks according to the `org-export-blocks' block
162 exportation alist. Does not export block types specified in
163 specified in BLOCKS which default to the value of
164 `org-export-blocks-witheld'."
165 (interactive)
166 (save-window-excursion
167 (let ((case-fold-search t)
168 (types '())
169 indentation type func start body headers preserve-indent progress-marker)
170 (flet ((interblock (start end)
171 (mapcar (lambda (pair) (funcall (second pair) start end))
172 org-export-interblocks)))
173 (goto-char (point-min))
174 (setq start (point))
175 (while (re-search-forward
176 "^\\([ \t]*\\)#\\+begin_\\(\\S-+\\)[ \t]*\\(.*\\)?[\r\n]\\([^\000]*?\\)[\r\n][ \t]*#\\+end_\\S-+.*" nil t)
177 (setq indentation (length (match-string 1)))
178 (setq type (intern (downcase (match-string 2))))
179 (setq headers (save-match-data (org-split-string (match-string 3) "[ \t]+")))
180 (setq body (match-string 4))
181 (setq preserve-indent (or org-src-preserve-indentation (member "-i" headers)))
182 (unless preserve-indent
183 (setq body (save-match-data (org-remove-indentation body))))
184 (unless (memq type types) (setq types (cons type types)))
185 (save-match-data (interblock start (match-beginning 0)))
186 (when (setq func (cadr (assoc type org-export-blocks)))
187 (let ((replacement (save-match-data
188 (if (memq type org-export-blocks-witheld) ""
189 (apply func body headers)))))
190 (when replacement
191 (replace-match replacement t t)
192 (unless preserve-indent
193 (indent-code-rigidly
194 (match-beginning 0) (match-end 0) indentation)))))
195 (setq start (match-end 0)))
196 (interblock start (point-max))))))
198 (add-hook 'org-export-preprocess-hook 'org-export-blocks-preprocess)
200 ;;================================================================================
201 ;; type specific functions
203 ;;--------------------------------------------------------------------------------
204 ;; ditaa: create images from ASCII art using the ditaa utility
205 (defvar org-ditaa-jar-path (expand-file-name
206 "ditaa.jar"
207 (file-name-as-directory
208 (expand-file-name
209 "scripts"
210 (file-name-as-directory
211 (expand-file-name
212 "../contrib"
213 (file-name-directory (or load-file-name buffer-file-name)))))))
214 "Path to the ditaa jar executable")
216 (defun org-export-blocks-format-ditaa (body &rest headers)
217 "Pass block BODY to the ditaa utility creating an image.
218 Specify the path at which the image should be saved as the first
219 element of headers, any additional elements of headers will be
220 passed to the ditaa utility as command line arguments."
221 (message "ditaa-formatting...")
222 (let* ((args (if (cdr headers) (mapconcat 'identity (cdr headers) " ")))
223 (data-file (make-temp-file "org-ditaa"))
224 (hash (sha1 (prin1-to-string (list body args))))
225 (raw-out-file (if headers (car headers)))
226 (out-file-parts (if (string-match "\\(.+\\)\\.\\([^\\.]+\\)$" raw-out-file)
227 (cons (match-string 1 raw-out-file)
228 (match-string 2 raw-out-file))
229 (cons raw-out-file "png")))
230 (out-file (concat (car out-file-parts) "_" hash "." (cdr out-file-parts))))
231 (unless (file-exists-p org-ditaa-jar-path)
232 (error (format "Could not find ditaa.jar at %s" org-ditaa-jar-path)))
233 (setq body (if (string-match "^\\([^:\\|:[^ ]\\)" body)
234 body
235 (mapconcat (lambda (x) (substring x (if (> (length x) 1) 2 1)))
236 (org-split-string body "\n")
237 "\n")))
238 (cond
239 ((or htmlp latexp docbookp)
240 (unless (file-exists-p out-file)
241 (mapc ;; remove old hashed versions of this file
242 (lambda (file)
243 (when (and (string-match (concat (regexp-quote (car out-file-parts))
244 "_\\([[:alnum:]]+\\)\\."
245 (regexp-quote (cdr out-file-parts)))
246 file)
247 (= (length (match-string 1 out-file)) 40))
248 (delete-file (expand-file-name file
249 (file-name-directory out-file)))))
250 (directory-files (or (file-name-directory out-file)
251 default-directory)))
252 (with-temp-file data-file (insert body))
253 (message (concat "java -jar " org-ditaa-jar-path " " args " " data-file " " out-file))
254 (shell-command (concat "java -jar " org-ditaa-jar-path " " args " " data-file " " out-file)))
255 (format "\n[[file:%s]]\n" out-file))
256 (t (concat
257 "\n#+BEGIN_EXAMPLE\n"
258 body (if (string-match "\n$" body) "" "\n")
259 "#+END_EXAMPLE\n")))))
261 ;;--------------------------------------------------------------------------------
262 ;; dot: create graphs using the dot graphing language
263 ;; (require the dot executable to be in your path)
264 (defun org-export-blocks-format-dot (body &rest headers)
265 "Pass block BODY to the dot graphing utility creating an image.
266 Specify the path at which the image should be saved as the first
267 element of headers, any additional elements of headers will be
268 passed to the dot utility as command line arguments. Don't
269 forget to specify the output type for the dot command, so if you
270 are exporting to a file with a name like 'image.png' you should
271 include a '-Tpng' argument, and your block should look like the
272 following.
274 #+begin_dot models.png -Tpng
275 digraph data_relationships {
276 \"data_requirement\" [shape=Mrecord, label=\"{DataRequirement|description\lformat\l}\"]
277 \"data_product\" [shape=Mrecord, label=\"{DataProduct|name\lversion\lpoc\lformat\l}\"]
278 \"data_requirement\" -> \"data_product\"
280 #+end_dot"
281 (message "dot-formatting...")
282 (let* ((args (if (cdr headers) (mapconcat 'identity (cdr headers) " ")))
283 (data-file (make-temp-file "org-ditaa"))
284 (hash (sha1 (prin1-to-string (list body args))))
285 (raw-out-file (if headers (car headers)))
286 (out-file-parts (if (string-match "\\(.+\\)\\.\\([^\\.]+\\)$" raw-out-file)
287 (cons (match-string 1 raw-out-file)
288 (match-string 2 raw-out-file))
289 (cons raw-out-file "png")))
290 (out-file (concat (car out-file-parts) "_" hash "." (cdr out-file-parts))))
291 (cond
292 ((or htmlp latexp docbookp)
293 (unless (file-exists-p out-file)
294 (mapc ;; remove old hashed versions of this file
295 (lambda (file)
296 (when (and (string-match (concat (regexp-quote (car out-file-parts))
297 "_\\([[:alnum:]]+\\)\\."
298 (regexp-quote (cdr out-file-parts)))
299 file)
300 (= (length (match-string 1 out-file)) 40))
301 (delete-file (expand-file-name file
302 (file-name-directory out-file)))))
303 (directory-files (or (file-name-directory out-file)
304 default-directory)))
305 (with-temp-file data-file (insert body))
306 (message (concat "dot " data-file " " args " -o " out-file))
307 (shell-command (concat "dot " data-file " " args " -o " out-file)))
308 (format "\n[[file:%s]]\n" out-file))
309 (t (concat
310 "\n#+BEGIN_EXAMPLE\n"
311 body (if (string-match "\n$" body) "" "\n")
312 "#+END_EXAMPLE\n")))))
314 ;;--------------------------------------------------------------------------------
315 ;; comment: export comments in author-specific css-stylable divs
316 (defun org-export-blocks-format-comment (body &rest headers)
317 "Format comment BODY by OWNER and return it formatted for export.
318 Currently, this only does something for HTML export, for all
319 other backends, it converts the comment into an EXAMPLE segment."
320 (let ((owner (if headers (car headers)))
321 (title (if (cdr headers) (mapconcat 'identity (cdr headers) " "))))
322 (cond
323 (htmlp ;; We are exporting to HTML
324 (concat "#+BEGIN_HTML\n"
325 "<div class=\"org-comment\""
326 (if owner (format " id=\"org-comment-%s\" " owner))
327 ">\n"
328 (if owner (concat "<b>" owner "</b> ") "")
329 (if (and title (> (length title) 0)) (concat " -- " title "</br>\n") "</br>\n")
330 "<p>\n"
331 "#+END_HTML\n"
332 body
333 "#+BEGIN_HTML\n"
334 "</p>\n"
335 "</div>\n"
336 "#+END_HTML\n"))
337 (t ;; This is not HTML, so just make it an example.
338 (concat "#+BEGIN_EXAMPLE\n"
339 (if title (concat "Title:" title "\n") "")
340 (if owner (concat "By:" owner "\n") "")
341 body
342 (if (string-match "\n\\'" body) "" "\n")
343 "#+END_EXAMPLE\n")))))
345 (provide 'org-exp-blocks)
347 ;; arch-tag: 1c365fe9-8808-4f72-bb15-0b00f36d8024
348 ;;; org-exp-blocks.el ends here