Back to index

moin  1.9.0~rc2
NewPage.py
Go to the documentation of this file.
00001 # -*- coding: iso-8859-1 -*-
00002 """
00003     MoinMoin - New Page macro
00004 
00005     Thanks to Jos Yule's "blogpost" action and his modified Form for
00006     giving me the pieces I needed to figure all this stuff out: MoinMoin:JosYule
00007 
00008     @copyright: 2004 Vito Miliano (vito_moinnewpagewithtemplate@perilith.com),
00009                 2004 by Nir Soffer <nirs@freeshell.org>,
00010                 2004 Alexander Schremmer <alex AT alexanderweb DOT de>,
00011                 2006-2008 MoinMoin:ReimarBauer
00012                 2008 MoinMoin:RadomirDopieralski
00013     @license: GNU GPL, see COPYING for details.
00014 """
00015 
00016 from MoinMoin import wikiutil
00017 
00018 Dependencies = ["language"]
00019 
00020 class NewPage:
00021     """ NewPage - create new pages
00022 
00023     Let you create new page using optional template, button text
00024     and parent page (for automatic subpages).
00025 
00026     Usage:
00027 
00028         <<NewPage(template, buttonLabel, parentPage)>>
00029 
00030     Examples:
00031 
00032         <<NewPage>>
00033 
00034             Create an input field with 'Create New Page' button. The new
00035             page will not use a template.
00036 
00037         <<NewPage(BugTemplate, Create New Bug, MoinMoinBugs)>>
00038 
00039             Create an input field with button labeled 'Create New
00040             Bug'.  The new page will use the BugTemplate template,
00041             and create the page as a subpage of MoinMoinBugs.
00042     """
00043 
00044     def __init__(self, macro, template=u'', button_label=u'',
00045                  parent_page=u'', name_template=u'%s'):
00046         self.macro = macro
00047         self.request = macro.request
00048         self.formatter = macro.formatter
00049         self.template = template
00050         _ = self.request.getText
00051         if button_label:
00052             # Try to get a translation, this will probably not work in
00053             # most cases, but better than nothing.
00054             self.label = self.request.getText(button_label)
00055         else:
00056             self.label = _("Create New Page")
00057         if parent_page == '@ME' and self.request.user.valid:
00058             self.parent = self.request.user.name
00059         elif parent_page == '@SELF':
00060             self.parent = self.formatter.page.page_name
00061         else:
00062             self.parent = parent_page
00063         self.nametemplate = name_template
00064 
00065     def renderInPage(self):
00066         """ Render macro in page context
00067 
00068         The parser should decide what to do if this macro is placed in a
00069         paragraph context.
00070         """
00071         f = self.formatter
00072         _ = self.request.getText
00073 
00074         requires_input = '%s' in self.nametemplate
00075 
00076 
00077         # TODO: better abstract this using the formatter
00078         html = [
00079             u'<form class="macro" method="POST" action="%s"><div>' % self.request.href(self.formatter.page.page_name),
00080             u'<input type="hidden" name="action" value="newpage">',
00081             u'<input type="hidden" name="parent" value="%s">' % wikiutil.escape(self.parent, 1),
00082             u'<input type="hidden" name="template" value="%s">' % wikiutil.escape(self.template, 1),
00083             u'<input type="hidden" name="nametemplate" value="%s">' % wikiutil.escape(self.nametemplate, 1),
00084         ]
00085 
00086         if requires_input:
00087             html += [
00088                 u'<input type="text" name="pagename" size="30">',
00089             ]
00090         html += [
00091             u'<input type="submit" value="%s">' % wikiutil.escape(self.label, 1),
00092             u'</div></form>',
00093             ]
00094         return self.formatter.rawHTML('\n'.join(html))
00095 
00096 def macro_NewPage(macro, template=u'', button_label=u'',
00097                   parent_page=u'', name_template=u'%s'):
00098     """ Temporary glue code to use with moin current macro system """
00099     return NewPage(macro, template, button_label, parent_page, name_template).renderInPage()
00100