Skip to content

Commit

Permalink
add compatibility for mkdocs-print-site-plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
tuunit committed Mar 24, 2024
1 parent 5599bb4 commit b26a7f3
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 7 deletions.
2 changes: 1 addition & 1 deletion .devcontainer/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
FROM mcr.microsoft.com/vscode/devcontainers/python:3-bullseye

RUN pip3 install wheel mkdocs mkdocs-material BeautifulSoup4
RUN pip3 install wheel mkdocs mkdocs-material mkdocs-print-site-plugin BeautifulSoup4
1 change: 1 addition & 0 deletions example/mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ nav:
plugins:
- search
- drawio
- print-site

repo_url: https://github.com/tuunit/mkdocs-drawio
13 changes: 9 additions & 4 deletions mkdocs_drawio/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,15 @@ def __init__(self):
self.pool = None

def on_post_page(self, output_content, config, page, **kwargs):
return self.render_drawio_diagrams(output_content, page)

def render_drawio_diagrams(self, output_content, page):
if ".drawio" not in output_content.lower():
# Skip unecessary HTML parsing
return output_content

plugin_config = self.config.copy()

soup = BeautifulSoup(output_content, "html.parser")

# search for images using drawio extension
Expand All @@ -49,7 +54,7 @@ def on_post_page(self, output_content, config, page, **kwargs):
return output_content

# add drawio library to body
lib = soup.new_tag("script", src=self.config["viewer_js"])
lib = soup.new_tag("script", src=plugin_config["viewer_js"])
soup.body.append(lib)

# substitute images with embedded drawio diagram
Expand All @@ -58,7 +63,7 @@ def on_post_page(self, output_content, config, page, **kwargs):
for diagram in diagrams:
diagram.replace_with(
BeautifulSoup(
self.substitute_image(path, diagram["src"], diagram["alt"]),
DrawioPlugin.substitute_image(path, diagram["src"], diagram["alt"]),
"html.parser",
)
)
Expand All @@ -68,8 +73,8 @@ def on_post_page(self, output_content, config, page, **kwargs):
@staticmethod
def substitute_image(path: Path, src: str, alt: str):
diagram_xml = etree.parse(path.joinpath(src).resolve())
diagram = self.parse_diagram(diagram_xml, alt)
escaped_xml = self.escape_diagram(diagram)
diagram = DrawioPlugin.parse_diagram(diagram_xml, alt)
escaped_xml = DrawioPlugin.escape_diagram(diagram)

return SUB_TEMPLATE.substitute(xml_drawio=escaped_xml)

Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "mkdocs-drawio"
version = "1.5.5"
version = "1.6.0"
description = "MkDocs plugin for embedding Drawio files"
authors = [
"Sergey Lukin <onixpro@gmail.com>",
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def read(name):

setup(
name="mkdocs-drawio",
version="1.5.5",
version="1.6.0",
packages=find_packages(),
url="https://github.com/tuunit/mkdocs-drawio",
license="MIT",
Expand Down

0 comments on commit b26a7f3

Please sign in to comment.