11# -*- coding: UTF-8 -*-
2+ import os
3+ import re
4+ import glob
5+ import warnings
6+
7+ import sphinx .errors
8+
9+ import matplotlib .image as image
10+
11+
12+ exclude_example_sections = ['units' ]
13+ multiimage = re .compile ('(.*?)(_\d\d){1,2}' )
214
315# generate a thumbnail gallery of examples
4- template = """\
5- {%% extends "layout.html" %% }
6- {%% set title = "Thumbnail gallery" %% }
16+ gallery_template = """\
17+ {{% extends "layout.html" %} }
18+ {{% set title = "Thumbnail gallery" %} }
719
820
9- {%% block body %% }
21+ {{% block body %} }
1022
1123<h3>Click on any image to see full size image and source code</h3>
1224<br/>
1325
14- <li><a class="reference internal" href="#">Gallery</a><ul>
15- %s
16- </ul>
26+ <li><a class="reference internal" href="#">Gallery</a>
27+ <ul>
28+ {toc}
29+ </ul>
1730</li>
1831
19- %s
20- {%% endblock %%}
32+ {gallery}
33+
34+ {{% endblock %}}
2135"""
2236
23- import os , glob , re , sys , warnings
24- import matplotlib .image as image
37+ header_template = """\
38+ <div class="section" id="{section}">
39+ <h4>
40+ {title}<a class="headerlink" href="#{section}" title="Permalink to this headline">¶</a>
41+ </h4>"""
42+
43+ link_template = """\
44+ <a href="{link}"><img src="{thumb}" border="0" alt="{basename}"/></a>
45+ """
46+
47+ toc_template = """\
48+ <li><a class="reference internal" href="#{section}">{title}</a></li>"""
2549
26- multiimage = re .compile ('(.*?)(_\d\d){1,2}' )
2750
2851def make_thumbnail (args ):
2952 image .thumbnail (args [0 ], args [1 ], 0.3 )
3053
54+
3155def out_of_date (original , derived ):
3256 return (not os .path .exists (derived ) or
3357 os .stat (derived ).st_mtime < os .stat (original ).st_mtime )
3458
59+
3560def gen_gallery (app , doctree ):
3661 if app .builder .name != 'html' :
3762 return
3863
3964 outdir = app .builder .outdir
4065 rootdir = 'plot_directive/mpl_examples'
4166
67+ example_sections = list (app .builder .config .mpl_example_sections )
68+ for i , (subdir , title ) in enumerate (example_sections ):
69+ if subdir in exclude_example_sections :
70+ example_sections .pop (i )
71+
4272 # images we want to skip for the gallery because they are an unusual
4373 # size that doesn't layout well in a table, or because they may be
4474 # redundant with other images or uninteresting
@@ -53,21 +83,9 @@ def gen_gallery(app, doctree):
5383 rows = []
5484 toc_rows = []
5585
56- link_template = """\
57- <a href="%s"><img src="%s" border="0" alt="%s"/></a>
58- """
59-
60- header_template = """<div class="section" id="%s">\
61- <h4>%s<a class="headerlink" href="#%s" title="Permalink to this headline">¶</a></h4>"""
62-
63- toc_template = """\
64- <li><a class="reference internal" href="#%s">%s</a></li>"""
65-
66- dirs = ('api' , 'pylab_examples' , 'mplot3d' , 'widgets' , 'axes_grid' )
67-
68- for subdir in dirs :
69- rows .append (header_template % (subdir , subdir , subdir ))
70- toc_rows .append (toc_template % (subdir , subdir ))
86+ for subdir , title in example_sections :
87+ rows .append (header_template .format (title = title , section = subdir ))
88+ toc_rows .append (toc_template .format (title = title , section = subdir ))
7189
7290 origdir = os .path .join ('build' , rootdir , subdir )
7391 thumbdir = os .path .join (outdir , rootdir , subdir , 'thumbnails' )
@@ -99,33 +117,34 @@ def gen_gallery(app, doctree):
99117 data .append ((subdir , basename ,
100118 os .path .join (rootdir , subdir , 'thumbnails' , filename )))
101119
102-
103-
104-
105120 for (subdir , basename , thumbfile ) in data :
106121 if thumbfile is not None :
107122 link = 'examples/%s/%s.html' % (subdir , basename )
108- rows .append (link_template % (link , thumbfile , basename ))
123+ rows .append (link_template .format (link = link ,
124+ thumb = thumbfile ,
125+ basename = basename ))
109126
110127 if len (data ) == 0 :
111128 warnings .warn ("No thumbnails were found in %s" % subdir )
112129
113130 # Close out the <div> opened up at the top of this loop
114131 rows .append ("</div>" )
115132
116- content = template % ( '\n ' .join (toc_rows ),
117- '\n ' .join (rows ))
133+ content = gallery_template . format ( toc = '\n ' .join (toc_rows ),
134+ gallery = '\n ' .join (rows ))
118135
119136 # Only write out the file if the contents have actually changed.
120137 # Otherwise, this triggers a full rebuild of the docs
121138
122- gallery_path = os .path .join (app .builder .srcdir , '_templates' , 'gallery.html' )
139+ gallery_path = os .path .join (app .builder .srcdir ,
140+ '_templates' , 'gallery.html' )
123141 if os .path .exists (gallery_path ):
124142 fh = open (gallery_path , 'r' )
125143 regenerate = fh .read () != content
126144 fh .close ()
127145 else :
128146 regenerate = True
147+
129148 if regenerate :
130149 fh = open (gallery_path , 'w' )
131150 fh .write (content )
@@ -136,5 +155,11 @@ def gen_gallery(app, doctree):
136155 length = len (thumbnails )):
137156 image .thumbnail (key , thumbnails [key ], 0.3 )
138157
158+
139159def setup (app ):
140160 app .connect ('env-updated' , gen_gallery )
161+
162+ try : # multiple plugins may use mpl_example_sections
163+ app .add_config_value ('mpl_example_sections' , [], True )
164+ except sphinx .errors .ExtensionError :
165+ pass # mpl_example_sections already defined
0 commit comments