diff options
Diffstat (limited to 'WikiTrans/wikitoken.py')
-rw-r--r-- | WikiTrans/wikitoken.py | 188 |
1 files changed, 0 insertions, 188 deletions
diff --git a/WikiTrans/wikitoken.py b/WikiTrans/wikitoken.py deleted file mode 100644 index 2238a66..0000000 --- a/WikiTrans/wikitoken.py +++ /dev/null @@ -1,188 +0,0 @@ -# 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() - } - - - |