summaryrefslogtreecommitdiffabout
path: root/wikitrans/wikitoken.py
Side-by-side diff
Diffstat (limited to 'wikitrans/wikitoken.py') (more/less context) (ignore whitespace changes)
-rw-r--r--wikitrans/wikitoken.py72
1 files changed, 44 insertions, 28 deletions
diff --git a/wikitrans/wikitoken.py b/wikitrans/wikitoken.py
index 49c6c68..0678a75 100644
--- a/wikitrans/wikitoken.py
+++ b/wikitrans/wikitoken.py
@@ -41,21 +41,26 @@ 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.jsonEncode()
+ 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.
@@ -71,7 +76,7 @@ class WikiNode(object):
def __init__(self, parser, **kwargs):
self.parser = parser
for key in kwargs:
- if hasattr(self,key):
+ if hasattr(self, key):
self.__dict__[key] = kwargs[key]
else:
raise AttributeError("'%s' has no attribute '%s'" % (self.__class__.__name__, key))
@@ -80,7 +85,7 @@ class WikiNode(object):
return json.dumps(self, cls=WikiNodeEncoder, sort_keys=True)
@jsonencoder
- def jsonEncode(self):
+ def json_encode(self):
ret = {}
for x in dir(self):
if x == 'parser' or x.startswith('_') or type(x) == 'function':
@@ -96,6 +101,7 @@ class WikiNode(object):
"""
pass
+
class WikiContentNode(WikiNode):
"""Generic content node.
@@ -110,21 +116,22 @@ class WikiContentNode(WikiNode):
pass
@jsonencoder
- def jsonEncode(self):
+ 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.jsonEncode(), self.content)
- elif isinstance(self.content,WikiNode):
- ret['content'] = self.content.jsonEncode()
+ 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.
@@ -138,14 +145,14 @@ class WikiSeqNode(WikiContentNode):
x.format()
@jsonencoder
- def jsonEncode(self):
+ def json_encode(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()
+ 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
@@ -165,11 +172,12 @@ class WikiTextNode(WikiContentNode):
type = 'TEXT'
@jsonencoder
- def jsonEncode(self):
+ def json_encode(self):
return {
'content': self.content
}
+
class WikiDelimNode(WikiContentNode):
"""Delimiter node.
@@ -185,6 +193,7 @@ class WikiDelimNode(WikiContentNode):
isblock=False
continuation = False
+
class WikiTagNode(WikiContentNode):
"""A Wiki tag.
@@ -205,20 +214,23 @@ class WikiTagNode(WikiContentNode):
def __init__(self, *args, **keywords):
super(WikiTagNode, self).__init__(*args, **keywords)
- if self.type == 'TAG' and self.tag == 'ref' and hasattr(self.parser,'references'):
+ 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):
+ def json_encode(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,
+ 'content': self.content.json_encode() if self.content else None,
'idx': self.idx
}
+
class WikiRefNode(WikiContentNode):
"""Reference node.
@@ -233,12 +245,13 @@ class WikiRefNode(WikiContentNode):
type = 'REF'
ref = None
@jsonencoder
- def jsonEncode(self):
+ def json_encode(self):
return {
'ref': self.ref,
- 'content': self.content.jsonEncode()
+ 'content': self.content.json_encode()
}
+
class WikiHdrNode(WikiContentNode):
"""A wiki markup header class.
@@ -252,12 +265,13 @@ class WikiHdrNode(WikiContentNode):
level = None
@jsonencoder
- def jsonEncode(self):
+ def json_encode(self):
return {
'level': self.level,
- 'content': self.content.jsonEncode()
+ 'content': self.content.json_encode()
}
+
class WikiEltNode(WikiContentNode):
"""Environment element node.
@@ -271,12 +285,13 @@ class WikiEltNode(WikiContentNode):
subtype = None
@jsonencoder
- def jsonEncode(self):
+ def json_encode(self):
return {
'subtype': self.subtype,
- 'content': self.content.jsonEncode()
+ 'content': self.content.json_encode()
}
+
class WikiEnvNode(WikiContentNode):
"""Wiki Environment Node
@@ -291,13 +306,14 @@ class WikiEnvNode(WikiContentNode):
level = None
@jsonencoder
- def jsonEncode(self):
+ def json_encode(self):
return {
'envtype': self.envtype,
'level': self.level,
- 'content': map(lambda x: x.jsonEncode(), self.content)
+ 'content': map(lambda x: x.json_encode(), self.content)
}
+
class WikiIndNode(WikiContentNode):
"""Indented block node.
@@ -311,8 +327,8 @@ class WikiIndNode(WikiContentNode):
level = None
@jsonencoder
- def jsonEncode(self):
+ def json_encode(self):
return {
'level': self.level,
- 'content': self.content.jsonEncode()
+ 'content': self.content.json_encode()
}

Return to:

Send suggestions and report system problems to the System administrator.