Commits

Roger Haase committed 3de9843

eliminate redundant code by moving common converter classes to _util.py

Comments (0)

Files changed (4)

MoinMoin/converter/_util.py

 from MoinMoin.config import uri_schemes
 from MoinMoin.util.iri import Iri
 from MoinMoin.util.mime import Type
+from MoinMoin.util.tree import moin_page
 
 def allowed_uri_scheme(uri):
     parsed = Iri(uri)
     text = text.replace(u'\r\n', u'\n')
     lines = text.split(u'\n')
     return lines
+
+
+class _Iter(object):
+    """
+    Iterator with push back support
+
+    Collected items can be pushed back into the iterator and further calls will
+    return them.
+    """
+
+    def __init__(self, parent):
+        self.__finished = False
+        self.__parent = iter(parent)
+        self.__prepend = []
+
+    def __iter__(self):
+        return self
+
+    def next(self):
+        if self.__finished:
+            raise StopIteration
+
+        if self.__prepend:
+            return self.__prepend.pop(0)
+
+        try:
+            return self.__parent.next()
+        except StopIteration:
+            self.__finished = True
+            raise
+
+    def push(self, item):
+        self.__prepend.append(item)
+
+
+class _Stack(object):
+    class Item(object):
+        def __init__(self, elem):
+            self.elem = elem
+            if elem.tag.uri == moin_page:
+                self.name = elem.tag.name
+            else:
+                self.name = None
+
+    def __init__(self, bottom=None):
+        self._list = []
+        if bottom:
+            self._list.append(self.Item(bottom))
+
+    def __len__(self):
+        return len(self._list)
+
+    def clear(self):
+        del self._list[1:]
+
+    def pop(self):
+        self._list.pop()
+
+    def pop_name(self, *names):
+        """
+        Remove anything from the stack including the given node.
+        """
+        while len(self._list) > 2 and not self.top_check(*names):
+            self.pop()
+        self.pop()
+
+    def push(self, elem):
+        self.top_append(elem)
+        self._list.append(self.Item(elem))
+
+    def top(self):
+        return self._list[-1].elem
+
+    def top_append(self, elem):
+        self.top().append(elem)
+
+    def top_append_ifnotempty(self, elem):
+        if elem:
+            self.top_append(elem)
+
+    def top_check(self, *names):
+        """
+        Checks if the name of the top of the stack matches the parameters.
+        """
+        return self._list[-1].name in names

MoinMoin/converter/creole_in.py

 
 from ._args_wiki import parse as parse_arguments
 from ._wiki_macro import ConverterMacro
-from ._util import decode_data, normalize_split_text
+from ._util import decode_data, normalize_split_text, _Iter, _Stack
 
 
-class _Iter(object):
-    """
-    Iterator with push back support
-
-    Collected items can be pushed back into the iterator and further calls will
-    return them.
-    """
-
-    def __init__(self, parent):
-        self.__finished = False
-        self.__parent = iter(parent)
-        self.__prepend = []
-
-    def __iter__(self):
-        return self
-
-    def next(self):
-        if self.__finished:
-            raise StopIteration
-
-        if self.__prepend:
-            return self.__prepend.pop(0)
-
-        try:
-            return self.__parent.next()
-        except StopIteration:
-            self.__finished = True
-            raise
-
-    def push(self, item):
-        self.__prepend.append(item)
-
-class _Stack(list):
-    def clear(self):
-        del self[1:]
-
-    def pop_name(self, *names):
-        """
-        Remove anything from the stack including the given node.
-        """
-        while len(self) > 2 and not self.top_check(*names):
-            self.pop()
-        self.pop()
-
-    def push(self, elem):
-        self.top_append(elem)
-        self.append(elem)
-
-    def top(self):
-        return self[-1]
-
-    def top_append(self, elem):
-        self[-1].append(elem)
-
-    def top_append_ifnotempty(self, elem):
-        if elem:
-            self.top_append(elem)
-
-    def top_check(self, *names):
-        """
-        Checks if the name of the top of the stack matches the parameters.
-        """
-        tag = self[-1].tag
-        return tag.uri == moin_page.namespace and tag.name in names
-
 class Converter(ConverterMacro):
     @classmethod
     def factory(cls, input, output, **kw):
 
         body = moin_page.body(attrib=attrib)
 
-        stack = _Stack([body])
+        stack = _Stack(body)
 
         # Please note that the iterator can be modified by other functions
         for line in iter_content:

MoinMoin/converter/mediawiki_in.py

 from MoinMoin import config
 from MoinMoin.util.iri import Iri
 from MoinMoin.util.tree import html, moin_page, xlink
-from MoinMoin.converter.moinwiki_in import _Iter, _Stack
 
 from ._args import Arguments
 from ._args_wiki import parse as parse_arguments
 from ._wiki_macro import ConverterMacro
-from ._util import decode_data, normalize_split_text
+from ._util import decode_data, normalize_split_text, _Iter, _Stack
 
 
 class _TableArguments(object):

MoinMoin/converter/moinwiki_in.py

 from ._args import Arguments
 from ._args_wiki import parse as parse_arguments
 from ._wiki_macro import ConverterMacro
-from ._util import decode_data, normalize_split_text
-
-
-class _Iter(object):
-    """
-    Iterator with push back support
-
-    Collected items can be pushed back into the iterator and further calls will
-    return them.
-    """
-
-    def __init__(self, parent):
-        self.__finished = False
-        self.__parent = iter(parent)
-        self.__prepend = []
-
-    def __iter__(self):
-        return self
-
-    def next(self):
-        if self.__finished:
-            raise StopIteration
-
-        if self.__prepend:
-            return self.__prepend.pop(0)
-
-        try:
-            return self.__parent.next()
-        except StopIteration:
-            self.__finished = True
-            raise
-
-    def push(self, item):
-        self.__prepend.append(item)
-
-
-class _Stack(object):
-    class Item(object):
-        def __init__(self, elem):
-            self.elem = elem
-            if elem.tag.uri == moin_page:
-                self.name = elem.tag.name
-            else:
-                self.name = None
-
-    def __init__(self, bottom=None):
-        self._list = []
-        if bottom:
-            self._list.append(self.Item(bottom))
-
-    def __len__(self):
-        return len(self._list)
-
-    def clear(self):
-        del self._list[1:]
-
-    def pop(self):
-        self._list.pop()
-
-    def pop_name(self, *names):
-        """
-        Remove anything from the stack including the given node.
-        """
-        while len(self._list) > 2 and not self.top_check(*names):
-            self.pop()
-        self.pop()
-
-    def push(self, elem):
-        self.top_append(elem)
-        self._list.append(self.Item(elem))
-
-    def top(self):
-        return self._list[-1].elem
-
-    def top_append(self, elem):
-        self.top().append(elem)
-
-    def top_append_ifnotempty(self, elem):
-        if elem:
-            self.top_append(elem)
-
-    def top_check(self, *names):
-        """
-        Checks if the name of the top of the stack matches the parameters.
-        """
-        return self._list[-1].name in names
+from ._util import decode_data, normalize_split_text, _Iter, _Stack
 
 
 class _TableArguments(object):