1212See the *FigureMpl* documentation below.
1313
1414"""
15- from docutils import nodes
16-
17- from docutils .parsers .rst import directives
18- from docutils .parsers .rst .directives .images import Figure , Image
19-
2015import os
2116from os .path import relpath
2217from pathlib import PurePath , Path
2318import shutil
2419
20+ from docutils import nodes
21+ from docutils .parsers .rst import directives
22+ from docutils .parsers .rst .directives .images import Figure , Image
2523from sphinx .errors import ExtensionError
2624
2725import matplotlib
@@ -193,12 +191,13 @@ def visit_figmpl_html(self, node):
193191 # make uri also be relative...
194192 nm = PurePath (node ['uri' ][1 :]).name
195193 uri = f'{ imagerel } /{ rel } { nm } '
194+ img_attrs = {'src' : uri , 'alt' : node ['alt' ]}
196195
197196 # make srcset str. Need to change all the prefixes!
198197 maxsrc = uri
199- srcsetst = ''
200198 if srcset :
201199 maxmult = - 1
200+ srcsetst = ''
202201 for mult , src in srcset .items ():
203202 nm = PurePath (src [1 :]).name
204203 # ../../_images/plot_1_2_0x.png
@@ -214,44 +213,43 @@ def visit_figmpl_html(self, node):
214213 maxsrc = path
215214
216215 # trim trailing comma and space...
217- srcsetst = srcsetst [:- 2 ]
216+ img_attrs [ 'srcset' ] = srcsetst [:- 2 ]
218217
219- alt = node ['alt' ]
220218 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' ]:
230221 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 ' )
249246 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 ' )
255253
256254
257255def visit_figmpl_latex (self , node ):
0 commit comments