Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1ed2b1a

Browse files
committed
Fix gen_rst.py to use the correct encoding even on Python 2.
1 parent 4256538 commit 1ed2b1a

File tree

3 files changed

+14
-20
lines changed

3 files changed

+14
-20
lines changed

doc/conf.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,19 @@
1616
# If your extensions are in another directory, add it here. If the directory
1717
# is relative to the documentation root, use os.path.abspath to make it
1818
# absolute, like shown here.
19-
sys.path.append(os.path.abspath('sphinxext'))
19+
sys.path.append(os.path.abspath('.'))
2020

2121
# General configuration
2222
# ---------------------
2323

2424
# Add any Sphinx extension module names here, as strings. They can be extensions
2525
# coming with Sphinx (named 'sphinx.ext.*') or your custom ones.
26-
extensions = ['matplotlib.sphinxext.mathmpl', 'math_symbol_table',
26+
extensions = ['matplotlib.sphinxext.mathmpl', 'sphinxext.math_symbol_table',
2727
'sphinx.ext.autodoc', 'matplotlib.sphinxext.only_directives',
2828
'sphinx.ext.doctest',
2929
'matplotlib.sphinxext.plot_directive', 'sphinx.ext.inheritance_diagram',
30-
'gen_gallery', 'gen_rst',
31-
'matplotlib.sphinxext.ipython_console_highlighting', 'github']
30+
'sphinxext.gen_gallery', 'sphinxext.gen_rst',
31+
'matplotlib.sphinxext.ipython_console_highlighting', 'sphinxext.github']
3232

3333
# Add any paths that contain templates here, relative to this directory.
3434
templates_path = ['_templates']

doc/sphinxext/__init__.py

Whitespace-only changes.

doc/sphinxext/gen_rst.py

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,7 @@ def generate_example_rst(app):
3838
continue
3939

4040
fullpath = os.path.join(root,fname)
41-
if sys.version_info[0] >= 3:
42-
contents = io.open(fullpath, encoding='utf8').read()
43-
else:
44-
contents = io.open(fullpath).read()
41+
contents = io.open(fullpath, encoding='utf8').read()
4542
# indent
4643
relpath = os.path.split(root)[-1]
4744
datad.setdefault(relpath, []).append((fullpath, fname, contents))
@@ -126,34 +123,31 @@ def generate_example_rst(app):
126123
) and
127124
not noplot_regex.search(contents))
128125
if not do_plot:
129-
fhstatic = open(outputfile, 'w')
126+
fhstatic = io.open(outputfile, 'w', encoding='utf-8')
130127
fhstatic.write(contents)
131128
fhstatic.close()
132129

133130
if not out_of_date(fullpath, outrstfile):
134131
continue
135132

136-
if sys.version_info[0] >= 3:
137-
fh = io.open(outrstfile, 'w', encoding='utf8')
138-
else:
139-
fh = io.open(outrstfile, 'w')
140-
fh.write('.. _%s-%s:\n\n'%(subdir, basename))
133+
fh = io.open(outrstfile, 'w', encoding='utf-8')
134+
fh.write(u'.. _%s-%s:\n\n' % (subdir, basename))
141135
title = '%s example code: %s'%(subdir, fname)
142136
#title = '<img src=%s> %s example code: %s'%(thumbfile, subdir, fname)
143137

144-
fh.write(title + '\n')
145-
fh.write('='*len(title) + '\n\n')
138+
fh.write(title + u'\n')
139+
fh.write(u'=' * len(title) + u'\n\n')
146140

147141
if do_plot:
148-
fh.write("\n\n.. plot:: %s\n\n::\n\n" % fullpath)
142+
fh.write(u"\n\n.. plot:: %s\n\n::\n\n" % fullpath)
149143
else:
150-
fh.write("[`source code <%s>`_]\n\n::\n\n" % fname)
144+
fh.write(u"[`source code <%s>`_]\n\n::\n\n" % fname)
151145

152146
# indent the contents
153-
contents = '\n'.join([' %s'%row.rstrip() for row in contents.split('\n')])
147+
contents = u'\n'.join([u' %s'%row.rstrip() for row in contents.split(u'\n')])
154148
fh.write(contents)
155149

156-
fh.write('\n\nKeywords: python, matplotlib, pylab, example, codex (see :ref:`how-to-search-examples`)')
150+
fh.write(u'\n\nKeywords: python, matplotlib, pylab, example, codex (see :ref:`how-to-search-examples`)')
157151
fh.close()
158152

159153
fhsubdirIndex.close()

0 commit comments

Comments
 (0)