ob-plantuml.el 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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.5
  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. (defcustom org-plantuml-jar-path nil
  33. "Path to the plantuml.jar file."
  34. :group 'org-babel
  35. :type 'string)
  36. (defun org-babel-execute:plantuml (body params)
  37. "Execute a block of plantuml code with org-babel.
  38. This function is called by `org-babel-execute-src-block'."
  39. (let* ((result-params (split-string (or (cdr (assoc :results params)) "")))
  40. (out-file (or (cdr (assoc :file params))
  41. (error "plantuml requires a \":file\" header argument")))
  42. (cmdline (cdr (assoc :cmdline params)))
  43. (in-file (org-babel-temp-file "plantuml-"))
  44. (cmd (if (not org-plantuml-jar-path)
  45. (error "`org-plantuml-jar-path' is not set")
  46. (concat "java -jar "
  47. (shell-quote-argument
  48. (expand-file-name org-plantuml-jar-path))
  49. (if (string= (file-name-extension out-file) "svg")
  50. " -tsvg" "")
  51. " -p " cmdline " < "
  52. (org-babel-process-file-name in-file)
  53. " > "
  54. (org-babel-process-file-name out-file)))))
  55. (unless (file-exists-p org-plantuml-jar-path)
  56. (error "Could not find plantuml.jar at %s" org-plantuml-jar-path))
  57. (with-temp-file in-file (insert (concat "@startuml\n" body "\n@enduml")))
  58. (message "%s" cmd) (org-babel-eval cmd "")
  59. nil)) ;; signal that output has already been written to file
  60. (defun org-babel-prep-session:plantuml (session params)
  61. "Return an error because plantuml does not support sessions."
  62. (error "Plantuml does not support sessions"))
  63. (provide 'ob-plantuml)
  64. ;; arch-tag: 451f50c5-e779-407e-ad64-70e0e8f161d1
  65. ;;; ob-plantuml.el ends here