org-babel-exp.el 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. ;;; org-babel-exp.el --- Exportation of org-babel source blocks
  2. ;; Copyright (C) 2009 Eric Schulte
  3. ;; Author: Eric Schulte
  4. ;; Keywords: literate programming, reproducible research
  5. ;; Homepage: http://orgmode.org
  6. ;; Version: 0.01
  7. ;;; License:
  8. ;; This program is free software; you can redistribute it and/or modify
  9. ;; it under the terms of the GNU General Public License as published by
  10. ;; the Free Software Foundation; either version 3, or (at your option)
  11. ;; any later version.
  12. ;;
  13. ;; This program is distributed in the hope that it will be useful,
  14. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. ;; GNU General Public License for more details.
  17. ;;
  18. ;; You should have received a copy of the GNU General Public License
  19. ;; along with GNU Emacs; see the file COPYING. If not, write to the
  20. ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  21. ;; Boston, MA 02110-1301, USA.
  22. ;;; Commentary:
  23. ;; for more information see the comments in org-babel.el
  24. ;;; Code:
  25. (require 'org-babel)
  26. (require 'org-exp-blocks)
  27. (org-export-blocks-add-block '(src org-babel-exp-src-blocks nil))
  28. (add-to-list 'org-export-interblocks '(src org-babel-exp-inline-src-blocks))
  29. (defun org-babel-exp-src-blocks (body &rest headers)
  30. "Process src block for export. Depending on the 'export'
  31. headers argument in replace the source code block with...
  32. both ---- display the code and the results
  33. code ---- the default, display the code inside the block but do
  34. not process
  35. results - process the block and replace it with the results of
  36. execution
  37. none ----- do not display either code or results upon export"
  38. (interactive)
  39. (unless headers (error "org-babel can't process a source block without knowing the source code"))
  40. (message "org-babel-exp processing...")
  41. (let* ((lang (car headers))
  42. (lang-headers (intern (concat "org-babel-default-header-args:" lang)))
  43. (params (org-babel-merge-params
  44. org-babel-default-header-args
  45. (org-babel-params-from-properties)
  46. (if (boundp lang-headers) (eval lang-headers) nil)
  47. (org-babel-parse-header-arguments
  48. (mapconcat #'identity (cdr headers) " ")))))
  49. (message "exporting params are %S" params)
  50. (org-babel-exp-do-export lang body params)))
  51. (defun org-babel-exp-inline-src-blocks (start end)
  52. "Process inline src blocks between START and END for export.
  53. See `org-babel-exp-src-blocks' for export options, currently the
  54. options and are taken from `org-babel-defualt-inline-header-args'."
  55. (interactive)
  56. (save-excursion
  57. (goto-char start)
  58. (while (and (< (point) end) (re-search-forward org-babel-inline-src-block-regexp end t))
  59. (let* ((info (save-match-data (org-babel-parse-inline-src-block-match)))
  60. (replacement (save-match-data
  61. (org-babel-exp-do-export (first info) (second info) (third info) t))))
  62. (setf end (+ end (- (length replacement)
  63. (+ 6 (length (first info)) (length (second info))))))
  64. (replace-match replacement t t)))))
  65. (defun org-babel-exp-do-export (lang body params &optional inline)
  66. (case (intern (or (cdr (assoc :exports params)) "code"))
  67. ('none "")
  68. ('code (org-babel-exp-code body lang params inline))
  69. ('results (org-babel-exp-results body lang params inline))
  70. ('both (concat (org-babel-exp-code body lang params inline)
  71. "\n\n"
  72. (org-babel-exp-results body lang params inline)))))
  73. (defun org-babel-exp-code (body lang params &optional inline)
  74. (if inline
  75. (format "=%s=" body)
  76. (format "#+BEGIN_SRC %s\n%s%s\n#+END_SRC" lang body
  77. (if (string-match "\n$" body) "" "\n"))))
  78. (defun org-babel-exp-results (body lang params &optional inline)
  79. ;; I expect there's a good reason why not, but would it be possible
  80. ;; to use org-babel-execute-src-block here? [ded]
  81. (let* ((cmd (intern (concat "org-babel-execute:" lang)))
  82. (result
  83. (multiple-value-bind (session vars result-params result-type)
  84. (org-babel-process-params params) (funcall cmd body params)))
  85. (result-as-org (org-babel-result-to-org-string result)))
  86. (if inline
  87. (format "=%s=" result)
  88. (if (stringp result)
  89. (format "#+BEGIN_EXAMPLE\n%s%s\n#+END_EXAMPLE" result
  90. (if (string-match "\n$" body) "" "\n"))
  91. result-as-org))))
  92. (provide 'org-babel-exp)
  93. ;;; org-babel-exp.el ends here