Skip to content

Commit

Permalink
Fix source path resolution
Browse files Browse the repository at this point in the history
Broke this in #75 🤦
  • Loading branch information
LukeCarrier committed Nov 10, 2024
1 parent dd05e4f commit 75669c4
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 8 deletions.
10 changes: 5 additions & 5 deletions mkdocs_drawio_exporter/exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,14 +126,14 @@ def __hash__(self):
def __repr__(self):
return f"Source({self.source_embed}, {self.page_index}, {self.source_rel})"

def resolve_rel_path(self, page_dest_path):
def resolve_rel_path(self, page_src_path):
"""Resolve the path of the source, relative to the documentation directory.
:param str page_dest_path: The destination path of the parent page.
:param str page_src_path: The source path of the parent page.
"""
unescaped_source_embed = urllib.parse.unquote(self.source_embed)
self.source_rel = os.path.normpath(os.path.join(
os.path.dirname(page_dest_path),
os.path.dirname(page_src_path),
unescaped_source_embed))


Expand Down Expand Up @@ -248,7 +248,7 @@ def validate_config(self, config: Configuration):
'embed_format', config['embed_format'],
'cannot inline content of non-SVG format')

def rewrite_image_embeds(self, page_dest_path, output_content, config: Configuration):
def rewrite_image_embeds(self, page_src_path, output_content, config: Configuration):
"""Rewrite image embeds.
:param str page_dest_path: Destination path.
Expand All @@ -270,7 +270,7 @@ def replace(match):

if fnmatch.fnmatch(filename, config['sources']):
source = Source(filename, page_index)
source.resolve_rel_path(page_dest_path)
source.resolve_rel_path(page_src_path)
content_sources.append(source)
img_src = f"{filename}-{page_index}.{config['format']}"

Expand Down
2 changes: 1 addition & 1 deletion mkdocs_drawio_exporter/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def on_config(self, config, **kwargs):

def on_page_markdown(self, markdown, page, **kwargs):
output_content, content_sources = self.exporter.rewrite_image_embeds(
page.file.dest_path, markdown, self.config)
page.file.src_path, markdown, self.config)

self.sources += content_sources

Expand Down
17 changes: 15 additions & 2 deletions mkdocs_drawio_exporter/tests/exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import os
from os.path import join, sep

from ..exporter import Configuration, ConfigurationError, DrawIoExporter
from ..exporter import Configuration, ConfigurationError, DrawIoExporter, Source


class FileMock:
Expand All @@ -17,12 +17,25 @@ def __init__(self, **kwargs):
setattr(self, attr, value)


class SourceTests(unittest.TestCase):
def test_resolve_rel_path(self):
cases = [
("dir/diagram.drawio", ("diagram.drawio", 0), "dir/page.md"),
("dir1/dir2/diagram.drawio", ("diagram.drawio", 0), "dir1/dir2/index.md"),
("dir1/dir2/dir3/diagram.drawio", ("diagram.drawio", 0), "dir1/dir2/dir3/index.md"),
]
for expect, source_args, page_src_path in cases:
with self.subTest(expect, source_args, page_src_path):
source = Source(*source_args)
result = source.resolve_rel_path(page_src_path)
self.assertEqual(expect, result)


class ExporterTests(unittest.TestCase):
log = None

def setUp(self):
self.log = logging.getLogger(__name__)

def make_exporter(self, docs_dir=None):
if not docs_dir:
docs_dir = sep + 'docs'
Expand Down

0 comments on commit 75669c4

Please sign in to comment.