emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: Thibault Marin <thibault.marin@gmx.com>
To: Nicolas Goaziou <mail@nicolasgoaziou.fr>
Cc: emacs-orgmode <emacs-orgmode@gnu.org>
Subject: Re: Add preamble support to ob-plantuml.el
Date: Fri, 09 Dec 2016 22:48:39 -0600	[thread overview]
Message-ID: <871sxgjsns.fsf@dell-desktop.WORKGROUP> (raw)
In-Reply-To: <87oa0kesa7.fsf@nicolasgoaziou.fr>

[-- Attachment #1: Type: text/plain, Size: 1916 bytes --]


Hi,

Nicolas Goaziou writes:
> Comments follow.
>
>> +(defun org-babel-plantuml-var-to-plantuml (var)
>> +  "Cleanup plantuml variable (remove quotes)."
>> +     (replace-regexp-in-string "\"" "" var))
>
> Since this function is used only once in the code, I suggest to not
> implement it and use `replace-regexp-in-string' at the appropriate
> place.
I was trying to match what other ob-*s do.  If the table assignment idea
was to be implemented (for instance), using a separate function may be
cleaner.  But the function is indeed currently not needed, so I removed
it.

>> +(defun org-babel-variable-assignments:plantuml (params)
>> +  "Return a list of PlantUML statements assigning the block's variables."
>
> Could you document what is PARAMS?
I have added more complete docstrings, please let me know if changes are
required.

>> +(defun org-babel-plantuml-make-body (body params)
>> +  "Form PlantUML input string."
>
> Do you mean "Return PlantUML" input string? Also you need to specify
> what are body and params.
Tentatively done.

> Besides, the same applies to `org-babel-plantuml-var-to-plantuml' above.
> Is this function really needed, as it is a mere `format'.
I use this function in the test as well to compare the full text output
so it is convenient to have a separate function.  Alternatively I guess
I could directly test the call to `org-babel-expand-body:generic' but
that seems less interesting as a test (should I remove the test
altogether then?).

The attached patch removes the useless definition of
`org-babel-plantuml-var-to-plantuml' (the regexp is moved to the
`org-babel-variable-assignments:plantuml' function) but keeps the
`org-babel-plantuml-make-body' function, useful for testing.  If you
would like me to remove the `org-babel-plantuml-make-body' function as
well, I will do that (how would you like the test to look like in this
case?)

Thanks for the guidance.

thibault

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-ob-plantuml.el-Add-support-for-prologue-and-header-v.patch --]
[-- Type: text/x-diff, Size: 5646 bytes --]

From 9e8addc14e628dc7c9c25d96d0cfd630ad15134e Mon Sep 17 00:00:00 2001
From: thibault <thibault.marin@gmx.com>
Date: Fri, 9 Dec 2016 22:43:32 -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.  Return content of
temporary file used as input to PlantUML program.
(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              | 28 ++++++++++++++-
 testing/lisp/test-ob-plantuml.el | 73 ++++++++++++++++++++++++++++++++++++++++
 2 files changed, 100 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..01739c8 100644
--- a/lisp/ob-plantuml.el
+++ b/lisp/ob-plantuml.el
@@ -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
 
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 <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
-- 
2.9.3


  reply	other threads:[~2016-12-10  4:48 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-12-02  5:09 Add preamble support to ob-plantuml.el Thibault Marin
2016-12-05 21:28 ` Nicolas Goaziou
2016-12-06  3:52   ` Thibault Marin
2016-12-06 11:42     ` Nicolas Goaziou
2016-12-06 12:14       ` Rainer M Krug
2016-12-07  1:39         ` Thibault Marin
2016-12-09 20:55           ` Nicolas Goaziou
2016-12-10  4:48             ` Thibault Marin [this message]
2016-12-10 11:08               ` Nicolas Goaziou
2016-12-10 14:35                 ` Thibault Marin
2016-12-10 16:05                   ` Nicolas Goaziou

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.orgmode.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=871sxgjsns.fsf@dell-desktop.WORKGROUP \
    --to=thibault.marin@gmx.com \
    --cc=emacs-orgmode@gnu.org \
    --cc=mail@nicolasgoaziou.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).