1
"""
2
Originally adapted from: https://github.com/mcmtroffaes/sphinxcontrib-bibtex
3
"""
4 1
import docutils
5 1
import sphinx.util
6

7 1
from ipypublish.sphinx.gls.cache import Cache
8 1
from ipypublish.sphinx.gls.bibgloss import latex_to_docutils
9

10 1
logger = sphinx.util.logging.getLogger(__name__)
11

12

13 1
def init_bibgloss_cache(app):
14
    """Create ``app.env.bibgloss_cache`` if it does not exist yet.
15
    :param app: The sphinx application.
16
    :type app: :class:`sphinx.application.Sphinx`
17
    """
18 1
    if not hasattr(app.env, "bibgloss_cache"):
19 1
        app.env.bibgloss_cache = Cache()
20

21

22 1
def purge_bibgloss_cache(app, env, docname):
23
    """Remove all information related to *docname* from the cache.
24
    :param app: The sphinx application.
25
    :type app: :class:`sphinx.application.Sphinx`
26
    :param env: The sphinx build environment.
27
    :type env: :class:`sphinx.environment.BuildEnvironment`
28
    """
29 1
    env.bibgloss_cache.purge(docname)
30

31

32 1
def process_citations(app, doctree, docname):
33
    """Replace labels of citation nodes by actual labels.
34
    :param app: The sphinx application.
35
    :type app: :class:`sphinx.application.Sphinx`
36
    :param doctree: The document tree.
37
    :type doctree: :class:`docutils.nodes.document`
38
    :param docname: The document name.
39
    :type docname: :class:`str`
40
    """
41
    # citation(rawsource='', *children, **attributes)
42 1
    for node in doctree.traverse(docutils.nodes.citation):
43 1
        if "bibglossary" not in node.attributes.get("classes", []):
44 0
            continue
45 1
        key = node[0].astext()
46 1
        try:
47 1
            label_str = app.env.bibgloss_cache.get_label_from_key(key)
48 0
        except KeyError:
49 0
            logger.warning(
50
                "could not relabel glossary item [%s]" % key,
51
                type="bibgloss",
52
                subtype="relabel",
53
            )
54
        else:
55 1
            if app.config.bibgloss_convert_latex:
56 1
                label = latex_to_docutils(label_str)
57
                # label(rawsource='', text='', *children, **attributes)
58 1
                node[0] = docutils.nodes.label("", "", *label.children[0])
59
            else:
60 0
                node[0] = docutils.nodes.label("", label_str)
61

62 1
            if sphinx.version_info >= (2,):
63
                # this creates a line break between the label and the definition
64 1
                node.insert(1, docutils.nodes.line())
65

66

67 1
def process_citation_references(app, doctree, docname):
68
    """Replace text of citation reference nodes by actual labels.
69
    :param app: The sphinx application.
70
    :type app: :class:`sphinx.application.Sphinx`
71
    :param doctree: The document tree.
72
    :type doctree: :class:`docutils.nodes.document`
73
    :param docname: The document name.
74
    :type docname: :class:`str`
75
    """
76
    # sphinx has already turned citation_reference nodes
77
    # into reference nodes, so iterate over reference nodes
78
    # reference(rawsource='', text='', *children, **attributes)
79 1
    for node in doctree.traverse(docutils.nodes.reference):
80 1
        if "bibglossary" not in node.attributes.get("classes", []):
81 0
            continue
82 1
        text = node[0].astext()
83 1
        key = text[1:-1]
84 1
        try:
85 1
            if "bibgplural" in node.attributes.get("classes", []):
86 1
                label = app.env.bibgloss_cache.get_plural_from_key(key)
87
            else:
88 1
                label = app.env.bibgloss_cache.get_label_from_key(key)
89 0
        except KeyError:
90
            pass
91 0
            logger.warning(
92
                "could not relabel glossary reference [%s]" % key,
93
                type="bibgloss",
94
                subtype="relabel",
95
            )
96
        else:
97 1
            if "bibgcapital" in node.attributes.get("classes", []):
98 1
                label = label.capitalize()
99

100 1
            if app.config.bibgloss_convert_latex:
101
                # note we use only the first child of the document
102
                # TODO is this the best way to do this
103 1
                label = latex_to_docutils(label)
104 1
                if sphinx.version_info < (2,):
105 0
                    node[0] = label.children[0]
106
                else:
107
                    # in sphinx v2 the child is a paragraph
108 1
                    node[0] = label.children[0].children[0]
109
            else:
110 0
                node[0] = docutils.nodes.Text(label)

Read our documentation on viewing source code .

Loading