11# -*- coding: UTF-8 -*-
2+ import os
3+ import re
4+ import glob
5+ import warnings
6+
7+ import matplotlib .image as image
8+
9+ multiimage = re .compile ('(.*?)(_\d\d){1,2}' )
10+
211
312# generate a thumbnail gallery of examples
4- template = """\
5- {%% extends "layout.html" %% }
6- {%% set title = "Thumbnail gallery" %% }
13+ gallery_template = """\
14+ {{% extends "layout.html" %} }
15+ {{% set title = "Thumbnail gallery" %} }
716
817
9- {%% block body %% }
18+ {{% block body %} }
1019
1120<h3>Click on any image to see full size image and source code</h3>
1221<br/>
1322
14- <li><a class="reference internal" href="#">Gallery</a><ul>
15- %s
16- </ul>
23+ <li><a class="reference internal" href="#">Gallery</a>
24+ <ul>
25+ {toc}
26+ </ul>
1727</li>
1828
19- %s
20- {%% endblock %%}
29+ {gallery}
30+
31+ {{% endblock %}}
2132"""
2233
23- import os , glob , re , sys , warnings
24- import matplotlib .image as image
34+ header_template = """\
35+ <div class="section" id="{section}">
36+ <h4>
37+ {title}<a class="headerlink" href="#{section}" title="Permalink to this headline">¶</a>
38+ </h4>"""
39+
40+ link_template = """\
41+ <a href="{link}"><img src="{thumb}" border="0" alt="{basename}"/></a>
42+ """
43+
44+ toc_template = """\
45+ <li><a class="reference internal" href="#{section}">{section}</a></li>"""
46+
47+
48+ custom_titles = {'pylab_examples' : 'pylab examples' }
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
@@ -52,22 +77,12 @@ def gen_gallery(app, doctree):
5277 thumbnails = {}
5378 rows = []
5479 toc_rows = []
55-
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-
6680 dirs = ('api' , 'pylab_examples' , 'mplot3d' , 'widgets' , 'axes_grid' )
6781
6882 for subdir in dirs :
69- rows .append (header_template % (subdir , subdir , subdir ))
70- toc_rows .append (toc_template % (subdir , subdir ))
83+ title = custom_titles .get (subdir , subdir )
84+ rows .append (header_template .format (title = title , section = subdir ))
85+ toc_rows .append (toc_template .format (section = subdir ))
7186
7287 origdir = os .path .join ('build' , rootdir , subdir )
7388 thumbdir = os .path .join (outdir , rootdir , subdir , 'thumbnails' )
@@ -99,33 +114,34 @@ def gen_gallery(app, doctree):
99114 data .append ((subdir , basename ,
100115 os .path .join (rootdir , subdir , 'thumbnails' , filename )))
101116
102-
103-
104-
105117 for (subdir , basename , thumbfile ) in data :
106118 if thumbfile is not None :
107119 link = 'examples/%s/%s.html' % (subdir , basename )
108- rows .append (link_template % (link , thumbfile , basename ))
120+ rows .append (link_template .format (link = link ,
121+ thumb = thumbfile ,
122+ basename = basename ))
109123
110124 if len (data ) == 0 :
111125 warnings .warn ("No thumbnails were found in %s" % subdir )
112126
113127 # Close out the <div> opened up at the top of this loop
114128 rows .append ("</div>" )
115129
116- content = template % ( '\n ' .join (toc_rows ),
117- '\n ' .join (rows ))
130+ content = gallery_template . format ( toc = '\n ' .join (toc_rows ),
131+ gallery = '\n ' .join (rows ))
118132
119133 # Only write out the file if the contents have actually changed.
120134 # Otherwise, this triggers a full rebuild of the docs
121135
122- gallery_path = os .path .join (app .builder .srcdir , '_templates' , 'gallery.html' )
136+ gallery_path = os .path .join (app .builder .srcdir ,
137+ '_templates' , 'gallery.html' )
123138 if os .path .exists (gallery_path ):
124139 fh = open (gallery_path , 'r' )
125140 regenerate = fh .read () != content
126141 fh .close ()
127142 else :
128143 regenerate = True
144+
129145 if regenerate :
130146 fh = open (gallery_path , 'w' )
131147 fh .write (content )
@@ -136,5 +152,6 @@ def gen_gallery(app, doctree):
136152 length = len (thumbnails )):
137153 image .thumbnail (key , thumbnails [key ], 0.3 )
138154
155+
139156def setup (app ):
140157 app .connect ('env-updated' , gen_gallery )
0 commit comments