chrisjsewell / ipypublish
1 3
from sphinx.roles import XRefRole
2

3 3
from ipypublish.bib2glossary import BibGlossDB
4 3
from ipypublish.sphinx.gls.bibgloss import docutils_citation_ref_node
5

6

7 3
class GLSRole(XRefRole):
8
    """Class for processing the `gls` role."""
9

10 3
    def result_nodes(self, document, env, node, is_ref):
11
        """Transform reference node into a citation reference,
12
        and note that the reference was cited.
13
        """
14 3
        keys = node["reftarget"].split(",")
15
        # Note that at this point, usually, env.bibgloss_cache.bibfiles
16
        # is still empty because the bibliography directive may not
17
        # have been processed yet, so we cannot get the actual entry.
18
        # Instead, we simply fake an entry with the desired key, and
19
        # fix the label at doctree-resolved time. This happens in
20
        # process_citation_references.
21 3
        refnodes = [
22
            docutils_citation_ref_node(
23
                BibGlossDB.get_fake_entry_obj(key), document, classes=["bibglossary"]
24
            )
25
            for key in keys
26
        ]
27 3
        for key in keys:
28 3
            env.bibgloss_cache.add_cited(key, env.docname)
29 3
        return refnodes, []
30

31

32 3
class GLSCapitalRole(XRefRole):
33
    """Class for processing the `glsc` role."""
34

35 3
    def result_nodes(self, document, env, node, is_ref):
36
        """Transform reference node into a citation reference,
37
        and note that the reference was cited.
38
        """
39 3
        keys = node["reftarget"].split(",")
40
        # Note that at this point, usually, env.bibgloss_cache.bibfiles
41
        # is still empty because the bibliography directive may not
42
        # have been processed yet, so we cannot get the actual entry.
43
        # Instead, we simply fake an entry with the desired key, and
44
        # fix the label at doctree-resolved time. This happens in
45
        # process_citation_references.
46 3
        refnodes = [
47
            docutils_citation_ref_node(
48
                BibGlossDB.get_fake_entry_obj(key),
49
                document,
50
                classes=["bibglossary", "bibgcapital"],
51
            )
52
            for key in keys
53
        ]
54 3
        for key in keys:
55 3
            env.bibgloss_cache.add_cited(key, env.docname)
56 3
        return refnodes, []
57

58

59 3
class GLSPluralRole(XRefRole):
60
    """Class for processing the `glspl` role."""
61

62 3
    def result_nodes(self, document, env, node, is_ref):
63
        """Transform reference node into a citation reference,
64
        and note that the reference was cited.
65
        """
66 3
        keys = node["reftarget"].split(",")
67
        # Note that at this point, usually, env.bibgloss_cache.bibfiles
68
        # is still empty because the bibliography directive may not
69
        # have been processed yet, so we cannot get the actual entry.
70
        # Instead, we simply fake an entry with the desired key, and
71
        # fix the label at doctree-resolved time. This happens in
72
        # process_citation_references.
73 3
        refnodes = [
74
            docutils_citation_ref_node(
75
                BibGlossDB.get_fake_entry_obj(key),
76
                document,
77
                classes=["bibglossary", "bibgplural"],
78
            )
79
            for key in keys
80
        ]
81 3
        for key in keys:
82 3
            env.bibgloss_cache.add_cited(key, env.docname)
83 3
        return refnodes, []
84

85

86 3
class GLSPluralCapitalRole(XRefRole):
87
    """Class for processing the `glscpl` role."""
88

89 3
    def result_nodes(self, document, env, node, is_ref):
90
        """Transform reference node into a citation reference,
91
        and note that the reference was cited.
92
        """
93 3
        keys = node["reftarget"].split(",")
94
        # Note that at this point, usually, env.bibgloss_cache.bibfiles
95
        # is still empty because the bibliography directive may not
96
        # have been processed yet, so we cannot get the actual entry.
97
        # Instead, we simply fake an entry with the desired key, and
98
        # fix the label at doctree-resolved time. This happens in
99
        # process_citation_references.
100 3
        refnodes = [
101
            docutils_citation_ref_node(
102
                BibGlossDB.get_fake_entry_obj(key),
103
                document,
104
                classes=["bibglossary", "bibgplural", "bibgcapital"],
105
            )
106
            for key in keys
107
        ]
108 3
        for key in keys:
109 3
            env.bibgloss_cache.add_cited(key, env.docname)
110 3
        return refnodes, []

Read our documentation on viewing source code .

Loading