litorgy-exp.el 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. ;;; litorgy-exp.el --- Exportation of litorgy 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 litorgy.el
  24. ;;; Code:
  25. (add-to-list 'org-export-blocks '(src litorgy-exp-src-blocks))
  26. (defun litorgy-exp-src-blocks (body &rest headers)
  27. "Process src block for export. Depending on the 'export'
  28. headers argument in replace the source code block with...
  29. both ---- the default, display the code and the results
  30. code ---- display the code inside the block but do not process
  31. results - process the block and replace it with the results of
  32. execution
  33. none ----- do not display either code or results upon export"
  34. (interactive)
  35. (unless headers (error "litorgy can't process a source block without knowing the source code"))
  36. (message "litorgy processing...")
  37. (let* ((lang (car headers))
  38. (params (litorgy-parse-header-arguments (mapconcat #'identity (cdr headers) " ")))
  39. (export (cdr (assoc :exports params))))
  40. (case (intern (or export "both"))
  41. ('none "")
  42. ('code (litorgy-exp-code body lang params))
  43. ('results (litorgy-exp-results body lang params))
  44. ('both (concat (litorgy-exp-code body lang params)
  45. "\n\n"
  46. (litorgy-exp-results body lang params))))))
  47. (defun litorgy-exp-code (body lang params)
  48. (format "#+BEGIN_SRC %s\n%s%s\n#+END_SRC" lang body
  49. (if (string-match "\n$" body) "" "\n")))
  50. (defun litorgy-exp-results (body lang params)
  51. (let* ((cmd (intern (concat "litorgy-execute:" lang)))
  52. (result (funcall cmd body params))
  53. (result-as-org (litorgy-result-to-org-string result)))
  54. (if (stringp result)
  55. (format "#+BEGIN_EXAMPLE\n%s%s\n#+END_EXAMPLE" result
  56. (if (string-match "\n$" body) "" "\n"))
  57. result-as-org)))
  58. (provide 'litorgy-exp)
  59. ;;; litorgy-exp.el ends here