|
53 | 53 | 'sphinx.ext.inheritance_diagram',
|
54 | 54 | 'sphinx.ext.intersphinx',
|
55 | 55 | 'sphinx.ext.ifconfig',
|
56 |
| - 'sphinx.ext.viewcode', |
57 | 56 | 'IPython.sphinxext.ipython_console_highlighting',
|
58 | 57 | 'IPython.sphinxext.ipython_directive',
|
59 | 58 | 'numpydoc', # Needs to be loaded *after* autodoc.
|
@@ -530,3 +529,80 @@ def setup(app):
|
530 | 529 | else:
|
531 | 530 | bld_type = 'rel'
|
532 | 531 | app.add_config_value('releaselevel', bld_type, 'env')
|
| 532 | + |
| 533 | +# ----------------------------------------------------------------------------- |
| 534 | +# Source code links |
| 535 | +# ----------------------------------------------------------------------------- |
| 536 | +link_github = True |
| 537 | +# You can add viewcode extension to build old way with link_github = False |
| 538 | +# extensions.append('sphinx.ext.viewcode') |
| 539 | +if link_github: |
| 540 | + import re |
| 541 | + import inspect |
| 542 | + from os.path import relpath, dirname |
| 543 | + for name in ['sphinx.ext.linkcode', 'linkcode', 'numpydoc.linkcode']: |
| 544 | + try: |
| 545 | + __import__(name) |
| 546 | + extensions.append(name) |
| 547 | + break |
| 548 | + except ImportError: |
| 549 | + pass |
| 550 | + else: |
| 551 | + print("NOTE: linkcode extension not found -- no links to source generated") |
| 552 | + |
| 553 | + def linkcode_resolve(domain, info): |
| 554 | + """ |
| 555 | + Determine the URL corresponding to Python object |
| 556 | + """ |
| 557 | + if domain != 'py': |
| 558 | + return None |
| 559 | + |
| 560 | + modname = info['module'] |
| 561 | + fullname = info['fullname'] |
| 562 | + |
| 563 | + submod = sys.modules.get(modname) |
| 564 | + if submod is None: |
| 565 | + return None |
| 566 | + |
| 567 | + obj = submod |
| 568 | + for part in fullname.split('.'): |
| 569 | + try: |
| 570 | + obj = getattr(obj, part) |
| 571 | + except Exception: |
| 572 | + return None |
| 573 | + |
| 574 | + try: |
| 575 | + fn = inspect.getsourcefile(obj) |
| 576 | + except Exception: |
| 577 | + fn = None |
| 578 | + if not fn: |
| 579 | + try: |
| 580 | + fn = inspect.getsourcefile(sys.modules[obj.__module__]) |
| 581 | + except Exception: |
| 582 | + fn = None |
| 583 | + if not fn: |
| 584 | + return None |
| 585 | + |
| 586 | + try: |
| 587 | + source, lineno = inspect.getsourcelines(obj) |
| 588 | + except Exception: |
| 589 | + lineno = None |
| 590 | + |
| 591 | + if lineno: |
| 592 | + linespec = "#L%d-L%d" % (lineno, lineno + len(source) - 1) |
| 593 | + else: |
| 594 | + linespec = "" |
| 595 | + |
| 596 | + startdir = os.path.abspath(os.path.join(dirname(matplotlib.__file__), '..')) |
| 597 | + fn = relpath(fn, start=startdir).replace(os.path.sep, '/') |
| 598 | + |
| 599 | + if fn.startswith('matplotlib/'): |
| 600 | + m = re.match(r'^.*post[0-9]+\+\w([a-z0-9]+).\w+$', matplotlib.__version__) |
| 601 | + if m: |
| 602 | + return "https://github.com/matplotlib/matplotlib/blob/%s/lib/%s%s" % ( |
| 603 | + m.group(1), fn, linespec) |
| 604 | + else: |
| 605 | + return "https://github.com/matplotlib/matplotlib/blob/v%s/lib/%s%s" % ( |
| 606 | + matplotlib.__version__, fn, linespec) |
| 607 | + else: |
| 608 | + return None |
0 commit comments