Attachment 'quiz_parser.py'
Download 1 #vim: fileencoding=utf-8
2
3 """
4 MoinMoin section parser
5 =======================
6
7 The parser is used to treat content as a page within a page. The section
8 have a header and a body, using the same format as moin regular
9 page. Only some of the headers are supported of course.
10
11 supported headers
12 -----------------
13 #format - any moin parser name
14 #class - any class name. The rendered class will be section-value
15 #language - any language known to moin
16
17 Bugs
18 ----
19
20 'pragma section-number' is not supported as enabling this in the section
21 cause section number to display in the page. To fix this request state
22 or maybe formatter state should be saved before rendering the section
23 and then resotored.
24
25 Other headers are ignored.
26
27
28 @copyright: 2005 by Nir Soffer <nirs@freeshell.org>
29 @license: GNU GPL, see COPYING for details.
30 """
31
32 import re
33 from MoinMoin import wikiutil, i18n
34
35 # This module is not part of MoinMoin currently, need to install this
36 from MoinMoin.util.header import Header
37
38
39 class SectionHeader(Header):
40 """ Customization for sections headers """
41 def set_class(self, value):
42 """ Save multiple values for class """
43 try:
44 self._headers['class'].append(value)
45 except KeyError:
46 self._headers['class'] = [value]
47
48
49 class SectionParser:
50
51 # One can use inline:sidebar.section to insert a section from an
52 # attachment.
53 extensions = ['.section']
54
55 # allow caching
56 caching = 1
57 Dependencies = []
58
59 form = None
60
61 baseClass = 'test'
62
63 def __init__(self, text, request, **keywords):
64 self.text = text
65 self.request = request
66 self.keyswords = keywords
67
68 def format(self, formatter):
69 """ Formatter the text inside classed div """
70 self.formatter = formatter
71 usingDefaultFormatter = self.request.page.default_formatter
72 self.header = SectionHeader(self.request, self.text)
73 savedLanguage = self.request.content_lang
74 self.request.setContentLanguage(self.sectionLanguage())
75 if usingDefaultFormatter:
76 self.request.write(self.section(1, **self.attributes()))
77 Parser = self.importParser(self.sectionFormat())
78 parser = Parser(self.text[self.header.length():], self.request)
79 parser.format(formatter)
80 if usingDefaultFormatter:
81 self.request.write(self.section(0))
82 self.request.setContentLanguage(savedLanguage)
83
84 # ------------------------------------------------------------------
85 # Private
86
87 def section(self, on, **attributes):
88 """ The missing section method in the formatter """
89 _ = self.request.getText
90 #tag = 'form'
91 formatter = self.request.formatter
92
93 if on:
94 group = self.header.get('group', '')
95 owner = self.header.get('owner', '')
96 if self.request.user.may.write(self.request.page.page_name):
97 names = self.request.dicts.dict(u'NazwiskaDict')
98 members = self.request.dicts.members(group)
99 save_text = u'#acl %s:read,write,delete,admin All:\n'%self.request.user.name
100 lista = formatter.rawHTML('<ul>')
101 for member in members:
102 lista += formatter.rawHTML('<li>')
103 lista += formatter.text('%s, '%member)
104 lista += formatter.pagelink(None, pagename='%s/%s'%(self.request.page.page_name,member), text=names.get(member,'?'))
105 lista += formatter.rawHTML('</li>')
106 save_text += ' * ["%s"]\n'%member
107 admin = ''.join([
108 formatter.rawHTML('<p>'),
109 self.formatter.text('Test for group '),
110 formatter.pagelink(None, pagename=group),
111 formatter.rawHTML('</p>'),
112 lista,
113 formatter.rawHTML('</ul>'),
114 formatter.rawHTML('<form>'),
115 formatter.rawHTML('<div>'),
116 formatter.rawHTML('<input type="hidden" name="action" value="createGroupSubpages">'),
117 formatter.rawHTML('<input type="hidden" name="group" value="%s">'%group),
118 formatter.rawHTML('<input type="hidden" name="ticket" value="%s">'%wikiutil.createTicket()),
119 formatter.rawHTML('<input type="submit" name="addgroup" value="%s">'%_("Start test")),
120 formatter.rawHTML('</div>'),
121 formatter.rawHTML('</form>'),
122 '<form method="post" action="%s/%s#preview">' % (
123 self.request.getScriptname(),
124 wikiutil.quoteWikinameURL(self.request.page.page_name+'/ZablokowanaGrupa')
125 ),
126 formatter.rawHTML('<div>'),
127 formatter.rawHTML('<input type="hidden" name="action" value="savepage">'),
128 formatter.rawHTML('<input type="hidden" name="comment" value="stop quiz">'),
129 formatter.rawHTML('<textarea name="savetext" class="hidden" rows="1" cols="1">%s</textarea>'%save_text),
130 formatter.rawHTML('<input type="submit" name="button_save" value="%s">'%_("Stop test")),
131 formatter.rawHTML('</div>'),
132 formatter.rawHTML('</form>'),
133
134 ])
135 else:
136 admin = ''
137 html = ''.join([
138 admin,
139 #self.formatter.open(tag, attr=attributes),
140 #self.
141 self.formatter.rawHTML('<div>'),
142 ])
143 else:
144 html = ''.join([
145 self.formatter.rawHTML('<input type="hidden" name="action" value="quiz">'),
146 self.formatter.rawHTML('<input type="submit" value="%s">'%_('Save')),
147 self.formatter.rawHTML('</div>'),
148 # self.formatter.close(tag),
149 ])
150 return html
151
152 def attributes(self):
153 """ Create attribute dict
154
155 Currently use only #class header, override to customize. we can
156 add here #style for direct css control in page.
157 """
158 return {'class': self.sectionClass(), 'method':'post', 'action':''}
159
160 def sectionClass(self):
161 """ Return class list starting with base class """
162 classes = [self.baseClass] + self.header.get('class', [])
163 return ' '.join(classes)
164
165 def sectionFormat(self):
166 format = self.header.get('format', 'wiki')
167 if format == 'section':
168 format = 'wiki'
169 return format
170
171 def sectionLanguage(self):
172 language = self.header.get('language',
173 self.request.content_lang)
174 return language
175
176 # This stuff is also needed by SlideShow, so it should not be part
177 # of action or parser code. Seems that this should be part of
178 # wikiutil, or maybe in some plugin module.
179
180 def importParser(self, name):
181 Parser = wikiutil.importPlugin(self.request.cfg, 'parser', name,
182 'Parser')
183 if Parser is None:
184 return self.defaultParser()
185 return Parser
186
187 def defaultParser(self):
188 from MoinMoin.parser.plain import Parser
189 return Parser
190
191
192 Parser = SectionParser
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.You are not allowed to attach a file to this page.