emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* [PATCH] ob-plantuml: Add PlantUML block post-processing.
@ 2021-04-08  1:56 Nick Daly
  2021-04-14  2:24 ` Nick Daly
  0 siblings, 1 reply; 6+ messages in thread
From: Nick Daly @ 2021-04-08  1:56 UTC (permalink / raw)
  To: emacs-orgmode

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

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


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: PlantUML post-processing patch --]
[-- Type: text/x-diff, Size: 3234 bytes --]

From 527610e8a415a45bffac53b9b508b472311627c3 Mon Sep 17 00:00:00 2001
From: Nick Daly <nick+orgmode-org@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


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH] ob-plantuml: Add PlantUML block post-processing.
  2021-04-08  1:56 [PATCH] ob-plantuml: Add PlantUML block post-processing Nick Daly
@ 2021-04-14  2:24 ` Nick Daly
  2021-05-01  9:03   ` Bastien
  2021-05-01 10:51   ` Bastien
  0 siblings, 2 replies; 6+ messages in thread
From: Nick Daly @ 2021-04-14  2:24 UTC (permalink / raw)
  To: emacs-orgmode

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

Nick Daly writes:

> Please see the attached patch that allows for post-processing of
> PlantUML diagrams based on the exported file extension.

If the above patch was too generic and heavy-weight for consideration,
please see this alternative, simpler, patch.  It just adds a flag that,
when enabled, runs for SVG images exported via PlantUML.

The motivation behind these patches should be fairly clear from the
attached image below demonstrating the unreadability of SVG-text images
when embedded in PDFs as viewed in one of my local PDFs produced with
OrgMode when viewed through Evince on Debian Stable.  Fortunately,
Inkscape is kind enough to keep the original text in the SVG, so the
images should be mostly human-readable after the conversion.

Thanks for your time,
Nick


[-- Attachment #2: SVGs with paths vs text as seen in Evince on Debian Stable --]
[-- Type: image/png, Size: 39209 bytes --]

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #3: Smaller text-to-path patch. --]
[-- Type: text/x-diff, Size: 1748 bytes --]

From 75035915a9722920e2194bcd8f07ec5cf2f97bf7 Mon Sep 17 00:00:00 2001
From: Nick Daly <nick+orgmode-org>
Date: Tue, 13 Apr 2021 21:18:41 -0500
Subject: [PATCH] Add PlantUML SVG to Path.

* lisp/ob-plantuml.el (org-babel-execute:plantuml): Add SVG-specific
post-export step that, when enabled, runs inkscape text-to-path
replacement over the output file.  Enabled with new custom variable
`org-babel-plantuml-svg-text-to-path'.
---
 lisp/ob-plantuml.el | 10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git a/lisp/ob-plantuml.el b/lisp/ob-plantuml.el
index 93c653870..ee781290c 100644
--- a/lisp/ob-plantuml.el
+++ b/lisp/ob-plantuml.el
@@ -71,6 +71,12 @@ You can also configure extra arguments via `org-plantuml-executable-args'."
   :package-version '(Org . "9.4")
   :type '(repeat string))
 
+(defcustom org-babel-plantuml-svg-text-to-path 'nil
+  "When set, export text in SVG images to paths using Inkscape."
+  :group 'org-babel
+  :version "24.1"
+  :type 'boolean)
+
 (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,6 +151,10 @@ 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)
+    (if (and (string= (file-name-extension out-file) "svg")
+             org-babel-plantuml-svg-text-to-path)
+        (org-babel-eval (format "inkscape %s -T -l %s" out-file out-file) ""))
     nil)) ;; signal that output has already been written to file
 
 (defun org-babel-prep-session:plantuml (_session _params)
-- 
2.20.1


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH] ob-plantuml: Add PlantUML block post-processing.
  2021-04-14  2:24 ` Nick Daly
@ 2021-05-01  9:03   ` Bastien
  2021-05-01 10:51   ` Bastien
  1 sibling, 0 replies; 6+ messages in thread
From: Bastien @ 2021-05-01  9:03 UTC (permalink / raw)
  To: Nick Daly; +Cc: emacs-orgmode

Hi Nick,

Nick Daly <nick+orgmode-org@despisinggravity.com> writes:

> Nick Daly writes:
>
>> Please see the attached patch that allows for post-processing of
>> PlantUML diagrams based on the exported file extension.
>
> If the above patch was too generic and heavy-weight for consideration,
> please see this alternative, simpler, patch.  It just adds a flag that,
> when enabled, runs for SVG images exported via PlantUML.

Yes, I think the smallest change here is better, we'll see if others
want to the bigger one later on.

Applied as commit 7da20ff21, with some small modifications, thanks.

-- 
 Bastien


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] ob-plantuml: Add PlantUML block post-processing.
  2021-04-14  2:24 ` Nick Daly
  2021-05-01  9:03   ` Bastien
@ 2021-05-01 10:51   ` Bastien
  2021-05-02 15:14     ` Nick Daly
  1 sibling, 1 reply; 6+ messages in thread
From: Bastien @ 2021-05-01 10:51 UTC (permalink / raw)
  To: Nick Daly; +Cc: emacs-orgmode

Hi Nick,

Nick Daly <nick+orgmode-org@despisinggravity.com> writes:

> +    (org-babel-plantuml-post-process out-file)

Note that I had to remove the line above from your second patch.

Can you confirm it is not necessary for your second patch to work?

Thanks,

-- 
 Bastien


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] ob-plantuml: Add PlantUML block post-processing.
  2021-05-01 10:51   ` Bastien
@ 2021-05-02 15:14     ` Nick Daly
  2021-05-02 17:31       ` Bastien
  0 siblings, 1 reply; 6+ messages in thread
From: Nick Daly @ 2021-05-02 15:14 UTC (permalink / raw)
  To: Bastien, Nick Daly; +Cc: emacs-orgmode

Hi Bastien,

Bastien <bzg@gnu.org> writes:

> Nick Daly <nick+orgmode-org@despisinggravity.com> writes:
>
>> +    (org-babel-plantuml-post-process out-file)
>
> Note that I had to remove the line above from your second patch.
>
> Can you confirm it is not necessary for your second patch to work?

Yes, I can confirm that `org-babel-plantuml-post-process` is not
necessary for the second patch.  That was a line accidentally left over
from the first, larger, patch.

Thank you for your time,
Nick


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] ob-plantuml: Add PlantUML block post-processing.
  2021-05-02 15:14     ` Nick Daly
@ 2021-05-02 17:31       ` Bastien
  0 siblings, 0 replies; 6+ messages in thread
From: Bastien @ 2021-05-02 17:31 UTC (permalink / raw)
  To: Nick Daly; +Cc: emacs-orgmode, Nick Daly

Nick Daly <nick@despisinggravity.com> writes:

> Yes, I can confirm that `org-babel-plantuml-post-process` is not
> necessary for the second patch.  That was a line accidentally left over
> from the first, larger, patch.

Thanks Nick for confirming.

-- 
 Bastien


^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2021-05-02 17:32 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2021-04-08  1:56 [PATCH] ob-plantuml: Add PlantUML block post-processing Nick Daly
2021-04-14  2:24 ` Nick Daly
2021-05-01  9:03   ` Bastien
2021-05-01 10:51   ` Bastien
2021-05-02 15:14     ` Nick Daly
2021-05-02 17:31       ` Bastien

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).