Tim Hansinger <[email protected]> writes:

> I do not know yet where exactly this might come from, but I can try to
> find it out.

Tim,

I took a closer look at your code, and I think we could do better.

As it stands, in your patch, `org-babel-plantuml-make-body' conceptually
duplicates the work of `org-babel-expand-body:generic' when the body is
wrapped in `@startuml' and `@enduml'.  That will bite us, and in fact,
it already did, as we do not take into account the prologue/epilogue,
unlike `org-babel-expand-body:generic'.

Perhaps a better approach would be to:

  1. Extract from BODY the INNER-BODY that is between `@startuml' and
  `@enduml'.
  
  2. Expand the INNER-BODY or BODY, whichever exists, using
  `org-babel-expand-body:generic'.
  
  3. If INNER-BODY existed, add back the the surrounding `@startuml' and
  `@enduml'.

In code:

(defun org-babel-plantuml-make-body (body params)
  "[...]"
  (let* ((default-start "@startuml")
         (default-end "@enduml")
         (wrapped-body (and (string-match (rx (literal default-start)
                                              (group (zero-or-more anything))
                                              (literal default-end))
                                          body)
                            (match-string 1 body)))
         (expanded-body (org-babel-expand-body:generic
                         (or wrapped-body body)
                         params
                         (org-babel-variable-assignments:plantuml params))))
    (if wrapped-body
        (string-join (list default-start
                           expanded-body
                           default-end)
                     "\n")
      expanded-body)))

What do you think?

Perhaps the regular expression for `wrapped-body' could be a bit more
robust, to match `default-start' and `default-end' at the beginning and
end of the string only, ideally ignoring leading/trailing whitespace.
If you agree, please do improve!

[Pertinently, the docstring says "If BODY does not contain @startXXX
... @endXXX clauses, @startuml ... @enduml will be added.".  We should
probably be a bit more precise, saying it "If BODY is not wrapped in
..." or something like that.]

Interestingly, I also had to change the following function, to keep the
quotes after `!define':

(defun org-babel-variable-assignments:plantuml (params)
  "[...]"
  (mapcar
   (lambda (pair)
     (format "!define %s \"%s\""
             (car pair)
             (cdr pair)))
   (org-babel--get-vars params)))

P.S. `!define' is documented as deprecated and slated for removal, so we
should probably address that too, but we can do that separately.

Rudy
-- 
"Genius is 1% inspiration and 99% perspiration."
--- Thomas Alva Edison, 1932

Rudolf Adamkovič <[email protected]> [he/him]
http://adamkovic.org

Reply via email to