Codificación de texto PlantUML
Introduction
PlantUML defines a standardized way to encode diagram text description to a simple string of characters that contains only digits, letters, underscore and minus character.
The goal of such an encoding is to facilitate communication of diagrams through URL (see server).
This encoding includes compression to keep encoded strings as short as possible.
The encoded metadata is stored in the generated PNG, so the diagram source can be extracted from the diagram itself! (see server#metadata).
Compression
Deflate algorithm is used by default.
You can also use simple HEX encoding, see below. An initial ~h is added to indicate this encoding.
Principle
For example, the following uml text description:
@startuml
Alice -> Bob: Authentication Request
Bob --> Alice: Authentication Response
@enduml
is encoded as:
Syp9J4vLqBLJSCfFib9mB2t9ICqhoKnEBCdCprC8IYqiJIqkuGBAAUW2rO0LOr5LN92VLvpA1G00
To achieve such encoding, the text diagram is:
- Encoded in UTF-8
- Compressed using Deflate algorithm
- Reencoded in ASCII using a transformation close to base64
ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/
For PlantUML, the mapping array for values 0-63 is:
0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-_
Compression comparison The following diagram:
|
🎉 Copied!
|
|
is compressed to 428-char string length using Deflate
Running
You can use -encodeurl or -decodeurl in the command line flags to encode or decode the text.
Implementations of the encoder in various languages:
- Code in PHP
- Code in Javascript or this (node.js or browser)
- Code in Python
- Code in Perl
- Code in Swift
Formato HEX sencillo
Si Deflate y Brotli le parecen demasiado complejos, puede probar con el formato HEX.
En ese caso, sólo tiene que codificar cada carácter en formato hexadecimal.
Por ejemplo :
@startuml
Alice->Bob : I am using hex
@enduml
se convertirá en:
407374617274756d6c0a416c6963652d3e426f62203a204920616d207573696e67206865780a40656e64756d6c
Para indicar el uso del formato HEX, debe añadir
~h al principio de los datos enviados al servidor PlantUML.
http://www.plantuml.com/plantuml/uml/~h4073...
Como aquí no hay compresión, la URL se hará muy larga a medida que crezca el diagrama.