mirror of
https://github.com/vyos/vyos-documentation.git
synced 2025-10-26 08:41:46 +01:00
Merge pull request #549 from rebortg/autosectionlabel
autosectionlabel: downgrade script from sphinx 1.8.4
This commit is contained in:
commit
19f7b6bf14
@ -1,67 +1,64 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
"""
|
"""
|
||||||
sphinx.ext.autosectionlabel
|
sphinx.ext.autosectionlabel
|
||||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
Allow reference sections by :ref: role using its title.
|
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.
|
: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 import nodes
|
||||||
from docutils.nodes import Node
|
|
||||||
|
|
||||||
from sphinx.application import Sphinx
|
|
||||||
from sphinx.domains.std import StandardDomain
|
|
||||||
from sphinx.locale import __
|
from sphinx.locale import __
|
||||||
from sphinx.util import logging
|
from sphinx.util import logging
|
||||||
from sphinx.util.nodes import clean_astext
|
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__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
if False:
|
||||||
def get_node_depth(node: Node) -> int:
|
# For type annotation
|
||||||
i = 0
|
from typing import Any, Dict # NOQA
|
||||||
cur_node = node
|
from sphinx.application import Sphinx # NOQA
|
||||||
while cur_node.parent != node.document:
|
|
||||||
cur_node = cur_node.parent
|
|
||||||
i += 1
|
|
||||||
return i
|
|
||||||
|
|
||||||
|
|
||||||
def register_sections_as_label(app: Sphinx, document: Node) -> None:
|
def register_sections_as_label(app, document):
|
||||||
domain = cast(StandardDomain, app.env.get_domain('std'))
|
# type: (Sphinx, nodes.Node) -> None
|
||||||
|
labels = app.env.domaindata['std']['labels']
|
||||||
|
anonlabels = app.env.domaindata['std']['anonlabels']
|
||||||
for node in document.traverse(nodes.section):
|
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]
|
labelid = node['ids'][0]
|
||||||
docname = app.env.docname
|
docname = app.env.docname
|
||||||
title = cast(nodes.title, node[0])
|
ref_name = getattr(node[0], 'rawsource', node[0].astext())
|
||||||
ref_name = getattr(title, 'rawsource', title.astext())
|
|
||||||
if app.config.autosectionlabel_prefix_document:
|
if app.config.autosectionlabel_prefix_document:
|
||||||
name = nodes.fully_normalize_name(docname + ':' + ref_name)
|
name = nodes.fully_normalize_name(docname + ':' + ref_name)
|
||||||
else:
|
else:
|
||||||
name = nodes.fully_normalize_name(ref_name)
|
name = nodes.fully_normalize_name(ref_name)
|
||||||
sectname = clean_astext(title)
|
sectname = clean_astext(node[0])
|
||||||
|
|
||||||
if name in domain.labels:
|
if name in labels:
|
||||||
# a ref befor a headline create 2 ids in the node object
|
|
||||||
if len(node['ids']) > 1:
|
if len(node['ids']) > 1:
|
||||||
continue
|
continue
|
||||||
logger.warning(__('duplicate label %s, other instance in %s'),
|
logger.warning(__('duplicate label %s, other instance in %s'),
|
||||||
name, app.env.doc2path(domain.labels[name][0]),
|
name, app.env.doc2path(labels[name][0]),
|
||||||
location=node, type='autosectionlabel', subtype=docname)
|
location=node)
|
||||||
|
|
||||||
|
anonlabels[name] = docname, labelid
|
||||||
|
labels[name] = docname, labelid, sectname
|
||||||
|
|
||||||
|
|
||||||
|
def setup(app):
|
||||||
domain.anonlabels[name] = docname, labelid
|
# type: (Sphinx) -> Dict[unicode, Any]
|
||||||
domain.labels[name] = docname, labelid, sectname
|
|
||||||
|
|
||||||
|
|
||||||
def setup(app: Sphinx) -> Dict[str, Any]:
|
|
||||||
app.add_config_value('autosectionlabel_prefix_document', False, 'env')
|
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)
|
app.connect('doctree-read', register_sections_as_label)
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user