1# -*- coding: iso-8859-1 -*-
2"""
3    MoinMoin - New Page macro
4
5    Thanks to Jos Yule's "blogpost" action and his modified Form for
6    giving me the pieces I needed to figure all this stuff out: MoinMoin:JosYule
7
8    @copyright: 2004 Vito Miliano (vito_moinnewpagewithtemplate@perilith.com),
9                2004 by Nir Soffer <nirs@freeshell.org>,
10                2004 Alexander Schremmer <alex AT alexanderweb DOT de>,
11                2006-2008 MoinMoin:ReimarBauer
12                2008 MoinMoin:RadomirDopieralski
13    @license: GNU GPL, see COPYING for details.
14"""
15
16from MoinMoin import wikiutil
17
18Dependencies = ["language"]
19
20class NewPage:
21    """ NewPage - create new pages
22
23    Let you create new page using optional template, button text
24    and parent page (for automatic subpages).
25
26    Usage:
27
28        <<NewPage(template, buttonLabel, parentPage)>>
29
30    Examples:
31
32        <<NewPage>>
33
34            Create an input field with 'Create New Page' button. The new
35            page will not use a template.
36
37        <<NewPage(BugTemplate, Create New Bug, MoinMoinBugs)>>
38
39            Create an input field with button labeled 'Create New
40            Bug'.  The new page will use the BugTemplate template,
41            and create the page as a subpage of MoinMoinBugs.
42    """
43
44    def __init__(self, macro, template=u'', button_label=u'',
45                 parent_page=u'', name_template=u'%s'):
46        self.macro = macro
47        self.request = macro.request
48        self.formatter = macro.formatter
49        self.template = template
50        _ = self.request.getText
51        if button_label:
52            # Try to get a translation, this will probably not work in
53            # most cases, but better than nothing.
54            self.label = self.request.getText(button_label)
55        else:
56            self.label = _("Create New Page")
57        if parent_page == '@ME' and self.request.user.valid:
58            self.parent = self.request.user.name
59        elif parent_page == '@SELF':
60            self.parent = self.formatter.page.page_name
61        else:
62            self.parent = parent_page
63        self.nametemplate = name_template
64
65    def renderInPage(self):
66        """ Render macro in page context
67
68        The parser should decide what to do if this macro is placed in a
69        paragraph context.
70        """
71        f = self.formatter
72        _ = self.request.getText
73
74        requires_input = '%s' in self.nametemplate
75
76
77        # TODO: better abstract this using the formatter
78        html = [
79            u'<form class="macro" method="POST" action="%s"><div>' % self.request.href(self.formatter.page.page_name),
80            u'<input type="hidden" name="action" value="newpage">',
81            u'<input type="hidden" name="parent" value="%s">' % wikiutil.escape(self.parent, 1),
82            u'<input type="hidden" name="template" value="%s">' % wikiutil.escape(self.template, 1),
83            u'<input type="hidden" name="nametemplate" value="%s">' % wikiutil.escape(self.nametemplate, 1),
84        ]
85
86        if requires_input:
87            html += [
88                u'<input type="text" name="pagename" size="30">',
89            ]
90        html += [
91            u'<input type="submit" value="%s">' % wikiutil.escape(self.label, 1),
92            u'</div></form>',
93            ]
94        return self.formatter.rawHTML('\n'.join(html))
95
96def macro_NewPage(macro, template=u'', button_label=u'',
97                  parent_page=u'', name_template=u'%s'):
98    """ Temporary glue code to use with moin current macro system """
99    return NewPage(macro, template, button_label, parent_page, name_template).renderInPage()
100
101