org-list: modifications to org-list-to-generic
[org-mode/org-mode-NeilSmithlineMods.git] / lisp / org-exp-blocks.el
blob1fac3bf05c9c67791dff26ba579fe52aa3b03b9e
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.4
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 :: (DEPRECATED--use "#+begin_src ditaa" code blocks) Convert
52 ;; ascii pictures to actual images using ditaa
53 ;; http://ditaa.sourceforge.net/. To use this set
54 ;; `org-ditaa-jar-path' to the path to ditaa.jar on your
55 ;; system (should be set automatically in most cases) .
57 ;; dot :: (DEPRECATED--use "#+begin_src dot" code blocks) Convert
58 ;; graphs defined using the dot graphing language to images
59 ;; using the dot utility. For information on dot see
60 ;; http://www.graphviz.org/
62 ;; comment :: Wrap comments with titles and author information, in
63 ;; their own divs with author-specific ids allowing for css
64 ;; coloring of comments based on the author.
66 ;;; Adding new blocks
68 ;; When adding a new block type first define a formatting function
69 ;; along the same lines as `org-export-blocks-format-dot' and then use
70 ;; `org-export-blocks-add-block' to add your block type to
71 ;; `org-export-blocks'.
73 ;;; Code:
75 (eval-when-compile
76 (require 'cl))
77 (require 'org)
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 alist to associate block types with block exporting functions.
96 The type of a block is determined by the text immediately
97 following the '#+BEGIN_' portion of the block header. Each block
98 export function should accept three arguments."
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'.
109 BLOCK-SPEC should be a three element list the first element of
110 which should indicate the name of the block, the second element
111 should be the formatting function called by
112 `org-export-blocks-preprocess' and the third element a flag
113 indicating whether these types of blocks should be fontified in
114 org-mode buffers (see `org-protecting-blocks'). For example the
115 BLOCK-SPEC for ditaa 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 functions.
125 The type of a block is determined by the text immediately
126 following the '#+BEGIN_' portion of the block header. Each block
127 export function should accept three arguments."
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 be exported."
134 :group 'org-export-general
135 :type 'list)
137 (defcustom org-export-blocks-postblock-hook nil
138 "Run after blocks have been processed with `org-export-blocks-preprocess'."
139 :group 'org-export-general
140 :type 'hook)
142 (defun org-export-blocks-html-quote (body &optional open close)
143 "Protect BODY from org html export.
144 The optional OPEN and CLOSE tags will be inserted around BODY."
146 (concat
147 "\n#+BEGIN_HTML\n"
148 (or open "")
149 body (if (string-match "\n$" body) "" "\n")
150 (or close "")
151 "#+END_HTML\n"))
153 (defun org-export-blocks-latex-quote (body &optional open close)
154 "Protect BODY from org latex export.
155 The optional OPEN and CLOSE tags will be inserted around BODY."
156 (concat
157 "\n#+BEGIN_LaTeX\n"
158 (or open "")
159 body (if (string-match "\n$" body) "" "\n")
160 (or close "")
161 "#+END_LaTeX\n"))
163 (defun org-export-blocks-preprocess ()
164 "Export all blocks according to the `org-export-blocks' block export alist.
165 Does not export block types specified in specified in BLOCKS
166 which defaults to the value of `org-export-blocks-witheld'."
167 (interactive)
168 (save-window-excursion
169 (let ((case-fold-search t)
170 (types '())
171 indentation type func start 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 (while (re-search-forward
178 "^\\([ \t]*\\)#\\+begin_\\(\\S-+\\)[ \t]*\\(.*\\)?[\r\n]\\([^\000]*?\\)[\r\n][ \t]*#\\+end_\\S-+.*[\r\n]?" nil t)
179 (setq indentation (length (match-string 1)))
180 (setq type (intern (downcase (match-string 2))))
181 (setq headers (save-match-data (org-split-string (match-string 3) "[ \t]+")))
182 (setq body (match-string 4))
183 (setq preserve-indent (or org-src-preserve-indentation (member "-i" headers)))
184 (unless preserve-indent
185 (setq body (save-match-data (org-remove-indentation body))))
186 (unless (memq type types) (setq types (cons type types)))
187 (save-match-data (interblock start (match-beginning 0)))
188 (when (setq func (cadr (assoc type org-export-blocks)))
189 (let ((replacement (save-match-data
190 (if (memq type org-export-blocks-witheld) ""
191 (apply func body headers)))))
192 (when replacement
193 (replace-match replacement t t)
194 (unless preserve-indent
195 (indent-code-rigidly
196 (match-beginning 0) (match-end 0) indentation)))))
197 (setq start (match-end 0)))
198 (interblock start (point-max))
199 (run-hooks 'org-export-blocks-postblock-hook)))))
201 ;;================================================================================
202 ;; type specific functions
204 ;;--------------------------------------------------------------------------------
205 ;; ditaa: create images from ASCII art using the ditaa utility
206 (defvar org-ditaa-jar-path (expand-file-name
207 "ditaa.jar"
208 (file-name-as-directory
209 (expand-file-name
210 "scripts"
211 (file-name-as-directory
212 (expand-file-name
213 "../contrib"
214 (file-name-directory (or load-file-name buffer-file-name)))))))
215 "Path to the ditaa jar executable.")
217 (defun org-export-blocks-format-ditaa (body &rest headers)
218 "DEPRECATED: use begin_src ditaa code blocks
220 Pass block BODY to the ditaa utility creating an image.
221 Specify the path at which the image should be saved as the first
222 element of headers, any additional elements of headers will be
223 passed to the ditaa utility as command line arguments."
224 (message "begin_ditaa blocks are DEPRECATED, use begin_src blocks")
225 (let* ((args (if (cdr headers) (mapconcat 'identity (cdr headers) " ")))
226 (data-file (make-temp-file "org-ditaa"))
227 (hash (progn
228 (set-text-properties 0 (length body) nil body)
229 (sha1 (prin1-to-string (list body args)))))
230 (raw-out-file (if headers (car headers)))
231 (out-file-parts (if (string-match "\\(.+\\)\\.\\([^\\.]+\\)$" raw-out-file)
232 (cons (match-string 1 raw-out-file)
233 (match-string 2 raw-out-file))
234 (cons raw-out-file "png")))
235 (out-file (concat (car out-file-parts) "_" hash "." (cdr out-file-parts))))
236 (unless (file-exists-p org-ditaa-jar-path)
237 (error (format "Could not find ditaa.jar at %s" org-ditaa-jar-path)))
238 (setq body (if (string-match "^\\([^:\\|:[^ ]\\)" body)
239 body
240 (mapconcat (lambda (x) (substring x (if (> (length x) 1) 2 1)))
241 (org-split-string body "\n")
242 "\n")))
243 (prog1
244 (cond
245 ((member backend '(html latex docbook))
246 (unless (file-exists-p out-file)
247 (mapc ;; remove old hashed versions of this file
248 (lambda (file)
249 (when (and (string-match (concat (regexp-quote (car out-file-parts))
250 "_\\([[:alnum:]]+\\)\\."
251 (regexp-quote (cdr out-file-parts)))
252 file)
253 (= (length (match-string 1 out-file)) 40))
254 (delete-file (expand-file-name file
255 (file-name-directory out-file)))))
256 (directory-files (or (file-name-directory out-file)
257 default-directory)))
258 (with-temp-file data-file (insert body))
259 (message (concat "java -jar " org-ditaa-jar-path " " args " " data-file " " out-file))
260 (shell-command (concat "java -jar " org-ditaa-jar-path " " args " " data-file " " out-file)))
261 (format "\n[[file:%s]]\n" out-file))
262 (t (concat
263 "\n#+BEGIN_EXAMPLE\n"
264 body (if (string-match "\n$" body) "" "\n")
265 "#+END_EXAMPLE\n")))
266 (message "begin_ditaa blocks are DEPRECATED, use begin_src blocks"))))
268 ;;--------------------------------------------------------------------------------
269 ;; dot: create graphs using the dot graphing language
270 ;; (require the dot executable to be in your path)
271 (defun org-export-blocks-format-dot (body &rest headers)
272 "DEPRECATED: use \"#+begin_src dot\" code blocks
274 Pass block BODY to the dot graphing utility creating an image.
275 Specify the path at which the image should be saved as the first
276 element of headers, any additional elements of headers will be
277 passed to the dot utility as command line arguments. Don't
278 forget to specify the output type for the dot command, so if you
279 are exporting to a file with a name like 'image.png' you should
280 include a '-Tpng' argument, and your block should look like the
281 following.
283 #+begin_dot models.png -Tpng
284 digraph data_relationships {
285 \"data_requirement\" [shape=Mrecord, label=\"{DataRequirement|description\lformat\l}\"]
286 \"data_product\" [shape=Mrecord, label=\"{DataProduct|name\lversion\lpoc\lformat\l}\"]
287 \"data_requirement\" -> \"data_product\"
289 #+end_dot"
290 (message "begin_dot blocks are DEPRECATED, use begin_src blocks")
291 (let* ((args (if (cdr headers) (mapconcat 'identity (cdr headers) " ")))
292 (data-file (make-temp-file "org-ditaa"))
293 (hash (progn
294 (set-text-properties 0 (length body) nil body)
295 (sha1 (prin1-to-string (list body args)))))
296 (raw-out-file (if headers (car headers)))
297 (out-file-parts (if (string-match "\\(.+\\)\\.\\([^\\.]+\\)$" raw-out-file)
298 (cons (match-string 1 raw-out-file)
299 (match-string 2 raw-out-file))
300 (cons raw-out-file "png")))
301 (out-file (concat (car out-file-parts) "_" hash "." (cdr out-file-parts))))
302 (prog1
303 (cond
304 ((member backend '(html latex docbook))
305 (unless (file-exists-p out-file)
306 (mapc ;; remove old hashed versions of this file
307 (lambda (file)
308 (when (and (string-match (concat (regexp-quote (car out-file-parts))
309 "_\\([[:alnum:]]+\\)\\."
310 (regexp-quote (cdr out-file-parts)))
311 file)
312 (= (length (match-string 1 out-file)) 40))
313 (delete-file (expand-file-name file
314 (file-name-directory out-file)))))
315 (directory-files (or (file-name-directory out-file)
316 default-directory)))
317 (with-temp-file data-file (insert body))
318 (message (concat "dot " data-file " " args " -o " out-file))
319 (shell-command (concat "dot " data-file " " args " -o " out-file)))
320 (format "\n[[file:%s]]\n" out-file))
321 (t (concat
322 "\n#+BEGIN_EXAMPLE\n"
323 body (if (string-match "\n$" body) "" "\n")
324 "#+END_EXAMPLE\n")))
325 (message "begin_dot blocks are DEPRECATED, use begin_src blocks"))))
327 ;;--------------------------------------------------------------------------------
328 ;; comment: export comments in author-specific css-stylable divs
329 (defun org-export-blocks-format-comment (body &rest headers)
330 "Format comment BODY by OWNER and return it formatted for export.
331 Currently, this only does something for HTML export, for all
332 other backends, it converts the comment into an EXAMPLE segment."
333 (let ((owner (if headers (car headers)))
334 (title (if (cdr headers) (mapconcat 'identity (cdr headers) " "))))
335 (cond
336 ((eq backend 'html) ;; We are exporting to HTML
337 (concat "#+BEGIN_HTML\n"
338 "<div class=\"org-comment\""
339 (if owner (format " id=\"org-comment-%s\" " owner))
340 ">\n"
341 (if owner (concat "<b>" owner "</b> ") "")
342 (if (and title (> (length title) 0)) (concat " -- " title "</br>\n") "</br>\n")
343 "<p>\n"
344 "#+END_HTML\n"
345 body
346 "#+BEGIN_HTML\n"
347 "</p>\n"
348 "</div>\n"
349 "#+END_HTML\n"))
350 (t ;; This is not HTML, so just make it an example.
351 (concat "#+BEGIN_EXAMPLE\n"
352 (if title (concat "Title:" title "\n") "")
353 (if owner (concat "By:" owner "\n") "")
354 body
355 (if (string-match "\n\\'" body) "" "\n")
356 "#+END_EXAMPLE\n")))))
358 (provide 'org-exp-blocks)
360 ;; arch-tag: 1c365fe9-8808-4f72-bb15-0b00f36d8024
361 ;;; org-exp-blocks.el ends here