|
3 | 3 | from typing import Any, Dict |
4 | 4 |
|
5 | 5 | from docutils.nodes import document |
| 6 | +from sphinx import addnodes |
6 | 7 | from sphinx.application import Sphinx |
7 | 8 |
|
| 9 | +from ansys_sphinx_theme.extension.linkcode import DOMAIN_KEYS, sphinx_linkcode_resolve |
8 | 10 | from ansys_sphinx_theme.latex import generate_404 # noqa: F401 |
9 | 11 |
|
10 | 12 | __version__ = "0.10.2" |
@@ -107,6 +109,103 @@ def setup_default_html_theme_options(app): |
107 | 109 | app.config.html_theme_options.setdefault("collapse_navigation", True) |
108 | 110 |
|
109 | 111 |
|
| 112 | +def fix_edit_html_page_context( |
| 113 | + app: Sphinx, pagename: str, templatename: str, context: dict, doctree: document |
| 114 | +) -> None: |
| 115 | + """Add a function that Jinja can access for returning an "edit this page" link . |
| 116 | +
|
| 117 | + This function creates an "edit this page" link for any library. |
| 118 | + The link points to the corresponding file on the main branch. |
| 119 | +
|
| 120 | + Parameters |
| 121 | + ---------- |
| 122 | + app : Sphinx |
| 123 | + Sphinx application instance for rendering the documentation. |
| 124 | + pagename : str |
| 125 | + Name of the current page. |
| 126 | + templatename : str |
| 127 | + Name of the template being used. |
| 128 | + context : dict |
| 129 | + Context dictionary for the page. |
| 130 | + doctree : document |
| 131 | + Document tree for the page. |
| 132 | +
|
| 133 | + Notes |
| 134 | + ----- |
| 135 | + .. [1] Originally implemented by `Alex Kaszynski <https://github.com/akaszynski>`_ in |
| 136 | + `PyVista <https://github.com/pyvista/pyvista>`_, |
| 137 | + see https://github.com/pyvista/pyvista/pull/4113 |
| 138 | + """ |
| 139 | + |
| 140 | + def fix_edit_link_button(link: str) -> str: |
| 141 | + """Transform "edit on GitHub" links to the correct URL. |
| 142 | +
|
| 143 | + This function fixes the URL for the "edit this page" link. |
| 144 | +
|
| 145 | + Parameters |
| 146 | + ---------- |
| 147 | + link : str |
| 148 | + Link to the GitHub edit interface. |
| 149 | +
|
| 150 | + Returns |
| 151 | + ------- |
| 152 | + str |
| 153 | + Link to the corresponding file on the main branch. |
| 154 | + """ |
| 155 | + github_user = context.get("github_user", "") |
| 156 | + github_repo = context.get("github_repo", "") |
| 157 | + github_source = context.get("source_path", "") |
| 158 | + kind = context.get("github_version", "") |
| 159 | + |
| 160 | + if "_autosummary" in pagename: |
| 161 | + for obj_node in list(doctree.findall(addnodes.desc)): |
| 162 | + domain = obj_node.get("domain") |
| 163 | + for signode in obj_node: |
| 164 | + if not isinstance(signode, addnodes.desc_signature): |
| 165 | + continue |
| 166 | + # Convert signode to a specified format |
| 167 | + info = {} |
| 168 | + for key in DOMAIN_KEYS.get(domain, []): |
| 169 | + value = signode.get(key) |
| 170 | + if not value: |
| 171 | + value = "" |
| 172 | + info[key] = value |
| 173 | + if not info: |
| 174 | + continue |
| 175 | + # This is an API example |
| 176 | + return sphinx_linkcode_resolve( |
| 177 | + domain=domain, |
| 178 | + info=info, |
| 179 | + library=f"{github_user}/{github_repo}", |
| 180 | + source_path=github_source, |
| 181 | + github_version=kind, |
| 182 | + edit=True, |
| 183 | + ) |
| 184 | + |
| 185 | + elif "autoapi" in pagename: |
| 186 | + for obj_node in list(doctree.findall(addnodes.desc)): |
| 187 | + domain = obj_node.get("domain") |
| 188 | + if domain != "py": |
| 189 | + return link |
| 190 | + |
| 191 | + for signode in obj_node: |
| 192 | + if not isinstance(signode, addnodes.desc_signature): |
| 193 | + continue |
| 194 | + |
| 195 | + fullname = signode["module"] |
| 196 | + modname = fullname.replace(".", "/") |
| 197 | + |
| 198 | + if github_source: |
| 199 | + return f"http://github.com/{github_user}/{github_repo}/edit/{kind}/{github_source}/{modname}.{domain}" # noqa: E501 |
| 200 | + else: |
| 201 | + return f"http://github.com/{github_user}/{github_repo}/edit/{kind}/{modname}.{domain}" # noqa: E501 |
| 202 | + |
| 203 | + else: |
| 204 | + return link |
| 205 | + |
| 206 | + context["fix_edit_link_button"] = fix_edit_link_button |
| 207 | + |
| 208 | + |
110 | 209 | def update_footer_theme( |
111 | 210 | app: Sphinx, pagename: str, templatename: str, context: Dict[str, Any], doctree: document |
112 | 211 | ) -> None: |
@@ -162,9 +261,8 @@ def setup(app: Sphinx) -> Dict: |
162 | 261 | app.add_js_file("https://cdn.datatables.net/1.10.23/js/jquery.dataTables.min.js") |
163 | 262 | app.add_css_file("https://cdn.datatables.net/1.10.23/css/jquery.dataTables.min.css") |
164 | 263 | app.connect("html-page-context", update_footer_theme) |
165 | | - # Add templates for autosummary |
| 264 | + app.connect("html-page-context", fix_edit_html_page_context) |
166 | 265 | app.config.templates_path.append(str(TEMPLATES_PATH)) |
167 | | - |
168 | 266 | return { |
169 | 267 | "version": __version__, |
170 | 268 | "parallel_read_safe": True, |
|
0 commit comments