10000 [doc build] Update to Sphinx-Gallery v0.1.11 · scikit-learn/scikit-learn@9f5e58d · GitHub
[go: up one dir, main page]

Skip to content

Commit 9f5e58d

Browse files
committed
[doc build] Update to Sphinx-Gallery v0.1.11
1 parent 4397e7e commit 9f5e58d

File tree

11 files changed

+295
-223
lines changed

11 files changed

+295
-223
lines changed

doc/conf.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,7 @@
236236

237237
sphinx_gallery_conf = {
238238
'doc_module': 'sklearn',
239+
'backreferences_dir': os.path.join('modules', 'generated'),
239240
'reference_url': {
240241
'sklearn': None,
241242
'matplotlib': 'http://matplotlib.org',

doc/sphinxext/sphinx_gallery/__init__.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
"""
2-
==============
32
Sphinx Gallery
43
==============
54
65
"""
76
import os
8-
__version__ = '0.1.7'
7+
__version__ = '0.1.11'
98

109

1110
def glr_path_static():

doc/sphinxext/sphinx_gallery/_static/gallery.css

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -190,25 +190,3 @@ p.sphx-glr-signature a.reference.external {
190190
margin-left: auto;
191191
display: table;
192192
}
193-
194-
a.sphx-glr-code-links:hover{
195-
text-decoration: none;
196-
}
197-
198-
a.sphx-glr-code-links[tooltip]:hover:before{
199-
background: rgba(0,0,0,.8);
200-
border-radius: 5px;
201-
color: white;
202-
content: attr(tooltip);
203-
padding: 5px 15px;
204-
position: absolute;
205-
z-index: 98;
206-
width: 16em;
207-
word-break: normal;
208-
white-space: normal;
209-
display: inline-block;
210-
text-align: center;
211-
text-indent: 0;
212-
margin-left: 0; /* Use zero to avoid overlapping with sidebar */
213-
margin-top: 1.2em;
214-
}

doc/sphinxext/sphinx_gallery/backreferences.py

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,10 @@
22
# Author: Óscar Nájera
33
# License: 3-clause BSD
44
"""
5-
========================
65
Backreferences Generator
76
========================
87
9-
Reviews generated example files in order to keep track of used modules
8+
Parses example file code in order to keep track of used functions
109
"""
1110

1211
from __future__ import print_function
@@ -97,7 +96,10 @@ def identify_names(code):
9796
e.HelloWorld HelloWorld d d
9897
"""
9998
finder = NameFinder()
100-
finder.visit(ast.parse(code))
99+
try:
100+
finder.visit(ast.parse(code))
101+
except SyntaxError:
102+
return {}
101103

102104
example_code_obj = {}
103105
for name, full_name in finder.get_mapping():
@@ -133,8 +135,6 @@ def scan_used_functions(example_file, gallery_conf):
133135
return backrefs
134136

135137

136-
# XXX This figure:: uses a forward slash even on Windows, but the op.join's
137-
# elsewhere will use backslashes...
138138
THUMBNAIL_TEMPLATE = """
139139
.. raw:: html
140140
@@ -162,6 +162,10 @@ def _thumbnail_div(full_dir, fname, snippet, is_backref=False):
162162
"""Generates RST to place a thumbnail in a gallery"""
163163
thumb = os.path.join(full_dir, 'images', 'thumb',
164164
'sphx_glr_%s_thumb.png' % fname[:-3])
165+
166+
# Inside rst files forward slash defines paths
167+
thumb = thumb.replace(os.sep, "/")
168+
165169
ref_name = os.path.join(full_dir, fname).replace(os.path.sep, '_')
166170

167171
template = BACKREF_THUMBNAIL_TEMPLATE if is_backref else THUMBNAIL_TEMPLATE
@@ -172,17 +176,22 @@ def write_backreferences(seen_backrefs, gallery_conf,
172176
target_dir, fname, snippet):
173177
"""Writes down back reference files, which include a thumbnail list
174178
of examples using a certain module"""
179+
if gallery_conf['backreferences_dir'] is None:
180+
return
181+
175182
example_file = os.path.join(target_dir, fname)
183+
build_target_dir = os.path.relpath(target_dir, gallery_conf['src_dir'])
176184
backrefs = scan_used_functions(example_file, gallery_conf)
177185
for backref in backrefs:
178-
include_path = os.path.join(gallery_conf['mod_example_dir'],
186+
include_path = os.path.join(gallery_conf['src_dir'],
187+
gallery_conf['backreferences_dir'],
179188
'%s.examples' % backref)
180189
seen = backref in seen_backrefs
181190
with open(include_path, 'a' if seen else 'w') as ex_file:
182191
if not seen:
183192
heading = '\n\nExamples using ``%s``' % backref
184193
ex_file.write(heading + '\n')
185194
ex_file.write('^' * len(heading) + '\n')
186-
ex_file.write(_thumbnail_div(target_dir, fname, snippet,
195+
ex_file.write(_thumbnail_div(build_target_dir, fname, snippet,
187196
is_backref=True))
188197
seen_backrefs.add(backref)

doc/sphinxext/sphinx_gallery/docs_resolv.py

Lines changed: 75 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
# -*- coding: utf-8 -*-
22
# Author: Óscar Nájera
33
# License: 3-clause BSD
4-
###############################################################################
5-
# Documentation link resolver objects
4+
"""
5+
Link resolver objects
6+
=====================
7+
"""
68
from __future__ import print_function
79
import gzip
810
import os
@@ -11,6 +13,8 @@
1113
import shelve
1214
import sys
1315

16+
from sphinx.util.console import fuchsia
17+
1418
# Try Python 2 first, otherwise load from Python 3
1519
try:
1620
import cPickle as pickle
@@ -291,7 +295,7 @@ def resolve(self, cobj, this_url):
291295
cobj : dict
292296
Dict with information about the "code object" for which we are
293297
resolving a link.
294-
cobi['name'] : function or class name (str)
298+
cobj['name'] : function or class name (str)
295299
cobj['module_short'] : shortened module name (str)
296300
cobj['module'] : module name (str)
297301
this_url: str
@@ -331,16 +335,17 @@ def _embed_code_links(app, gallery_conf, gallery_dir):
331335
# Add resolvers for the packages for which we want to show links
332336
doc_resolvers = {}
333337

338+
src_gallery_dir = os.path.join(app.builder.srcdir, gallery_dir)
334339
for this_module, url in gallery_conf['reference_url'].items():
335340
try:
336341
if url is None:
337342
doc_resolvers[this_module] = SphinxDocLinkResolver(
338343
app.builder.outdir,
339-
gallery_dir,
344+
src_gallery_dir,
340345
relative=True)
341346
else:
342347
doc_resolvers[this_module] = SphinxDocLinkResolver(url,
343-
gallery_dir)
348+
src_gallery_dir)
344349

345350
except HTTPError as e:
346351
print("The following HTTP Error has occurred:\n")
@@ -357,72 +362,74 @@ def _embed_code_links(app, gallery_conf, gallery_dir):
357362
gallery_dir))
358363

359364
# patterns for replacement
360-
link_pattern = ('<a href="%s" class="sphx-glr-code-links" '
361-
'tooltip="Link to documentation for %s">%s</a>')
365+
link_pattern = ('<a href="%s" title="View documentation for %s">%s</a>')
362366
orig_pattern = '<span class="n">%s</span>'
363367
period = '<span class="o">.</span>'
364368

365-
for dirpath, _, filenames in os.walk(html_gallery_dir):
366-
for fname in filenames:
367-
print('\tprocessing: %s' % fname)
368-
full_fname = os.path.join(html_gallery_dir, dirpath, fname)
369-
subpath = dirpath[len(html_gallery_dir) + 1:]
370-
pickle_fname = os.path.join(gallery_dir, subpath,
371-
fname[:-5] + '_codeobj.pickle')
372-
373-
if os.path.exists(pickle_fname):
374-
# we have a pickle file with the objects to embed links for
375-
with open(pickle_fname, 'rb') as fid:
376-
example_code_obj = pickle.load(fid)
377-
fid.close()
378-
str_repl = {}
379-
# generate replacement strings with the links
380-
for name, cobj in example_code_obj.items():
381-
this_module = cobj['module'].split('.')[0]
382-
383-
if this_module not in doc_resolvers:
384-
continue
385-
386-
try:
387-
link = doc_resolvers[this_module].resolve(cobj,
388-
full_fname)
389-
except (HTTPError, URLError) as e:
390-
if isinstance(e, HTTPError):
391-
extra = e.code
392-
else:
393-
extra = e.reason
394-
print("\t\tError resolving %s.%s: %r (%s)"
395-
% (cobj['module'], cobj['name'], e, extra))
396-
continue
397-
398-
if link is not None:
399-
parts = name.split('.')
400-
name_html = period.join(orig_pattern % part
401-
for part in parts)
402-
full_function_name = '%s.%s' % (
403-
cobj['module'], cobj['name'])
404-
str_repl[name_html] = link_pattern % (
405-
link, full_function_name, name_html)
406-
# do the replacement in the html file
407-
408-
# ensure greediness
409-
names = sorted(str_repl, key=len, reverse=True)
410-
expr = re.compile(r'(?<!\.)\b' + # don't follow . or word
411-
'|'.join(re.escape(name)
412-
for name in names))
413-
414-
def substitute_link(match):
415-
return str_repl[match.group()]
416-
417-
if len(str_repl) > 0:
418-
with open(full_fname, 'rb') as fid:
419-
lines_in = fid.readlines()
420-
with open(full_fname, 'wb') as fid:
421-
for line in lines_in:
422-
line = line.decode('utf-8')
423-
line = expr.sub(substitute_link, line)
424-
fid.write(line.encode('utf-8'))
425-
print('[done]')
369+
# This could be turned into a generator if necessary, but should be okay
370+
flat = [[dirpath, filename]
371+
for dirpath, _, filenames in os.walk(html_gallery_dir)
372+
for filename in filenames]
373+
iterator = app.status_iterator(
374+
flat, os.path.basename(html_gallery_dir), colorfunc=fuchsia,
375+
length=len(flat), stringify_func=lambda x: os.path.basename(x[1]))
376+
for dirpath, fname in iterator:
377+
full_fname = os.path.join(html_gallery_dir, dirpath, fname)
378+
subpath = dirpath[len(html_gallery_dir) + 1:]
379+
pickle_fname = os.path.join(src_gallery_dir, subpath,
380+
fname[:-5] + '_codeobj.pickle')
381+
382+
if os.path.exists(pickle_fname):
383+
# we have a pickle file with the objects to embed links for
384+
with open(pickle_fname, 'rb') as fid:
385+
example_code_obj = pickle.load(fid)
386+
fid.close()
387+
str_repl = {}
388+
# generate replacement strings with the links
389+
for name, cobj in example_code_obj.items():
390+
this_module = cobj['module'].split('.')[0]
391+
392+
if this_module not in doc_resolvers:
393+
continue
394+
395+
try:
396+
link = doc_resolvers[this_module].resolve(cobj,
397+
full_fname)
398+
except (HTTPError, URLError) as e:
399+
if isinstance(e, HTTPError):
400+
extra = e.code
401+
else:
402+
extra = e.reason
403+
print("\n\t\tError resolving %s.%s: %r (%s)"
404+
% (cobj['module'], cobj['name'], e, extra))
405+
continue
406+
407+
if link is not None:
408+
parts = name.split('.')
409+
name_html = period.join(orig_pattern % part
410+
for part in parts)
411+
full_function_name = '%s.%s' % (
412+
cobj['module'], cobj['name'])
413+
str_repl[name_html] = link_pattern % (
414+
link, full_function_name, name_html)
415+
# do the replacement in the html file
416+
417+
# ensure greediness
418+
names = sorted(str_repl, key=len, reverse=True)
419+
regex_str = '|'.join(re.escape(name) for name in names)
420+
regex = re.compile(regex_str)
421+
422+
def substitute_link(match):
423+
return str_repl[match.group()]
424+
425+
if len(str_repl) > 0:
426+
with open(full_fname, 'rb') as fid:
427+
lines_in = fid.readlines()
428+
with open(full_fname, 'wb') as fid:
429+
for line in lines_in:
430+
line = line.decode('utf-8')
431+
line = regex.sub(substitute_link, line)
432+
fid.write(line.encode('utf-8'))
426433

427434

428435
def embed_code_links(app, exception):

doc/sphinxext/sphinx_gallery/downloads.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,14 @@ def python_zip(file_list, gallery_path, extension='.py'):
5555
zip file name, written as `target_dir_{python,jupyter}.zip`
5656
depending on the extension
5757
"""
58-
zipname = gallery_path.replace(os.path.sep, '_')
58+
zipname = os.path.basename(gallery_path)
5959
zipname += '_python' if extension == '.py' else '_jupyter'
6060
zipname = os.path.join(gallery_path, zipname + '.zip')
6161

6262
zipf = zipfile.ZipFile(zipname, mode='w')
6363
for fname in file_list:
6464
file_src = os.path.splitext(fname)[0] + extension
65-
zipf.write(file_src)
65+
zipf.write(file_src, os.path.relpath(file_src, gallery_path))
6666
zipf.close()
6767

6868
return zipname
@@ -110,8 +110,11 @@ def generate_zipfiles(gallery_dir):
110110
py_zipfile = python_zip(listdir, gallery_dir)
111111
jy_zipfile = python_zip(listdir, gallery_dir, ".ipynb")
112112

113+
def rst_path(filepath):
114+
return filepath.replace(os.sep, '/')
115+
113116
dw_rst = CODE_ZIP_DOWNLOAD.format(os.path.basename(py_zipfile),
114-
py_zipfile,
117+
rst_path(py_zipfile),
115118
os.path.basename(jy_zipfile),
116-
jy_zipfile)
119+
rst_path(jy_zipfile))
117120
return dw_rst

0 commit comments

Comments
 (0)
0