7
class GetError(LookupError):
10
def get_fullname(node):
11
if isinstance(node, docutils.nodes.section):
12
return get_sectionname(node)
13
if isinstance(node, sphinx.addnodes.desc):
14
return get_descname(node)
15
raise TypeError("Unrecognized node type '%s'" % (node.__class__,))
17
def get_descname(desc):
21
raise GetError("No fullname: missing children in desc")
26
"No fullname: missing names attribute in desc's child")
30
raise GetError("No fullname: desc's child has empty names list")
32
def get_sectionname(section):
34
names = section['names']
36
raise GetError("No fullname: missing names attribute in section")
40
raise GetError("No fullname: section has empty names list")
43
return as_refuri(get_refid(node))
47
return get_ids(node)[0]
49
raise GetError("Node has emtpy ids list")
55
return NUMBERSIGN + refid
58
if isinstance(node, docutils.nodes.section):
62
raise GetError("No ids: section missing ids attribute")
63
if isinstance(node, sphinx.addnodes.desc):
67
raise GetError("No ids: missing desc children")
71
raise GetError("No ids: desc's child missing ids attribute")
72
raise TypeError("Unrecognized node type '%s'" % (node.__class__,))
74
def isections(doctree):
76
if isinstance(node, docutils.nodes.section):
79
def get_name(fullname):
80
return fullname.split('.')[-1]
83
return sys.exc_info()[1]
90
# Represent escaped bytes and strings in a portable way.
92
# as_bytes: Allow a Python 3.x string to represent a bytes object.
93
# e.g.: as_bytes("a\x01\b") == b"a\x01b" # Python 3.x
94
# as_bytes("a\x01\b") == "a\x01b" # Python 2.x
95
# as_unicode: Allow a Python "r" string to represent a unicode string.
96
# e.g.: as_unicode(r"Bo\u00F6tes") == u"Bo\u00F6tes" # Python 2.x
97
# as_unicode(r"Bo\u00F6tes") == "Bo\u00F6tes" # Python 3.x
98
if sys.version_info < (3,):
100
""" '<binary literal>' => '<binary literal>' """
103
def as_unicode(rstring):
104
""" r'<Unicode literal>' => u'<Unicode literal>' """
105
return rstring.decode('unicode_escape', 'strict')
108
def as_bytes(string):
109
""" '<binary literal>' => b'<binary literal>' """
110
return string.encode('latin-1', 'strict')
112
def as_unicode(rstring):
113
""" r'<Unicode literal>' => '<Unicode literal>' """
114
return rstring.encode('ascii', 'strict').decode('unicode_escape',
117
# Ensure Visitor is a new-style class
118
_SparseNodeVisitor = docutils.nodes.SparseNodeVisitor
119
if not hasattr(_SparseNodeVisitor, '__class__'):
120
class _SparseNodeVisitor(object, docutils.nodes.SparseNodeVisitor):
123
class Visitor(_SparseNodeVisitor):
125
skip_node = docutils.nodes.SkipNode()
126
skip_departure = docutils.nodes.SkipDeparture()
128
def __init__(self, app, document_node):
129
docutils.nodes.SparseNodeVisitor.__init__(self, document_node)
131
self.env = app.builder.env
133
def unknown_visit(self, node):
136
def unknown_departure(self, node):
139
EMPTYSTR = as_unicode('')
140
NUMBERSIGN = as_unicode('#')