Version 2 of dia2plantuml

Updated 2023-07-28 16:45:39 by DDG

NAME

dia2plantuml - convert textual descriptions for various diagram tools to image URL's using the https://www.plantuml.com/ server.

CODE

### file dia2plantuml.tcl
proc dia2plantuml {text {ext svg}} {
    # plantuml is using a different encoding order
    set b64 ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/
    set pml 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-_
    set lmapper [list]
    set i 0
    foreach char [split $b64 ""] {
        lappend lmapper $char
        lappend lmapper [string range $pml $i $i]
        incr i
    }
    # magically remove the first two and the last four chars
    set b64 [string map $lmapper [binary encode base64 [string range [zlib compress $text] 2 end-4]]]
    set uri https://www.plantuml.com/plantuml/$ext/$b64
    return $uri
}

This code was translated from Python to Tcl, for the Python code look here: https://github.com/dougn/python-plantuml/blob/master/plantuml.py - the encoding used by PlantUML is explained here: https://plantuml.com/text-encoding

TODO: plantuml2dia - decoding image to code, see dia2kroki for starting. Due to the removal of some chars above which is required the reverse did not work. But anyway, here my fist approach to translate an URL back into diagram code:

### this coding back to diagram code does not work due to the missing characters 
### at the beginning and the end from the encoding, see above.

proc plantuml2dia {url} {
    # does not work
    set b64 ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/
    set pml 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-_
    set lmapper [list]
    set i 0
    foreach char [split $pml ""] {
        lappend lmapper $char
        lappend lmapper [string range $b64 $i $i]
        incr i
    }
    set text [regsub {.+/} $url ""]
    set dia [zlib decompress [binary decode base64 [string map $lmapper $text]]]
}

EXAMPLES

dia2plantuml

% source dia2plantuml.tcl
% puts [dia2plantuml "class TclExample"]
https://www.plantuml.com/plantuml/svg/Iyv9B2vM24dESQr8p2t8IGK0

Here the image:

.

Discussion

Please discuss here.