Skip to content

Commit 1e59391

Browse files
authored
Merge pull request #760 from DimitriPapadopoulos/readme_renderer
Update vendorized readme_renderer
2 parents 033e4f8 + dc971e5 commit 1e59391

File tree

1 file changed

+38
-30
lines changed

1 file changed

+38
-30
lines changed

flit/vendorized/readme/rst.py

Lines changed: 38 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -12,43 +12,39 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
# Copied from https://github.com/pypa/readme_renderer
16-
# Commit 5b455a9c5bafc1732dafad9619bcbfa8e15432c9
17-
18-
from __future__ import absolute_import, division, print_function
19-
2015
import io
21-
import os.path
16+
from typing import Any, Dict, IO, Optional, Union
2217

2318
from docutils.core import publish_parts
24-
from docutils.writers.html4css1 import HTMLTranslator, Writer
19+
from docutils.nodes import colspec, image
20+
from docutils.writers.html5_polyglot import HTMLTranslator, Writer
2521
from docutils.utils import SystemMessage
2622

2723
from .clean import clean
2824

2925

30-
class ReadMeHTMLTranslator(HTMLTranslator):
26+
class ReadMeHTMLTranslator(HTMLTranslator): # type: ignore[misc] # docutils is incomplete, returns `Any` python/typeshed#7256 # noqa E501
3127

32-
def depart_image(self, node):
33-
uri = node["uri"]
34-
ext = os.path.splitext(uri)[1].lower()
35-
# we need to swap RST's use of `object` with `img` tags
36-
# see http://git.io/5me3dA
37-
if ext == ".svg":
38-
# preserve essential attributes
39-
atts = {}
40-
for attribute, value in node.attributes.items():
41-
# we have no time for empty values
42-
if value:
43-
if attribute == "uri":
44-
atts["src"] = value
45-
else:
46-
atts[attribute] = value
28+
# Overrides base class not to output `<object>` tag for SVG images.
29+
object_image_types: Dict[str, str] = {}
4730

48-
# toss off `object` tag
49-
self.body.pop()
50-
# add on `img` with attributes
51-
self.body.append(self.starttag(node, "img", **atts))
31+
def emptytag(
32+
self,
33+
node: Union[colspec, image],
34+
tagname: str,
35+
suffix: str = "\n",
36+
**attributes: Any
37+
) -> Any:
38+
"""Override this to add back the width/height attributes."""
39+
if tagname == "img":
40+
if "width" in node:
41+
attributes["width"] = node["width"]
42+
if "height" in node:
43+
attributes["height"] = node["height"]
44+
45+
return super().emptytag(
46+
node, tagname, suffix, **attributes
47+
)
5248

5349

5450
SETTINGS = {
@@ -80,7 +76,8 @@ def depart_image(self, node):
8076

8177
# Output math blocks as LaTeX that can be interpreted by MathJax for
8278
# a prettier display of Math formulas.
83-
"math_output": "MathJax",
79+
# Pass a dummy path to supress docutils warning and emit HTML.
80+
"math_output": "MathJax /dummy.js",
8481

8582
# Disable raw html as enabling it is a security risk, we do not want
8683
# people to be able to include any old HTML in the final output.
@@ -100,10 +97,18 @@ def depart_image(self, node):
10097
# Disable syntax highlighting so we don't need Pygments installed.
10198
"syntax_highlight": "none",
10299
# -------------------------------------------------
100+
101+
# Maximum width (in characters) for one-column field names.
102+
# 0 means "no limit"
103+
"field_name_limit": 0,
103104
}
104105

105106

106-
def render(raw, stream=None):
107+
def render(
108+
raw: str,
109+
stream: Optional[IO[str]] = None,
110+
**kwargs: Any
111+
) -> Optional[str]:
107112
if stream is None:
108113
# Use a io.StringIO as the warning stream to prevent warnings from
109114
# being printed to sys.stderr.
@@ -120,9 +125,12 @@ def render(raw, stream=None):
120125
except SystemMessage:
121126
rendered = None
122127
else:
123-
rendered = parts.get("fragment")
128+
rendered = parts.get("docinfo", "") + parts.get("fragment", "")
124129

125130
if rendered:
126131
return clean(rendered)
127132
else:
133+
# If the warnings stream is empty, docutils had none, so add ours.
134+
if not stream.tell():
135+
stream.write("No content rendered from RST source.")
128136
return None

0 commit comments

Comments
 (0)