comparison MoinMoin/macro/NewPage.py @ 3377:4aeb96183e04

macro NewPage converted to the new arg parser
author Radomir Dopieralski <moindev@sheep.art.pl>
date Thu, 20 Mar 2008 22:50:41 +0100
parents 16ae95df840a
children 0b56ed4808a6
comparison
equal deleted inserted replaced
3372:55b787c6c083 3377:4aeb96183e04
38 Create an input field with button labeled 'Create New 38 Create an input field with button labeled 'Create New
39 Bug'. The new page will use the BugTemplate template, 39 Bug'. The new page will use the BugTemplate template,
40 and create the page as a subpage of MoinMoinBugs. 40 and create the page as a subpage of MoinMoinBugs.
41 """ 41 """
42 42
43 arguments = ['template', 'buttonLabel', 'parentPage', 'nameTemplate'] 43 def __init__(self, macro, template=u'', button_label=u'',
44 44 parent_page=u'', name_template=u'%s'):
45 def __init__(self, macro, args):
46 self.macro = macro 45 self.macro = macro
47 self.request = macro.request 46 self.request = macro.request
48 self.formatter = macro.formatter 47 self.formatter = macro.formatter
49 self.args = self.getArgs(args) 48 self.template = template
50 49 _ = self.request.getText
51 def getArgs(self, argstr): 50 if button_label:
52 """ Temporary function until Oliver Graf args parser is finished 51 # Try to get a translation, this will probably not work in
53 52 # most cases, but better than nothing.
54 @param argstr: string from the wiki markup <<NewPage(string)>> 53 self.label = self.request.getText(button_label)
55 @rtype: dict 54 else:
56 @return: dictionary with macro options 55 self.label = _("Create New Page")
57 """ 56 if parent_page == '@ME' and self.request.user.valid:
58 if not argstr: 57 self.parent = self.request.user.name
59 return {} 58 elif parent_page == '@SELF':
60 args = [s.strip() for s in argstr.split(',')] 59 self.parent = self.formatter.page.page_name
61 args = dict(zip(self.arguments, args)) 60 else:
62 return args 61 self.parent = parent_page
62 self.nametemplate = name_template
63 63
64 def renderInPage(self): 64 def renderInPage(self):
65 """ Render macro in page context 65 """ Render macro in page context
66 66
67 The parser should decide what to do if this macro is placed in a 67 The parser should decide what to do if this macro is placed in a
68 paragraph context. 68 paragraph context.
69 """ 69 """
70 f = self.formatter 70 f = self.formatter
71 _ = self.request.getText 71 _ = self.request.getText
72 72
73 parent = self.args.get('parentPage') or '' 73 requires_input = '%s' in self.nametemplate
74 template = self.args.get('template') or ''
75 label = self.args.get('buttonLabel')
76 nametemplate = self.args.get('nameTemplate') or u'%s'
77 74
78 if parent == '@ME' and self.request.user.valid:
79 parent = self.request.user.name
80 if parent == '@SELF':
81 parent = f.page.page_name
82
83 requires_input = '%s' in nametemplate
84
85 if label:
86 # Try to get a translation, this will probably not work in
87 # most cases, but better than nothing.
88 label = self.request.getText(label)
89 else:
90 label = _("Create New Page")
91 75
92 # TODO: better abstract this using the formatter 76 # TODO: better abstract this using the formatter
93 html = [ 77 html = [
94 u'<form class="macro" method="get" action="%s/%s"><div>' % (self.request.getScriptname(), wikiutil.quoteWikinameURL(self.formatter.page.page_name)), 78 u'<form class="macro" method="get" action="%s/%s"><div>' % (self.request.getScriptname(), wikiutil.quoteWikinameURL(self.formatter.page.page_name)),
95 u'<input type="hidden" name="action" value="newpage">', 79 u'<input type="hidden" name="action" value="newpage">',
96 u'<input type="hidden" name="parent" value="%s">' % wikiutil.escape(parent, 1), 80 u'<input type="hidden" name="parent" value="%s">' % wikiutil.escape(self.parent, 1),
97 u'<input type="hidden" name="template" value="%s">' % wikiutil.escape(template, 1), 81 u'<input type="hidden" name="template" value="%s">' % wikiutil.escape(self.template, 1),
98 u'<input type="hidden" name="nametemplate" value="%s">' % wikiutil.escape(nametemplate, 1), 82 u'<input type="hidden" name="nametemplate" value="%s">' % wikiutil.escape(self.nametemplate, 1),
99 ] 83 ]
100 84
101 if requires_input: 85 if requires_input:
102 html += [ 86 html += [
103 u'<input type="text" name="pagename" size="30">', 87 u'<input type="text" name="pagename" size="30">',
104 ] 88 ]
105 html += [ 89 html += [
106 u'<input type="submit" value="%s">' % wikiutil.escape(label, 1), 90 u'<input type="submit" value="%s">' % wikiutil.escape(self.label, 1),
107 u'</div></form>', 91 u'</div></form>',
108 ] 92 ]
109 return self.formatter.rawHTML('\n'.join(html)) 93 return self.formatter.rawHTML('\n'.join(html))
110 94
111 def execute(macro, args): 95 def macro_NewPage(macro, template=u'', button_label=u'',
96 parent_page=u'', name_template=u'%s'):
112 """ Temporary glue code to use with moin current macro system """ 97 """ Temporary glue code to use with moin current macro system """
113 return NewPage(macro, args).renderInPage() 98 return NewPage(macro, template, button_label, parent_page, name_template).renderInPage()
114 99