Hi folks, Please see the attached patch that allows for post-processing of PlantUML diagrams based on the exported file extension. I currently use this to transform text in PlantUML SVGs to paths with Inkscape so that they can be embedded in PDFs with the text displaying at the correct size in the image. Since this post-processing occurs as part of the export process it works perfectly with Babel's caching. This ends up saving me a bunch of time when making changes to only one of a half-dozen figures. The concept could also work as post-execute advice on `org-babel-execute:plantuml', but I thought it would be more accessible to users as a new customizable variable built into `ob-plantuml.el'.
The only part I'm unsure about is that I use "%s" as the replacement character when running commands over the exported file. For example, my "use Inkscape to pathify SVG text" command looks like: inkscape %s -T -l %s I'm uncertain whether that's a non-recommended replacement character but, either way, it functions well enough. Please let me know if the patch is attached incorrectly: I tried git-send-email but think I screwed it up somewhere as the previous emails never appeared on the list. Thanks for your time, Nick
>From 527610e8a415a45bffac53b9b508b472311627c3 Mon Sep 17 00:00:00 2001 From: Nick Daly <nick+orgmode-...@despisinggravity.com> Date: Mon, 5 Apr 2021 21:48:03 -0500 Subject: [PATCH] ob-plantuml: Add PlantUML block post-processing. * lisp/ob-plantuml.el (org-babel-plantuml-post-process): New function. After `org-babel-execute:plantuml' finishes exporting a file, read the file's extension and synchronously performs commands associated with that extension, in order, as defined in `org-babel-plantuml-post-export-commands'. If a command contains "%s", that token is replaced with the output file's name. If a command errors, it is skipped and execution continues with subsequent commands. (org-babel-execute:plantuml): Use new function. --- lisp/ob-plantuml.el | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/lisp/ob-plantuml.el b/lisp/ob-plantuml.el index 93c653870..682fcbe48 100644 --- a/lisp/ob-plantuml.el +++ b/lisp/ob-plantuml.el @@ -71,6 +71,20 @@ You can also configure extra arguments via `org-plantuml-executable-args'." :package-version '(Org . "9.4") :type '(repeat string)) +(defcustom org-babel-plantuml-post-export-commands '(("svg" "inkscape %s -T -l %s")) + "List of file extensions and associated commands. + +The commands are run, in sequence, as a post-processing step for +each exported file with the associated extension. Any \"%s\" in +the command is replaced with the output file's name. + +For example, the default value converts text in an SVG to +paths, so that the text displays at the correct size when the +image is embedded in a PDF." + :group 'org-babel + :version "24.1" + :type '(alist :key-type string :value-type (repeat 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 @@ -145,12 +159,32 @@ This function is called by `org-babel-execute-src-block'." " "))) (with-temp-file in-file (insert full-body)) (message "%s" cmd) (org-babel-eval cmd "") + (org-babel-plantuml-post-process out-file) nil)) ;; signal that output has already been written to file (defun org-babel-prep-session:plantuml (_session _params) "Return an error because plantuml does not support sessions." (error "Plantuml does not support sessions")) +(defun org-babel-plantuml-post-process (out-file) + "Run post-processing commands on the output file. + +See also `org-babel-plantuml-post-export-commands'." + + (defun org-babel-plantuml-post-process-loop (out-file command-list) + "Run each command in the command list over the output file." + (if command-list + (progn + (let ((cmd (replace-regexp-in-string "%s" out-file (car command-list))) + (rest (cdr command-list))) + (message "%s" cmd) + (org-babel-eval cmd "") + (org-babel-plantuml-post-process-loop out-file rest))))) + + (org-babel-plantuml-post-process-loop out-file + (cdr (assoc (file-name-extension out-file) + org-babel-plantuml-post-export-commands)))) + (provide 'ob-plantuml) ;;; ob-plantuml.el ends here -- 2.20.1