Skip to content

Commit bfc7dca

Browse files
committed
FIX: fix template for multi image [ci doc]
1 parent aeada98 commit bfc7dca

File tree

2 files changed

+54
-32
lines changed

2 files changed

+54
-32
lines changed

lib/matplotlib/sphinxext/figmpl_directive.py

Lines changed: 51 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
from sphinx.errors import ExtensionError
2020

21+
import matplotlib
22+
2123

2224
class figmplnode(nodes.General, nodes.Element):
2325
pass
@@ -106,7 +108,10 @@ def _copy_images_figmpl(self, node):
106108

107109
# these will be the temporary place the plot-directive put the images eg:
108110
# ../../../build/html/plot_directive/users/explain/artists/index-1.png
109-
srcset = _parse_srcsetNodes(node['srcset'])
111+
if node['srcset']:
112+
srcset = _parse_srcsetNodes(node['srcset'])
113+
else:
114+
srcset = None
110115

111116
# the rst file's location: eg /Users/username/matplotlib/doc/users/explain/artists
112117
docsource = PurePosixPath(self.document['source']).parent
@@ -123,19 +128,24 @@ def _copy_images_figmpl(self, node):
123128
Path(imagedir).mkdir(parents=True, exist_ok=True)
124129

125130
# copy all the sources to the imagedir:
126-
for mult in srcset:
127-
# the entries in srcset are relative to docsource's directory
128-
abspath = PurePosixPath(docsource, srcset[mult])
131+
if srcset:
132+
for mult in srcset:
133+
# the entries in srcset are relative to docsource's directory
134+
abspath = PurePosixPath(docsource, srcset[mult])
135+
shutil.copyfile(abspath, imagedir / abspath.name)
136+
else:
137+
abspath = PurePosixPath(docsource, node['uri'])
129138
shutil.copyfile(abspath, imagedir / abspath.name)
130139

140+
131141
return imagedir, srcset
132142

133143

134144
def visit_figmpl_html(self, node):
135145

136-
if node['srcset'] is None:
137-
self.visit_image(node)
138-
return
146+
#if node['srcset'] is None:
147+
# print('NODE: NONE')
148+
# return self.visit_figure(node)
139149

140150
imagedir, srcset = _copy_images_figmpl(self, node)
141151

@@ -158,34 +168,42 @@ def visit_figmpl_html(self, node):
158168
else: # html
159169
imagerel = imagerel + ''
160170

161-
# make srcset str. Need to change all the prefixes!
162-
srcsetst = ''
163-
maxmult = -1
164-
for mult in srcset:
165-
nm = PurePosixPath(srcset[mult][1:]).name
166-
# ../../_images/plot_1_2_0x.png
167-
path = imagerel + '/' + nm
168-
srcsetst += f'{path}'
169-
if mult == 0:
170-
srcsetst += ', '
171-
else:
172-
srcsetst += f' {mult:1.2f}x, '
173-
174-
if mult > maxmult:
175-
maxmult = mult
176-
maxsrc = path
177-
178-
# trim trailing comma and space...
179-
srcsetst = srcsetst[:-2]
180-
181171
# make uri also be relative...
182172
nm = PurePosixPath(node['uri'][1:]).name
183173
uri = imagerel + '/' + nm
184174

175+
176+
# make srcset str. Need to change all the prefixes!
177+
if srcset:
178+
srcsetst = ''
179+
maxmult = -1
180+
for mult in srcset:
181+
nm = PurePosixPath(srcset[mult][1:]).name
182+
# ../../_images/plot_1_2_0x.png
183+
path = imagerel + '/' + nm
184+
srcsetst += f'{path}'
185+
if mult == 0:
186+
srcsetst += ', '
187+
else:
188+
srcsetst += f' {mult:1.2f}x, '
189+
190+
if mult > maxmult:
191+
maxmult = mult
192+
maxsrc = path
193+
194+
# trim trailing comma and space...
195+
srcsetst = srcsetst[:-2]
196+
else:
197+
srcsetst = ''
198+
maxsrc = uri
199+
185200
alt = node['alt']
186201
if node['class'] is not None:
187-
classst = node['class'][0]
202+
classst = ''
203+
for cl in node['class']:
204+
classst += cl + ' '
188205
classst = f'class="{classst}"'
206+
189207
else:
190208
classst = ''
191209

@@ -230,7 +248,7 @@ def visit_figmpl_latex(self, node):
230248
maxmult = max(maxmult, key)
231249
node['uri'] = str(PurePosixPath(srcset[maxmult]).name)
232250

233-
self.visit_image(node)
251+
self.visit_figure(node)
234252

235253

236254
def depart_figmpl_html(self, node):
@@ -248,5 +266,9 @@ def figurempl_addnode(app):
248266

249267

250268
def setup(app):
269+
print('SETUPPPPP')
251270
app.add_directive("figure-mpl", FigureMpl)
252271
figurempl_addnode(app)
272+
metadata = {'parallel_read_safe': True, 'parallel_write_safe': True,
273+
'version': matplotlib.__version__}
274+
return metadata

lib/matplotlib/sphinxext/plot_directive.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -531,7 +531,9 @@ def get_plot_formats(config):
531531

532532

533533
def _parse_srcset(st):
534-
""" parse srcset"""
534+
"""
535+
Parse srcset for multiples...
536+
"""
535537
entries = st.split(',')
536538

537539
srcset = {}
@@ -860,8 +862,6 @@ def run(arguments, content, options, state_machine, state, lineno):
860862
source_code=source_code,
861863
html_show_formats=config.plot_html_show_formats and len(images),
862864
caption=caption)
863-
#print('RESULT:', result)
864-
#print('######################')
865865
total_lines.extend(result.split("\n"))
866866
total_lines.extend("\n")
867867

0 commit comments

Comments
 (0)