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