Version 4 of dia2plantuml

Updated 2023-07-29 06:04:13 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 does use a different order in encoding
    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
    }
    # unclear while string range 2 end-4
    set b64 [string map $lmapper [binary encode base64 [string range [zlib compress [encoding convertto utf-8 $text]] 0 end]]]
    set uri https://www.plantuml.com/plantuml/$ext/~1$b64
    return $uri
}

### converting URL to diagram code back
proc plantuml2dia {url} {
    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 text [regsub {^~1} $text ""]
    set dia [encoding convertfrom utf-8 [zlib decompress [binary decode base64 [string map $lmapper $text]]]]
}

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

EXAMPLES

% source dia2plantuml.tcl
% puts [dia2plantuml "class TclExample"]
https://www.plantuml.com/plantuml/svg/~1U9nBpaaiBbO8ISvnhKZCBSX91G0pUGOc
% puts [plantuml2dia [dia2plantuml "class TclExample"]]
TclExample

Here the image:

.

SEE ALSO


DISCUSSION

Please discuss here.

DDG - 2023-07-29: decoding an URL now works now as well.