ob-plantuml.el 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. ;;; ob-plantuml.el --- Babel Functions for Plantuml -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2010-2015 Free Software Foundation, Inc.
  3. ;; Author: Zhang Weize
  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 plantuml script.
  19. ;;
  20. ;; Inspired by Ian Yang's org-export-blocks-format-plantuml
  21. ;; http://www.emacswiki.org/emacs/org-export-blocks-format-plantuml.el
  22. ;;; Requirements:
  23. ;; plantuml | http://plantuml.sourceforge.net/
  24. ;; plantuml.jar | `org-plantuml-jar-path' should point to the jar file
  25. ;;; Code:
  26. (require 'ob)
  27. (defvar org-babel-default-header-args:plantuml
  28. '((:results . "file") (:exports . "results"))
  29. "Default arguments for evaluating a plantuml source block.")
  30. (defcustom org-plantuml-jar-path ""
  31. "Path to the plantuml.jar file."
  32. :group 'org-babel
  33. :version "24.1"
  34. :type 'string)
  35. (defun org-babel-execute:plantuml (body params)
  36. "Execute a block of plantuml code with org-babel.
  37. This function is called by `org-babel-execute-src-block'."
  38. (let* ((out-file (or (cdr (assoc :file params))
  39. (error "PlantUML requires a \":file\" header argument")))
  40. (cmdline (cdr (assoc :cmdline params)))
  41. (in-file (org-babel-temp-file "plantuml-"))
  42. (java (or (cdr (assoc :java params)) ""))
  43. (cmd (if (string= "" org-plantuml-jar-path)
  44. (error "`org-plantuml-jar-path' is not set")
  45. (concat "java " java " -jar "
  46. (shell-quote-argument
  47. (expand-file-name org-plantuml-jar-path))
  48. (if (string= (file-name-extension out-file) "svg")
  49. " -tsvg" "")
  50. (if (string= (file-name-extension out-file) "eps")
  51. " -teps" "")
  52. " -p " cmdline " < "
  53. (org-babel-process-file-name in-file)
  54. " > "
  55. (org-babel-process-file-name out-file)))))
  56. (unless (file-exists-p org-plantuml-jar-path)
  57. (error "Could not find plantuml.jar at %s" org-plantuml-jar-path))
  58. (with-temp-file in-file (insert (concat "@startuml\n" body "\n@enduml")))
  59. (message "%s" cmd) (org-babel-eval cmd "")
  60. nil)) ;; signal that output has already been written to file
  61. (defun org-babel-prep-session:plantuml (_session _params)
  62. "Return an error because plantuml does not support sessions."
  63. (error "Plantuml does not support sessions"))
  64. (provide 'ob-plantuml)
  65. ;;; ob-plantuml.el ends here