mirror of
https://github.com/sphinx-doc/sphinx.git
synced 2025-02-25 18:55:22 -06:00
Enable automatic formatting for `sphinx/ext/viewcode.py
`
This commit is contained in:
parent
e66c81b01d
commit
22562d40ee
@ -415,5 +415,4 @@ exclude = [
|
||||
"sphinx/domains/python/_object.py",
|
||||
"sphinx/domains/rst.py",
|
||||
"sphinx/domains/std/__init__.py",
|
||||
"sphinx/ext/viewcode.py",
|
||||
]
|
||||
|
@ -23,7 +23,7 @@ from sphinx.util.nodes import make_refnode
|
||||
from sphinx.util.osutil import _last_modified_time
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from collections.abc import Iterable, Iterator
|
||||
from collections.abc import Iterator, Set
|
||||
|
||||
from sphinx.application import Sphinx
|
||||
from sphinx.builders import Builder
|
||||
@ -79,11 +79,11 @@ def _get_full_modname(modname: str, attribute: str) -> str | None:
|
||||
|
||||
|
||||
def is_supported_builder(builder: Builder) -> bool:
|
||||
if builder.format != 'html':
|
||||
return False
|
||||
if builder.name == 'singlehtml':
|
||||
return False
|
||||
return not (builder.name.startswith('epub') and not builder.config.viewcode_enable_epub)
|
||||
return (
|
||||
builder.format == 'html'
|
||||
and builder.name != 'singlehtml'
|
||||
and (not builder.name.startswith('epub') or builder.config.viewcode_enable_epub)
|
||||
)
|
||||
|
||||
|
||||
def doctree_read(app: Sphinx, doctree: Node) -> None:
|
||||
@ -132,7 +132,7 @@ def doctree_read(app: Sphinx, doctree: Node) -> None:
|
||||
refname = modname
|
||||
if env.config.viewcode_follow_imported_members:
|
||||
new_modname = app.emit_firstresult(
|
||||
'viewcode-follow-imported', modname, fullname,
|
||||
'viewcode-follow-imported', modname, fullname
|
||||
)
|
||||
if not new_modname:
|
||||
new_modname = _get_full_modname(modname, fullname)
|
||||
@ -147,11 +147,14 @@ def doctree_read(app: Sphinx, doctree: Node) -> None:
|
||||
continue
|
||||
names.add(fullname)
|
||||
pagename = posixpath.join(OUTPUT_DIRNAME, modname.replace('.', '/'))
|
||||
signode += viewcode_anchor(reftarget=pagename, refid=fullname, refdoc=env.docname)
|
||||
signode += viewcode_anchor(
|
||||
reftarget=pagename, refid=fullname, refdoc=env.docname
|
||||
)
|
||||
|
||||
|
||||
def env_merge_info(app: Sphinx, env: BuildEnvironment, docnames: Iterable[str],
|
||||
other: BuildEnvironment) -> None:
|
||||
def env_merge_info(
|
||||
app: Sphinx, env: BuildEnvironment, docnames: Set[str], other: BuildEnvironment
|
||||
) -> None:
|
||||
if not hasattr(other, '_viewcode_modules'):
|
||||
return
|
||||
# create a _viewcode_modules dict on the main environment
|
||||
@ -199,8 +202,13 @@ class ViewcodeAnchorTransform(SphinxPostTransform):
|
||||
def convert_viewcode_anchors(self) -> None:
|
||||
for node in self.document.findall(viewcode_anchor):
|
||||
anchor = nodes.inline('', _('[source]'), classes=['viewcode-link'])
|
||||
refnode = make_refnode(self.app.builder, node['refdoc'], node['reftarget'],
|
||||
node['refid'], anchor)
|
||||
refnode = make_refnode(
|
||||
self.app.builder,
|
||||
node['refdoc'],
|
||||
node['reftarget'],
|
||||
node['refid'],
|
||||
anchor,
|
||||
)
|
||||
node.replace_self(refnode)
|
||||
|
||||
def remove_viewcode_anchors(self) -> None:
|
||||
@ -254,10 +262,13 @@ def collect_pages(app: Sphinx) -> Iterator[tuple[str, dict[str, Any], str]]:
|
||||
modnames = set(env._viewcode_modules)
|
||||
|
||||
for modname, entry in status_iterator(
|
||||
sorted(env._viewcode_modules.items()),
|
||||
__('highlighting module code... '), "blue",
|
||||
len(env._viewcode_modules),
|
||||
app.verbosity, operator.itemgetter(0)):
|
||||
sorted(env._viewcode_modules.items()),
|
||||
__('highlighting module code... '),
|
||||
'blue',
|
||||
len(env._viewcode_modules),
|
||||
app.verbosity,
|
||||
operator.itemgetter(0),
|
||||
):
|
||||
if not entry:
|
||||
continue
|
||||
if not should_generate_module_page(app, modname):
|
||||
@ -287,9 +298,11 @@ def collect_pages(app: Sphinx) -> Iterator[tuple[str, dict[str, Any], str]]:
|
||||
for name, docname in used.items():
|
||||
type, start, end = tags[name]
|
||||
backlink = urito(pagename, docname) + '#' + refname + '.' + name
|
||||
lines[start] = (f'<div class="viewcode-block" id="{name}">\n'
|
||||
f'<a class="viewcode-back" href="{backlink}">{link_text}</a>\n'
|
||||
+ lines[start])
|
||||
lines[start] = (
|
||||
f'<div class="viewcode-block" id="{name}">\n'
|
||||
f'<a class="viewcode-back" href="{backlink}">{link_text}</a>\n'
|
||||
+ lines[start]
|
||||
)
|
||||
lines[min(end, max_index)] += '</div>\n'
|
||||
|
||||
# try to find parents (for submodules)
|
||||
@ -299,18 +312,22 @@ def collect_pages(app: Sphinx) -> Iterator[tuple[str, dict[str, Any], str]]:
|
||||
parent = parent.rsplit('.', 1)[0]
|
||||
if parent in modnames:
|
||||
parents.append({
|
||||
'link': urito(pagename,
|
||||
posixpath.join(OUTPUT_DIRNAME, parent.replace('.', '/'))),
|
||||
'title': parent})
|
||||
parents.append({'link': urito(pagename, posixpath.join(OUTPUT_DIRNAME, 'index')),
|
||||
'title': _('Module code')})
|
||||
'link': urito(
|
||||
pagename,
|
||||
posixpath.join(OUTPUT_DIRNAME, parent.replace('.', '/')),
|
||||
),
|
||||
'title': parent,
|
||||
})
|
||||
parents.append({
|
||||
'link': urito(pagename, posixpath.join(OUTPUT_DIRNAME, 'index')),
|
||||
'title': _('Module code'),
|
||||
})
|
||||
parents.reverse()
|
||||
# putting it all together
|
||||
context = {
|
||||
'parents': parents,
|
||||
'title': modname,
|
||||
'body': (_('<h1>Source code for %s</h1>') % modname +
|
||||
'\n'.join(lines)),
|
||||
'body': (_('<h1>Source code for %s</h1>') % modname + '\n'.join(lines)),
|
||||
}
|
||||
yield pagename, context, 'page.html'
|
||||
|
||||
@ -330,14 +347,15 @@ def collect_pages(app: Sphinx) -> Iterator[tuple[str, dict[str, Any], str]]:
|
||||
stack.pop()
|
||||
html.append('</ul>')
|
||||
stack.append(modname + '.')
|
||||
relative_uri = urito(posixpath.join(OUTPUT_DIRNAME, 'index'),
|
||||
posixpath.join(OUTPUT_DIRNAME, modname.replace('.', '/')))
|
||||
relative_uri = urito(
|
||||
posixpath.join(OUTPUT_DIRNAME, 'index'),
|
||||
posixpath.join(OUTPUT_DIRNAME, modname.replace('.', '/')),
|
||||
)
|
||||
html.append(f'<li><a href="{relative_uri}">{modname}</a></li>\n')
|
||||
html.append('</ul>' * (len(stack) - 1))
|
||||
context = {
|
||||
'title': _('Overview: module code'),
|
||||
'body': (_('<h1>All modules for which code is available</h1>') +
|
||||
''.join(html)),
|
||||
'body': (_('<h1>All modules for which code is available</h1>') + ''.join(html)),
|
||||
}
|
||||
|
||||
yield posixpath.join(OUTPUT_DIRNAME, 'index'), context, 'page.html'
|
||||
|
Loading…
Reference in New Issue
Block a user