1 #+TITLE: org-exp-blocks.el --- pre-process blocks when exporting org files
2 #+OPTIONS: ^:{} author:nil toc:2
7 =org-exp-blocks= can be used to pre-process blocks when exporting org
8 files. An extensible framework for block exportation is provided, as
9 well as block exporters for [[http://ditaa.sourceforge.net/][ditaa]], [[http://www.graphviz.org/][dot]], comments, and [[http://www.r-project.org/][R]] code in a
10 manner similar to [[http://en.wikipedia.org/wiki/Sweave][Sweave]].
16 To use =org-exp-blocks= first [[* Loading it][load it]] as described below. Then try
21 [[http://ditaa.sourceforge.net/][ditaa]] is a tool for converting ASCII images into actual images. I
22 believe ditaa is distributed with newer versions of org-mode. To make
23 sure that you have ditaa installed check the value of
24 `org-ditaa-jar-path', it should point to a =ditaa.jar= file. Once
25 ditaa is installed and the `org-ditaa-jar-path' variable is set
26 appropriately copy the following block in an org-mode file
28 : #+begin_ditaa blue.png -r -S
38 Then export that file to HTML or LaTeX. You should see an image like
39 the following appear in the exported file.
41 [[file:../images/org-exp-blocks/blue.png]]
46 dot is a language for representing structural information as diagrams
47 of abstract graphs and networks. It is part of [[http://www.graphviz.org/][Graphviz]]. To try out
48 =org-exp-blocks= dot export install the =dot= shell command on your
49 system, copy the following into an org-mode file
51 : #+begin_dot dot.png -Tpng
52 : digraph data_relationships {
57 : "HTML" [shape=Mrecord, label="{HTML|publish on the web\l}"]
58 : "LaTeX" [shape=Mrecord, label="{LaTeX|publish in PDF\l}"]
59 : "org-mode" -> "org-exp-blocks"
61 : "ditaa" -> "org-mode"
62 : "org-exp-blocks" -> "HTML"
63 : "org-exp-blocks" -> LaTeX
67 Then export that file to HTML or LaTeX. You should see an image like
68 the following appear in the exported file.
70 [[file:../images/org-exp-blocks/dot.png]]
73 *** Loading it (No surprises here)
76 M-x customize-apropos org-modules
78 Check the line for exp-blocks. This will cause it to be loaded every
79 time you start org-mode.
81 You'll still have to load it manually the first time.
83 Of course, you can also just try it out by loading it manually.
85 If you prefer to manually customize your emacs then make sure that the
86 path to org's contrib directory is in your load-path and add the
87 following to your =.emacs=.
89 : (require 'org-exp-blocks)
92 *** Adding new source-code types
93 =org-exp-blocks= is extensible. If you would like to add a new block
94 type code to =org-exp-blocks= you may do so by defining an export
95 function for that block which will be called by
96 `org-export-blocks-preprocess'. Then add the block name, and the name
97 of the function to the `org-export-blocks' variable.
99 If you add a new block type, and get it working please share your
100 changes with the mailing list or post them [[additional-block-types][here]].
104 =org-exp-blocks= was developed by Eric Schulte with much-appreciated
105 help from Carsten Dominik.
107 * Additional Block Types
108 #<<additional-block-types>>
112 Asymptote is a "powerful descriptive vector graphics language for
113 technical drawing". For more information see
114 [[http://asymptote.sourceforge.net/]].
118 The following can be used to add asymptote support to
121 #+begin_src emacs-lisp
122 (setq org-export-blocks
123 (cons '(asy org-export-blocks-format-asy) org-export-blocks))
125 (defun org-export-blocks-format-asy (body &rest headers)
126 "Pass block BODY to the asy utility creating an image.
127 Specify the path at which the image should be saved as the first
128 element of headers, any additional elements of headers will be
129 passed to the asy utility as command line arguments. The default
130 output format is pdf, but you can specify any format supported by
131 Imagemagick convert program with '-f outformat'."
132 (message "asy-formatting...")
133 (let* ((out-file (if headers (car headers)))
134 (format (or (and (string-match ".+\\.\\(.+\\)" out-file)
135 (match-string 1 out-file))
137 (args (if (cdr headers) (mapconcat 'identity (cdr headers) " ")))
138 (data-file (make-temp-file "org-asy")))
139 (setq body (if (string-match "^\\([^:\\|:[^ ]\\)" body)
141 (mapconcat (lambda (x) (substring x (if (> (length x) 1) 2 1)))
142 (org-split-string body "\n")
146 (with-temp-file data-file (insert body))
147 (message (concat "asy -globalwrite -f " format " -o " out-file " " args " " data-file))
148 (shell-command (concat "asy -globalwrite -f " format " -o " out-file " " args " " data-file))
149 (format "\n[[file:%s]]\n" out-file))
151 "\n#+BEGIN_EXAMPLE\n"
152 body (if (string-match "\n$" body) "" "\n")
153 "#+END_EXAMPLE\n")))))
157 Here is a simple asymptote block :
159 : #+begin_asy out.png
164 : real f(real t) {return 1+cos(t);}
166 : path g=polargraph(f,0,2pi,operator ..)--cycle;
169 : xaxis("$x$",above=true);
170 : yaxis("$y$",above=true);
172 : dot("$(a,0)$",(1,0),N);
173 : dot("$(2a,0)$",(2,0),N+E);
176 The output should be [[file:../images/org-exp-blocks/cardioid.png]]
179 Thanks to Nicolas Goaziou for adding support for asymptote.
182 While dot is capable of generating pdf images directly the results are
183 more pleasing when =dot= is used to generate an eps file and
184 =epstopdf= is used to generate the actual pdf.
186 The following block type takes the name of a file, and generates both
187 and EPS and a PDF file at that base name.
190 The following can be used to add =dot-and-eps= block support to
193 #+begin_src emacs-lisp
194 (defun org-export-blocks-format-dot-and-eps (body &rest headers)
195 "Pass block BODY to the dot graphing utility creating an eps
196 file which is then processed by eps to create a pdf. Specify the
197 path at which the final pdf image should be created as the first
198 element of headers, any additional elements of headers will be
199 passed to the dot utility as command line arguments.
201 #+begin_dot_and_eps duh
207 (message "dot-and-eps-formatting...")
208 (let ((out-file (if headers (car headers)))
209 (args (if (cdr headers) (mapconcat 'identity (cdr headers) " ")))
210 (data-file (make-temp-file "org-dot")))
212 ((or htmlp latexp docbookp)
213 (with-temp-file data-file (insert body))
214 (shell-command (message (concat "dot -Teps " data-file " " args " -o " out-file ".eps")))
215 (shell-command (message (concat "epstopdf " out-file ".eps")))
216 (format "\n[[file:%s.pdf]]\n" out-file))
218 "\n#+BEGIN_EXAMPLE\n"
219 body (if (string-match "\n$" body) "" "\n")
220 "#+END_EXAMPLE\n")))))
222 (org-export-blocks-add-block '(dot-and-eps org-export-blocks-format-dot-and-eps nil))
226 Here is an example =dot-and-eps= block
228 : #+begin_dot-and-eps out-w-eps
236 Thanks to Russell Adams for noticing this need, and supplying the