Skip to content

Commit

Permalink
Get sizes from image files for better LaTeX output
Browse files Browse the repository at this point in the history
Fixes #12.
  • Loading branch information
mgeier committed Oct 31, 2018
1 parent 1ce1c6c commit 3f2a29c
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/nbsphinx.py
Original file line number Diff line number Diff line change
Expand Up @@ -720,6 +720,7 @@ def get_transforms(self):
CreateNotebookSectionAnchors,
ReplaceAlertDivs,
CopyLinkedFiles,
GetSizeFromImages,
]

def parse(self, inputstring, document):
Expand Down Expand Up @@ -1373,6 +1374,23 @@ 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."""

default_priority = 500 # Doesn't really matter?

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)


def builder_inited(app):
# Add LaTeX definitions to preamble
latex_elements = app.builder.config.latex_elements
Expand Down

0 comments on commit 3f2a29c

Please sign in to comment.