ob-plantuml.el 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. ;;; ob-plantuml.el --- Babel Functions for Plantuml -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2010-2021 Free Software Foundation, Inc.
  3. ;; Author: Zhang Weize
  4. ;; Keywords: literate programming, reproducible research
  5. ;; Homepage: https://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. ;; https://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 (when exec mode is `jar')
  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. (defcustom org-plantuml-exec-mode 'jar
  36. "Method to use for PlantUML diagram generation.
  37. `jar' means to use java together with the JAR.
  38. The JAR can be configured via `org-plantuml-jar-path'.
  39. `plantuml' means to use the PlantUML executable.
  40. The executable can be configured via `org-plantuml-executable-path'.
  41. You can also configure extra arguments via `org-plantuml-executable-args'."
  42. :group 'org-babel
  43. :package-version '(Org . "9.4")
  44. :type 'symbol
  45. :options '(jar plantuml))
  46. (defcustom org-plantuml-executable-path "plantuml"
  47. "File name of the PlantUML executable."
  48. :group 'org-babel
  49. :package-version '(Org . "9.4")
  50. :type 'string)
  51. (defcustom org-plantuml-executable-args (list "-headless")
  52. "The arguments passed to plantuml executable when executing PlantUML."
  53. :group 'org-babel
  54. :package-version '(Org . "9.4")
  55. :type '(repeat string))
  56. (defun org-babel-variable-assignments:plantuml (params)
  57. "Return a list of PlantUML statements assigning the block's variables.
  58. PARAMS is a property list of source block parameters, which may
  59. contain multiple entries for the key `:var'. `:var' entries in PARAMS
  60. are expected to be scalar variables."
  61. (mapcar
  62. (lambda (pair)
  63. (format "!define %s %s"
  64. (car pair)
  65. (replace-regexp-in-string "\"" "" (cdr pair))))
  66. (org-babel--get-vars params)))
  67. (defun org-babel-plantuml-make-body (body params)
  68. "Return PlantUML input string.
  69. BODY is the content of the source block and PARAMS is a property list
  70. of source block parameters. This function relies on the
  71. `org-babel-expand-body:generic' function to extract `:var' entries
  72. from PARAMS and on the `org-babel-variable-assignments:plantuml'
  73. function to convert variables to PlantUML assignments.
  74. If BODY does not contain @startXXX ... @endXXX clauses, @startuml
  75. ... @enduml will be added."
  76. (let ((full-body
  77. (org-babel-expand-body:generic
  78. body params (org-babel-variable-assignments:plantuml params))))
  79. (if (string-prefix-p "@start" body t) full-body
  80. (format "@startuml\n%s\n@enduml" full-body))))
  81. (defun org-babel-execute:plantuml (body params)
  82. "Execute a block of plantuml code with org-babel.
  83. This function is called by `org-babel-execute-src-block'."
  84. (let* ((out-file (or (cdr (assq :file params))
  85. (error "PlantUML requires a \":file\" header argument")))
  86. (cmdline (cdr (assq :cmdline params)))
  87. (in-file (org-babel-temp-file "plantuml-"))
  88. (java (or (cdr (assq :java params)) ""))
  89. (executable (cond ((eq org-plantuml-exec-mode 'plantuml) org-plantuml-executable-path)
  90. (t "java")))
  91. (executable-args (cond ((eq org-plantuml-exec-mode 'plantuml) org-plantuml-executable-args)
  92. ((string= "" org-plantuml-jar-path)
  93. (error "`org-plantuml-jar-path' is not set"))
  94. ((not (file-exists-p org-plantuml-jar-path))
  95. (error "Could not find plantuml.jar at %s" org-plantuml-jar-path))
  96. (t (list java
  97. "-jar"
  98. (shell-quote-argument (expand-file-name org-plantuml-jar-path))))))
  99. (full-body (org-babel-plantuml-make-body body params))
  100. (cmd (mapconcat #'identity
  101. (append
  102. (list executable)
  103. executable-args
  104. (pcase (file-name-extension out-file)
  105. ("png" '("-tpng"))
  106. ("svg" '("-tsvg"))
  107. ("eps" '("-teps"))
  108. ("pdf" '("-tpdf"))
  109. ("tex" '("-tlatex"))
  110. ("vdx" '("-tvdx"))
  111. ("xmi" '("-txmi"))
  112. ("scxml" '("-tscxml"))
  113. ("html" '("-thtml"))
  114. ("txt" '("-ttxt"))
  115. ("utxt" '("-utxt")))
  116. (list
  117. "-p"
  118. cmdline
  119. "<"
  120. (org-babel-process-file-name in-file)
  121. ">"
  122. (org-babel-process-file-name out-file)))
  123. " ")))
  124. (with-temp-file in-file (insert full-body))
  125. (message "%s" cmd) (org-babel-eval cmd "")
  126. nil)) ;; signal that output has already been written to file
  127. (defun org-babel-prep-session:plantuml (_session _params)
  128. "Return an error because plantuml does not support sessions."
  129. (error "Plantuml does not support sessions"))
  130. (provide 'ob-plantuml)
  131. ;;; ob-plantuml.el ends here