Merge pull request #549 from rebortg/autosectionlabel

autosectionlabel: downgrade script from sphinx 1.8.4
This commit is contained in:
Robert Göhler 2021-06-14 21:17:12 +02:00 committed by GitHub
commit 19f7b6bf14
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,67 +1,64 @@
# -*- coding: utf-8 -*-
"""
sphinx.ext.autosectionlabel
~~~~~~~~~~~~~~~~~~~~~~~~~~~
Allow reference sections by :ref: role using its title.
:copyright: Copyright 2007-2021 by the Sphinx team, see AUTHORS.
:copyright: Copyright 2007-2019 by the Sphinx team, see AUTHORS.
:license: BSD, see LICENSE for details.
from sphinx version 1.8.4 to get readthedocs working
"""
from typing import Any, Dict, cast
from docutils import nodes
from docutils.nodes import Node
from sphinx.application import Sphinx
from sphinx.domains.std import StandardDomain
from sphinx.locale import __
from sphinx.util import logging
from sphinx.util.nodes import clean_astext
if False:
# For type annotation
from typing import Any, Dict # NOQA
from sphinx.application import Sphinx # NOQA
logger = logging.getLogger(__name__)
def get_node_depth(node: Node) -> int:
i = 0
cur_node = node
while cur_node.parent != node.document:
cur_node = cur_node.parent
i += 1
return i
if False:
# For type annotation
from typing import Any, Dict # NOQA
from sphinx.application import Sphinx # NOQA
def register_sections_as_label(app: Sphinx, document: Node) -> None:
domain = cast(StandardDomain, app.env.get_domain('std'))
def register_sections_as_label(app, document):
# type: (Sphinx, nodes.Node) -> None
labels = app.env.domaindata['std']['labels']
anonlabels = app.env.domaindata['std']['anonlabels']
for node in document.traverse(nodes.section):
if (app.config.autosectionlabel_maxdepth and
get_node_depth(node) >= app.config.autosectionlabel_maxdepth):
continue
labelid = node['ids'][0]
docname = app.env.docname
title = cast(nodes.title, node[0])
ref_name = getattr(title, 'rawsource', title.astext())
ref_name = getattr(node[0], 'rawsource', node[0].astext())
if app.config.autosectionlabel_prefix_document:
name = nodes.fully_normalize_name(docname + ':' + ref_name)
else:
name = nodes.fully_normalize_name(ref_name)
sectname = clean_astext(title)
sectname = clean_astext(node[0])
if name in domain.labels:
# a ref befor a headline create 2 ids in the node object
if name in labels:
if len(node['ids']) > 1:
continue
logger.warning(__('duplicate label %s, other instance in %s'),
name, app.env.doc2path(domain.labels[name][0]),
location=node, type='autosectionlabel', subtype=docname)
name, app.env.doc2path(labels[name][0]),
location=node)
anonlabels[name] = docname, labelid
labels[name] = docname, labelid, sectname
domain.anonlabels[name] = docname, labelid
domain.labels[name] = docname, labelid, sectname
def setup(app: Sphinx) -> Dict[str, Any]:
def setup(app):
# type: (Sphinx) -> Dict[unicode, Any]
app.add_config_value('autosectionlabel_prefix_document', False, 'env')
app.add_config_value('autosectionlabel_maxdepth', None, 'env')
app.connect('doctree-read', register_sections_as_label)
return {