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
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
23 changes: 2 additions & 21 deletions readme_renderer/rst.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
from __future__ import absolute_import, division, print_function

import io
import os.path

from docutils.core import publish_parts
from docutils.writers.html4css1 import HTMLTranslator, Writer
Expand All @@ -25,26 +24,8 @@

class ReadMeHTMLTranslator(HTMLTranslator):

def depart_image(self, node):
uri = node["uri"]
ext = os.path.splitext(uri)[1].lower()
# we need to swap RST's use of `object` with `img` tags
# see http://git.io/5me3dA
if ext == ".svg":
# preserve essential attributes
atts = {}
for attribute, value in node.attributes.items():
# we have no time for empty values
if value:
if attribute == "uri":
atts["src"] = value
else:
atts[attribute] = value

# toss off `object` tag
self.body.pop()
# 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 = {}


SETTINGS = {
Expand Down
1 change: 0 additions & 1 deletion tests/fixtures/test_rst_img_attrs.html

This file was deleted.

1 change: 1 addition & 0 deletions tests/fixtures/test_rst_png.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<img alt="https://example.com/badge.png" src="https://example.com/badge.png">
1 change: 1 addition & 0 deletions tests/fixtures/test_rst_png.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.. image:: https://example.com/badge.png
1 change: 1 addition & 0 deletions tests/fixtures/test_rst_png_attrs.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<img alt="alternate text" src="https://example.com/badge.png">
5 changes: 5 additions & 0 deletions tests/fixtures/test_rst_png_attrs.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.. image:: https://example.com/badge.png
:height: 100px
:width: 25.0%
:alt: alternate text
:align: right
2 changes: 1 addition & 1 deletion tests/fixtures/test_rst_svg.html
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<img src="https://example.com/badge.svg">
<img alt="https://example.com/badge.svg" src="https://example.com/badge.svg">
1 change: 1 addition & 0 deletions tests/fixtures/test_rst_svg_attrs.html
Original file line number Diff line number Diff line change
@@ -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.