1
1
# -*- 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
+
2
11
3
12
# 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" %} }
7
16
8
17
9
- {%% block body %% }
18
+ {{% block body %} }
10
19
11
20
<h3>Click on any image to see full size image and source code</h3>
12
21
<br/>
13
22
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>
17
27
</li>
18
28
19
- %s
20
- {%% endblock %%}
29
+ {gallery}
30
+
31
+ {{% endblock %}}
21
32
"""
22
33
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' }
25
49
26
- multiimage = re .compile ('(.*?)(_\d\d){1,2}' )
27
50
28
51
def make_thumbnail (args ):
29
52
image .thumbnail (args [0 ], args [1 ], 0.3 )
30
53
54
+
31
55
def out_of_date (original , derived ):
32
56
return (not os .path .exists (derived ) or
33
57
os .stat (derived ).st_mtime < os .stat (original ).st_mtime )
34
58
59
+
35
60
def gen_gallery (app , doctree ):
36
61
if app .builder .name != 'html' :
37
62
return
@@ -52,22 +77,12 @@ def gen_gallery(app, doctree):
52
77
thumbnails = {}
53
78
rows = []
54
79
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
80
dirs = ('api' , 'pylab_examples' , 'mplot3d' , 'widgets' , 'axes_grid' )
67
81
68
82
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 ))
71
86
72
87
origdir = os .path .join ('build' , rootdir , subdir )
73
88
thumbdir = os .path .join (outdir , rootdir , subdir , 'thumbnails' )
@@ -99,33 +114,34 @@ def gen_gallery(app, doctree):
99
114
data .append ((subdir , basename ,
100
115
os .path .join (rootdir , subdir , 'thumbnails' , filename )))
101
116
102
-
103
-
104
-
105
117
for (subdir , basename , thumbfile ) in data :
106
118
if thumbfile is not None :
107
119
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 ))
109
123
110
124
if len (data ) == 0 :
111
125
warnings .warn ("No thumbnails were found in %s" % subdir )
112
126
113
127
# Close out the <div> opened up at the top of this loop
114
128
rows .append ("</div>" )
115
129
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 ))
118
132
119
133
# Only write out the file if the contents have actually changed.
120
134
# Otherwise, this triggers a full rebuild of the docs
121
135
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' )
123
138
if os .path .exists (gallery_path ):
124
139
fh = file (gallery_path , 'r' )
125
140
regenerate = fh .read () != content
126
141
fh .close ()
127
142
else :
128
143
regenerate = True
144
+
129
145
if regenerate :
130
146
fh = file (gallery_path , 'w' )
131
147
fh .write (content )
@@ -136,5 +152,6 @@ def gen_gallery(app, doctree):
136
152
length = len (thumbnails )):
137
153
image .thumbnail (key , thumbnails [key ], 0.3 )
138
154
155
+
139
156
def setup (app ):
140
157
app .connect ('env-updated' , gen_gallery )
0 commit comments