ob-ditaa.el 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. ;;; ob-ditaa.el --- org-babel functions for ditaa evaluation
  2. ;; Copyright (C) 2009-2013 Free Software Foundation, Inc.
  3. ;; Author: Eric Schulte
  4. ;; Keywords: literate programming, reproducible research
  5. ;; Homepage: http://orgmode.org
  6. ;; This file is part of GNU Emacs.
  7. ;; GNU Emacs is free software: you can redistribute it and/or modify
  8. ;; it under the terms of the GNU General Public License as published by
  9. ;; the Free Software Foundation, either version 3 of the License, or
  10. ;; (at your option) any later version.
  11. ;; GNU Emacs is distributed in the hope that it will be useful,
  12. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;; GNU General Public License for more details.
  15. ;; You should have received a copy of the GNU General Public License
  16. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  17. ;;; Commentary:
  18. ;; Org-Babel support for evaluating ditaa source code.
  19. ;;
  20. ;; This differs from most standard languages in that
  21. ;;
  22. ;; 1) there is no such thing as a "session" in ditaa
  23. ;;
  24. ;; 2) we are generally only going to return results of type "file"
  25. ;;
  26. ;; 3) we are adding the "file" and "cmdline" header arguments
  27. ;;
  28. ;; 4) there are no variables (at least for now)
  29. ;;
  30. ;; 5) it depends on a variable defined in org-exp-blocks (namely
  31. ;; `org-ditaa-jar-path') so be sure you have org-exp-blocks loaded
  32. ;;; Code:
  33. (require 'ob)
  34. (require 'org-compat)
  35. (defvar org-ditaa-jar-path) ;; provided by org-exp-blocks
  36. (defvar org-babel-default-header-args:ditaa
  37. '((:results . "file")
  38. (:exports . "results")
  39. (:java . "-Dfile.encoding=UTF-8"))
  40. "Default arguments for evaluating a ditaa source block.")
  41. (defcustom org-ditaa-jar-option "-jar"
  42. "Option for the ditaa jar file.
  43. Do not leave leading or trailing spaces in this string."
  44. :group 'org-babel
  45. :version "24.1"
  46. :type 'string)
  47. (defun org-babel-execute:ditaa (body params)
  48. "Execute a block of Ditaa code with org-babel.
  49. This function is called by `org-babel-execute-src-block'."
  50. (let* ((result-params (split-string (or (cdr (assoc :results params)) "")))
  51. (out-file ((lambda (el)
  52. (or el
  53. (error
  54. "ditaa code block requires :file header argument")))
  55. (cdr (assoc :file params))))
  56. (cmdline (cdr (assoc :cmdline params)))
  57. (java (cdr (assoc :java params)))
  58. (in-file (org-babel-temp-file "ditaa-"))
  59. (cmd (concat "java " java " " org-ditaa-jar-option " "
  60. (shell-quote-argument
  61. (expand-file-name org-ditaa-jar-path))
  62. " " cmdline
  63. " " (org-babel-process-file-name in-file)
  64. " " (org-babel-process-file-name out-file))))
  65. (unless (file-exists-p org-ditaa-jar-path)
  66. (error "Could not find ditaa.jar at %s" org-ditaa-jar-path))
  67. (with-temp-file in-file (insert body))
  68. (message cmd) (shell-command cmd)
  69. nil)) ;; signal that output has already been written to file
  70. (defun org-babel-prep-session:ditaa (session params)
  71. "Return an error because ditaa does not support sessions."
  72. (error "Ditaa does not support sessions"))
  73. (provide 'ob-ditaa)
  74. ;;; ob-ditaa.el ends here