1 """
   2     [[DataFromKVPairs()]] will display a value that matches the key
   3     from the named table (or the first table) in the Page
   4 
   5     @copyright: 2004 by Shandy
   6     @license: GNU GPL, see COPYING for details.
   7 """
   8 
   9 # Imports
  10 from MoinMoin import config, wikiutil, Page
  11 import re
  12 
  13 
  14 _args_re_pattern = r'''(?P<pquote>[\'"])(?P<pname>[^,]+)(?P=pquote)
  15                        ,\s*(?P<tquote>[\'"])(?P<tname>[^,]+)(?P=tquote)
  16                        ,\s*(?P<kquote>[\'"])(?P<kname>[^,]+)(?P=kquote)
  17                        ,\s*(?P<fquote>[\'"])(?P<format>[^,]+)(?P=fquote)
  18                        '''
  19 
  20 _available_formats = [ "plain", "img", "link" ]
  21 def execute(macro, text ):
  22         value = ''
  23 
  24         p = re.compile( _args_re_pattern, re.VERBOSE | re.DOTALL )
  25         m = p.search( text )
  26 
  27         if not m:
  28                 return '<h2 class="error">Macro error: args</h2>'+ \
  29                 '''<p>Should be DataFromKVPairs("PageName",
  30                 "TableName", "KeyName", "Format")</p>
  31                 <p>See DataFromKVPairs.py on dev or ask Shandy</p>
  32                 '''
  33 
  34         pageName = m.group('pname')
  35         tableName = m.group('tname')
  36         key = m.group('kname')
  37         format = m.group('format')
  38 
  39         if format not in _available_formats:
  40                 return '''<h2 class="error">Macro error: args</h2>
  41                 <p>Format is not in the available formats:''' +\
  42                 str( _available_formats ) + '''</p>
  43                 <p>See DataFromKVPairs.py on dev or ask Shandy</p>
  44                 '''
  45 
  46         wPage = Page.Page( pageName )
  47 
  48         if not wPage.exists():
  49                 return '''<h2 class="error">Macro error: page not found</h2>
  50                 <p>The requested page doesn't exist.
  51                 Perhaps its name has changed.</p>'''
  52 
  53 
  54         rawBody = wPage.get_raw_body()
  55         #_eol_re = re.compile(r'\r?\n')
  56         #lines = eol_re.split(rawBody)
  57         lines = rawBody.splitlines()
  58         for line in lines:
  59                 #check to see if it's a table row
  60                 if not (line[0:2] == '||'):
  61                         continue
  62 
  63                 #get the cells as a list
  64                 cells = line.split( '||' )
  65 
  66                 if len(cells) < 3:
  67                         continue
  68 
  69                 #note, cells[0] should be empty as || is the first string
  70                 k,v = cells[1].strip(), cells[2].strip()
  71                 if k == key:
  72                         value = v
  73                         break
  74 
  75         if value == '':
  76                 return '''<h2 class="error">Macro error: key not found</h2>
  77                 <p>The requested key wasn't found in a table on the page
  78                 '''+ pageName +'''.</p>
  79                 <p>Key requested: '''+ key +'</p>'
  80 
  81         returnString = ''
  82 
  83         #format the value as requested
  84         if format == "plain":
  85                 returnString = value
  86         elif format == "img":
  87                 returnString = '<img src="'+ value +'" />'
  88         elif format == "link":
  89                 #TODO: check for in-wiki link with isStrictWikiname(name)
  90                 returnString = '<a href="'+ value +'">'+ key +'</a>'
  91 
  92 
  93         return returnString

MoinMoin: macro/DataFromKVPairs.py (last edited 2007-10-29 19:10:42 by localhost)