Merge branch 'maint'
[org-mode/org-tableheadings.git] / lisp / ob-plantuml.el
blob8093100edaff8249ce85227748f8d8559b07e5fe
1 ;;; ob-plantuml.el --- Babel Functions for Plantuml -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2010-2017 Free Software Foundation, Inc.
5 ;; Author: Zhang Weize
6 ;; Keywords: literate programming, reproducible research
7 ;; Homepage: http://orgmode.org
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
24 ;;; Commentary:
26 ;; Org-Babel support for evaluating plantuml script.
28 ;; Inspired by Ian Yang's org-export-blocks-format-plantuml
29 ;; http://www.emacswiki.org/emacs/org-export-blocks-format-plantuml.el
31 ;;; Requirements:
33 ;; plantuml | http://plantuml.sourceforge.net/
34 ;; plantuml.jar | `org-plantuml-jar-path' should point to the jar file
36 ;;; Code:
37 (require 'ob)
39 (defvar org-babel-default-header-args:plantuml
40 '((:results . "file") (:exports . "results"))
41 "Default arguments for evaluating a plantuml source block.")
43 (defcustom org-plantuml-jar-path ""
44 "Path to the plantuml.jar file."
45 :group 'org-babel
46 :version "24.1"
47 :type 'string)
49 (defun org-babel-variable-assignments:plantuml (params)
50 "Return a list of PlantUML statements assigning the block's variables.
51 PARAMS is a property list of source block parameters, which may
52 contain multiple entries for the key `:var'. `:var' entries in PARAMS
53 are expected to be scalar variables."
54 (mapcar
55 (lambda (pair)
56 (format "!define %s %s"
57 (car pair)
58 (replace-regexp-in-string "\"" "" (cdr pair))))
59 (org-babel--get-vars params)))
61 (defun org-babel-plantuml-make-body (body params)
62 "Return PlantUML input string.
63 BODY is the content of the source block and PARAMS is a property list
64 of source block parameters. This function relies on the
65 `org-babel-expand-body:generic' function to extract `:var' entries
66 from PARAMS and on the `org-babel-variable-assignments:plantuml'
67 function to convert variables to PlantUML assignments."
68 (concat
69 "@startuml\n"
70 (org-babel-expand-body:generic
71 body params (org-babel-variable-assignments:plantuml params))
72 "\n@enduml"))
74 (defun org-babel-execute:plantuml (body params)
75 "Execute a block of plantuml code with org-babel.
76 This function is called by `org-babel-execute-src-block'."
77 (let* ((out-file (or (cdr (assq :file params))
78 (error "PlantUML requires a \":file\" header argument")))
79 (cmdline (cdr (assq :cmdline params)))
80 (in-file (org-babel-temp-file "plantuml-"))
81 (java (or (cdr (assq :java params)) ""))
82 (full-body (org-babel-plantuml-make-body body params))
83 (cmd (if (string= "" org-plantuml-jar-path)
84 (error "`org-plantuml-jar-path' is not set")
85 (concat "java " java " -jar "
86 (shell-quote-argument
87 (expand-file-name org-plantuml-jar-path))
88 (if (string= (file-name-extension out-file) "png")
89 " -tpng" "")
90 (if (string= (file-name-extension out-file) "svg")
91 " -tsvg" "")
92 (if (string= (file-name-extension out-file) "eps")
93 " -teps" "")
94 (if (string= (file-name-extension out-file) "pdf")
95 " -tpdf" "")
96 (if (string= (file-name-extension out-file) "vdx")
97 " -tvdx" "")
98 (if (string= (file-name-extension out-file) "xmi")
99 " -txmi" "")
100 (if (string= (file-name-extension out-file) "scxml")
101 " -tscxml" "")
102 (if (string= (file-name-extension out-file) "html")
103 " -thtml" "")
104 (if (string= (file-name-extension out-file) "txt")
105 " -ttxt" "")
106 (if (string= (file-name-extension out-file) "utxt")
107 " -utxt" "")
108 " -p " cmdline " < "
109 (org-babel-process-file-name in-file)
110 " > "
111 (org-babel-process-file-name out-file)))))
112 (unless (file-exists-p org-plantuml-jar-path)
113 (error "Could not find plantuml.jar at %s" org-plantuml-jar-path))
114 (with-temp-file in-file (insert full-body))
115 (message "%s" cmd) (org-babel-eval cmd "")
116 nil)) ;; signal that output has already been written to file
118 (defun org-babel-prep-session:plantuml (_session _params)
119 "Return an error because plantuml does not support sessions."
120 (error "Plantuml does not support sessions"))
122 (provide 'ob-plantuml)
126 ;;; ob-plantuml.el ends here