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

Skip to content

Commit c45ab91

Browse files
committed
Fix plot directive so it handles source files in various encodings.
svn path=/trunk/matplotlib/; revision=7973
1 parent a8aa115 commit c45ab91

1 file changed

Lines changed: 18 additions & 10 deletions

File tree

lib/matplotlib/sphinxext/plot_directive.py

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@
3838
3939
Additionally, if the `:include-source:` option is provided, the
4040
literal source will be displayed inline in the text, (as well as a
41-
link to the source in HTML).
41+
link to the source in HTML). If this source file is in a non-UTF8 or
42+
non-ASCII encoding, the encoding must be specified using the
43+
`:encoding:` option.
4244
4345
The set of file formats to generate can be specified with the
4446
`plot_formats` configuration variable.
@@ -331,14 +333,21 @@ def _plot_directive(plot_path, basedir, function_name, plot_code, caption,
331333
# Now start generating the lines of output
332334
lines = []
333335

336+
if plot_code is None:
337+
shutil.copyfile(plot_path, os.path.join(destdir, fname))
338+
334339
if options.has_key('include-source'):
335340
if plot_code is None:
336-
fd = open(plot_path, 'r')
337-
plot_code = fd.read()
338-
fd.close()
339-
lines.extend(['::', ''])
340-
lines.extend([' %s' % row.rstrip()
341-
for row in plot_code.split('\n')])
341+
lines.extend(
342+
['.. include:: %s' % os.path.join(setup.app.builder.srcdir, plot_path),
343+
' :literal:'])
344+
if options.has_key('encoding'):
345+
lines.append(' :encoding: %s' % options['encoding'])
346+
del options['encoding']
347+
else:
348+
lines.extend(['::', ''])
349+
lines.extend([' %s' % row.rstrip()
350+
for row in plot_code.split('\n')])
342351
lines.append('')
343352
del options['include-source']
344353
else:
@@ -348,8 +357,6 @@ def _plot_directive(plot_path, basedir, function_name, plot_code, caption,
348357
options = ['%s:%s: %s' % (template_content_indent, key, val)
349358
for key, val in options.items()]
350359
options = "\n".join(options)
351-
if plot_code is None:
352-
shutil.copyfile(plot_path, os.path.join(destdir, fname))
353360

354361
for i in range(num_figs):
355362
if num_figs == 1:
@@ -425,7 +432,8 @@ def setup(app):
425432
'scale': directives.nonnegative_int,
426433
'align': align,
427434
'class': directives.class_option,
428-
'include-source': directives.flag }
435+
'include-source': directives.flag,
436+
'encoding': directives.encoding }
429437

430438
app.add_directive('plot', plot_directive, True, (0, 2, 0), **options)
431439
app.add_config_value(

0 commit comments

Comments
 (0)