Skip to content

Initial rework of gen_gallery.py #714

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 5, 2012
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 42 additions & 13 deletions doc/sphinxext/gen_gallery.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# -*- coding: UTF-8 -*-

# generate a thumbnail gallery of examples
template = """\
{%% extends "layout.html" %%}
Expand All @@ -9,6 +11,11 @@
<h3>Click on any image to see full size image and source code</h3>
<br/>

<li><a class="reference internal" href="#">Gallery</a><ul>
%s
</ul>
</li>

%s
{%% endblock %%}
"""
Expand Down Expand Up @@ -42,15 +49,33 @@ def gen_gallery(app, doctree):
'matplotlib_icon',
])

data = []
thumbnails = {}
rows = []
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe rename to body_rows or gallery_rows or data_rows (but this may be confusing b/c it has no relation to the existing data variable)?

toc_rows = []

link_template = """\
<a href="%s"><img src="%s" border="0" alt="%s"/></a>
"""

header_template = """<div class="section" id="%s">\
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe the div class could be "gallery" so that someone can change the style for the gallery later. The image gallery is different than most pages (mostly images instead of text) and may require different styling (for example, underline the heading, extra spacing between sections, etc.)

<h4>%s<a class="headerlink" href="#%s" title="Permalink to this headline">¶</a></h4>"""

toc_template = """\
<li><a class="reference internal" href="#%s">%s</a></li>"""

dirs = ('api', 'pylab_examples', 'mplot3d', 'widgets', 'axes_grid' )

for subdir in dirs :
rows.append(header_template % (subdir, subdir, subdir))
toc_rows.append(toc_template % (subdir, subdir))

for subdir in ('api', 'pylab_examples', 'mplot3d', 'widgets', 'axes_grid' ):
origdir = os.path.join('build', rootdir, subdir)
thumbdir = os.path.join(outdir, rootdir, subdir, 'thumbnails')
if not os.path.exists(thumbdir):
os.makedirs(thumbdir)

data = []

for filename in sorted(glob.glob(os.path.join(origdir, '*.png'))):
if filename.endswith("hires.png"):
continue
Expand All @@ -77,22 +102,26 @@ def gen_gallery(app, doctree):
data.append((subdir, basename,
os.path.join(rootdir, subdir, 'thumbnails', filename)))

link_template = """\
<a href="%s"><img src="%s" border="0" alt="%s"/></a>
"""

if len(data) == 0:
warnings.warn("No thumbnails were found")

rows = []
for (subdir, basename, thumbfile) in data:
if thumbfile is not None:
link = 'examples/%s/%s.html'%(subdir, basename)
rows.append(link_template%(link, thumbfile, basename))

for (subdir, basename, thumbfile) in data:
if thumbfile is not None:
link = 'examples/%s/%s.html'%(subdir, basename)
rows.append(link_template%(link, thumbfile, basename))

if len(data) == 0:
warnings.warn("No thumbnails were found in %s" % subdir)

# Close out the <div> opened up at the top of this loop
rows.append("</div>")

content = template % ('\n'.join(toc_rows),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe rename to gallery_template to contrast it with the new templates you added?

'\n'.join(rows))

# Only write out the file if the contents have actually changed.
# Otherwise, this triggers a full rebuild of the docs
content = template%'\n'.join(rows)

gallery_path = os.path.join(app.builder.srcdir, '_templates', 'gallery.html')
if os.path.exists(gallery_path):
fh = file(gallery_path, 'r')
Expand Down