summaryrefslogtreecommitdiff
path: root/WikiTrans/wikitoken.py
diff options
context:
space:
mode:
Diffstat (limited to 'WikiTrans/wikitoken.py')
-rw-r--r--WikiTrans/wikitoken.py166
1 files changed, 166 insertions, 0 deletions
diff --git a/WikiTrans/wikitoken.py b/WikiTrans/wikitoken.py
new file mode 100644
index 0000000..88c168e
--- /dev/null
+++ b/WikiTrans/wikitoken.py
@@ -0,0 +1,166 @@
+# Wiki tokens. -*- coding: utf-8 -*-
+# Copyright (C) 2015 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
+
+def mkstr(func):
+ def _indent(s, n=0):
+ return ' ' * n + s
+ def _do_str(self):
+ ret = _indent('NODE %s' % self.type)
+ s = func(self)
+ if s:
+ if not s.startswith('\n'):
+ ret += '\n'
+ ret += '\n'.join(map(lambda x: _indent(x, 1), s.split("\n")))
+ if not ret.endswith('\n'):
+ ret += '\n'
+ return ret + _indent('END NODE %s' % self.type)
+ return _do_str
+
+def mkcontentstr(func):
+ def _indent(s, n=0):
+ return ' ' * n + s
+ def _do_str(self):
+ ret = _indent('NODE %s' % self.type)
+ s = func(self)
+ if s:
+ if not s.startswith('\n'):
+ ret += '\n'
+ ret += '\n'.join(map(lambda x: _indent(x,1), s.split("\n")))
+ if not ret.endswith('\n'):
+ ret += '\n'
+ if self.content:
+ ret += _indent('CONTENT BEGIN\n',1)
+ if isinstance(self.content,list):
+ ret += ',\n'.join(map(lambda x: re.sub('^', _indent('',2), str(x), 0, re.MULTILINE), self.content))
+ else:
+ ret += '\n'.join(map(lambda x: _indent(x, 2), str(self.content).split("\n")))
+ if not ret.endswith('\n'):
+ ret += '\n'
+ ret += _indent('CONTENT END\n',1)
+ return ret + _indent('END NODE %s' % self.type)
+ return _do_str
+
+class WikiNode(object):
+ type = 'UNDEF'
+ nesting = 0
+
+ def __init__(self, **kwargs):
+ 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))
+
+ @mkstr
+ def __str__(self):
+ s = ''
+ for x in dir(self):
+ if x == 'type' or x.startswith('_') or type(x) == 'function':
+ continue
+ if x in self.__dict__:
+ s += '\n%s=%s' % (x, self.__dict__[x])
+ return s
+
+class WikiContentNode(WikiNode):
+ content = None
+ @mkcontentstr
+ def __str__(self):
+ pass
+
+class WikiSeqNode(WikiContentNode):
+ pass
+
+# ##############
+
+class WikiTextNode(WikiContentNode):
+ type = 'TEXT'
+ @mkstr
+ def __str__(self):
+ return '\nCONTENT: "%s"' % self.content
+
+class WikiDelimNode(WikiContentNode):
+ type = 'DELIM'
+ isblock=False
+ continuation = False
+ @mkstr
+ def __str__(self):
+ s = '\nPLACEMENT: %s' % ('BLOCK' if self.isblock else 'INLINE')
+ if self.content:
+ s += '\nCONTENT: "%s"' % self.content
+ if self.continuation:
+ s += '\nCONTINUATION'
+ return s
+
+class WikiTagNode(WikiContentNode):
+ tag = None
+ isblock = False
+ args = None
+ @mkcontentstr
+ def __str__(self):
+ s = '\nTAG: %s' % self.tag
+ s += '\nPLACEMENT: %s' % ('BLOCK' if self.isblock else 'INLINE')
+ if self.args:
+ s += '\nARGS: %s' % str(self.args)
+ return s
+
+class WikiRefNode(WikiContentNode):
+ type = 'REF'
+ ref = None
+ @mkcontentstr
+ def __str__(self):
+ s = '\nREF: %s' % self.ref
+ return s
+
+class WikiHdrNode(WikiContentNode):
+ type = 'HDR'
+ level = None
+ @mkcontentstr
+ def __str__(self):
+ s = '\nLEVEL: %s' % self.level
+ return s
+
+class WikiEltNode(WikiContentNode):
+ type = 'ELT'
+ subtype = None
+ @mkcontentstr
+ def __str__(self):
+ s = '\nSUBTYPE: %s' % self.subtype
+ return s
+
+class WikiEnvNode(WikiContentNode):
+ type = 'ENV'
+ envtype = None
+ level = None
+ @mkcontentstr
+ def __str__(self):
+ s = '\nLEVEL: %s' % self.level
+ s += '\nENVTYPE: %s' % self.envtype
+ return s
+
+class WikiIndNode(WikiContentNode):
+ type = 'IND'
+ level = None
+ @mkcontentstr
+ def __str__(self):
+ return '\nLEVEL: %s' % self.level
+
+class WikiLinkNode(WikiContentNode):
+ type = 'LINK'
+
+

Return to:

Send suggestions and report system problems to the System administrator.