From fc30d9246e69c608f77c734acc0d244c58a24a9a Mon Sep 17 00:00:00 2001 From: Michael Droettboom Date: Tue, 18 Oct 2011 15:39:41 -0400 Subject: [PATCH] Fix inclusion of source code in the documentation build. Closes #520. --- doc/sphinxext/gen_rst.py | 24 +++++++++++----------- lib/matplotlib/sphinxext/plot_directive.py | 11 ++++++---- 2 files changed, 19 insertions(+), 16 deletions(-) diff --git a/doc/sphinxext/gen_rst.py b/doc/sphinxext/gen_rst.py index 2502076c9f93..1bf72bba8ed6 100644 --- a/doc/sphinxext/gen_rst.py +++ b/doc/sphinxext/gen_rst.py @@ -113,6 +113,18 @@ def generate_example_rst(app): fhsubdirIndex.write(' %s <%s>\n'%(os.path.basename(basename),rstfile)) + do_plot = (subdir in ('api', + 'pylab_examples', + 'units', + 'mplot3d', + 'axes_grid', + ) and + not noplot_regex.search(contents)) + if not do_plot: + fhstatic = file(outputfile, 'w') + fhstatic.write(contents) + fhstatic.close() + if not out_of_date(fullpath, outrstfile): continue @@ -121,25 +133,13 @@ def generate_example_rst(app): title = '%s example code: %s'%(subdir, fname) #title = ' %s example code: %s'%(thumbfile, subdir, fname) - fh.write(title + '\n') fh.write('='*len(title) + '\n\n') - do_plot = (subdir in ('api', - 'pylab_examples', - 'units', - 'mplot3d', - 'axes_grid', - ) and - not noplot_regex.search(contents)) - if do_plot: fh.write("\n\n.. plot:: %s\n\n::\n\n" % fullpath) else: fh.write("[`source code <%s>`_]\n\n::\n\n" % fname) - fhstatic = file(outputfile, 'w') - fhstatic.write(contents) - fhstatic.close() # indent the contents contents = '\n'.join([' %s'%row.rstrip() for row in contents.split('\n')]) diff --git a/lib/matplotlib/sphinxext/plot_directive.py b/lib/matplotlib/sphinxext/plot_directive.py index f9ca13630114..5d7b2c524b72 100644 --- a/lib/matplotlib/sphinxext/plot_directive.py +++ b/lib/matplotlib/sphinxext/plot_directive.py @@ -764,10 +764,13 @@ def run(arguments, content, options, state_machine, state, lineno): shutil.copyfile(fn, destimg) # copy script (if necessary) + target_name = os.path.join(dest_dir, output_base + source_ext) + f = open(target_name, 'w') if source_file_name == rst_file: - target_name = os.path.join(dest_dir, output_base + source_ext) - f = open(target_name, 'w') - f.write(unescape_doctest(code)) - f.close() + code_escaped = unescape_doctest(code) + else: + code_escaped = code + f.write(code_escaped) + f.close() return errors