Skip to content

Commit

Permalink
Merge pull request #65 from AVATEAM-IT-SYSTEMHAUS/58-improve-handling…
Browse files Browse the repository at this point in the history
…-of-the-fenced-code-blocks

58 improve handling of the fenced code blocks
  • Loading branch information
oniboni authored May 17, 2024
2 parents cbeb823 + 709f7b6 commit c9a5556
Show file tree
Hide file tree
Showing 7 changed files with 238 additions and 43 deletions.
4 changes: 0 additions & 4 deletions kroki/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,5 @@ def __init__(

log.debug("File and Diagram types configured: %s", self.diagram_types_supporting_file)

def get_block_regex(self, fence_prefix: str) -> str:
diagram_types_re = "|".join(self.diagram_types_supporting_file.keys())
return rf"(?:```{fence_prefix})({diagram_types_re})((?:\s?[a-zA-Z0-9\-_]+=[a-zA-Z0-9\-_]+)*)\n(.*?)(?:```)"

def get_file_ext(self, kroki_type: str) -> str:
return self.diagram_types_supporting_file[kroki_type]
37 changes: 27 additions & 10 deletions kroki/plugin.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os
import re
import textwrap
from pathlib import Path

from mkdocs.config import config_options
Expand Down Expand Up @@ -54,6 +55,14 @@ class KrokiPlugin(MkDocsBasePlugin[KrokiPluginConfig]):
from_file_prefix = "@from_file:"
global_config: MkDocsConfig
fail_fast: bool
_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: MkDocsConfig) -> MkDocsConfig:
log.debug("Configuring", extra={"config": self.config})
Expand Down Expand Up @@ -82,11 +91,9 @@ def on_config(self, config: MkDocsConfig) -> MkDocsConfig:

return config

def _replace_kroki_block(self, match_obj: re.Match, files: MkDocsFiles, page: MkDocsPage) -> str:
kroki_type = match_obj.group(1).lower()
kroki_options = match_obj.group(2)
kroki_data = match_obj.group(3)

def _replace_kroki_block(
self, kroki_type: str, kroki_options: str, kroki_data: str, files: MkDocsFiles, page: MkDocsPage
) -> str:
if kroki_data.startswith(self.from_file_prefix):
file_name = kroki_data.removeprefix(self.from_file_prefix).strip()
file_path = Path(self.global_config.docs_dir) / file_name
Expand Down Expand Up @@ -119,10 +126,20 @@ def _replace_kroki_block(self, match_obj: re.Match, files: MkDocsFiles, page: Mk
def on_page_markdown(self, markdown: str, files: MkDocsFiles, page: MkDocsPage, **_kwargs) -> str:
log.debug("on_page_markdown [page: %s]", page)

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

def replace_kroki_block(match_obj: re.Match):
kroki_type = match_obj.group("lang").lower()
if kroki_type.startswith(fence_prefix):
kroki_type = kroki_type[len(fence_prefix) :]

if kroki_type in key_types:
return match_obj.group("indent") + self._replace_kroki_block(
kroki_type, match_obj.group("opts"), textwrap.dedent(match_obj.group("code")), files, page
)

def replace_kroki_block(match_obj):
return self._replace_kroki_block(match_obj, files, page)
# Not supported, skip over whole block
return match_obj.group()

return re.sub(pattern, replace_kroki_block, markdown)
return re.sub(self._FENCE_RE, replace_kroki_block, markdown)
3 changes: 3 additions & 0 deletions tests/data/template/docs/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Test

$code_block
8 changes: 8 additions & 0 deletions tests/data/template/mkdocs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
site_name: Test pages for mkdocs-kroki-plugin

theme:
name: material

plugins:
- kroki:
FencePrefix: ''
186 changes: 186 additions & 0 deletions tests/test_fences.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
import pytest

from tests.utils import MkDocsTemplateHelper

TEST_CASES = {
"#35": """
```` plantuml
stuff containing ```
````
""",
"https://spec.commonmark.org/0.31.2/#example-119": """
``` mermaid
<
>
```
""",
"https://spec.commonmark.org/0.31.2/#example-120": """
~~~ mermaid
<
>
~~~
""",
"https://spec.commonmark.org/0.31.2/#example-122": """
``` mermaid
aaa
~~~
```
""",
"https://spec.commonmark.org/0.31.2/#example-123": """
~~~ mermaid
aaa
```
~~~
""",
"https://spec.commonmark.org/0.31.2/#example-125": """
~~~~ mermaid
aaa
~~~
~~~~
""",
"https://spec.commonmark.org/0.31.2/#example-129": """
``` mermaid
```
""",
"https://spec.commonmark.org/0.31.2/#example-130": """
``` mermaid
```
""",
}

TEST_CASES_NOT_SUPPORTED = {
"https://spec.commonmark.org/0.31.2/#example-121": """
`` mermaid
foo
``
""",
"https://spec.commonmark.org/0.31.2/#example-124": """
```` mermaid
aaa
```
``````
""",
"https://spec.commonmark.org/0.31.2/#example-126": """
``` mermaid
""",
"https://spec.commonmark.org/0.31.2/#example-127": """
````` mermaid
```
aaa
""",
"https://spec.commonmark.org/0.31.2/#example-128": """
> ``` mermaid
> aaa
bbb
""",
"https://spec.commonmark.org/0.31.2/#example-131": """
``` mermaid
aaa
aaa
```
""",
"https://spec.commonmark.org/0.31.2/#example-132": """
```
aaa
aaa
aaa
```
""",
"https://spec.commonmark.org/0.31.2/#example-133": """
```
aaa
aaa
aaa
```
""",
"https://spec.commonmark.org/0.31.2/#example-135": """
```
aaa
```
""",
"https://spec.commonmark.org/0.31.2/#example-136": """
```
aaa
```
""",
"https://spec.commonmark.org/0.31.2/#example-140": """
foo
```
bar
```
baz
""",
"https://spec.commonmark.org/0.31.2/#example-141": """
foo
---
~~~
bar
~~~
# baz
""",
"https://spec.commonmark.org/0.31.2/#example-146": """
~~~ aa ``` ~~~
foo
~~~
""",
"https://spec.commonmark.org/0.31.2/#example-147": """
```
``` aaa
```
""",
}

TEST_CASES_ESCAPED = {
"https://spec.commonmark.org/0.31.2/#example-134": """
```
aaa
```
""",
}


@pytest.mark.parametrize("test_code_block", [pytest.param(v, id=k) for k, v in TEST_CASES.items()])
@pytest.mark.usefixtures("kroki_dummy")
def test_fences(test_code_block) -> None:
with MkDocsTemplateHelper(test_code_block) as mkdocs_helper:
mkdocs_helper.set_http_method("POST")
result = mkdocs_helper.invoke_build()

assert result.exit_code == 0
image_files = list((mkdocs_helper.test_dir / "site").glob("*.svg"))
assert len(image_files) == 1


@pytest.mark.parametrize(
"test_code_block",
[pytest.param(v, id=k) for k, v in TEST_CASES_NOT_SUPPORTED.items()]
+ [pytest.param(v, id=k) for k, v in TEST_CASES_ESCAPED.items()],
)
@pytest.mark.usefixtures("kroki_dummy")
def test_fences_not_supported(test_code_block) -> None:
with MkDocsTemplateHelper(test_code_block) as mkdocs_helper:
mkdocs_helper.set_http_method("POST")
result = mkdocs_helper.invoke_build()

assert result.exit_code == 0
image_files = list((mkdocs_helper.test_dir / "site").glob("*.svg"))
assert len(image_files) == 0


@pytest.mark.usefixtures("kroki_dummy")
def test_pandoc_fenced_code_blocks() -> None:
with MkDocsTemplateHelper("""~~~~~~~~~~~~~~~~ mermaid
~~~~~~~~~~
code including tildes
~~~~~~~~~~
~~~~~~~~~~~~~~~~""") as mkdocs_helper:
mkdocs_helper.set_http_method("POST")
result = mkdocs_helper.invoke_build()

assert result.exit_code == 0
image_files = list((mkdocs_helper.test_dir / "site").glob("*.svg"))
assert len(image_files) == 1
25 changes: 0 additions & 25 deletions tests/test_whitespace.py

This file was deleted.

18 changes: 14 additions & 4 deletions tests/utils.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import logging
import os
import pathlib
import shutil
import tempfile
from contextlib import AbstractContextManager
from pathlib import Path
from string import Template
from typing import Literal

import yaml
Expand All @@ -13,8 +13,6 @@

from tests.compat import chdir

logging.basicConfig(level=logging.INFO)


class NoPluginEntryError(ValueError):
def __init__(self) -> None:
Expand All @@ -26,6 +24,7 @@ def __init__(self, test_case: str) -> None:
self.config_file = None
self.test_case = test_case
self.test_dir = Path(tempfile.mkdtemp())
self._copy_test_case()

def _copy_test_case(self) -> None:
# equals to `../data`, using current source file as a pin
Expand All @@ -41,7 +40,6 @@ def _dump_config(self) -> None:
yaml.safe_dump(self.config_file, file)

def __enter__(self) -> "MkDocsHelper":
self._copy_test_case()
self._load_config()

return self
Expand Down Expand Up @@ -69,3 +67,15 @@ def invoke_build(self) -> Result:
runner = CliRunner()
with chdir(self.test_dir):
return runner.invoke(build_command)


class MkDocsTemplateHelper(MkDocsHelper):
def _substitute_code_block(self, code_block: str):
with open(self.test_dir / "docs/index.md") as in_file:
file_content = Template(in_file.read())
with open(self.test_dir / "docs/index.md", "w") as out_file:
out_file.write(file_content.substitute(code_block=code_block))

def __init__(self, code_block: str) -> None:
super().__init__("template")
self._substitute_code_block(code_block)

0 comments on commit c9a5556

Please sign in to comment.