summaryrefslogtreecommitdiff
path: root/sandbox/py-rest-doc/converter/docnodes.py
blob: 336499e9df7fbac3eb489402dd7e44bf8f191c86 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
# -*- coding: utf-8 -*-
"""
    Python documentation LaTeX parser - document nodes
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    :copyright: 2007 by Georg Brandl.
    :license: Python license.
"""


class DocNode(object):
    """ A node in the document tree. """
    def __repr__(self):
        return '%s()' % self.__class__.__name__

    def __str__(self):
        raise RuntimeError('cannot stringify docnodes')

    def walk(self):
        return []


class CommentNode(DocNode):
    """ A comment. """
    def __init__(self, comment):
        assert isinstance(comment, basestring)
        self.comment = comment

    def __repr__(self):
        return 'CommentNode(%r)' % self.comment


class RootNode(DocNode):
    """ A whole document. """
    def __init__(self, filename, children):
        self.filename = filename
        self.children = children
        self.params = {}
        self.labels = {}

    def __repr__(self):
        return 'RootNode(%r, %r)' % (self.filename, self.children)

    def walk(self):
        return self.children

    def transform(self):
        """ Do restructurings not possible during parsing. """
        def do_descenvs(node):
            r""" Make \xxxlines an attribute of the parent xxxdesc node. """
            for subnode in node.walk():
                do_descenvs(subnode)
            if isinstance(node, DescEnvironmentNode):
                for subnode in node.content.walk():
                    if isinstance(subnode, DescLineCommandNode):
                        node.additional.append((subnode.cmdname, subnode.args))

        do_descenvs(self)


class NodeList(DocNode, list):
    """ A list of subnodes. """
    def __init__(self, children=None):
        list.__init__(self, children or [])

    def __repr__(self):
        return 'NL%s' % list.__repr__(self)

    def walk(self):
        return self

    def append(self, node):
        assert isinstance(node, DocNode)
        if type(node) is EmptyNode:
            return
        elif self and isinstance(node, TextNode) and \
                 type(self[-1]) is TextNode:
            self[-1].text += node.text
        elif type(node) is NodeList:
            list.extend(self, node)
        elif type(node) is VerbatimNode and self and \
                 isinstance(self[-1], ParaSepNode):
            # don't allow a ParaSepNode before VerbatimNode
            # because this breaks ReST's '::'
            self[-1] = node
        else:
            list.append(self, node)

    def flatten(self):
        if len(self) > 1:
            return self
        elif len(self) == 1:
            return self[0]
        else:
            return EmptyNode()


class ParaSepNode(DocNode):
    """ A node for paragraph separator. """
    def __repr__(self):
        return 'Para'


class TextNode(DocNode):
    """ A node containing text. """
    def __init__(self, text):
        assert isinstance(text, basestring)
        self.text = text

    def __repr__(self):
        if type(self) is TextNode:
            return 'T%r' % self.text
        else:
            return '%s(%r)' % (self.__class__.__name__, self.text)


class EmptyNode(TextNode):
    """ An empty node. """
    def __init__(self, *args):
        self.text = ''


class NbspNode(TextNode):
    """ A non-breaking space. """
    def __init__(self, *args):
        # this breaks ReST markup (!)
        #self.text = u'\N{NO-BREAK SPACE}'
        self.text = ' '

    def __repr__(self):
        return 'NBSP'


simplecmd_mapping = {
    'ldots': u'...',
    'moreargs': '...',
    'unspecified': '...',
    'ASCII': 'ASCII',
    'UNIX': 'Unix',
    'Unix': 'Unix',
    'POSIX': 'POSIX',
    'LaTeX': 'LaTeX',
    'EOF': 'EOF',
    'Cpp': 'C++',
    'C': 'C',
    'sub': u'--> ',
    'textbackslash': '\\\\',
    'textunderscore': '_',
    'texteuro': u'\N{EURO SIGN}',
    'textasciicircum': u'^',
    'textasciitilde': u'~',
    'textgreater': '>',
    'textless': '<',
    'textbar': '|',
    'backslash': '\\\\',
    'tilde': '~',
    'copyright': u'\N{COPYRIGHT SIGN}',
    # \e is mostly inside \code and therefore not escaped.
    'e': '\\',
    'infinity': u'\N{INFINITY}',
    'plusminus': u'\N{PLUS-MINUS SIGN}',
    'leq': u'\N{LESS-THAN OR EQUAL TO}',
    'geq': u'\N{GREATER-THAN OR EQUAL TO}',
    'pi': u'\N{GREEK SMALL LETTER PI}',
    'AA': u'\N{LATIN CAPITAL LETTER A WITH RING ABOVE}',
}

class SimpleCmdNode(TextNode):
    """ A command resulting in simple text. """
    def __init__(self, cmdname, args):
        self.text = simplecmd_mapping[cmdname]


class BreakNode(DocNode):
    """ A line break. """
    def __repr__(self):
        return 'BR'


class CommandNode(DocNode):
    """ A general command. """
    def __init__(self, cmdname, args):
        self.cmdname = cmdname
        self.args = args

    def __repr__(self):
        return '%s(%r, %r)' % (self.__class__.__name__, self.cmdname, self.args)

    def walk(self):
        return self.args


class DescLineCommandNode(CommandNode):
    """ A \\xxxline command. """


class InlineNode(CommandNode):
    """ A node with inline markup. """
    def walk(self):
        return []


class IndexNode(InlineNode):
    """ An index-generating command. """
    def __init__(self, cmdname, args):
        self.cmdname = cmdname
        # tricky -- this is to make this silent in paragraphs
        # while still generating index entries for textonly()
        self.args = []
        self.indexargs = args


class SectioningNode(CommandNode):
    """ A heading node. """


class EnvironmentNode(DocNode):
    """ An environment. """
    def __init__(self, envname, args, content):
        self.envname = envname
        self.args = args
        self.content = content

    def __repr__(self):
        return 'EnvironmentNode(%r, %r, %r)' % (self.envname,
                                                self.args, self.content)

    def walk(self):
        return [self.content]


class DescEnvironmentNode(EnvironmentNode):
    """ An xxxdesc environment. """
    def __init__(self, envname, args, content):
        self.envname = envname
        self.args = args
        self.additional = []
        self.content = content

    def __repr__(self):
        return 'DescEnvironmentNode(%r, %r, %r)' % (self.envname,
                                                    self.args, self.content)


class TableNode(EnvironmentNode):
    def __init__(self, numcols, headings, lines):
        self.numcols = numcols
        self.headings = headings
        self.lines = lines

    def __repr__(self):
        return 'TableNode(%r, %r, %r)' % (self.numcols,
                                          self.headings, self.lines)

    def walk(self):
        return []


class VerbatimNode(DocNode):
    """ A verbatim code block. """
    def __init__(self, content):
        self.content = content

    def __repr__(self):
        return 'VerbatimNode(%r)' % self.content


class ListNode(DocNode):
    """ A list. """
    def __init__(self, items):
        self.items = items

    def __repr__(self):
        return '%s(%r)' % (self.__class__.__name__, self.items)

    def walk(self):
        return [item[1] for item in self.items]


class ItemizeNode(ListNode):
    """ An enumeration with bullets. """


class EnumerateNode(ListNode):
    """ An enumeration with numbers. """


class DescriptionNode(ListNode):
    """ A description list. """


class DefinitionsNode(ListNode):
    """ A definition list. """


class ProductionListNode(ListNode):
    """ A grammar production list. """