1 ;;; org-babel-exp.el --- Exportation of org-babel source blocks
3 ;; Copyright (C) 2009 Eric Schulte
5 ;; Author: Eric Schulte
6 ;; Keywords: literate programming, reproducible research
7 ;; Homepage: http://orgmode.org
12 ;; This program is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 3, or (at your option)
17 ;; This program is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
25 ;; Boston, MA 02110-1301, USA.
29 ;; for more information see the comments in org-babel.el
33 (require 'org-exp-blocks
)
34 (org-export-blocks-add-block '(src org-babel-exp-src-blocks nil
))
35 (add-to-list 'org-export-interblocks
'(src org-babel-exp-inline-src-blocks
))
37 (defun org-babel-exp-src-blocks (body &rest headers
)
38 "Process src block for export. Depending on the 'export'
39 headers argument in replace the source code block with...
41 both ---- display the code and the results
43 code ---- the default, display the code inside the block but do
46 results - just like none only the block is run on export ensuring
47 that it's results are present in the org-mode buffer
49 none ----- do not display either code or results upon export"
51 (unless headers
(error "org-babel can't process a source block without knowing the source code"))
52 (message "org-babel-exp processing...")
53 (let* ((lang (car headers
))
54 (lang-headers (intern (concat "org-babel-default-header-args:" lang
)))
55 (params (org-babel-merge-params
56 org-babel-default-header-args
57 (org-babel-params-from-properties)
58 (if (boundp lang-headers
) (eval lang-headers
) nil
)
59 (org-babel-parse-header-arguments
60 (mapconcat #'identity
(cdr headers
) " ")))))
61 (org-babel-exp-do-export lang body params
)))
63 (defun org-babel-exp-inline-src-blocks (start end
)
64 "Process inline src blocks between START and END for export.
65 See `org-babel-exp-src-blocks' for export options, currently the
66 options and are taken from `org-babel-defualt-inline-header-args'."
70 (while (and (< (point) end
) (re-search-forward org-babel-inline-src-block-regexp end t
))
71 (let* ((info (save-match-data (org-babel-parse-inline-src-block-match)))
72 (replacement (save-match-data
73 (org-babel-exp-do-export (first info
) (second info
) (third info
) t
))))
74 (setf end
(+ end
(- (length replacement
)
75 (+ 6 (length (first info
)) (length (second info
))))))
76 (replace-match replacement t t
)))))
78 (defun org-babel-exp-do-export (lang body params
&optional inline
)
79 (case (intern (or (cdr (assoc :exports params
)) "code"))
81 ('code
(org-babel-exp-code body lang params inline
))
82 ('results
(save-excursion
83 ;; org-exp-blocks places us at the end of the block
84 (re-search-backward org-babel-src-block-regexp nil t
)
85 (org-babel-execute-src-block) ""))
86 ('both
(concat (org-babel-exp-code body lang params inline
)
88 (org-babel-exp-results body lang params inline
)))))
90 (defun org-babel-exp-code (body lang params
&optional inline
)
93 (format "#+BEGIN_SRC %s\n%s%s\n#+END_SRC" lang body
94 (if (string-match "\n$" body
) "" "\n"))))
96 (provide 'org-babel-exp
)
97 ;;; org-babel-exp.el ends here