From 3a4aabb937b9ef0b2c73ac0126d571fd37c43f71 Mon Sep 17 00:00:00 2001 From: grund Date: Fri, 10 Jun 2022 08:32:29 +0200 Subject: [PATCH 1/6] add structurizr type --- kroki/config.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/kroki/config.py b/kroki/config.py index 682a736..b5345d1 100644 --- a/kroki/config.py +++ b/kroki/config.py @@ -13,6 +13,7 @@ class KrokiDiagramTypes(): "wavedrom", "pikchr", "umlet", + "structurizr" ) kroki_blockdiag = ( @@ -50,4 +51,4 @@ def __init__(self, blockdiag_enabled, bpmn_enabled, excalidraw_enabled, mermaid_ def get_block_regex(self, fence_prefix): diagram_types_re = "|".join(self.diagram_types) - return rf'(?:```{fence_prefix})({diagram_types_re})\n(.*?)(?:```)' + return rf'(?:```{fence_prefix})({diagram_types_re})((?:\s?[a-zA-Z0-9\-_]+=[a-zA-Z0-9\-_]+)*)\n(.*?)(?:```)' From 6458ff471c05d4e69b0b641de4e35fe69a1cfd13 Mon Sep 17 00:00:00 2001 From: grund Date: Fri, 10 Jun 2022 08:32:50 +0200 Subject: [PATCH 2/6] allow diagram options --- kroki/client.py | 20 ++++++++++++-------- kroki/plugin.py | 10 ++++++---- 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/kroki/client.py b/kroki/client.py index 6a518eb..2189f9e 100644 --- a/kroki/client.py +++ b/kroki/client.py @@ -25,31 +25,34 @@ def __init__(self, server_url, http_method): def _kroki_uri(self, kroki_type): return f'{self.server_url}/{kroki_type}/svg' - def _get_url(self, kroki_type, kroki_diagram_data): + def _get_url(self, kroki_type, kroki_diagram_data, kroki_diagram_options={}): kroki_data_param = \ base64.urlsafe_b64encode( zlib.compress(str.encode(kroki_diagram_data), 9)).decode() - + + kroki_query_param = \ + "&".join([ f'{k}={v}' for k, v in kroki_diagram_options.items()]) if len(kroki_diagram_options)>0 else '' if len(kroki_data_param) >= 4096: debug(f'Length of encoded diagram is {len(kroki_data_param)}. ' 'Kroki may not be able to read the data completely!') kroki_uri = self._kroki_uri(kroki_type) - return f'{kroki_uri}/{kroki_data_param}' + error(f'{kroki_uri}/{kroki_data_param}?{kroki_query_param}') + return f'{kroki_uri}/{kroki_data_param}?{kroki_query_param}' - def get_url(self, kroki_type, kroki_diagram_data): + def get_url(self, kroki_type, kroki_diagram_data,kroki_diagram_options={}): debug(f'get_url: {kroki_type}') if self.http_method != 'GET': error(f'HTTP method is {self.http_method}. Config error!') return None - return self._get_url(kroki_type, kroki_diagram_data) + return self._get_url(kroki_type, kroki_diagram_data, kroki_diagram_options) - def get_image_data(self, kroki_type, kroki_diagram_data): + def get_image_data(self, kroki_type, kroki_diagram_data, kroki_diagram_options={}): try: if self.http_method == 'GET': - url = self._get_url(kroki_type, kroki_diagram_data) + url = self._get_url(kroki_type, kroki_diagram_data, kroki_diagram_options) debug(f'get_image_data [GET {url[:50]}..]') r = requests.get(url) @@ -59,7 +62,8 @@ def get_image_data(self, kroki_type, kroki_diagram_data): debug(f'get_image_data [POST {url}]') r = requests.post(url, json={ - "diagram_source": kroki_diagram_data + "diagram_source": kroki_diagram_data, + "diagram_options": kroki_diagram_options }) debug(f'get_image_data [Response: {r}]') diff --git a/kroki/plugin.py b/kroki/plugin.py index adc444c..309523d 100644 --- a/kroki/plugin.py +++ b/kroki/plugin.py @@ -85,19 +85,21 @@ def _save_kroki_image_and_get_url(self, file_name, image_data, files): return f'/{get_url}' - def _replace_kroki_block(self, match_obj, files, page): + def _replace_kroki_block(self, match_obj, files, page): kroki_type = match_obj.group(1).lower() - kroki_data = match_obj.group(2) + kroki_options = match_obj.group(2) + kroki_data = match_obj.group(3) + kroki_diagram_options=dict(x.split('=') for x in kroki_options.strip().split(' ')) if kroki_options else {} get_url = None if self.config["DownloadImages"]: - image_data = self.kroki_client.get_image_data(kroki_type, kroki_data) + image_data = self.kroki_client.get_image_data(kroki_type, kroki_data, kroki_diagram_options) if image_data: file_name = self._kroki_filename(kroki_data, page) get_url = self._save_kroki_image_and_get_url(file_name, image_data, files) else: - get_url = self.kroki_client.get_url(kroki_type, kroki_data) + get_url = self.kroki_client.get_url(kroki_type, kroki_data, kroki_diagram_options) if get_url is not None: return f'![Kroki]({get_url})' From ad8625fe0a599507f8db538832262b7554c0636a Mon Sep 17 00:00:00 2001 From: grund Date: Fri, 10 Jun 2022 08:36:38 +0200 Subject: [PATCH 3/6] readme update --- README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 0db1a29..bfcdd79 100644 --- a/README.md +++ b/README.md @@ -32,10 +32,12 @@ plugins: Use code-fences with a tag of kroki-`` to replace the code with the wanted diagram. +[Diagram options](https://docs.kroki.io/kroki/setup/diagram-options/) can be set as well. + Example for BlockDiag: ````markdown -```kroki-blockdiag +```kroki-blockdiag no-transparency=false blockdiag { blockdiag -> generates -> "block-diagrams"; blockdiag -> is -> "very easy!"; From 4cbb28293a9899351e8ae117d41fec564e333828 Mon Sep 17 00:00:00 2001 From: grund Date: Fri, 10 Jun 2022 08:39:27 +0200 Subject: [PATCH 4/6] sorted types --- kroki/config.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/kroki/config.py b/kroki/config.py index b5345d1..903cb47 100644 --- a/kroki/config.py +++ b/kroki/config.py @@ -5,15 +5,15 @@ class KrokiDiagramTypes(): "erd", "graphviz", "nomnoml", + "pikchr", "plantuml", "c4plantuml", + "structurizr", "svgbob", + "umlet", "vega", "vegalite", - "wavedrom", - "pikchr", - "umlet", - "structurizr" + "wavedrom" ) kroki_blockdiag = ( From bbd669ed2f9195dd75a11064319a2b562fcfd748 Mon Sep 17 00:00:00 2001 From: grund Date: Mon, 20 Jun 2022 21:40:19 +0200 Subject: [PATCH 5/6] use os env as default if set for server url --- kroki/plugin.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/kroki/plugin.py b/kroki/plugin.py index 309523d..b6c55f4 100644 --- a/kroki/plugin.py +++ b/kroki/plugin.py @@ -9,6 +9,7 @@ from mkdocs.plugins import log from pathlib import Path from os.path import relpath +import os from .config import KrokiDiagramTypes from .client import KrokiClient @@ -21,7 +22,7 @@ class KrokiPlugin(BasePlugin): config_scheme = ( - ('ServerURL', config.config_options.Type(str, default='https://kroki.io')), + ('ServerURL', config.config_options.Type(str, default=os.getenv('KROKI_SERVER','https://kroki.io'))), ('EnableBlockDiag', config.config_options.Type(bool, default=True)), ('Enablebpmn', config.config_options.Type(bool, default=True)), ('EnableExcalidraw', config.config_options.Type(bool, default=True)), From 568852186a89128afea7fa982c6209da6e0b3f52 Mon Sep 17 00:00:00 2001 From: grund Date: Wed, 14 Sep 2022 15:50:23 +0200 Subject: [PATCH 6/6] fix trailing spaces and os env name --- kroki/config.py | 2 +- kroki/plugin.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/kroki/config.py b/kroki/config.py index 903cb47..6f9b807 100644 --- a/kroki/config.py +++ b/kroki/config.py @@ -51,4 +51,4 @@ def __init__(self, blockdiag_enabled, bpmn_enabled, excalidraw_enabled, mermaid_ def get_block_regex(self, fence_prefix): diagram_types_re = "|".join(self.diagram_types) - return rf'(?:```{fence_prefix})({diagram_types_re})((?:\s?[a-zA-Z0-9\-_]+=[a-zA-Z0-9\-_]+)*)\n(.*?)(?:```)' + return rf'(?:```{fence_prefix})({diagram_types_re})((?:\s?[a-zA-Z0-9\-_]+=[a-zA-Z0-9\-_]+)*)\n(.*?)(?:```)' diff --git a/kroki/plugin.py b/kroki/plugin.py index b6c55f4..b9baf28 100644 --- a/kroki/plugin.py +++ b/kroki/plugin.py @@ -22,7 +22,7 @@ class KrokiPlugin(BasePlugin): config_scheme = ( - ('ServerURL', config.config_options.Type(str, default=os.getenv('KROKI_SERVER','https://kroki.io'))), + ('ServerURL', config.config_options.Type(str, default=os.getenv('KROKI_SERVER_URL','https://kroki.io'))), ('EnableBlockDiag', config.config_options.Type(bool, default=True)), ('Enablebpmn', config.config_options.Type(bool, default=True)), ('EnableExcalidraw', config.config_options.Type(bool, default=True)), @@ -86,7 +86,7 @@ def _save_kroki_image_and_get_url(self, file_name, image_data, files): return f'/{get_url}' - def _replace_kroki_block(self, match_obj, files, page): + def _replace_kroki_block(self, match_obj, files, page): kroki_type = match_obj.group(1).lower() kroki_options = match_obj.group(2) kroki_data = match_obj.group(3)