@@ -31,8 +31,9 @@ class figmplnode(nodes.General, nodes.Element):
31
31
32
32
class FigureMpl (Figure ):
33
33
"""
34
- Implements a directive to allow an optional hidpi image. Meant to be
35
- used with the *plot_srcset* configuration option in conf.py,
34
+ Implements a directive to allow an optional hidpi image.
35
+
36
+ Meant to be used with the *plot_srcset* configuration option in conf.py,
36
37
and gets set in the TEMPLATE of plot_directive.py
37
38
38
39
e.g.::
@@ -47,7 +48,7 @@ class FigureMpl(Figure):
47
48
48
49
<img src="sphx_glr_bar_001_hidpi.png"
49
50
srcset="_images/some_plot-1.png,
50
- _images/some_plots-1.2x.png 2.00 x ",
51
+ _images/some_plots-1.2x.png 2.00x ",
51
52
alt="bar"
52
53
class="plot_directive" />
53
54
@@ -64,7 +65,7 @@ class FigureMpl(Figure):
64
65
65
66
<img src="../_images/nestedpage-index-1.png"
66
67
srcset="../_images/nestedpage-index-1.png,
67
- ../_images/_images/nestedpage-index-1.2x.png 2x ",
68
+ ../_images/_images/nestedpage-index-1.2x.png 2.00x ",
68
69
alt="bar"
69
70
class="sphx-glr-single-img" />
70
71
@@ -101,7 +102,7 @@ def run(self):
101
102
102
103
# we would like uri to be the highest dpi version so that
103
104
# latex etc will use that. But for now, lets just make
104
- # imagenm
105
+ # imagenm... maybe pdf one day?
105
106
106
107
image_node ['uri' ] = imagenm
107
108
image_node ['srcset' ] = self .options .get ('srcset' , None )
@@ -186,23 +187,22 @@ def visit_figmpl_html(self, node):
186
187
# ../../_images/ for dirhtml and ../_images/ for html
187
188
imagerel = relpath (imagedir , os .path .dirname (dest ))
188
189
if self .builder .name == "dirhtml" :
189
- imagerel = '..' + imagerel + ''
190
- else : # html
191
- imagerel = imagerel + ''
190
+ imagerel = f'..{ imagerel } '
192
191
193
192
# make uri also be relative...
194
193
nm = PurePosixPath (node ['uri' ][1 :]).name
195
- uri = imagerel + '/' + rel + nm
194
+ uri = f' { imagerel } / { rel } { nm } '
196
195
197
196
# make srcset str. Need to change all the prefixes!
197
+ maxsrc = uri
198
+ srcsetst = ''
198
199
if srcset :
199
- srcsetst = ''
200
200
maxmult = - 1
201
201
for mult in srcset :
202
202
nm = PurePosixPath (srcset [mult ][1 :]).name
203
203
# ../../_images/plot_1_2_0x.png
204
- path = imagerel + '/' + rel + nm
205
- srcsetst += f' { path } '
204
+ path = f' { imagerel } / { rel } { nm } '
205
+ srcsetst += path
206
206
if mult == 0 :
207
207
srcsetst += ', '
208
208
else :
@@ -214,20 +214,23 @@ def visit_figmpl_html(self, node):
214
214
215
215
# trim trailing comma and space...
216
216
srcsetst = srcsetst [:- 2 ]
217
- else :
218
- srcsetst = ''
219
- maxsrc = uri
220
217
221
218
alt = node ['alt' ]
222
219
if node ['class' ] is not None :
223
- classst = ''
224
- for cl in node ['class' ]:
225
- classst += cl + ' '
220
+ classst = ' ' .join (node ['class' ])
226
221
classst = f'class="{ classst } "'
227
222
228
223
else :
229
224
classst = ''
230
225
226
+ stylers = ['width' , 'height' , 'scale' ]
227
+ stylest = ''
228
+ for style in stylers :
229
+ if node [style ]:
230
+ stylest += f'{ style } : { node [style ]} ;'
231
+
232
+ figalign = node ['align' ] if node ['align' ] else 'center'
233
+
231
234
# <figure class="align-default" id="id1">
232
235
# <a class="reference internal image-reference" href="_images/index-1.2x.png">
233
236
# <img alt="_images/index-1.2x.png" src="_images/index-1.2x.png" style="width: 53%;" />
@@ -237,15 +240,6 @@ def visit_figmpl_html(self, node):
237
240
# <a class="headerlink" href="#id1" title="Permalink to this image">#</a></p>
238
241
# </figcaption>
239
242
# </figure>
240
-
241
- stylers = ['width' , 'height' , 'scale' ]
242
- stylest = ''
243
- for style in stylers :
244
- if node [style ]:
245
- stylest += f'{ style } : { node [style ]} ;'
246
-
247
- figalign = node ['align' ] if node ['align' ] else 'center'
248
-
249
243
img_block = (f'<img src="{ uri } " style="{ stylest } " srcset="{ srcsetst } " ' +
250
244
f'alt="{ alt } " { classst } />' )
251
245
html_block = f'<figure class="align-{ figalign } ">\n '
@@ -265,9 +259,9 @@ def visit_figmpl_latex(self, node):
265
259
imagedir , srcset = _copy_images_figmpl (self , node )
266
260
maxmult = - 1
267
261
# choose the highest res version for latex:
268
- for key in srcset . keys () :
269
- maxmult = max (maxmult , key )
270
- node ['uri' ] = str ( PurePosixPath (srcset [maxmult ]).name )
262
+ # choose the highest res version for latex :
263
+ maxmult = max (srcset , default = - 1 )
264
+ node ['uri' ] = PurePosixPath (srcset [maxmult ]).name
271
265
272
266
self .visit_figure (node )
273
267
0 commit comments