192
192
# TODO: Nicer error message, to help authors
193
193
req.throw_error(req.HTTP_INTERNAL_SERVER_ERROR)
194
194
worksheetname = worksheetdom.getAttribute("name")
195
elements = [] # List of DOM elements
196
for elem in worksheetdom.childNodes:
197
if elem.nodeType == elem.ELEMENT_NODE:
198
elements.append(elem)
200
196
# Now all the errors are out the way, we can begin writing
201
197
req.title = "Tutorial - %s" % worksheetname
203
199
req.write('<div id="ivle_padding">\n')
204
200
req.write("<h1>IVLE Tutorials - %s</h1>\n<h2>%s</h2>\n"
205
201
% (cgi.escape(subject), cgi.escape(worksheetname)))
206
203
# Write each element
208
for elem in elements:
209
if elem.tagName == "problem":
210
present_problem(req, subject, elem.getAttribute("src"), problemid)
213
# Just treat this as a normal HTML element
214
req.write(elem.toxml() + '\n')
205
for elem in worksheetdom.childNodes:
206
if elem.nodeType == elem.ELEMENT_NODE:
207
problemid = present_worksheet_element(req, elem, problemid)
215
208
req.write("</div>\n") # tutorialbody
210
def present_worksheet_element(req, elem, problemid):
211
"""Given an element of a worksheet XML document, writes it out to the
212
request. This recursively searches for "problem" elements and handles
213
those specially (presenting their XML problem spec and input box), and
214
just dumps the other elements as regular HTML.
216
problemid is the ID to use for the first problem.
217
Returns the new problemid after all the problems have been written
218
(since we need unique IDs for each problem).
220
if elem.tagName == "problem":
221
present_problem(req, elem.getAttribute("src"), problemid)
224
# Just treat this as a normal HTML element
225
req.write(elem.toxml() + '\n')
217
228
def innerXML(elem):
218
229
"""Given an element, returns its children as XML strings concatenated
236
247
return data.strip()
238
def present_problem(req, subject, problemsrc, problemid):
249
def present_problem(req, problemsrc, problemid):
239
250
"""Open a problem file, and write out the problem to the request in HTML.
240
subject: Subject name.
241
251
problemsrc: "src" of the problem file. A path relative to the top-level
242
subjects base directory, as configured in conf.
252
problems base directory, as configured in conf.
244
254
req.write('<div class="tuteproblem" id="problem%d">\n'