From 1a82ebedac26e61164defa03a580c753f810f042 Mon Sep 17 00:00:00 2001 From: Ben Root Date: Thu, 23 Feb 2012 12:27:15 -0600 Subject: [PATCH] Initial rework of gen_gallery.py * Creates a toc at the top of the gallery page * entries are created by a list of sub-directories * examples from each sub-dir is sectioned --- doc/sphinxext/gen_gallery.py | 55 +++++++++++++++++++++++++++--------- 1 file changed, 42 insertions(+), 13 deletions(-) diff --git a/doc/sphinxext/gen_gallery.py b/doc/sphinxext/gen_gallery.py index aadc2c0e9b8c..3b1323345934 100644 --- a/doc/sphinxext/gen_gallery.py +++ b/doc/sphinxext/gen_gallery.py @@ -1,3 +1,5 @@ +# -*- coding: UTF-8 -*- + # generate a thumbnail gallery of examples template = """\ {%% extends "layout.html" %%} @@ -9,6 +11,11 @@

Click on any image to see full size image and source code


+
  • Gallery +
  • + %s {%% endblock %%} """ @@ -42,15 +49,33 @@ def gen_gallery(app, doctree): 'matplotlib_icon', ]) - data = [] thumbnails = {} + rows = [] + toc_rows = [] + + link_template = """\ + %s + """ + + header_template = """
    \ +

    %sΒΆ

    """ + + toc_template = """\ +
  • %s
  • """ + + 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 @@ -77,22 +102,26 @@ def gen_gallery(app, doctree): data.append((subdir, basename, os.path.join(rootdir, subdir, 'thumbnails', filename))) - link_template = """\ - %s - """ - 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
    opened up at the top of this loop + rows.append("
    ") + + content = template % ('\n'.join(toc_rows), + '\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')