Skip to content

Commit

Permalink
Merge pull request #51 from MikhailKravets/1.4.1
Browse files Browse the repository at this point in the history
1.4.1
  • Loading branch information
MikhailKravets authored Oct 5, 2024
2 parents e7968de + efff4c7 commit 1aff5f6
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 3 deletions.
2 changes: 1 addition & 1 deletion mkdocs_puml/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
"""Package that brings PlantUML to MkDocs"""

__version__ = "1.4.0"
__version__ = "1.4.1"
18 changes: 16 additions & 2 deletions mkdocs_puml/puml.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import logging
import re
import typing
from concurrent.futures import ThreadPoolExecutor
Expand All @@ -9,6 +10,9 @@
from mkdocs_puml.encoder import encode


logger = logging.getLogger("mkdocs.plugins.plantuml")


class PlantUML:
"""PlantUML converter class.
It requests PUML service, updates received `svg`
Expand All @@ -28,6 +32,7 @@ class PlantUML:
"""

_html_comment_regex = re.compile(r"<!--.*?-->", flags=re.DOTALL)
ERROR_SVG = "<svg><text>Error</text></svg>"

def __init__(
self,
Expand Down Expand Up @@ -107,7 +112,16 @@ def request(self, encoded_diagram: str) -> str:
Returns:
SVG representation of the diagram
"""
resp = requests.get(urljoin(self.base_url, encoded_diagram), verify=self.verify_ssl)
resp = requests.get(
urljoin(self.base_url, encoded_diagram), verify=self.verify_ssl
)

if not resp.ok:
logger.warning(
f"While building diagram \n\n{encoded_diagram}\n\nServer responded"
f" with a status {resp.status_code}"
)
return self.ERROR_SVG

# Use 'ignore' to strip non-utf chars
return resp.content.decode("utf-8", errors="ignore")
Expand All @@ -129,5 +143,5 @@ def _stylize_svg(self, svg: Element):
Notes:
It can be used to add support of light / dark theme.
"""
svg.setAttribute('preserveAspectRatio', "xMidYMid meet")
svg.setAttribute("preserveAspectRatio", "xMidYMid meet")
svg.setAttribute("style", "background: var(--md-default-bg-color)")
14 changes: 14 additions & 0 deletions tests/plugins/test_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,3 +151,17 @@ def test_replace_method(plant_uml_plugin):
result = plant_uml_plugin._replace(key, content)
assert '<div class="puml light"><svg>light</svg></div>' in result
assert '<div class="puml dark"><svg>dark</svg></div>' in result


def test_unsupported_output(mock_requests, plant_uml_plugin, diagrams_dict, plugin_environment):
# All requests return an erroneous response
mock_requests.return_value.ok = False

# Try converting to SVG
plant_uml_plugin.auto_dark = True
plant_uml_plugin.diagrams = diagrams_dict
plant_uml_plugin.on_env(plugin_environment)

for diagram in plant_uml_plugin.diagrams.values():
for variant in diagram:
assert '<text>Error</text>' in variant

0 comments on commit 1aff5f6

Please sign in to comment.