Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix source path resolution #79

Merged
merged 1 commit into from
Nov 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading