From mboxrd@z Thu Jan 1 00:00:00 1970 From: Thibault Marin Subject: Re: Add preamble support to ob-plantuml.el Date: Tue, 06 Dec 2016 19:39:01 -0600 Message-ID: <87vauwv7pm.fsf@dell-desktop.WORKGROUP> References: <87bmwu29yk.fsf@dell-desktop.WORKGROUP> <87shq29ib5.fsf@nicolasgoaziou.fr> <87h96hpvcq.fsf@dell-desktop.WORKGROUP> <87k2bd9tcr.fsf@nicolasgoaziou.fr> Reply-To: thibault.marin@gmx.com Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Return-path: Received: from eggs.gnu.org ([2001:4830:134:3::10]:53154) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cERCf-0004S0-8z for emacs-orgmode@gnu.org; Tue, 06 Dec 2016 20:39:26 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1cERCb-0008S1-AN for emacs-orgmode@gnu.org; Tue, 06 Dec 2016 20:39:25 -0500 Received: from mout.gmx.net ([212.227.17.21]:58291) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1cERCa-0008Ra-Uw for emacs-orgmode@gnu.org; Tue, 06 Dec 2016 20:39:21 -0500 In-reply-to: List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org Sender: "Emacs-orgmode" To: Rainer M Krug , Nicolas Goaziou Cc: emacs-orgmode --=-=-= Content-Type: text/plain Thanks for the feedback. >> What about using noweb syntax then? > > Or prologue? I didn't know about the prologue header. This is exactly what I need (it seems to be more convenient than the noweb approach). However, ob-plantuml does not seem to support the prologue option. So I am modifying my patch to add support for the :prologue and :epilogue header arguments instead of using a new customization variable. In the process, I have added support for header variables which are passed to the PlantUML file via the !define macro. I am also adding a test file which checks that the temporary file passed to the plantuml program is properly generated (it does not run or check the output of plantuml). Please let me know you have any comment on the patch. Thanks in advance. I have a slightly tangential question about handling of list variables. Considering the following org content: #+BEGIN_SRC org ,#+name: variable_values | CLASSNAME | test_class | | PROPERTY | test_prop | ,#+header: :file tmp.puml ,#+header: :var x=variable_values ,#+begin_src plantuml class CLASSNAME { +PROPERTY } ,#+end_src" #+END_SRC would it make sense to resolve to the following output: #+BEGIN_SRC plantuml @startuml !define CLASSNAME test_class !define PROPERTY test_prop class CLASSNAME { +PROPERTY } @enduml #+END_SRC ? On the first hand, this doesn't make much sense because the =x= variable is never actually defined in the source block, but for languages that don't handle array variables, that would allow the user to define multiple variables at once (I first thought about this when looking at ob-css, where I wanted to (1) use header variables and (2) group them in a single org-table). What do you think? Best, thibault --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=0001-ob-plantuml.el-Add-support-for-prologue-and-header-v.patch >From 40e27d7e36806694c47fe4331b24db48c19f31f4 Mon Sep 17 00:00:00 2001 From: thibault Date: Tue, 6 Dec 2016 17:29:55 -0600 Subject: [PATCH] ob-plantuml.el: 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. Form content of temporary file used as input to PlantUML program. (org-babel-plantuml-var-to-plantuml): New function. Convert header variable to PlantUML variable. This reduces to removing quotes from string. (org-babel-variable-assignments:plantuml): New function. Build list of variable assignments for source block. * testing/lisp/test-ob-plantuml.el: New file. Test body text produced by `org-babel-plantuml-make-body'. --- lisp/ob-plantuml.el | 24 ++++++++++++- testing/lisp/test-ob-plantuml.el | 73 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 96 insertions(+), 1 deletion(-) create mode 100644 testing/lisp/test-ob-plantuml.el diff --git a/lisp/ob-plantuml.el b/lisp/ob-plantuml.el index 9ce65a9..5b7ae20 100644 --- a/lisp/ob-plantuml.el +++ b/lisp/ob-plantuml.el @@ -46,6 +46,27 @@ :version "24.1" :type 'string) +(defun org-babel-plantuml-var-to-plantuml (var) + "Cleanup plantuml variable (remove quotes)." + (replace-regexp-in-string "\"" "" var)) + +(defun org-babel-variable-assignments:plantuml (params) + "Return a list of PlantUML statements assigning the block's variables." + (mapcar + (lambda (pair) + (format "!define %s %s" + (car pair) + (org-babel-plantuml-var-to-plantuml (cdr pair)))) + (org-babel--get-vars params))) + +(defun org-babel-plantuml-make-body (body params) + "Form PlantUML input string." + (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 +75,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 +107,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 diff --git a/testing/lisp/test-ob-plantuml.el b/testing/lisp/test-ob-plantuml.el new file mode 100644 index 0000000..794d313 --- /dev/null +++ b/testing/lisp/test-ob-plantuml.el @@ -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 . + +;;; 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 -- 2.9.3 --=-=-=--