20
20
21
21
22
22
def clean_gallery_out (build_dir ):
23
+ """Deletes images under the sphx_glr namespace in the build directory"""
23
24
# Sphinx hack: sphinx copies generated images to the build directory
24
25
# each time the docs are made. If the desired image name already
25
26
# exists, it appends a digit to prevent overwrites. The problem is,
@@ -30,10 +31,11 @@ def clean_gallery_out(build_dir):
30
31
# was no response: http://osdir.com/ml/sphinx-dev/2011-02/msg00123.html
31
32
#
32
33
# 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
+
37
39
build_image_dir = os .path .join (build_dir , '_images' )
38
40
if os .path .exists (build_image_dir ):
39
41
filelist = os .listdir (build_image_dir )
@@ -101,11 +103,26 @@ def generate_gallery_rst(app):
101
103
fhindex .flush ()
102
104
103
105
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
+
104
121
gallery_conf = {
105
122
'filename_pattern' : re .escape (os .sep ) + 'plot' ,
106
123
'examples_dirs' : '../examples' ,
107
124
'gallery_dirs' : 'auto_examples' ,
108
- 'mod_example_dir' : 'modules/ generated' ,
125
+ 'mod_example_dir' : os . path . join ( 'modules' , ' generated') ,
109
126
'doc_module' : (),
110
127
'reference_url' : {},
111
128
}
@@ -118,6 +135,9 @@ def setup(app):
118
135
app .add_config_value ('sphinx_gallery_conf' , gallery_conf , 'html' )
119
136
app .add_stylesheet ('gallery.css' )
120
137
138
+ if 'sphinx.ext.autodoc' in app ._extensions :
139
+ app .connect ('autodoc-process-docstring' , touch_empty_backreferences )
140
+
121
141
app .connect ('builder-inited' , generate_gallery_rst )
122
142
123
143
app .connect ('build-finished' , embed_code_links )
0 commit comments