astropy / astropy/sphinx-automodapi
Broken "Edit on Github" links
- Dominant language
- Python
- Stars
- 67
- Forks
- 48
- Avg merge
- 11h 52m
- Merged PRs (30d)
- 1
Description
Hey,
some themes like sphinx_rtd_theme emit "Edit on GitHub" links to the page source in the git repository. For autogenerated pages this link points to something that doesn't exist, i.e. using such a theme with autogenerated modules often results in invalid links in the documentation.
I've seen this issue several times in the wild now, so I believe many would profit from a better default handling of this situation, or at least some prominent documentation. It's of course arguable whether the fix should go here or into the theme code. This issue was also raised in https://github.com/readthedocs/sphinx_rtd_theme/issues/324 and their resolution (see https://github.com/readthedocs/sphinx_rtd_theme/pull/393) was to simply use sphinx's [`hasdoc()`](https://github.com/sphinx-doc/sphinx/blob/df71e62dd8c66109e8b5839d8e66f42f8c927c24/sphinx/builders/html/__init__.py#L1003-L1010) function to check whether the current page refers to a physical (vs autogenerated) document. A can't comment on whether their interpretation of the purpose of `hasdoc` is correct since there is very little documentation on it, but that's the status quo.
The [relevant section](https://github.com/readthedocs/sphinx_rtd_theme/blob/671703c3fbb5629fb5004afc941b59310976c28b/sphinx_rtd_theme/breadcrumbs.html#L33-L39) in sphinx_rtd_theme's page generation template checks for `hasdoc(page) and display_github`.
A user can therefore fix this problem in their `conf.py` by setting `display_github = False` for automod generated pages like this:
```python
def setup(app):
app.connect("html-page-context", html_page_context)
def html_page_context(app, pagename, templatename, context, doctree):
"""Avoid broken source links to auto-generated API-docs."""
if pagename.startswith('automod/'):
context['display_github'] = False
context['meta'] = None
```
The `context['meta'] = None` assignment is needed to prevent an [automatic setting of `display_github = True`](https://github.com/readthedocs/sphinx_rtd_theme/blob/671703c3fbb5629fb5004afc941b59310976c28b/sphinx_rtd_theme/breadcrumbs.html#L1-L9).
Alternatively, one could override `hasdoc` similar to this (untested):
```python
def html_page_context(app, pagename, templatename, context, doctree):
"""Avoid broken source links to auto-generated API-docs."""
orig_hasdoc = context['hasdoc']
context['hasdoc'] = lambda name: orig_hasdoc(name) and not name.startswith('automod/')
```
So possible fixes in sphinx-automodapi could amount to one of:
- add one of the above snippets to the documentation
- include equivalent of one of the above snippets in setup code
- make `hasdoc()` return False by preventing autogenerated pages from being included in `Builder.env.all_docs` (see first line of hasdoc)
Contributor guide
No contributing guide indexed for this repository
Research direction
Start by reviewing the generated-page setup and the Sphinx HTML page context, then compare them with the theme's hasdoc() and display_github checks described in the issue. Decide which proposed behavior belongs in sphinx-automodapi, and consider documentation or coverage for autogenerated pages; done means those pages no longer produce invalid Edit on GitHub links.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- documentation
- Issue type
- Bug
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 30/100