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

Unify handling of SVG and other images in RST. #113

Merged
merged 3 commits into from
Jul 31, 2018
Merged

Unify handling of SVG and other images in RST. #113

merged 3 commits into from
Jul 31, 2018

Conversation

mila
Copy link
Contributor

@mila mila commented Jul 28, 2018

ReadMeHTMLTranslator was overidden to not to render object tags for SVG.
Unfortunately it produced different results than for PNG or other
image types.

This a first pull request, which addresses #112. Once image handling is unified, other issues
can can fixed.

ReadMeHTMLTranslator was overriden to not to render object tags for SVG.
Unfortunately it produced different results than for PNG or other
image types.

With this commit, HTML code for SVG images will be same as for other
images. It means that it won't have width, height or align attributes.
But it will be better to address this issue for all images types at once.
@nlhkabu nlhkabu requested a review from di July 28, 2018 13:27
@theacodes
Copy link
Member

Hmm, based on https://sourceforge.net/p/docutils/bugs/247/, docutils should already be outputting img tags for svgs. Can you help me understand why we need object_image_types = {'.swf': 'application/x-shockwave-flash'}?

@mila
Copy link
Contributor Author

mila commented Jul 28, 2018

I have docutils 0.14 installed and they still render <object> for svg.

ReadMeHTMLTranslator extends docutils.writers.html4css1.HTMLTranslator which extends docutils.writers._html_base.HTMLTranslator.

  • _html_base.HTMLTranslator sets object_image_types = {'.swf': 'application/x-shockwave-flash'}
  • html4css1.HTMLTranslator adds svg there {'.svg': 'image/svg+xml', '.swf': 'application/x-shockwave-flash'} with a comment that "html4css1 strives for IE6 compatibility"
  • I removed .svg and kept .swf there, just to make the change minimal.

@theacodes
Copy link
Member

theacodes commented Jul 28, 2018 via email

@mila
Copy link
Contributor Author

mila commented Jul 28, 2018

Gotcha. Could we use a newer base translator?

I'm not sure about that. There are two implementations: html4css1 and html5_polyglot.
The first one is the one we are using now, the latter claims to "generates polyglot markup: HTML5 that is also valid XML."

The polyglot markup may be a better choice, but it generates different output and makes tests failing.
So if the translator has to be changes, I would do that in a separate issue.

@theacodes
Copy link
Member

Sounds pretty reasonable, we should do that in a follow-up.

I'd like @di to take a look at this as well, as anything that affects the sandboxed output here should be double-checked.

self.body.pop()
# add on `img` with attributes
self.body.append(self.starttag(node, "img", **atts))
# We need to swap RST's use of `object` with `img` tags
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you update this comment to mention that we're override the base class to not output svg with object?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

Copy link
Member

@di di left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for digging into this! Just a few comments.

# add on `img` with attributes
self.body.append(self.starttag(node, "img", **atts))
# Overrides base class not to output `<object>` tag for SVG images.
object_image_types = {'.swf': 'application/x-shockwave-flash'}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's just make this object_image_types = {}, we don't actually want to support any <object> tags at all.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@@ -0,0 +1 @@
<img alt="alternate text" src="https://example.com/badge.svg">
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We're kind of experiencing a regression here, right? Before, this:

.. image:: https://example.com/badge.svg
   :height: 100px
   :width: 25.0%
   :alt: alternate text
   :align: right

would become:

<img align="right" alt="alternate text" height="100px" src="https://example.com/badge.svg" width="25.0%">

but now it becomes:

<img alt="alternate text" src="https://example.com/badge.svg">

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but the current behavior is problematic:

  • align="right" does not actually align the image, CSS is necessary for that.
  • height="100px" wont have any effect, because units (px) must be added to CSS, not image attributes
  • Those attributes are preserved for SVG images only (or more precisely for URLs ending ".svg")

#114 addresses image alignment and dimensions. Without unifying SVG with other image types, the subsequent PR would fix styling of non-SVG images only.

(I created two PRs because the second relaxes what HTML is allowed, so there can be more discussion.)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it, thanks for the explanation.

@theacodes theacodes merged commit 9155cfe into pypa:master Jul 31, 2018
@theacodes
Copy link
Member

Thank you, @mila!

@mila mila deleted the images-unification branch July 31, 2018 19:44
di added a commit that referenced this pull request Apr 21, 2020
This fixes a regression introduced in #113 where height/width attributes
were removed from images defined in reStructuredText.

More discussion here: #113 (comment)
di added a commit that referenced this pull request Apr 21, 2020
This fixes a regression introduced in #113 where height/width attributes
were removed from images defined in reStructuredText.

More discussion here: #113 (comment)
di added a commit that referenced this pull request Apr 21, 2020
This fixes a regression introduced in #113 where height/width attributes
were removed from images defined in reStructuredText.

More discussion here: #113 (comment)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants