ob-plantuml.el 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. ;;; ob-plantuml.el --- org-babel functions for plantuml evaluation
  2. ;; Copyright (C) 2010 Free Software Foundation, Inc.
  3. ;; Author: Zhang Weize
  4. ;; Keywords: literate programming, reproducible research
  5. ;; Homepage: http://orgmode.org
  6. ;; Version: 7.01trans
  7. ;; This file is part of GNU Emacs.
  8. ;; GNU Emacs 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 of the License, or
  11. ;; (at your option) any later version.
  12. ;; GNU Emacs is distributed in the hope that it will be useful,
  13. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;; GNU General Public License for more details.
  16. ;; You should have received a copy of the GNU General Public License
  17. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  18. ;;; Commentary:
  19. ;; Org-Babel support for evaluating plantuml script.
  20. ;;
  21. ;; Inspired by Ian Yang's org-export-blocks-format-plantuml
  22. ;; http://www.emacswiki.org/emacs/org-export-blocks-format-plantuml.el
  23. ;;; Requirements:
  24. ;; plantuml | http://plantuml.sourceforge.net/
  25. ;; plantuml.jar | `org-plantuml-jar-path' should point to the jar file
  26. ;;; Code:
  27. (require 'ob)
  28. (require 'ob-eval)
  29. (defvar org-babel-default-header-args:plantuml
  30. '((:results . "file") (:exports . "results"))
  31. "Default arguments for evaluating a plantuml source block.")
  32. (defun org-babel-expand-body:plantuml (body params &optional processed-params)
  33. "Expand BODY according to PARAMS, return the expanded body." body)
  34. (defcustom org-plantuml-jar-path nil
  35. "Path to the plantuml.jar file."
  36. :group 'org-babel
  37. :type 'string)
  38. (defun org-babel-execute:plantuml (body params)
  39. "Execute a block of plantuml code with org-babel.
  40. This function is called by `org-babel-execute-src-block'."
  41. (let* ((result-params (split-string (or (cdr (assoc :results params)) "")))
  42. (out-file (or (cdr (assoc :file params))
  43. (error "plantuml requires a \":file\" header argument")))
  44. (cmdline (cdr (assoc :cmdline params)))
  45. (in-file (org-babel-temp-file "plantuml-"))
  46. (cmd (if (not org-plantuml-jar-path)
  47. (error "`org-plantuml-jar-path' is not set")
  48. (concat "java -jar "
  49. (shell-quote-argument
  50. (expand-file-name org-plantuml-jar-path))
  51. (if (string= (file-name-extension out-file) "svg")
  52. " -tsvg" "")
  53. " -p " cmdline " < "
  54. (org-babel-process-file-name in-file)
  55. " > "
  56. (org-babel-process-file-name out-file)))))
  57. (unless (file-exists-p org-plantuml-jar-path)
  58. (error "Could not find plantuml.jar at %s" org-plantuml-jar-path))
  59. (with-temp-file in-file (insert (concat "@startuml\n" body "\n@enduml")))
  60. (message "%s" cmd) (org-babel-eval cmd "")
  61. out-file))
  62. (defun org-babel-prep-session:plantuml (session params)
  63. "Return an error because plantuml does not support sessions."
  64. (error "Plantuml does not support sessions"))
  65. (provide 'ob-plantuml)
  66. ;; arch-tag: 451f50c5-e779-407e-ad64-70e0e8f161d1
  67. ;;; ob-plantuml.el ends here