Skip to content

Commit

Permalink
Export on-demand if we need {content}
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
LukeCarrier committed Apr 20, 2024
1 parent a5d791c commit 9f1d9b3
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions mkdocs_drawio_exporter/exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down

0 comments on commit 9f1d9b3

Please sign in to comment.