From 1ed2b1a631314e548253d2d874f2fcaf44daff4c Mon Sep 17 00:00:00 2001 From: Michael Droettboom Date: Fri, 12 Apr 2013 09:37:35 -0400 Subject: [PATCH] Fix gen_rst.py to use the correct encoding even on Python 2. --- doc/conf.py | 8 ++++---- doc/sphinxext/__init__.py | 0 doc/sphinxext/gen_rst.py | 26 ++++++++++---------------- 3 files changed, 14 insertions(+), 20 deletions(-) create mode 100644 doc/sphinxext/__init__.py diff --git a/doc/conf.py b/doc/conf.py index d2001853ad1a..2127500966fb 100644 --- a/doc/conf.py +++ b/doc/conf.py @@ -16,19 +16,19 @@ # If your extensions are in another directory, add it here. If the directory # is relative to the documentation root, use os.path.abspath to make it # absolute, like shown here. -sys.path.append(os.path.abspath('sphinxext')) +sys.path.append(os.path.abspath('.')) # General configuration # --------------------- # Add any Sphinx extension module names here, as strings. They can be extensions # coming with Sphinx (named 'sphinx.ext.*') or your custom ones. -extensions = ['matplotlib.sphinxext.mathmpl', 'math_symbol_table', +extensions = ['matplotlib.sphinxext.mathmpl', 'sphinxext.math_symbol_table', 'sphinx.ext.autodoc', 'matplotlib.sphinxext.only_directives', 'sphinx.ext.doctest', 'matplotlib.sphinxext.plot_directive', 'sphinx.ext.inheritance_diagram', - 'gen_gallery', 'gen_rst', - 'matplotlib.sphinxext.ipython_console_highlighting', 'github'] + 'sphinxext.gen_gallery', 'sphinxext.gen_rst', + 'matplotlib.sphinxext.ipython_console_highlighting', 'sphinxext.github'] # Add any paths that contain templates here, relative to this directory. templates_path = ['_templates'] diff --git a/doc/sphinxext/__init__.py b/doc/sphinxext/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/doc/sphinxext/gen_rst.py b/doc/sphinxext/gen_rst.py index a251917fe490..be78956295ff 100644 --- a/doc/sphinxext/gen_rst.py +++ b/doc/sphinxext/gen_rst.py @@ -38,10 +38,7 @@ def generate_example_rst(app): continue fullpath = os.path.join(root,fname) - if sys.version_info[0] >= 3: - contents = io.open(fullpath, encoding='utf8').read() - else: - contents = io.open(fullpath).read() + contents = io.open(fullpath, encoding='utf8').read() # indent relpath = os.path.split(root)[-1] datad.setdefault(relpath, []).append((fullpath, fname, contents)) @@ -126,34 +123,31 @@ def generate_example_rst(app): ) and not noplot_regex.search(contents)) if not do_plot: - fhstatic = open(outputfile, 'w') + fhstatic = io.open(outputfile, 'w', encoding='utf-8') fhstatic.write(contents) fhstatic.close() if not out_of_date(fullpath, outrstfile): continue - if sys.version_info[0] >= 3: - fh = io.open(outrstfile, 'w', encoding='utf8') - else: - fh = io.open(outrstfile, 'w') - fh.write('.. _%s-%s:\n\n'%(subdir, basename)) + fh = io.open(outrstfile, 'w', encoding='utf-8') + fh.write(u'.. _%s-%s:\n\n' % (subdir, basename)) 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') + fh.write(title + u'\n') + fh.write(u'=' * len(title) + u'\n\n') if do_plot: - fh.write("\n\n.. plot:: %s\n\n::\n\n" % fullpath) + fh.write(u"\n\n.. plot:: %s\n\n::\n\n" % fullpath) else: - fh.write("[`source code <%s>`_]\n\n::\n\n" % fname) + fh.write(u"[`source code <%s>`_]\n\n::\n\n" % fname) # indent the contents - contents = '\n'.join([' %s'%row.rstrip() for row in contents.split('\n')]) + contents = u'\n'.join([u' %s'%row.rstrip() for row in contents.split(u'\n')]) fh.write(contents) - fh.write('\n\nKeywords: python, matplotlib, pylab, example, codex (see :ref:`how-to-search-examples`)') + fh.write(u'\n\nKeywords: python, matplotlib, pylab, example, codex (see :ref:`how-to-search-examples`)') fh.close() fhsubdirIndex.close()