12
12
# See the License for the specific language governing permissions and
13
13
# limitations under the License.
14
14
15
- # Copied from https://github.com/pypa/readme_renderer
16
- # Commit 5b455a9c5bafc1732dafad9619bcbfa8e15432c9
17
-
18
- from __future__ import absolute_import , division , print_function
19
-
20
15
import io
21
- import os . path
16
+ from typing import Any , Dict , IO , Optional , Union
22
17
23
18
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
25
21
from docutils .utils import SystemMessage
26
22
27
23
from .clean import clean
28
24
29
25
30
- class ReadMeHTMLTranslator (HTMLTranslator ):
26
+ class ReadMeHTMLTranslator (HTMLTranslator ): # type: ignore[misc] # docutils is incomplete, returns `Any` python/typeshed#7256 # noqa E501
31
27
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 ] = {}
47
30
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
+ )
52
48
53
49
54
50
SETTINGS = {
@@ -80,7 +76,8 @@ def depart_image(self, node):
80
76
81
77
# Output math blocks as LaTeX that can be interpreted by MathJax for
82
78
# 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" ,
84
81
85
82
# Disable raw html as enabling it is a security risk, we do not want
86
83
# people to be able to include any old HTML in the final output.
@@ -100,10 +97,18 @@ def depart_image(self, node):
100
97
# Disable syntax highlighting so we don't need Pygments installed.
101
98
"syntax_highlight" : "none" ,
102
99
# -------------------------------------------------
100
+
101
+ # Maximum width (in characters) for one-column field names.
102
+ # 0 means "no limit"
103
+ "field_name_limit" : 0 ,
103
104
}
104
105
105
106
106
- def render (raw , stream = None ):
107
+ def render (
108
+ raw : str ,
109
+ stream : Optional [IO [str ]] = None ,
110
+ ** kwargs : Any
111
+ ) -> Optional [str ]:
107
112
if stream is None :
108
113
# Use a io.StringIO as the warning stream to prevent warnings from
109
114
# being printed to sys.stderr.
@@ -120,9 +125,12 @@ def render(raw, stream=None):
120
125
except SystemMessage :
121
126
rendered = None
122
127
else :
123
- rendered = parts .get ("fragment" )
128
+ rendered = parts .get ("docinfo" , "" ) + parts . get ( " fragment" , " " )
124
129
125
130
if rendered :
126
131
return clean (rendered )
127
132
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." )
128
136
return None
0 commit comments