8000 Update to Sphinx-Gallery v0.1.11 (#8222) · glemaitre/scikit-learn@85ebe0a · GitHub
[go: up one dir, main page]

Skip to content

Commit 85ebe0a

Browse files
Titan-Cglemaitre
authored andcommitted
Update to Sphinx-Gallery v0.1.11 (scikit-learn#8222)
1 parent 50c57a8 commit 85ebe0a

File tree

11 files changed

+447
-235
lines changed

11 files changed

+447
-235
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: 10 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/*
2-
Sphinx-Gallery is has compatible CSS to fix default sphinx themes
2+
Sphinx-Gallery has compatible CSS to fix default sphinx themes
33
Tested for Sphinx 1.3.1 for all themes: default, alabaster, sphinxdoc,
44
scrolls, agogo, traditional, nature, haiku, pyramid
5-
Tested for Read the docs theme 0.1.7 */
5+
Tested for Read the Docs theme 0.1.7 */
66
.sphx-glr-thumbcontainer {
77
background: #fff;
88
border: solid #fff 1px;
@@ -103,9 +103,6 @@ thumbnail with its default link Background color */
103103
blockquote.sphx-glr-script-out {
104104
margin-left: 0pt;
105105
}
106-
<<<<<<< HEAD
107-
.sphx-glr-download {
108-
=======
109106

110107
div.sphx-glr-footer {
111108
text-align: center;
@@ -118,15 +115,10 @@ div.sphx-glr-download {
118115
}
119116

120117
div.sphx-glr-download a {
121-
>>>>>>> bddda7bc6... [MRG + 2] [MAINT] Update to Sphinx-Gallery 0.1.7 (#7986)
122118
background-color: #ffc;
123-
border: 1px solid #c2c22d;
124-
<<<<<<< HEAD
119+
background-image: linear-gradient(to bottom, #FFC, #d5d57e);
125120
border-radius: 4px;
126-
margin: 1em auto 1ex auto;
127-
max-width: 45ex;
128-
padding: 1ex;
129-
=======
121+
border: 1px solid #c2c22d;
130122
color: #000;
131123
display: inline-block;
132124
/* Not valid in old browser, hence we keep the line above to override */
@@ -168,10 +160,13 @@ div.sphx-glr-download code.download {
168160
/* border and background are given by the enclosing 'a' */
169161
border: none;
170162
background: none;
171< F438 /td>-
>>>>>>> bddda7bc6... [MRG + 2] [MAINT] Update to Sphinx-Gallery 0.1.7 (#7986)
172163
}
173-
.sphx-glr-download a {
174-
color: #4b4600;
164+
165+
div.sphx-glr-download a:hover {
166+
box-shadow: inset 0 1px 0 rgba(255,255,255,.1), 0 1px 5px rgba(0,0,0,.25);
167+
text-decoration: none;
168+
background-image: none;
169+
background-color: #d5d57e;
175170
}
176171

177172
ul.sphx-glr-horizontal {
@@ -184,8 +179,6 @@ ul.sphx-glr-horizontal li {
184179
ul.sphx-glr-horizontal img {
185180
height: auto !important;
186181
}
187-
<<<<<<< HEAD
188-
=======
189182

190183
p.sphx-glr-signature a.reference.external {
191184
-moz-border-radius: 5px;
@@ -197,26 +190,3 @@ p.sphx-glr-signature a.reference.external {
197190
margin-left: auto;
198191
display: table;
199192
}
200-
201-
a.sphx-glr-code-links:hover{
202-
text-decoration: none;
203-
}
204-
205-
a.sphx-glr-code-links[tooltip]:hover:before{
206-
background: rgba(0,0,0,.8);
207-
border-radius: 5px;
208-
color: white;
209-
content: attr(tooltip);
210-
padding: 5px 15px;
211-
position: absolute;
212-
z-index: 98;
213-
width: 16em;
214-
word-break: normal;
215-
white-space: normal;
216-
display: inline-block;
217-
text-align: center;
218-
text-indent: 0;
219-
margin-left: 0; /* Use zero to avoid overlapping with sidebar */
220-
margin-top: 1.2em;
221-
}
222-
>>>>>>> bddda7bc6... [MRG + 2] [MAINT] Update to Sphinx-Gallery 0.1.7 (#7986)

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
from StringIO import StringIO
@@ -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):

0 commit comments

Comments
 (0)
0