1
1
# -*- coding: utf-8 -*-
2
2
# Author: Óscar Nájera
3
3
# License: 3-clause BSD
4
- ###############################################################################
5
- # Documentation link resolver objects
4
+ """
5
+ Link resolver objects
6
+ =====================
7
+ """
6
8
from __future__ import print_function
7
9
import gzip
8
10
import os
11
13
import shelve
12
14
import sys
13
15
16
+ from sphinx .util .console import fuchsia
17
+
14
18
# Try Python 2 first, otherwise load from Python 3
15
19
try :
16
20
import cPickle as pickle
@@ -291,7 +295,7 @@ def resolve(self, cobj, this_url):
291
295
cobj : dict
292
296
Dict with information about the "code object" for which we are
293
297
resolving a link.
294
- cobi ['name'] : function or class name (str)
298
+ cobj ['name'] : function or class name (str)
295
299
cobj['module_short'] : shortened module name (str)
296
300
cobj['module'] : module name (str)
297
301
this_url: str
@@ -331,16 +335,17 @@ def _embed_code_links(app, gallery_conf, gallery_dir):
331
335
# Add resolvers for the packages for which we want to show links
332
336
doc_resolvers = {}
333
337
338
+ src_gallery_dir = os .path .join (app .builder .srcdir , gallery_dir )
334
339
for this_module , url in gallery_conf ['reference_url' ].items ():
335
340
try :
336
341
if url is None :
337
342
doc_resolvers [this_module ] = SphinxDocLinkResolver (
338
343
app .builder .outdir ,
339
- gallery_dir ,
344
+ src_gallery_dir ,
340
345
relative = True )
341
346
else :
342
347
doc_resolvers [this_module ] = SphinxDocLinkResolver (url ,
343
- gallery_dir )
348
+ src_gallery_dir )
344
349
345
350
except HTTPError as e :
346
351
print ("The following HTTP Error has occurred:\n " )
@@ -357,72 +362,74 @@ def _embed_code_links(app, gallery_conf, gallery_dir):
357
362
gallery_dir ))
358
363
359
364
# 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>' )
362
366
orig_pattern = '<span class="n">%s</span>'
363
367
period = '<span class="o">.</span>'
364
368
365
- for dirpath , _ , filenames in os .walk (html_gallery_dir ):
366
- for fname in filenames :
367
- print ('\t processing: %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 \t Error 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 \t Error 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' ))
426
433
427
434
428
435
def embed_code_links (app , exception ):
0 commit comments