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
|
# 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/>.
from __future__ import print_function
import re
import json
class WikiNodeEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj,WikiNode):
return obj.jsonEncode()
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):
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 jsonEncode(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):
pass
class WikiContentNode(WikiNode):
content = None
def format(self):
pass
@jsonencoder
def jsonEncode(self):
ret = {}
if self.content:
if self.type == 'TEXT':
ret['content'] = self.content
elif isinstance(self.content,list):
ret['content'] = map(lambda x: x.jsonEncode(), self.content)
elif isinstance(self.content,WikiNode):
ret['content'] = self.content.jsonEncode()
else:
ret['content'] = self.content
else:
ret['content'] = None
return ret
class WikiSeqNode(WikiContentNode):
def format(self):
for x in self.content:
x.format()
@jsonencoder
def jsonEncode(self):
ret = {}
if not self.content:
ret['content'] = None
elif isinstance(self.content,list):
ret['content'] = map(lambda x: x.jsonEncode(), self.content)
elif isinstance(self.content,WikiNode):
ret['content'] = self.content.jsonEncode()
else:
ret['content'] = self.content
return ret
# ##############
class WikiTextNode(WikiContentNode):
type = 'TEXT'
@jsonencoder
def jsonEncode(self):
return {
'content': self.content
}
class WikiDelimNode(WikiContentNode):
type = 'DELIM'
isblock=False
continuation = False
class WikiTagNode(WikiContentNode):
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 jsonEncode(self):
return {
'tag': self.tag,
'isblock': self.isblock,
'args': self.args.tab if self.args else None,
'content': self.content.jsonEncode() if self.content else None,
'idx': self.idx
}
class WikiRefNode(WikiContentNode):
type = 'REF'
ref = None
@jsonencoder
def jsonEncode(self):
return {
'ref': self.ref,
'content': self.content.jsonEncode()
}
class WikiHdrNode(WikiContentNode):
type = 'HDR'
level = None
@jsonencoder
def jsonEncode(self):
return {
'level': self.level,
'content': self.content.jsonEncode()
}
class WikiEltNode(WikiContentNode):
type = 'ELT'
subtype = None
@jsonencoder
def jsonEncode(self):
return {
'subtype': self.subtype,
'content': self.content.jsonEncode()
}
class WikiEnvNode(WikiContentNode):
type = 'ENV'
envtype = None
level = None
@jsonencoder
def jsonEncode(self):
return {
'envtype': self.envtype,
'level': self.level,
'content': map(lambda x: x.jsonEncode(), self.content)
}
class WikiIndNode(WikiContentNode):
type = 'IND'
level = None
@jsonencoder
def jsonEncode(self):
return {
'level': self.level,
'content': self.content.jsonEncode()
}
|