utils.PlantEncoder

 1import zlib
 2import base64
 3import string
 4
 5PLANTUML_ALPHABET: str = string.digits + string.ascii_uppercase + string.ascii_lowercase + '-_'
 6BASE64_ALPHABET: str = string.ascii_uppercase + string.ascii_lowercase + string.digits + '+/'
 7
 8
 9def encode(diagram: str, render_type: str) -> str:
10    """
11    Encodes a PlantUML diagram, so it can be passed to the online renderer.
12
13    :param diagram: The diagram to encode.
14    :param render_type: A valid render type string (`uml`, `png`, `svg`, `txt`).
15    :return: The encoded URI component.
16    """
17    return 'http://www.plantuml.com/plantuml/' + render_type + '/' + base64.b64encode(
18        zlib.compress(diagram.encode('utf-8'))[2:-4]).translate(
19        bytes.maketrans(BASE64_ALPHABET.encode('utf-8'), PLANTUML_ALPHABET.encode('utf-8'))).decode(
20        'utf-8')
PLANTUML_ALPHABET: str = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-_'
BASE64_ALPHABET: str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
def encode(diagram: str, render_type: str) -> str:
10def encode(diagram: str, render_type: str) -> str:
11    """
12    Encodes a PlantUML diagram, so it can be passed to the online renderer.
13
14    :param diagram: The diagram to encode.
15    :param render_type: A valid render type string (`uml`, `png`, `svg`, `txt`).
16    :return: The encoded URI component.
17    """
18    return 'http://www.plantuml.com/plantuml/' + render_type + '/' + base64.b64encode(
19        zlib.compress(diagram.encode('utf-8'))[2:-4]).translate(
20        bytes.maketrans(BASE64_ALPHABET.encode('utf-8'), PLANTUML_ALPHABET.encode('utf-8'))).decode(
21        'utf-8')

Encodes a PlantUML diagram, so it can be passed to the online renderer.

Parameters
  • diagram: The diagram to encode.
  • render_type: A valid render type string (uml, png, svg, txt).
Returns

The encoded URI component.