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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
|
# Wiki tokens. -*- coding: utf-8 -*-
# Copyright (C) 2015-2018 Sergey Poznyakoff
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
Wiki markup tokens and associated classes.
This module defines classes for the basic nodes of the Wiki markup parse tree:
WikiNode -- Abstract parse tree node.
WikiContentNode -- A node associated with some content.
WikiSeqNode -- A sequence of nodes.
WikiTextNode -- Textual content.
WikiDelimNode -- Delimiter.
WikiTagNode -- Tag (e.g. <tt>, </tt>, <tt />, etc.)
WikiRefNode -- Wiki reference (e.g. [target|name])
WikiHdrNode -- Heading (e.g. == Section ==)
WikiEltNode -- Environment element.
WikiEnvNode -- Environment (numbered or unnumbered list, definition, etc.)
WikiIndNode -- Indent node.
Auxiliary classes:
WikiNodeEncoder -- Custom JSONEncoder subclass for serializing objects of the
above classes.
"""
from __future__ import print_function
import re
import json
class WikiNodeEncoder(json.JSONEncoder):
"""Custom JSONEncoder subclass for serializing WikiNode and its subclasses."""
def default(self, obj):
if isinstance(obj, WikiNode):
return obj.json_encode()
return json.JSONEncoder.default(self, obj)
def jsonencoder(func):
def _mkencoder(self):
json = func(self)
json['wikinode'] = self.__class__.__name__
json['type'] = self.type
return json
return _mkencoder
class WikiNode(object):
"""Generic parse tree node.
Attributes:
type -- actual type of this object (string)
parser -- parser instance that owns this node
"""
type = 'UNDEF'
parser = None
def __init__(self, parser, **kwargs):
self.parser = parser
for key in kwargs:
if hasattr(self, key):
self.__dict__[key] = kwargs[key]
else:
raise AttributeError("'%s' has no attribute '%s'" % (self.__class__.__name__, key))
def __str__(self):
return json.dumps(self, cls=WikiNodeEncoder, sort_keys=True)
@jsonencoder
def json_encode(self):
ret = {}
for x in dir(self):
if x == 'parser' or x.startswith('_') or type(x) == 'function':
continue
if x in self.__dict__:
ret[x] = self.__dict__[x]
return ret
def format(self):
"""Abstract formatting function.
Derived classes must override it.
"""
pass
class WikiContentNode(WikiNode):
"""Generic content node.
Attributes:
content -- Actual content
"""
content = None
def format(self):
pass
@jsonencoder
def json_encode(self):
ret = {}
if self.content:
if self.type == 'TEXT':
ret['content'] = self.content
elif isinstance(self.content, list):
ret['content'] = map(lambda x: x.json_encode(), self.content)
elif isinstance(self.content, WikiNode):
ret['content'] = self.content.json_encode()
else:
ret['content'] = self.content
else:
ret['content'] = None
return ret
class WikiSeqNode(WikiContentNode):
"""Generic sequence of nodes.
Attributes:
content -- list of nodes.
"""
def format(self):
for x in self.content:
x.format()
@jsonencoder
def json_encode(self):
ret = {}
if not self.content:
ret['content'] = None
elif isinstance(self.content, list):
ret['content'] = map(lambda x: x.json_encode(), self.content)
elif isinstance(self.content, WikiNode):
ret['content'] = self.content.json_encode()
else:
ret['content'] = self.content
return ret
# ##############
class WikiTextNode(WikiContentNode):
"""Text node.
Attributes:
type -- 'TEXT'
content -- string
"""
type = 'TEXT'
@jsonencoder
def json_encode(self):
return {
'content': self.content
}
class WikiDelimNode(WikiContentNode):
"""Delimiter node.
Attributes:
type -- 'DELIM'
content -- actual delimiter string
isblock -- boolean indicating whether it is a block delimiter
continuation -- True if continuation is expected
"""
type = 'DELIM'
isblock=False
continuation = False
class WikiTagNode(WikiContentNode):
"""A Wiki tag.
Attributes:
tag -- actual tag name (with '<', '>', and eventual '/' stripped)
isblock -- True if this is a block tag
args -- List of tag arguments
idx -- If this is a "see also" reference, index of this ref in the
list of references.
FIXME: Perhaps this merits a subclass?
"""
tag = None
isblock = False
args = None
idx = None
def __init__(self, *args, **keywords):
super(WikiTagNode, self).__init__(*args, **keywords)
if (self.type == 'TAG'
and self.tag == 'ref'
and hasattr(self.parser, 'references')):
self.idx = len(self.parser.references)
self.parser.references.append(self)
@jsonencoder
def json_encode(self):
return {
'tag': self.tag,
'isblock': self.isblock,
'args': self.args.tab if self.args else None,
'content': self.content.json_encode() if self.content else None,
'idx': self.idx
}
class WikiRefNode(WikiContentNode):
"""Reference node.
This class represents a wiki reference, such as "[ref|content]".
Attributes:
ref -- actual reference
content -- content string
"""
type = 'REF'
ref = None
@jsonencoder
def json_encode(self):
return {
'ref': self.ref,
'content': self.content.json_encode()
}
class WikiHdrNode(WikiContentNode):
"""A wiki markup header class.
Attributes:
level -- header level
content -- header content (WikiNode subclass object)
"""
type = 'HDR'
level = None
@jsonencoder
def json_encode(self):
return {
'level': self.level,
'content': self.content.json_encode()
}
class WikiEltNode(WikiContentNode):
"""Environment element node.
Attributes:
subtype -- type of the environment (numbered, unnumbered, defn)
content -- content of the element (WikiNode subclass object)
"""
type = 'ELT'
subtype = None
@jsonencoder
def json_encode(self):
return {
'subtype': self.subtype,
'content': self.content.json_encode()
}
class WikiEnvNode(WikiContentNode):
"""Wiki Environment Node
Attributes:
envtype -- type of the environment (numbered, unnumbered, defn)
level -- nesting level of the environment
"""
type = 'ENV'
envtype = None
level = None
@jsonencoder
def json_encode(self):
return {
'envtype': self.envtype,
'level': self.level,
'content': map(lambda x: x.json_encode(), self.content)
}
class WikiIndNode(WikiContentNode):
"""Indented block node.
Attributes:
level -- Indentation level.
content -- Indented content (WikiNode subclass object).
"""
type = 'IND'
level = None
@jsonencoder
def json_encode(self):
return {
'level': self.level,
'content': self.content.json_encode()
}
|