changeset 4839:e8beef3218b9

Groups2009: Formatter is used instead of regex to get group definitions from a page.
author Dmitrijs Milajevs <dimazest@gmail.com>
date Wed, 08 Jul 2009 19:30:15 +0200
parents d1996f3b59d5
children ce2667a4d982
files MoinMoin/datastruct/backends/_formatters.py MoinMoin/datastruct/backends/_tests/test_formatters.py MoinMoin/datastruct/backends/wiki_groups.py
diffstat 3 files changed, 172 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MoinMoin/datastruct/backends/_formatters.py	Wed Jul 08 19:30:15 2009 +0200
@@ -0,0 +1,83 @@
+# -*- coding: iso-8859-1 -*-
+"""
+MoinMoin - datastruct (dicts and groups) formatters
+
+@copyright: 2009 MoinMoin:DmitrijsMilajevs
+@license: GNU GPL, see COPYING for details.
+"""
+
+from MoinMoin.formatter import FormatterBase
+from MoinMoin import wikiutil
+
+class GroupFormatter(FormatterBase):
+    """
+    Collect groups and format nothing
+    """
+
+    def __init__(self, request, **kw):
+        FormatterBase.__init__(self, request, **kw)
+        self.bullet_list_level = 0
+        self.inside_list_item = False
+        self.inside_link = False
+        self.members = []
+        self.new_member = ''
+        self.new_link_member = ''
+
+    def pagelink(self, on, pagename='', page=None, **kw):
+        self.inside_link = on
+        if self.inside_list_item:
+            if not pagename and page:
+                pagename = page.page_name
+            pagename = wikiutil.normalize_pagename(pagename, self.request.cfg)
+            self.new_link_member = pagename
+        return ''
+
+    def bullet_list(self, on, **kw):
+        if on:
+            self.bullet_list_level += 1
+        else:
+            self.bullet_list_level -= 1
+
+        assert self.bullet_list_level >= 0
+        return ''
+
+    def listitem(self, on, **kw):
+        if self.bullet_list_level == 1:
+            self.inside_list_item = on
+            if not on:
+                if self.new_member.strip() == '' and self.new_link_member != '':
+                    self.members.append(self.new_link_member.strip())
+                if self.new_member != '' and self.new_link_member == '':
+                    self.members.append(self.new_member.strip())
+            self.new_member = ''
+            self.new_link_member = ''
+        return ''
+
+    def definition_list(self, on, **kw):
+        return ''
+
+    def definition_term(self, on, compact=0, **kw):
+        return ''
+
+    def definition_desc(self, on, **kw):
+        return ''
+
+    def text(self, text, **kw):
+        if self.inside_list_item and not self.inside_link:
+            self.new_member += text
+        return ''
+
+    def null(self, *args, **kw):
+        return ''
+
+    # All these must be overriden here because they raise
+    # NotImplementedError!@#! or return html?! in the base class.
+    set_highlight_re = rawHTML = url = image = smiley = null
+    strong = emphasis = underline = highlight = sup = sub = strike = null
+    code = preformatted = small = big = code_area = code_line = null
+    code_token = linebreak = paragraph = rule = icon = null
+    number_list = null
+    heading = table = null
+    table_row = table_cell = attachment_link = attachment_image = attachment_drawing = null
+    transclusion = transclusion_param = null
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MoinMoin/datastruct/backends/_tests/test_formatters.py	Wed Jul 08 19:30:15 2009 +0200
@@ -0,0 +1,83 @@
+# -*- coding: iso-8859-1 -*-
+"""
+    MoinMoin - MoinMoin.datasttructs.backends._formatters Tests
+
+    @copyright: 2009 by MoinMoin:DmitrijsMilajevs
+    @license: GNU GPL, see COPYING for details.
+"""
+
+from MoinMoin.datastruct.backends._formatters import GroupFormatter
+from MoinMoin.Page import Page
+from MoinMoin._tests import become_trusted, create_page, nuke_page
+
+class TestDatastruct(object):
+
+    def test_CamelCase(self):
+        text = """
+ * CamelCase
+"""
+        assert u'CamelCase' in self.get_group(text)
+
+    def test_extended_name(self):
+        text = """
+ * extended name
+"""
+        assert u'extended name' in self.get_group(text)
+
+    def test_extended_link(self):
+        text = """
+ * [[extended link]]
+"""
+        assert u'extended link' in self.get_group(text)
+
+    def test_extended_link_with_label(self):
+        text = """
+ * [[extended link| label]]
+"""
+        assert u'extended link' in self.get_group(text)
+
+    def test_extended_link_and_text(self):
+        text = """
+ * [[extended link]] other text
+ * other text [[extended link]]
+ * other text [[extended link]] other text
+
+"""
+        assert len(self.get_group(text)) == 0
+
+    def test_ignore_second_level_list(self):
+        text = """
+  * second level
+   * third level
+    * forth level
+     * and then some...
+"""
+        assert len([x for x in self.get_group(text)]) == 0
+
+    def test_ignore_other(self):
+        text = """
+= ignore this =
+ * take this
+
+Ignore previous line and this text.
+"""
+        assert u'take this' in self.get_group(text)
+
+    def test_strip_whitespace(self):
+        text = """
+ *   take this
+"""
+        assert u'take this' in self.get_group(text)
+
+    def get_group(self, text):
+        request = self.request
+        formatter = GroupFormatter(self.request)
+
+        become_trusted(request)
+        create_page(request, u'TestPageGroup', text)
+        page = Page(request, 'TestPageGroup', formatter=formatter)
+        page.send_page(content_only=True)
+        nuke_page(request, u'TestPageGroup')
+
+        return formatter.members
+
--- a/MoinMoin/datastruct/backends/wiki_groups.py	Wed Jul 08 12:25:50 2009 +0200
+++ b/MoinMoin/datastruct/backends/wiki_groups.py	Wed Jul 08 19:30:15 2009 +0200
@@ -13,11 +13,10 @@
 @license: GPL, see COPYING for details
 """
 
-import re
-
 from MoinMoin import caching, wikiutil
 from MoinMoin.Page import Page
 from MoinMoin.datastruct.backends import BaseGroup, BaseGroupsBackend, GroupDoesNotExistError
+from MoinMoin.datastruct.backends._formatters import GroupFormatter
 
 
 class WikiGroup(BaseGroup):
@@ -60,13 +59,9 @@
     def __getitem__(self, group_name):
         return WikiGroup(request=self.request, name=group_name, backend=self)
 
-    # * Member - ignore all but first level list items, strip
-    # whitespace, strip free links markup. This is used for parsing
-    # pages in order to find group page members.
-    _group_page_parse_regex = re.compile(ur'^ \* +(?:\[\[)?(?P<member>.+?)(?:\]\])? *$', re.MULTILINE | re.UNICODE)
+    def _retrieve_members(self, group_name):
+        formatter = GroupFormatter(self.request)
+        page = Page(self.request, group_name, formatter=formatter)
+        page.send_page(content_only=True)
+        return formatter.members
 
-    def _retrieve_members(self, group_name):
-        page = Page(self.request, group_name)
-        text = page.get_raw_body()
-        return [match.group('member') for match in self._group_page_parse_regex.finditer(text)]
-