ob-plantuml: Add support for prologue and header variables

* lisp/ob-plantuml.el (org-babel-execute:plantuml) Include prologue and
  header variables to temporary file body.
(org-babel-plantuml-make-body): New function.
(org-babel-variable-assignments:plantuml): New function.

* testing/lisp/test-ob-plantuml.el: New file.
This commit is contained in:
thibault 2016-12-09 22:43:32 -06:00 committed by Nicolas Goaziou
parent d2c834b215
commit cb33cd066d
2 changed files with 100 additions and 1 deletions

View File

@ -46,6 +46,31 @@
:version "24.1"
:type 'string)
(defun org-babel-variable-assignments:plantuml (params)
"Return a list of PlantUML statements assigning the block's variables.
PARAMS is a property list of source block parameters, which may
contain multiple entries for the key `:var'. `:var' entries in PARAMS
are expected to be scalar variables."
(mapcar
(lambda (pair)
(format "!define %s %s"
(car pair)
(replace-regexp-in-string "\"" "" (cdr pair))))
(org-babel--get-vars params)))
(defun org-babel-plantuml-make-body (body params)
"Return PlantUML input string.
BODY is the content of the source block and PARAMS is a property list
of source block parameters. This function relies on the
`org-babel-expand-body:generic' function to extract `:var' entries
from PARAMS and on the `org-babel-variable-assignments:plantuml'
function to convert variables to PlantUML assignments."
(concat
"@startuml\n"
(org-babel-expand-body:generic
body params (org-babel-variable-assignments:plantuml params))
"\n@enduml"))
(defun org-babel-execute:plantuml (body params)
"Execute a block of plantuml code with org-babel.
This function is called by `org-babel-execute-src-block'."
@ -54,6 +79,7 @@ This function is called by `org-babel-execute-src-block'."
(cmdline (cdr (assq :cmdline params)))
(in-file (org-babel-temp-file "plantuml-"))
(java (or (cdr (assq :java params)) ""))
(full-body (org-babel-plantuml-make-body body params))
(cmd (if (string= "" org-plantuml-jar-path)
(error "`org-plantuml-jar-path' is not set")
(concat "java " java " -jar "
@ -85,7 +111,7 @@ This function is called by `org-babel-execute-src-block'."
(org-babel-process-file-name out-file)))))
(unless (file-exists-p org-plantuml-jar-path)
(error "Could not find plantuml.jar at %s" org-plantuml-jar-path))
(with-temp-file in-file (insert (concat "@startuml\n" body "\n@enduml")))
(with-temp-file in-file (insert full-body))
(message "%s" cmd) (org-babel-eval cmd "")
nil)) ;; signal that output has already been written to file

View File

@ -0,0 +1,73 @@
;;; test-ob-plantuml.el --- tests for ob-plantuml.el
;; Copyright (c) 2016 Thibault Marin
;; Authors: Thibault Marin
;; This file is not part of GNU Emacs.
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
;;; Code:
(unless (featurep 'ob-plantuml)
(signal 'missing-test-dependency "Support for PlantUML code blocks"))
(ert-deftest test-ob-plantuml/single-var ()
"Test file output with input variable."
(should
(string=
"@startuml
!define CLASSNAME test_class
class CLASSNAME
@enduml"
(let ((org-plantuml-jar-path nil))
(org-test-with-temp-text
"#+name: variable_value
: test_class
#+header: :file tmp.puml
#+header: :var CLASSNAME=variable_value
#+begin_src plantuml
class CLASSNAME
#+end_src"
(org-babel-next-src-block)
(let ((src-block-info (cdr (org-babel-get-src-block-info))))
(org-babel-plantuml-make-body
(car src-block-info)
(car (cdr src-block-info)))))))))
(ert-deftest test-ob-plantuml/prologue ()
"Test file output with prologue."
(should
(string=
"@startuml
skinparam classBackgroundColor #FF0000
class test_class
@enduml"
(let ((org-plantuml-jar-path nil))
(org-test-with-temp-text
"#+header: :file tmp.puml
#+header: :prologue skinparam classBackgroundColor #FF0000
#+begin_src plantuml
class test_class
#+end_src"
(org-babel-next-src-block)
(let ((src-block-info (cdr (org-babel-get-src-block-info))))
(org-babel-plantuml-make-body
(car src-block-info)
(car (cdr src-block-info)))))))))
(provide 'test-ob-plantuml)
;;; test-ob-plantuml.el ends here