Skip to content

Commit e2e17ed

Browse files
committed
[MAINT] update to sphinx-gallery v0.1.2
1 parent 96f6162 commit e2e17ed

File tree

3 files changed

+41
-13
lines changed

3 files changed

+41
-13
lines changed

doc/sphinxext/sphinx_gallery/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
66
"""
77
import os
8-
__version__ = '0.1.1'
8+
__version__ = '0.1.2'
99

1010

1111
def glr_path_static():

doc/sphinxext/sphinx_gallery/gen_gallery.py

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121

2222
def clean_gallery_out(build_dir):
23+
"""Deletes images under the sphx_glr namespace in the build directory"""
2324
# Sphinx hack: sphinx copies generated images to the build directory
2425
# each time the docs are made. If the desired image name already
2526
# exists, it appends a digit to prevent overwrites. The problem is,
@@ -30,10 +31,11 @@ def clean_gallery_out(build_dir):
3031
# was no response: http://osdir.com/ml/sphinx-dev/2011-02/msg00123.html
3132
#
3233
# The following is a hack that prevents this behavior by clearing the
33-
# image build directory each time the docs are built. If sphinx
34-
# changes their layout between versions, this will not work (though
35-
# it should probably not cause a crash). Tested successfully
36-
# on Sphinx 1.0.7
34+
# image build directory from gallery images each time the docs are built.
35+
# If sphinx changes their layout between versions, this will not
36+
# work (though it should probably not cause a crash).
37+
# Tested successfully on Sphinx 1.0.7
38+
3739
build_image_dir = os.path.join(build_dir, '_images')
3840
if os.path.exists(build_image_dir):
3941
filelist = os.listdir(build_image_dir)
@@ -101,11 +103,26 @@ def generate_gallery_rst(app):
101103
fhindex.flush()
102104

103105

106+
def touch_empty_backreferences(app, what, name, obj, options, lines):
107+
"""Generate empty back-reference example files
108+
109+
This avoids inclusion errors/warnings if there are no gallery
110+
examples for a class / module that is being parsed by autodoc"""
111+
112+
examples_path = os.path.join(app.srcdir,
113+
app.config.sphinx_gallery_conf["mod_example_dir"],
114+
"%s.examples" % name)
115+
116+
if not os.path.exists(examples_path):
117+
# touch file
118+
open(examples_path, 'w').close()
119+
120+
104121
gallery_conf = {
105122
'filename_pattern': re.escape(os.sep) + 'plot',
106123
'examples_dirs': '../examples',
107124
'gallery_dirs': 'auto_examples',
108-
'mod_example_dir': 'modules/generated',
125+
'mod_example_dir': os.path.join('modules', 'generated'),
109126
'doc_module': (),
110127
'reference_url': {},
111128
}
@@ -118,6 +135,9 @@ def setup(app):
118135
app.add_config_value('sphinx_gallery_conf', gallery_conf, 'html')
119136
app.add_stylesheet('gallery.css')
120137

138+
if 'sphinx.ext.autodoc' in app._extensions:
139+
app.connect('autodoc-process-docstring', touch_empty_backreferences)
140+
121141
app.connect('builder-inited', generate_gallery_rst)
122142

123143
app.connect('build-finished', embed_code_links)

doc/sphinxext/sphinx_gallery/gen_rst.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -553,13 +553,18 @@ def generate_file_rst(fname, target_dir, src_dir, gallery_conf):
553553

554554
filename_pattern = gallery_conf.get('filename_pattern')
555555
if re.search(filename_pattern, src_file) and gallery_conf['plot_gallery']:
556-
# A lot of examples contains 'print(__doc__)' for example in
557-
# scikit-learn so that running the example prints some useful
558-
# information. Because the docstring has been separated from
559-
# the code blocks in sphinx-gallery, __doc__ is actually
560-
# __builtin__.__doc__ in the execution context and we do not
561-
# want to print it
562-
example_globals = {'__doc__': ''}
556+
example_globals = {
557+
# A lot of examples contains 'print(__doc__)' for example in
558+
# scikit-learn so that running the example prints some useful
559+
# information. Because the docstring has been separated from
560+
# the code blocks in sphinx-gallery, __doc__ is actually
561+
# __builtin__.__doc__ in the execution context and we do not
562+
# want to print it
563+
'__doc__': '',
564+
# Examples may contain if __name__ == '__main__' guards
565+
# for in example scikit-learn if the example uses multiprocessing
566+
'__name__': '__main__'}
567+
563568
fig_count = 0
564569
# A simple example has two blocks: one for the
565570
# example introduction/explanation and one for the code
@@ -581,6 +586,9 @@ def generate_file_rst(fname, target_dir, src_dir, gallery_conf):
581586
example_rst += code_output
582587
else:
583588
example_rst += code_output
589+
if 'sphx-glr-script-out' in code_output:
590+
# Add some vertical space after output
591+
example_rst += "\n\n|\n\n"
584592
example_rst += codestr2rst(bcontent) + '\n'
585593

586594
else:

0 commit comments

Comments
 (0)