From ec350f046f655d78d35770b67f26f87eb47d3983 Mon Sep 17 00:00:00 2001 From: Ali-Tehrani Date: Mon, 13 May 2024 12:57:53 -0400 Subject: [PATCH] Update to add github plus source files --- doc/conf.py | 49 ++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 46 insertions(+), 3 deletions(-) diff --git a/doc/conf.py b/doc/conf.py index 3c5aade71..7de0eb003 100644 --- a/doc/conf.py +++ b/doc/conf.py @@ -10,6 +10,8 @@ # add these directories to sys.path here. If the directory is relative to the # documentation root, use os.path.abspath to make it absolute, like shown here. # +import importlib +import inspect import os import sys @@ -37,7 +39,8 @@ "sphinx.ext.mathjax", "nbsphinx", "nbsphinx_link", - "IPython.sphinxext.ipython_console_highlighting" + "IPython.sphinxext.ipython_console_highlighting", + "sphinx.ext.linkcode", ] # List of arguments to be passed to the kernel that executes the notebooks: @@ -92,8 +95,7 @@ # Theme options are theme-specific and customize the look and feel of a theme # further. For a list of options available for each theme, see the # documentation. -html_theme_options = {"collapse_navigation": False} - +html_theme_options = {"collapse_navigation": False,} # -- Configuration for autodoc extensions --------------------------------- @@ -121,3 +123,44 @@ def autodoc_skip_member(app, what, name, obj, skip, options): def setup(app): """Set up sphinx.""" app.connect("autodoc-skip-member", autodoc_skip_member) + + +# -- Link to Github ------------------------------ +# Configure viewcode extension. +code_url = f"https://github.com/theochem/grid/blob/master/src/grid" + + +def linkcode_resolve(domain, info): + # Non-linkable objects from the starter kit in the tutorial. + if domain != 'py': + return None + if not info['module']: + return None + + mod = importlib.import_module(info["module"]) + if "." in info["fullname"]: + objname, attrname = info["fullname"].split(".") + obj = getattr(mod, objname) + try: + # object is a method of a class + obj = getattr(obj, attrname) + except AttributeError: + # object is an attribute of a class + return None + else: + obj = getattr(mod, info["fullname"]) + try: + file = inspect.getsourcefile(obj) + lines = inspect.getsourcelines(obj) + except TypeError: + # e.g. object is a typing.Union + return None + file = os.path.relpath(file, os.path.abspath("..")) + + # Get the file name e.g. /usr/conda/py37/grid/angular.py -> angular.py + split = file.split("grid/") + file = split[-1] + + start, end = lines[1], lines[1] + len(lines[0]) - 1 + + return f"{code_url}/{file}#L{start}-L{end}"