From 9f1d9b3434ae74a70b3ee9fa43d0e8330e73d8f0 Mon Sep 17 00:00:00 2001 From: Luke Carrier Date: Sat, 20 Apr 2024 13:48:19 +0100 Subject: [PATCH] Export on-demand if we need {content} There will be some performance hit doing this as we'll end up calling ensure_file_cached() again for every diagram in on_post_page(), and we'll call it for every reference here rather than once per diagram, but it'll do for now. It may later make sense to keep a set of already-referenced Sources so we don't repeat work. --- mkdocs_drawio_exporter/exporter.py | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/mkdocs_drawio_exporter/exporter.py b/mkdocs_drawio_exporter/exporter.py index 991860a..b54a7df 100644 --- a/mkdocs_drawio_exporter/exporter.py +++ b/mkdocs_drawio_exporter/exporter.py @@ -257,10 +257,22 @@ def replace(match): content_sources.append(source) img_src = f"{filename}-{page_index}.{config["format"]}" - # Read file content only if we actually need it + # Cache the file on-demand and read file content only if we + # need to inline the file's content. content = None if "{content}" in config["embed_format"]: - img_path = self.make_cache_filename(source.source_rel, page_index, config['cache_dir']) + img_path = self.make_cache_filename( + source.source_rel, page_index, config['cache_dir']) + + abs_src_path = os.path.join(self.docs_dir, source.source_rel) + _, exit_status = self.ensure_file_cached( + abs_src_path, source.source_rel, source.page_index, + config) + + if exit_status not in (None, 0): + self.log.error(f'Export failed with exit status {exit_status}; skipping rewrite') + return match.group(0) + with open(img_path, "r") as f: content = f.read()