ob-plantuml.el 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. ;;; ob-plantuml.el --- Babel Functions for Plantuml -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2010-2018 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 <https://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-variable-assignments:plantuml (params)
  36. "Return a list of PlantUML statements assigning the block's variables.
  37. PARAMS is a property list of source block parameters, which may
  38. contain multiple entries for the key `:var'. `:var' entries in PARAMS
  39. are expected to be scalar variables."
  40. (mapcar
  41. (lambda (pair)
  42. (format "!define %s %s"
  43. (car pair)
  44. (replace-regexp-in-string "\"" "" (cdr pair))))
  45. (org-babel--get-vars params)))
  46. (defun org-babel-plantuml-make-body (body params)
  47. "Return PlantUML input string.
  48. BODY is the content of the source block and PARAMS is a property list
  49. of source block parameters. This function relies on the
  50. `org-babel-expand-body:generic' function to extract `:var' entries
  51. from PARAMS and on the `org-babel-variable-assignments:plantuml'
  52. function to convert variables to PlantUML assignments."
  53. (concat
  54. "@startuml\n"
  55. (org-babel-expand-body:generic
  56. body params (org-babel-variable-assignments:plantuml params))
  57. "\n@enduml"))
  58. (defun org-babel-execute:plantuml (body params)
  59. "Execute a block of plantuml code with org-babel.
  60. This function is called by `org-babel-execute-src-block'."
  61. (let* ((out-file (or (cdr (assq :file params))
  62. (error "PlantUML requires a \":file\" header argument")))
  63. (cmdline (cdr (assq :cmdline params)))
  64. (in-file (org-babel-temp-file "plantuml-"))
  65. (java (or (cdr (assq :java params)) ""))
  66. (full-body (org-babel-plantuml-make-body body params))
  67. (cmd (if (string= "" org-plantuml-jar-path)
  68. (error "`org-plantuml-jar-path' is not set")
  69. (concat "java " java " -jar "
  70. (shell-quote-argument
  71. (expand-file-name org-plantuml-jar-path))
  72. (if (string= (file-name-extension out-file) "png")
  73. " -tpng" "")
  74. (if (string= (file-name-extension out-file) "svg")
  75. " -tsvg" "")
  76. (if (string= (file-name-extension out-file) "eps")
  77. " -teps" "")
  78. (if (string= (file-name-extension out-file) "pdf")
  79. " -tpdf" "")
  80. (if (string= (file-name-extension out-file) "vdx")
  81. " -tvdx" "")
  82. (if (string= (file-name-extension out-file) "xmi")
  83. " -txmi" "")
  84. (if (string= (file-name-extension out-file) "scxml")
  85. " -tscxml" "")
  86. (if (string= (file-name-extension out-file) "html")
  87. " -thtml" "")
  88. (if (string= (file-name-extension out-file) "txt")
  89. " -ttxt" "")
  90. (if (string= (file-name-extension out-file) "utxt")
  91. " -utxt" "")
  92. " -p " cmdline " < "
  93. (org-babel-process-file-name in-file)
  94. " > "
  95. (org-babel-process-file-name out-file)))))
  96. (unless (file-exists-p org-plantuml-jar-path)
  97. (error "Could not find plantuml.jar at %s" org-plantuml-jar-path))
  98. (with-temp-file in-file (insert full-body))
  99. (message "%s" cmd) (org-babel-eval cmd "")
  100. nil)) ;; signal that output has already been written to file
  101. (defun org-babel-prep-session:plantuml (_session _params)
  102. "Return an error because plantuml does not support sessions."
  103. (error "Plantuml does not support sessions"))
  104. (provide 'ob-plantuml)
  105. ;;; ob-plantuml.el ends here