1+ # -*- coding: UTF-8 -*-
2+
13# generate a thumbnail gallery of examples
24template = """\
35 {%% extends "layout.html" %%}
911<h3>Click on any image to see full size image and source code</h3>
1012<br/>
1113
14+ <li><a class="reference internal" href="#">Gallery</a><ul>
15+ %s
16+ </ul>
17+ </li>
18+
1219%s
1320{%% endblock %%}
1421"""
@@ -42,15 +49,33 @@ def gen_gallery(app, doctree):
4249 'matplotlib_icon' ,
4350 ])
4451
45- data = []
4652 thumbnails = {}
53+ rows = []
54+ 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+
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 ))
4771
48- for subdir in ('api' , 'pylab_examples' , 'mplot3d' , 'widgets' , 'axes_grid' ):
4972 origdir = os .path .join ('build' , rootdir , subdir )
5073 thumbdir = os .path .join (outdir , rootdir , subdir , 'thumbnails' )
5174 if not os .path .exists (thumbdir ):
5275 os .makedirs (thumbdir )
5376
77+ data = []
78+
5479 for filename in sorted (glob .glob (os .path .join (origdir , '*.png' ))):
5580 if filename .endswith ("hires.png" ):
5681 continue
@@ -74,22 +99,26 @@ def gen_gallery(app, doctree):
7499 data .append ((subdir , basename ,
75100 os .path .join (rootdir , subdir , 'thumbnails' , filename )))
76101
77- link_template = """\
78- <a href="%s"><img src="%s" border="0" alt="%s"/></a>
79- """
80102
81- if len (data ) == 0 :
82- warnings .warn ("No thumbnails were found" )
83103
84- rows = []
85- for (subdir , basename , thumbfile ) in data :
86- if thumbfile is not None :
87- link = 'examples/%s/%s.html' % (subdir , basename )
88- rows .append (link_template % (link , thumbfile , basename ))
104+
105+ for (subdir , basename , thumbfile ) in data :
106+ if thumbfile is not None :
107+ link = 'examples/%s/%s.html' % (subdir , basename )
108+ rows .append (link_template % (link , thumbfile , basename ))
109+
110+ if len (data ) == 0 :
111+ warnings .warn ("No thumbnails were found in %s" % subdir )
112+
113+ # Close out the <div> opened up at the top of this loop
114+ rows .append ("</div>" )
115+
116+ content = template % ('\n ' .join (toc_rows ),
117+ '\n ' .join (rows ))
89118
90119 # Only write out the file if the contents have actually changed.
91120 # Otherwise, this triggers a full rebuild of the docs
92- content = template % ' \n ' . join ( rows )
121+
93122 gallery_path = os .path .join (app .builder .srcdir , '_templates' , 'gallery.html' )
94123 if os .path .exists (gallery_path ):
95124 fh = file (gallery_path , 'r' )
0 commit comments