Skip to content

Commit fa9bbe4

Browse files
committed
Turn GetSizeFromImages into a "post transform"
Closes #230.
1 parent b036c27 commit fa9bbe4

File tree

1 file changed

+20
-14
lines changed

1 file changed

+20
-14
lines changed

src/nbsphinx.py

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -762,7 +762,6 @@ def get_transforms(self):
762762
CreateNotebookSectionAnchors,
763763
ReplaceAlertDivs,
764764
CopyLinkedFiles,
765-
GetSizeFromImages,
766765
]
767766

768767
def parse(self, inputstring, document):
@@ -1484,21 +1483,27 @@ def apply(self):
14841483
env.nbsphinx_files.setdefault(env.docname, []).append(file)
14851484

14861485

1487-
class GetSizeFromImages(docutils.transforms.Transform):
1488-
"""Get size from images and store it as node attributes."""
1486+
class GetSizeFromImages(
1487+
sphinx.transforms.post_transforms.images.BaseImageConverter):
1488+
"""Get size from images and store it as node attributes.
14891489
1490-
default_priority = 500 # Doesn't really matter?
1490+
This is only done for LaTeX output.
14911491
1492-
def apply(self):
1493-
env = self.document.settings.env
1494-
for node in self.document.traverse(docutils.nodes.image):
1495-
if 'width' not in node and 'height' not in node:
1496-
uri = node['uri']
1497-
srcdir = os.path.dirname(env.doc2path(env.docname))
1498-
size = sphinx.util.images.get_image_size(
1499-
os.path.join(srcdir, uri))
1500-
if size is not None:
1501-
node['width'], node['height'] = map(str, size)
1492+
"""
1493+
1494+
# After ImageDownloader (100) and DataURIExtractor (150):
1495+
default_priority = 200
1496+
1497+
def match(self, node):
1498+
return self.app.builder.format == 'latex'
1499+
1500+
def handle(self, node):
1501+
if 'width' not in node and 'height' not in node:
1502+
srcdir = os.path.dirname(self.env.doc2path(self.env.docname))
1503+
image_path = os.path.normpath(os.path.join(srcdir, node['uri']))
1504+
size = sphinx.util.images.get_image_size(image_path)
1505+
if size is not None:
1506+
node['width'], node['height'] = map(str, size)
15021507

15031508

15041509
def builder_inited(app):
@@ -1750,6 +1755,7 @@ def setup(app):
17501755
app.add_transform(CreateSectionLabels)
17511756
app.add_transform(CreateDomainObjectLabels)
17521757
app.add_transform(RewriteLocalLinks)
1758+
app.add_post_transform(GetSizeFromImages)
17531759

17541760
# Make docutils' "code" directive (generated by markdown2rst/pandoc)
17551761
# behave like Sphinx's "code-block",

0 commit comments

Comments
 (0)