Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get sizes from image files for better LaTeX output #222

Merged
merged 4 commits into from
Nov 2, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ matrix:
- env: ADDITIONAL_COMMAND="python -m sphinx doc/ doc/_build/ -b linkcheck"

# a few older Sphinx releases using default Python version
- env: SPHINX="==1.3.2 docutils<0.13.1"
- env: SPHINX="==1.3.6 docutils<0.13.1"
- env: SPHINX="==1.4.0 docutils<0.13.1"
- env: SPHINX="==1.4.9 docutils<0.13.1"
- env: SPHINX="==1.5.0 docutils<0.13.1"
- env: SPHINX="==1.5.1"
Expand Down
3 changes: 3 additions & 0 deletions doc/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
# Don't add .txt suffix to source files (available for Sphinx >= 1.5):
html_sourcelink_suffix = ''

# Work-around until https://github.com/sphinx-doc/sphinx/issues/4229 is solved:
html_scaled_image_link = False

# Execute notebooks before conversion: 'always', 'never', 'auto' (default)
#nbsphinx_execute = 'never'

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
'nbconvert!=5.4',
'traitlets',
'nbformat',
'sphinx>=1.3.2',
'sphinx>=1.4',
],
author='Matthias Geier',
author_email='[email protected]',
Expand Down
28 changes: 28 additions & 0 deletions src/nbsphinx.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,16 @@
{%- elif datatype in ['image/svg+xml', 'image/png', 'image/jpeg', 'application/pdf'] %}

.. image:: {{ output.metadata.filenames[datatype] | posix_path }}
{%- if datatype in output.metadata %}
{%- set width = output.metadata[datatype].width %}
{%- if width %}
:width: {{ width }}
{%- endif %}
{%- set height = output.metadata[datatype].height %}
{%- if height %}
:height: {{ height }}
{% endif %}
{% endif %}
{%- elif datatype in ['text/markdown'] %}

{{ output.data['text/markdown'] | markdown2rst | indent }}
Expand Down Expand Up @@ -720,6 +730,7 @@ def get_transforms(self):
CreateNotebookSectionAnchors,
ReplaceAlertDivs,
CopyLinkedFiles,
GetSizeFromImages,
]

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