Skip to content

Commit

Permalink
Change how fence blocks are processed
Browse files Browse the repository at this point in the history
Use a fixed regex to match all fence blocks, not just the
ones that are modified by this plugin.

de-indent the code block before processing.

This will avoid having wrong matches within a fenced block. ex:

````
Hey, this is just regular text, presenting a plantuml code block.

``` plantuml
@startuml
...
@enduml
```
````

De-indentind the code is necessary if the code block is in a structural
element like a list.
(Apart from being not correct, handling from_file_prefix did not work in
that case)
  • Loading branch information
aag-norbert-lange committed May 8, 2023
1 parent 40fbdd9 commit f1e8fdc
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 16 deletions.
4 changes: 0 additions & 4 deletions kroki/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,5 @@ def __init__(

info(f"File and Diagram types configured: {self.diagram_types_supporting_file}")

def get_block_regex(self, fence_prefix):
diagram_types_re = "|".join(self.diagram_types_supporting_file.keys())
return rf"(?P<fence>^([ ]*)(?:````*|~~~~*))[ ]*{fence_prefix}({diagram_types_re})((?:[ ]?[a-zA-Z0-9\-_]+=[a-zA-Z0-9\-_]+)*)\n(.*?)(?<=\n)(?P=fence)[ ]*$" # noqa

def get_file_ext(self, kroki_type):
return self.diagram_types_supporting_file[kroki_type]
36 changes: 24 additions & 12 deletions kroki/plugin.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import hashlib
import re
import tempfile
import textwrap

from functools import partial
from mkdocs.plugins import BasePlugin
Expand Down Expand Up @@ -57,6 +58,13 @@ class KrokiPlugin(BasePlugin):
from_file_prefix = "@from_file:"
from_file_prefix_len = len(from_file_prefix)

_FENCE_RE = re.compile(r"(?P<fence>^(?P<indent>[ ]*)(?:````*|~~~~*))[ ]*"
r"(\.?(?P<lang>[\w#.+-]*)[ ]*)?"
r"(?P<opts>(?:[ ]?[a-zA-Z0-9\-_]+=[a-zA-Z0-9\-_]+)*)\n"
r"(?P<code>.*?)(?<=\n)"
r"(?P=fence)[ ]*$",
flags=re.IGNORECASE + re.DOTALL + re.MULTILINE)

def on_config(self, config, **_kwargs):
info(f"Configuring: {self.config}")

Expand Down Expand Up @@ -119,12 +127,7 @@ def _save_kroki_image_and_get_url(self, file_name, image_data, files):

return f"{self._site_url}{mkdocs_file.url}"

def _replace_kroki_block(self, match_obj, files, page):
kroki_indent = match_obj.group(2)
kroki_type = match_obj.group(3).lower()
kroki_options = match_obj.group(4)
kroki_data = match_obj.group(5)

def _replace_kroki_block(self, kroki_type, kroki_options, kroki_data, files, page):
if kroki_data.startswith(self.from_file_prefix):
file_name = kroki_data[self.from_file_prefix_len :].strip() # noqa
file_path = self._docs_dir / file_name
Expand Down Expand Up @@ -159,20 +162,29 @@ def _replace_kroki_block(self, match_obj, files, page):
)

if get_url is not None:
return kroki_indent + f"![Kroki]({get_url})"
return f"![Kroki]({get_url})"

return f'!!! error "Could not render!"\n\n```\n{kroki_data}\n```'

def on_page_markdown(self, markdown, files, page, **_kwargs):
debug(f"on_page_markdown [page: {page}]")

kroki_regex = self.diagram_types.get_block_regex(self.fence_prefix)
pattern = re.compile(kroki_regex, flags=re.IGNORECASE + re.DOTALL + re.MULTILINE)
key_types = self.diagram_types.diagram_types_supporting_file.keys()
fence_prefix = self.fence_prefix

def replace_kroki_block(match_obj):
return self._replace_kroki_block(match_obj, files, page)
kroki_type = match_obj.group('lang').lower()
if kroki_type.startswith(fence_prefix):
kroki_type = kroki_type[len(fence_prefix):]

if kroki_type not in key_types:
# Not supported, skip over whole block
return match_obj.group()

return match_obj.group('indent') + \
self._replace_kroki_block(kroki_type, match_obj.group('opts'), textwrap.dedent(match_obj.group('code')),
files, page)

return re.sub(pattern, replace_kroki_block, markdown)
return re.sub(self._FENCE_RE, replace_kroki_block, markdown)

def on_post_build(self, **_kwargs):
if hasattr(self, "_tmp_dir"):
Expand Down

0 comments on commit f1e8fdc

Please sign in to comment.