diff data/plugin/macro/DictColumns.py @ 511:ef1f74bf4776

macro DictColumns: new keyword markup defined (default is "definition list"). By setting it to "title" the column headings are defined by the titles of the pageThe text below a title becomes assigned to the column.
author Reimar Bauer <rb.proj AT googlemail DOT com>
date Fri, 19 Mar 2010 22:59:00 +0100
parents b7a9843c5730
children 31e327c450ca
line wrap: on
line diff
--- a/data/plugin/macro/DictColumns.py	Fri Mar 19 21:22:57 2010 +0100
+++ b/data/plugin/macro/DictColumns.py	Fri Mar 19 22:59:00 2010 +0100
@@ -12,11 +12,13 @@
 import re
 from MoinMoin import wikiutil, search
 from MoinMoin.Page import Page
+from MoinMoin.action.SlideShow import SlidePage
 from MoinMoin.util.dataset import TupleDataset, Column
 from MoinMoin.widget.browser import DataBrowserWidget
 
 Dependencies = ["pages"]
 
+
 def _csv2list(csv):
     """
     converts a string of comma separated values into a list
@@ -50,7 +52,7 @@
                  sort=u'', reverse=u'',
                  hide=u'', filter_name=u'NeverExistingDefaultFilter',
                  filter_value=u'', template_page=u'', alias_page=u'',
-                 parser=u'text_moin_wiki', search_term=None):
+                 parser=u'text_moin_wiki', markup="definition list", search_term=None):
 
         self.formatter = macro.formatter
         self.request = macro.request
@@ -67,7 +69,7 @@
         self.filter_name = filter_name
         self.filter_value = filter_value
         self.filter_key, self.filter_word = (u"", u"")
-        regex = re.compile(ur'(?P<key>\w*):: (?P<value>.*)', re.UNICODE)
+        regex = re.compile(ur'(?P<key>\w*)=(?P<value>.*)', re.UNICODE)
         try:
             self.filter_key, self.filter_word = regex.search(filter_value).groups()
         except AttributeError:
@@ -82,9 +84,32 @@
         except wikiutil.PluginMissingError:
             self.wiki_parser = None
         self.search_term = search_term
+        self.markup = markup
         if search_term is None:
             self.search_term = u'regex:title:^%s/' % self.pagename
 
+    def get_dict(self, dict_source):
+        """
+        gets the dictionary dependent of the markup
+        @param dict_source: pagename to read dict data from 
+        """
+        if self.markup == "definition list":
+            return self.request.dicts[dict_source]
+        elif self.markup == "title":
+            return self.parse_title(dict_source)
+
+    def parse_title(self, dict_source):
+        """
+        creates a dictionary based on page titles 
+        @param dict_source: pagename to read dict data from
+        """
+        body = Page(self.request, dict_source).get_raw_body()
+        parser = SlidePage(self.request, dict_source).createSlideParser()
+        ddict = {}
+        for title, bodyStart, bodyEnd in parser.parse(body):
+            ddict[title] = body[bodyStart:bodyEnd].strip()
+        return ddict
+
     def get_page_list(self):
         """
         selects the pages dependent on a search term,
@@ -117,13 +142,13 @@
             names = self.names
         # use keys from template page, no order
         elif Page(request, self.template_page).exists():
-            page_dict = request.dicts[self.template_page]
+            page_dict = self.get_dict(self.template_page)
             names = page_dict.keys()
         else:
             # fallback use the keys used on selected pages
             names = []
             for page_name in selected_pages:
-                page_dict = request.dicts[page_name]
+                page_dict = self.get_dict(page_name)
                 keys = page_dict.keys()
                 names = names + keys
         return list(set(names))
@@ -142,7 +167,7 @@
         for name in names:
             alias_dict[name] = name
         if Page(request, self.alias_page).exists():
-            alias = request.dicts[self.alias_page]
+            alias = self.get_dict(self.alias_page)
             for name in names:
                 alias_dict[name] = alias.get(name, name)
 
@@ -156,7 +181,7 @@
 
         for page_name in selected_pages:
             page = Page(request, page_name)
-            page_dict = request.dicts[page_name]
+            page_dict = self.get_dict(page_name)
             if self.filter_value and page_dict.get(self.filter_key, '') != self.filter_word:
                 continue
 
@@ -222,7 +247,8 @@
 def macro_DictColumns(macro, pagename=unicode, title=u'', names=u'', sort=u'', reverse=u'',
                       hide=u'', filter_name=u'NeverExistingDefaultFilter',
                       filter_value=u'', template_page=u'', alias_page=u'',
-                      parser=u'text_moin_wiki', search_term=None):
+                      parser=u'text_moin_wiki', markup=("definition list", "title"),
+                      search_term=None):
     """
     Creates a table by data browser widget from definition lists key value pairs.
     @param pagename: name of the page
@@ -236,6 +262,7 @@
     @param template_page: pagename of the template for setting column names
     @param alias_page: pagename of the page for setting aliases for column names
     @param parser: name of the parser used to render markup
+    @param markup: type of markup for separating key value pairs
     @param search_term: regex used to search for selecting pages
     """
     kw = locals()