Attachment 'NewPageMacroWithMultipleTemplateSupport_0.1.patch'

Download

   1 # User Jiang Xin <worldhello.net AT gmail.com>
   2 # Changes: 
   3 # * NewPage macro support multiple template selection.
   4 
   5 --- a/MoinMoin/macro/NewPage.py	2008-10-16 20:01:25.000000000 +0800
   6 +++ b/MoinMoin/macro/NewPage.py	2008-10-17 02:49:19.000000000 +0800
   7 @@ -41,12 +41,19 @@
   8              and create the page as a subpage of MoinMoinBugs.
   9      """
  10  
  11 -    def __init__(self, macro, template=u'', button_label=u'',
  12 -                 parent_page=u'', name_template=u'%s'):
  13 +    def __init__(self, macro, template="",  button_label=u'',
  14 +                 parent_page=u'', name_template=u'%s', selection_type=u'select', layout=u''):
  15          self.macro = macro
  16          self.request = macro.request
  17          self.formatter = macro.formatter
  18 -        self.template = template
  19 +        if isinstance(template, (list,tuple)):
  20 +            self.template = {}
  21 +            for item in template:
  22 +                self.template[item] = item
  23 +        else:
  24 +            self.template = template
  25 +        self.selection_type = selection_type
  26 +        self.layout = layout
  27          _ = self.request.getText
  28          if button_label:
  29              # Try to get a translation, this will probably not work in
  30 @@ -77,7 +84,16 @@
  31          _ = self.request.getText
  32  
  33          requires_input = '%s' in self.nametemplate
  34 -
  35 +        if not self.layout:
  36 +            self.layout = """
  37 +<table class="borderless">
  38 +  <tr>
  39 +    <td>""" + _("Input title: ") + """</td><td>%(input)s</td></tr>
  40 +  <tr>
  41 +    <td>""" + _("Select template: ") + """</td><td>%(select)s</td></tr>
  42 +  <tr><td colspan="2" align="right">%(button)s</td></tr>
  43 +</table>
  44 +"""
  45  
  46          # TODO: better abstract this using the formatter
  47          # OSSXP: self.request.page.page_name and self.formatter.page.page_name may different, 
  48 @@ -91,26 +107,72 @@
  49                  except:
  50                      self.parent = self.formatter.page.page_name
  51              
  52 -        html = [
  53 +        header = [
  54              u'<form class="macro" method="POST" action="%s/%s"><div>' % (self.request.getScriptname(), wikiutil.quoteWikinameURL(self.formatter.page.page_name)),
  55              u'<input type="hidden" name="action" value="newpage">',
  56              u'<input type="hidden" name="parent" value="%s">' % wikiutil.escape(self.parent, 1),
  57 -            u'<input type="hidden" name="template" value="%s">' % wikiutil.escape(self.template, 1),
  58              u'<input type="hidden" name="nametemplate" value="%s">' % wikiutil.escape(self.nametemplate, 1),
  59          ]
  60 -
  61 +        header = '\n'.join(header)
  62 +        footer = u'</div></form>'
  63 +        button = u'<input type="submit" value="%s">' % wikiutil.escape(self.label, 1)
  64 +        
  65 +        if isinstance(self.template, basestring):
  66 +            select = u''
  67 +            header += u'\n<input type="hidden" name="template" value="%s">' % wikiutil.escape(self.template, 1)
  68 +        elif isinstance(self.template, dict):
  69 +            select = []
  70 +            if self.selection_type == "select":
  71 +                select.append(u'<select name="template" size="0">')
  72 +                for k,v in self.template.iteritems():
  73 +                    select.append(u'<option value="%(key)s" label="%(key)s">%(value)s</option>' % \
  74 +                               {'key':k, 'value':v})
  75 +                select.append(u'</select>')
  76 +            else: # selection_type == radio
  77 +                for k,v in self.template.iteritems():
  78 +                    select.append(u'<input type="radio" name="template" value="%(key)s">%(value)s' % \
  79 +                               {'key':k, 'value':v})
  80 +            select = '\n'.join(select)
  81          if requires_input:
  82 -            html += [
  83 -                u'<input type="text" name="pagename" size="30">',
  84 -            ]
  85 -        html += [
  86 -            u'<input type="submit" value="%s">' % wikiutil.escape(self.label, 1),
  87 -            u'</div></form>',
  88 -            ]
  89 -        return self.formatter.rawHTML('\n'.join(html))
  90 +            input = u'<input type="text" name="pagename" size="30">'
  91 +        else:
  92 +            input = u''
  93 +        
  94 +        if input and select and self.layout:
  95 +            html = '\n'.join([header,
  96 +                              self.layout % {'button': button, 'select':select, 'input':input},
  97 +                              footer])
  98 +        else:
  99 +            html = '\n'.join([header, select, input, button,footer])
 100 +
 101 +        return self.formatter.rawHTML(html)
 102 +
 103  
 104 -def macro_NewPage(macro, template=u'', button_label=u'',
 105 -                  parent_page=u'', name_template=u'%s'):
 106 +def execute(macro, args):
 107      """ Temporary glue code to use with moin current macro system """
 108 -    return NewPage(macro, template, button_label, parent_page, name_template).renderInPage()
 109 +    request = macro.request
 110 +    import re
 111 +    pattern = re.compile(ur"""
 112 +^(?P<template>
 113 +      \[.*?\] # list
 114 +    |
 115 +      \(.*?\) # tuple
 116 +    |
 117 +      \{.*?\} # dict
 118 +    |
 119 +      (?P<mark>"').*?(?P=mark) # string with mark
 120 +    |
 121 +      [^,]* # string without mark
 122 +),?(?P<args>.*)$    # others arguments
 123 +""", re.UNICODE|re.VERBOSE)
 124 +    template = ""
 125 +    if args:
 126 +        match = pattern.search(args)
 127 +        if match:
 128 +            template = match.group('template')
 129 +            args = match.group('args')
 130 +            if template:
 131 +                if template[0] in '[({\'"':
 132 +                    template = eval(template)
 133  
 134 +    return wikiutil.invoke_extension_function(request, NewPage, args, fixed_args=[macro, template]).renderInPage()
 135 

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.
  • [get | view] (2008-11-26 08:25:23, 15.3 KB) [[attachment:30470_multi_templates_and_categories_for_newpage_macro.patch]]
  • [get | view] (2008-10-16 19:51:16, 5.3 KB) [[attachment:NewPageMacroWithMultipleTemplateSupport_0.1.patch]]
  • [get | view] (2008-11-23 12:41:39, 15.3 KB) [[attachment:NewPageMacroWithMultipleTemplateSupport_0.2.patch]]
 All files | Selected Files: delete move to page copy to page

You are not allowed to attach a file to this page.