12
12
See the *FigureMpl* documentation below.
13
13
14
14
"""
15
- from docutils import nodes
16
-
17
- from docutils .parsers .rst import directives
18
- from docutils .parsers .rst .directives .images import Figure , Image
19
-
20
15
import os
21
16
from os .path import relpath
22
17
from pathlib import PurePath , Path
23
18
import shutil
24
19
20
+ from docutils import nodes
21
+ from docutils .parsers .rst import directives
22
+ from docutils .parsers .rst .directives .images import Figure , Image
25
23
from sphinx .errors import ExtensionError
26
24
27
25
import matplotlib
@@ -193,12 +191,13 @@ def visit_figmpl_html(self, node):
193
191
# make uri also be relative...
194
192
nm = PurePath (node ['uri' ][1 :]).name
195
193
uri = f'{ imagerel } /{ rel } { nm } '
194
+ img_attrs = {'src' : uri , 'alt' : node ['alt' ]}
196
195
197
196
# make srcset str. Need to change all the prefixes!
198
197
maxsrc = uri
199
- srcsetst = ''
200
198
if srcset :
201
199
maxmult = - 1
200
+ srcsetst = ''
202
201
for mult , src in srcset .items ():
203
202
nm = PurePath (src [1 :]).name
204
203
# ../../_images/plot_1_2_0x.png
@@ -214,44 +213,43 @@ def visit_figmpl_html(self, node):
214
213
maxsrc = path
215
214
216
215
# trim trailing comma and space...
217
- srcsetst = srcsetst [:- 2 ]
216
+ img_attrs [ 'srcset' ] = srcsetst [:- 2 ]
218
217
219
- alt = node ['alt' ]
220
218
if node ['class' ] is not None :
221
- classst = ' ' .join (node ['class' ])
222
- classst = f'class="{ classst } "'
223
-
224
- else :
225
- classst = ''
226
-
227
- stylers = ['width' , 'height' , 'scale' ]
228
- stylest = ''
229
- for style in stylers :
219
+ img_attrs ['class' ] = ' ' .join (node ['class' ])
220
+ for style in ['width' , 'height' , 'scale' ]:
230
221
if node [style ]:
231
- stylest += f'{ style } : { node [style ]} ;'
232
-
233
- figalign = node ['align' ] if node ['align' ] else 'center'
234
-
235
- # <figure class="align-default" id="id1">
236
- # <a class="reference internal image-reference" href="_images/index-1.2x.png">
237
- # <img alt="_images/index-1.2x.png" src="_images/index-1.2x.png" style="width: 53%;" />
238
- # </a>
239
- # <figcaption>
240
- # <p><span class="caption-text">Figure caption is here....</span>
241
- # <a class="headerlink" href="#id1" title="Permalink to this image">#</a></p>
242
- # </figcaption>
243
- # </figure>
244
- img_block = (f'<img src="{ uri } " style="{ stylest } " srcset="{ srcsetst } " '
245
- f'alt="{ alt } " { classst } />' )
246
- html_block = f'<figure class="align-{ figalign } ">\n '
247
- html_block += f' <a class="reference internal image-reference" href="{ maxsrc } ">\n '
248
- html_block += f' { img_block } \n </a>\n '
222
+ if 'style' not in img_attrs :
223
+ img_attrs ['style' ] = f'{ style } : { node [style ]} ;'
224
+ else :
225
+ img_attrs ['style' ] += f'{ style } : { node [style ]} ;'
226
+
227
+ # <figure class="align-default" id="id1">
228
+ # <a class="reference internal image-reference" href="_images/index-1.2x.png">
229
+ # <img alt="_images/index-1.2x.png"
230
+ # src="_images/index-1.2x.png" style="width: 53%;" />
231
+ # </a>
232
+ # <figcaption>
233
+ # <p><span class="caption-text">Figure caption is here....</span>
234
+ # <a class="headerlink" href="#id1" title="Permalink to this image">#</a></p>
235
+ # </figcaption>
236
+ # </figure>
237
+ self .body .append (
238
+ self .starttag (
239
+ node , 'figure' ,
240
+ CLASS = f'align-{ node ["align" ]} ' if node ['align' ] else 'align-center' ))
241
+ self .body .append (
242
+ self .starttag (node , 'a' , CLASS = 'reference internal image-reference' ,
243
+ href = maxsrc ) +
244
+ self .emptytag (node , 'img' , ** img_attrs ) +
245
+ '</a>\n ' )
249
246
if node ['caption' ]:
250
- html_block += ' <figcaption>\n '
251
- html_block += f' <p><span class="caption-text">{ node ["caption" ]} </span></p>\n '
252
- html_block += ' </figcaption>\n '
253
- html_block += '</figure>\n '
254
- self .body .append (html_block )
247
+ self .body .append (self .starttag (node , 'figcaption' ))
248
+ self .body .append (self .starttag (node , 'p' ))
249
+ self .body .append (self .starttag (node , 'span' , CLASS = 'caption-text' ))
250
+ self .body .append (node ['caption' ])
251
+ self .body .append ('</span></p></figcaption>\n ' )
252
+ self .body .append ('</figure>\n ' )
255
253
256
254
257
255
def visit_figmpl_latex (self , node ):
0 commit comments