|
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. |
@@ -536,3 +535,74 @@ def setup(app): |
536 | 535 | else: |
537 | 536 | bld_type = 'rel' |
538 | 537 | app.add_config_value('releaselevel', bld_type, 'env') |
| 538 | + |
| 539 | +# ----------------------------------------------------------------------------- |
| 540 | +# Source code links |
| 541 | +# ----------------------------------------------------------------------------- |
| 542 | +link_github = True |
| 543 | +# You can add build old with link_github = False |
| 544 | + |
| 545 | +if link_github: |
| 546 | + import re |
| 547 | + import inspect |
| 548 | + |
| 549 | + extensions.append('sphinx.ext.linkcode') |
| 550 | + |
| 551 | + def linkcode_resolve(domain, info): |
| 552 | + """ |
| 553 | + Determine the URL corresponding to Python object |
| 554 | + """ |
| 555 | + if domain != 'py': |
| 556 | + return None |
| 557 | + |
| 558 | + modname = info['module'] |
| 559 | + fullname = info['fullname'] |
| 560 | + |
| 561 | + submod = sys.modules.get(modname) |
| 562 | + if submod is None: |
| 563 | + return None |
| 564 | + |
| 565 | + obj = submod |
| 566 | + for part in fullname.split('.'): |
| 567 | + try: |
| 568 | + obj = getattr(obj, part) |
| 569 | + except AttributeError: |
| 570 | + return None |
| 571 | + |
| 572 | + try: |
| 573 | + fn = inspect.getsourcefile(obj) |
| 574 | + except TypeError: |
| 575 | + fn = None |
| 576 | + if not fn or fn.endswith('__init__.py'): |
| 577 | + try: |
| 578 | + fn = inspect.getsourcefile(sys.modules[obj.__module__]) |
| 579 | + except (TypeError, AttributeError, KeyError): |
| 580 | + fn = None |
| 581 | + if not fn: |
| 582 | + return None |
| 583 | + |
| 584 | + try: |
| 585 | + source, lineno = inspect.getsourcelines(obj) |
| 586 | + except (OSError, TypeError): |
| 587 | + lineno = None |
| 588 | + |
| 589 | + if lineno: |
| 590 | + linespec = "#L%d-L%d" % (lineno, lineno + len(source) - 1) |
| 591 | + else: |
| 592 | + linespec = "" |
| 593 | + |
| 594 | + startdir = Path(matplotlib.__file__).parent.parent |
| 595 | + fn = os.path.relpath(fn, start=startdir).replace(os.path.sep, '/') |
| 596 | + |
| 597 | + if not fn.startswith(('matplotlib/', 'mpl_toolkits/')): |
| 598 | + return None |
| 599 | + |
| 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 | + extensions.append('sphinx.ext.viewcode') |
0 commit comments