ob-plantuml.el 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. ;;; ob-plantuml.el --- Babel Functions for Plantuml -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2010-2016 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) "png")
  49. " -tpng" "")
  50. (if (string= (file-name-extension out-file) "svg")
  51. " -tsvg" "")
  52. (if (string= (file-name-extension out-file) "eps")
  53. " -teps" "")
  54. (if (string= (file-name-extension out-file) "pdf")
  55. " -tpdf" "")
  56. (if (string= (file-name-extension out-file) "vdx")
  57. " -tvdx" "")
  58. (if (string= (file-name-extension out-file) "xmi")
  59. " -txmi" "")
  60. (if (string= (file-name-extension out-file) "scxml")
  61. " -tscxml" "")
  62. (if (string= (file-name-extension out-file) "html")
  63. " -thtml" "")
  64. (if (string= (file-name-extension out-file) "txt")
  65. " -ttxt" "")
  66. (if (string= (file-name-extension out-file) "utxt")
  67. " -utxt" "")
  68. " -p " cmdline " < "
  69. (org-babel-process-file-name in-file)
  70. " > "
  71. (org-babel-process-file-name out-file)))))
  72. (unless (file-exists-p org-plantuml-jar-path)
  73. (error "Could not find plantuml.jar at %s" org-plantuml-jar-path))
  74. (with-temp-file in-file (insert (concat "@startuml\n" body "\n@enduml")))
  75. (message "%s" cmd) (org-babel-eval cmd "")
  76. nil)) ;; signal that output has already been written to file
  77. (defun org-babel-prep-session:plantuml (_session _params)
  78. "Return an error because plantuml does not support sessions."
  79. (error "Plantuml does not support sessions"))
  80. (provide 'ob-plantuml)
  81. ;;; ob-plantuml.el ends here