Skip to content

Commit

Permalink
Turn GetSizeFromImages into a "post transform"
Browse files Browse the repository at this point in the history
Closes #230.
  • Loading branch information
mgeier committed Dec 11, 2018
1 parent 68b709e commit 008c6e5
Showing 1 changed file with 20 additions and 14 deletions.
34 changes: 20 additions & 14 deletions src/nbsphinx.py
Original file line number Diff line number Diff line change
Expand Up @@ -760,7 +760,6 @@ def get_transforms(self):
CreateNotebookSectionAnchors,
ReplaceAlertDivs,
CopyLinkedFiles,
GetSizeFromImages,
]

def parse(self, inputstring, document):
Expand Down Expand Up @@ -1482,21 +1481,27 @@ def apply(self):
env.nbsphinx_files.setdefault(env.docname, []).append(file)


class GetSizeFromImages(docutils.transforms.Transform):
"""Get size from images and store it as node attributes."""
class GetSizeFromImages(
sphinx.transforms.post_transforms.images.BaseImageConverter):
"""Get size from images and store it as node attributes.
default_priority = 500 # Doesn't really matter?
This is only done for LaTeX output.
def apply(self):
env = self.document.settings.env
for node in self.document.traverse(docutils.nodes.image):
if 'width' not in node and 'height' not in node:
uri = node['uri']
srcdir = os.path.dirname(env.doc2path(env.docname))
size = sphinx.util.images.get_image_size(
os.path.join(srcdir, uri))
if size is not None:
node['width'], node['height'] = map(str, size)
"""

# After ImageDownloader (100) and DataURIExtractor (150):
default_priority = 200

def match(self, node):
return self.app.builder.format == 'latex'

def handle(self, node):
if 'width' not in node and 'height' not in node:
srcdir = os.path.dirname(self.env.doc2path(self.env.docname))
image_path = os.path.normpath(os.path.join(srcdir, node['uri']))
size = sphinx.util.images.get_image_size(image_path)
if size is not None:
node['width'], node['height'] = map(str, size)


def builder_inited(app):
Expand Down Expand Up @@ -1747,6 +1752,7 @@ def setup(app):
app.add_transform(CreateSectionLabels)
app.add_transform(CreateDomainObjectLabels)
app.add_transform(RewriteLocalLinks)
app.add_post_transform(GetSizeFromImages)

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

0 comments on commit 008c6e5

Please sign in to comment.