Commit | Line | Data |
---|---|---|
c8d0cf5c CD |
1 | ;;; org-exp-blocks.el --- pre-process blocks when exporting org files |
2 | ||
73b0cd50 | 3 | ;; Copyright (C) 2009-2011 |
c8d0cf5c CD |
4 | ;; Free Software Foundation, Inc. |
5 | ||
6 | ;; Author: Eric Schulte | |
acedf35c | 7 | ;; Version: 7.4 |
c8d0cf5c CD |
8 | |
9 | ;; This file is part of GNU Emacs. | |
10 | ;; | |
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. | |
15 | ||
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. | |
20 | ||
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/>. | |
23 | ||
24 | ;;; Commentary: | |
25 | ;; | |
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. | |
33 | ;; | |
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_=. | |
38 | ;; | |
39 | ;; #+begin_ditaa blue.png -r -S | |
40 | ;; +---------+ | |
41 | ;; | cBLU | | |
42 | ;; | | | |
43 | ;; | +----+ | |
44 | ;; | |cPNK| | |
45 | ;; | | | | |
46 | ;; +----+----+ | |
47 | ;; #+end_ditaa | |
48 | ;; | |
49 | ;;; Currently Implemented Block Types | |
50 | ;; | |
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) . | |
55 | ;; | |
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/ | |
59 | ;; | |
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. | |
63 | ;; | |
c8d0cf5c CD |
64 | ;;; Adding new blocks |
65 | ;; | |
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'. | |
70 | ||
86fbb8ca CD |
71 | ;;; Code: |
72 | ||
c8d0cf5c CD |
73 | (eval-when-compile |
74 | (require 'cl)) | |
75 | (require 'org) | |
76 | ||
c8d0cf5c CD |
77 | (defvar htmlp) |
78 | (defvar latexp) | |
79 | (defvar docbookp) | |
80 | (defvar asciip) | |
81 | ||
c8d0cf5c CD |
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)) | |
93 | ||
94 | (defcustom org-export-blocks | |
95 | '((comment org-export-blocks-format-comment t) | |
96 | (ditaa org-export-blocks-format-ditaa nil) | |
8bfe682a | 97 | (dot org-export-blocks-format-dot nil)) |
86fbb8ca CD |
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." | |
c8d0cf5c CD |
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) | |
109 | ||
110 | (defun org-export-blocks-add-block (block-spec) | |
86fbb8ca CD |
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. | |
c8d0cf5c CD |
119 | |
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))) | |
124 | ||
125 | (defcustom org-export-interblocks | |
8bfe682a | 126 | '() |
86fbb8ca CD |
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." | |
c8d0cf5c CD |
131 | :group 'org-export-general |
132 | :type 'alist) | |
133 | ||
134 | (defcustom org-export-blocks-witheld | |
135 | '(hidden) | |
86fbb8ca | 136 | "List of block types (see `org-export-blocks') which should not be exported." |
c8d0cf5c CD |
137 | :group 'org-export-general |
138 | :type 'list) | |
139 | ||
86fbb8ca CD |
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) | |
c8d0cf5c CD |
144 | |
145 | (defun org-export-blocks-html-quote (body &optional open close) | |
86fbb8ca CD |
146 | "Protect BODY from org html export. |
147 | The optional OPEN and CLOSE tags will be inserted around BODY." | |
148 | ||
c8d0cf5c CD |
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")) | |
155 | ||
156 | (defun org-export-blocks-latex-quote (body &optional open close) | |
86fbb8ca CD |
157 | "Protect BODY from org latex export. |
158 | The optional OPEN and CLOSE tags will be inserted around BODY." | |
c8d0cf5c CD |
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")) | |
165 | ||
166 | (defun org-export-blocks-preprocess () | |
86fbb8ca CD |
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'." | |
c8d0cf5c CD |
170 | (interactive) |
171 | (save-window-excursion | |
8bfe682a | 172 | (let ((case-fold-search t) |
c8d0cf5c | 173 | (types '()) |
ed21c5c8 | 174 | indentation type func start body headers preserve-indent progress-marker) |
8bfe682a CD |
175 | (flet ((interblock (start end) |
176 | (mapcar (lambda (pair) (funcall (second pair) start end)) | |
177 | org-export-interblocks))) | |
c8d0cf5c | 178 | (goto-char (point-min)) |
8bfe682a | 179 | (setq start (point)) |
c8d0cf5c | 180 | (while (re-search-forward |
86fbb8ca | 181 | "^\\([ \t]*\\)#\\+begin_\\(\\S-+\\)[ \t]*\\(.*\\)?[\r\n]\\([^\000]*?\\)[\r\n][ \t]*#\\+end_\\S-+.*[\r\n]?" nil t) |
8bfe682a CD |
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))) | |
ed21c5c8 CD |
191 | (when (setq func (cadr (assoc type org-export-blocks))) |
192 | (let ((replacement (save-match-data | |
8bfe682a | 193 | (if (memq type org-export-blocks-witheld) "" |
ed21c5c8 CD |
194 | (apply func body headers))))) |
195 | (when replacement | |
196 | (replace-match replacement t t) | |
8bfe682a | 197 | (unless preserve-indent |
ed21c5c8 CD |
198 | (indent-code-rigidly |
199 | (match-beginning 0) (match-end 0) indentation))))) | |
8bfe682a | 200 | (setq start (match-end 0))) |
86fbb8ca CD |
201 | (interblock start (point-max)) |
202 | (run-hooks 'org-export-blocks-postblock-hook))))) | |
c8d0cf5c | 203 | |
c8d0cf5c CD |
204 | ;;================================================================================ |
205 | ;; type specific functions | |
206 | ||
207 | ;;-------------------------------------------------------------------------------- | |
208 | ;; ditaa: create images from ASCII art using the ditaa utility | |
209 | (defvar org-ditaa-jar-path (expand-file-name | |
210 | "ditaa.jar" | |
211 | (file-name-as-directory | |
212 | (expand-file-name | |
213 | "scripts" | |
214 | (file-name-as-directory | |
215 | (expand-file-name | |
216 | "../contrib" | |
217 | (file-name-directory (or load-file-name buffer-file-name))))))) | |
86fbb8ca | 218 | "Path to the ditaa jar executable.") |
c8d0cf5c CD |
219 | |
220 | (defun org-export-blocks-format-ditaa (body &rest headers) | |
221 | "Pass block BODY to the ditaa utility creating an image. | |
222 | Specify the path at which the image should be saved as the first | |
223 | element of headers, any additional elements of headers will be | |
224 | passed to the ditaa utility as command line arguments." | |
225 | (message "ditaa-formatting...") | |
5dec9555 CD |
226 | (let* ((args (if (cdr headers) (mapconcat 'identity (cdr headers) " "))) |
227 | (data-file (make-temp-file "org-ditaa")) | |
86fbb8ca CD |
228 | (hash (progn |
229 | (set-text-properties 0 (length body) nil body) | |
230 | (sha1 (prin1-to-string (list body args))))) | |
231 | (raw-out-file (if headers (car headers))) | |
232 | (out-file-parts (if (string-match "\\(.+\\)\\.\\([^\\.]+\\)$" raw-out-file) | |
233 | (cons (match-string 1 raw-out-file) | |
234 | (match-string 2 raw-out-file)) | |
235 | (cons raw-out-file "png"))) | |
236 | (out-file (concat (car out-file-parts) "_" hash "." (cdr out-file-parts)))) | |
c8d0cf5c CD |
237 | (unless (file-exists-p org-ditaa-jar-path) |
238 | (error (format "Could not find ditaa.jar at %s" org-ditaa-jar-path))) | |
239 | (setq body (if (string-match "^\\([^:\\|:[^ ]\\)" body) | |
240 | body | |
241 | (mapconcat (lambda (x) (substring x (if (> (length x) 1) 2 1))) | |
242 | (org-split-string body "\n") | |
243 | "\n"))) | |
244 | (cond | |
245 | ((or htmlp latexp docbookp) | |
5dec9555 CD |
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))) | |
c8d0cf5c CD |
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 | ||
267 | ;;-------------------------------------------------------------------------------- | |
268 | ;; dot: create graphs using the dot graphing language | |
269 | ;; (require the dot executable to be in your path) | |
270 | (defun org-export-blocks-format-dot (body &rest headers) | |
271 | "Pass block BODY to the dot graphing utility creating an image. | |
272 | Specify the path at which the image should be saved as the first | |
273 | element of headers, any additional elements of headers will be | |
274 | passed to the dot utility as command line arguments. Don't | |
275 | forget to specify the output type for the dot command, so if you | |
276 | are exporting to a file with a name like 'image.png' you should | |
277 | include a '-Tpng' argument, and your block should look like the | |
278 | following. | |
279 | ||
280 | #+begin_dot models.png -Tpng | |
281 | digraph data_relationships { | |
282 | \"data_requirement\" [shape=Mrecord, label=\"{DataRequirement|description\lformat\l}\"] | |
283 | \"data_product\" [shape=Mrecord, label=\"{DataProduct|name\lversion\lpoc\lformat\l}\"] | |
284 | \"data_requirement\" -> \"data_product\" | |
285 | } | |
286 | #+end_dot" | |
287 | (message "dot-formatting...") | |
5dec9555 CD |
288 | (let* ((args (if (cdr headers) (mapconcat 'identity (cdr headers) " "))) |
289 | (data-file (make-temp-file "org-ditaa")) | |
86fbb8ca CD |
290 | (hash (progn |
291 | (set-text-properties 0 (length body) nil body) | |
292 | (sha1 (prin1-to-string (list body args))))) | |
293 | (raw-out-file (if headers (car headers))) | |
294 | (out-file-parts (if (string-match "\\(.+\\)\\.\\([^\\.]+\\)$" raw-out-file) | |
295 | (cons (match-string 1 raw-out-file) | |
296 | (match-string 2 raw-out-file)) | |
297 | (cons raw-out-file "png"))) | |
298 | (out-file (concat (car out-file-parts) "_" hash "." (cdr out-file-parts)))) | |
c8d0cf5c CD |
299 | (cond |
300 | ((or htmlp latexp docbookp) | |
5dec9555 CD |
301 | (unless (file-exists-p out-file) |
302 | (mapc ;; remove old hashed versions of this file | |
303 | (lambda (file) | |
304 | (when (and (string-match (concat (regexp-quote (car out-file-parts)) | |
305 | "_\\([[:alnum:]]+\\)\\." | |
306 | (regexp-quote (cdr out-file-parts))) | |
307 | file) | |
308 | (= (length (match-string 1 out-file)) 40)) | |
309 | (delete-file (expand-file-name file | |
310 | (file-name-directory out-file))))) | |
311 | (directory-files (or (file-name-directory out-file) | |
312 | default-directory))) | |
313 | (with-temp-file data-file (insert body)) | |
314 | (message (concat "dot " data-file " " args " -o " out-file)) | |
315 | (shell-command (concat "dot " data-file " " args " -o " out-file))) | |
c8d0cf5c CD |
316 | (format "\n[[file:%s]]\n" out-file)) |
317 | (t (concat | |
318 | "\n#+BEGIN_EXAMPLE\n" | |
319 | body (if (string-match "\n$" body) "" "\n") | |
320 | "#+END_EXAMPLE\n"))))) | |
321 | ||
322 | ;;-------------------------------------------------------------------------------- | |
323 | ;; comment: export comments in author-specific css-stylable divs | |
324 | (defun org-export-blocks-format-comment (body &rest headers) | |
325 | "Format comment BODY by OWNER and return it formatted for export. | |
326 | Currently, this only does something for HTML export, for all | |
327 | other backends, it converts the comment into an EXAMPLE segment." | |
328 | (let ((owner (if headers (car headers))) | |
329 | (title (if (cdr headers) (mapconcat 'identity (cdr headers) " ")))) | |
330 | (cond | |
331 | (htmlp ;; We are exporting to HTML | |
332 | (concat "#+BEGIN_HTML\n" | |
333 | "<div class=\"org-comment\"" | |
334 | (if owner (format " id=\"org-comment-%s\" " owner)) | |
335 | ">\n" | |
336 | (if owner (concat "<b>" owner "</b> ") "") | |
337 | (if (and title (> (length title) 0)) (concat " -- " title "</br>\n") "</br>\n") | |
338 | "<p>\n" | |
339 | "#+END_HTML\n" | |
340 | body | |
341 | "#+BEGIN_HTML\n" | |
342 | "</p>\n" | |
343 | "</div>\n" | |
344 | "#+END_HTML\n")) | |
345 | (t ;; This is not HTML, so just make it an example. | |
346 | (concat "#+BEGIN_EXAMPLE\n" | |
347 | (if title (concat "Title:" title "\n") "") | |
348 | (if owner (concat "By:" owner "\n") "") | |
349 | body | |
350 | (if (string-match "\n\\'" body) "" "\n") | |
351 | "#+END_EXAMPLE\n"))))) | |
352 | ||
c8d0cf5c CD |
353 | (provide 'org-exp-blocks) |
354 | ||
355 | ;;; org-exp-blocks.el ends here |