1 # -*- coding: utf-8 -*-
3 # This file is part of Librarian, licensed under GNU Affero GPLv3 or later.
4 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
7 from librarian import IOFile, RDFNS, DCNS, Format
8 from xmlutils import Xmill, tag, tagged, ifoption
9 from librarian import functions
14 DEFAULT_MATERIAL_FORMAT = 'odt'
17 class EduModule(Xmill):
18 def __init__(self, options=None):
19 super(EduModule, self).__init__(options)
20 self.activity_counter = 0
21 self.register_text_filter(functions.substitute_entities)
23 def handle_powiesc(self, element):
25 <div class="module" id="book-text">
26 <!-- <span class="teacher-toggle">
27 <input type="checkbox" name="teacher-toggle" id="teacher-toggle"/>
28 <label for="teacher-toggle">Pokaż treść dla nauczyciela</label>
33 handle_autor_utworu = tag("span", "author")
34 handle_nazwa_utworu = tag("h1", "title")
35 handle_dzielo_nadrzedne = tag("span", "collection")
36 handle_podtytul = tag("span", "subtitle")
37 handle_naglowek_akt = handle_naglowek_czesc = handle_srodtytul = tag("h2")
38 handle_naglowek_scena = handle_naglowek_rozdzial = tag('h2')
39 handle_naglowek_osoba = handle_naglowek_podrozdzial = tag('h3')
40 handle_akap = handle_akap_dialog = handle_akap_cd = tag('p', 'paragraph')
41 handle_strofa = tag('div', 'stanza')
42 handle_wyroznienie = tag('em')
43 handle_tytul_dziela = tag('em', 'title')
44 handle_slowo_obce = tag('em', 'foreign')
46 def handle_uwaga(self, _e):
49 def handle_aktywnosc(self, element):
50 self.activity_counter += 1
53 'activity_counter': self.activity_counter,
55 submill = EduModule(dict(self.options.items() + {'sub_gen': True}.items()))
57 opis = submill.generate(element.xpath('opis')[0])
59 n = element.xpath('wskazowki')
60 if n: wskazowki = submill.generate(n[0])
63 n = element.xpath('pomoce')
65 if n: pomoce = submill.generate(n[0])
68 forma = ''.join(element.xpath('forma/text()'))
70 czas = ''.join(element.xpath('czas/text()'))
72 counter = self.activity_counter
75 <div class="activity">
77 <span class="act_counter">%(counter)d.</span>
82 <section class="infobox time"><h1>Czas</h1><p>%(czas)s min</p></section>
83 <section class="infobox kind"><h1>Forma</h1><p>%(forma)s</p></section>
86 <div class="clearboth"></div>
90 handle_opis = ifoption(sub_gen=True)(tag('div', 'description'))
91 handle_wskazowki = ifoption(sub_gen=True)(tag('div', ('hints', 'teacher')))
93 @ifoption(sub_gen=True)
94 @tagged('section', 'infobox materials')
95 def handle_pomoce(self, _):
96 return """<h1>Pomoce</h1>""", ""
98 def handle_czas(self, *_):
101 def handle_forma(self, *_):
104 def handle_cwiczenie(self, element):
105 exercise_handlers = {
107 'uporzadkuj': Uporzadkuj,
110 'przyporzadkuj': Przyporzadkuj,
111 'prawdafalsz': PrawdaFalsz
114 typ = element.attrib['typ']
115 handler = exercise_handlers[typ](self.options)
116 return handler.generate(element)
119 def handle_lista(self, element, attrs={}):
120 ltype = element.attrib.get('typ', 'punkt')
121 if ltype == 'slowniczek':
122 surl = element.attrib.get('href', None)
125 sxml = etree.fromstring(self.options['provider'].by_uri(surl).get_string())
126 self.options = {'slowniczek': True, 'slowniczek_xml': sxml }
127 return '<div class="slowniczek">', '</div>'
129 listtag = {'num': 'ol',
132 'czytelnia': 'ul'}[ltype]
134 classes = attrs.get('class', '')
135 if classes: del attrs['class']
137 attrs_s = ' '.join(['%s="%s"' % kv for kv in attrs.items()])
138 if attrs_s: attrs_s = ' ' + attrs_s
140 return '<%s class="lista %s %s"%s>' % (listtag, ltype, classes, attrs_s), '</%s>' % listtag
142 def handle_punkt(self, element):
143 if self.options['slowniczek']:
144 return '<dl>', '</dl>'
146 return '<li>', '</li>'
148 def handle_definiendum(self, element):
149 nxt = element.getnext()
152 # let's pull definiens from another document
153 if self.options['slowniczek_xml'] and (not nxt or nxt.tag != 'definiens'):
154 sxml = self.options['slowniczek_xml']
155 assert element.text != ''
156 defloc = sxml.xpath("//definiendum[text()='%s']" % element.text)
158 definiens = defloc[0].getnext()
159 if definiens.tag == 'definiens':
160 subgen = EduModule(self.options)
161 definiens_s = subgen.generate(definiens)
163 return u"<dt>", u"</dt>" + definiens_s
165 def handle_definiens(self, element):
166 return u"<dd>", u"</dd>"
168 def handle_podpis(self, element):
169 return u"""<div class="caption">""", u"</div>"
171 def handle_tabela(self, element):
172 has_frames = int(element.attrib.get("ramki", "0"))
173 if has_frames: frames_c = "framed"
175 return u"""<table class="%s">""" % frames_c, u"</table>"
177 def handle_wiersz(self, element):
178 return u"<tr>", u"</tr>"
180 def handle_kol(self, element):
181 return u"<td>", u"</td>"
183 def handle_rdf__RDF(self, _):
184 # ustal w opcjach rzeczy :D
187 def handle_link(self, element):
188 if 'url' in element.attrib:
189 return tag('a', href=element.attrib['url'])(self, element)
190 elif 'material' in element.attrib:
191 formats = re.split(r"[, ]+",
192 element.attrib.get('format', DEFAULT_MATERIAL_FORMAT))
193 make_url = lambda f: self.options['urlmapper'] \
194 .url_for_material(element.attrib['material'], f)
195 def_href = make_url(formats[0])
197 for f in formats[1:]:
198 fmt_links.append(u'<a href="%s">%s</a>' % (make_url(f), f.upper()))
200 return u"<a href='%s'>" % def_href, u'</a> (%s)' % u' '.join(fmt_links)
203 class Exercise(EduModule):
204 def __init__(self, *args, **kw):
205 self.question_counter = 0
206 super(Exercise, self).__init__(*args, **kw)
208 handle_opis = tag('div', 'description')
210 def handle_rozw_kom(self, element):
211 return u"""<div style="display:none" class="comment">""", u"""</div>"""
213 def handle_cwiczenie(self, element):
214 self.options = {'exercise': element.attrib['typ']}
215 self.question_counter = 0
216 self.piece_counter = 0
219 <div class="exercise %(typ)s" data-type="%(typ)s">
220 <form action="#" method="POST">
223 <div class="buttons">
224 <span class="message"></span>
225 <input type="button" class="check" value="sprawdź"/>
226 <input type="button" class="retry" style="display:none" value="spróbuj ponownie"/>
227 <input type="button" class="solutions" value="pokaż rozwiązanie"/>
228 <input type="button" class="reset" value="reset"/>
233 # Add a single <pytanie> tag if it's not there
234 if not element.xpath(".//pytanie"):
235 qpre, qpost = self.handle_pytanie(element)
240 def handle_pytanie(self, element):
241 """This will handle <cwiczenie> element, when there is no <pytanie>
244 self.question_counter += 1
245 self.piece_counter = 0
246 solution = element.attrib.get('rozw', None)
247 if solution: solution_s = ' data-solution="%s"' % solution
248 else: solution_s = ''
250 handles = element.attrib.get('uchwyty', None)
252 add_class += ' handles handles-%s' % handles
253 self.options = {'handles': handles}
255 minimum = element.attrib.get('min', None)
256 if minimum: minimum_s = ' data-minimum="%d"' % int(minimum)
259 return '<div class="question%s" data-no="%d" %s>' %\
260 (add_class, self.question_counter, solution_s + minimum_s), \
264 class Wybor(Exercise):
265 def handle_cwiczenie(self, element):
266 pre, post = super(Wybor, self).handle_cwiczenie(element)
267 is_single_choice = True
268 for p in element.xpath(".//pytanie"):
269 solutions = re.split(r"[, ]+", p.attrib['rozw'])
270 if len(solutions) != 1:
271 is_single_choice = False
273 choices = element.xpath(".//*[@nazwa]")
275 for n in choices: uniq.add(n.attrib['nazwa'])
276 if len(choices) != len(uniq):
277 is_single_choice = False
280 self.options = {'single': is_single_choice}
283 def handle_punkt(self, element):
284 if self.options['exercise'] and element.attrib.get('nazwa', None):
285 qc = self.question_counter
286 self.piece_counter += 1
287 no = self.piece_counter
288 eid = "q%(qc)d_%(no)d" % locals()
289 aname = element.attrib.get('nazwa', None)
290 if self.options['single']:
292 <li class="question-piece" data-qc="%(qc)d" data-no="%(no)d" data-name="%(aname)s">
293 <input type="radio" name="q%(qc)d" id="%(eid)s" value="%(aname)s" />
294 <label for="%(eid)s">
295 """ % locals(), u"</label></li>"
298 <li class="question-piece" data-qc="%(qc)d" data-no="%(no)d" data-name="%(aname)s">
299 <input type="checkbox" name="%(eid)s" id="%(eid)s" />
300 <label for="%(eid)s">
301 """ % locals(), u"</label></li>"
304 return super(Wybor, self).handle_punkt(element)
307 class Uporzadkuj(Exercise):
308 def handle_pytanie(self, element):
310 Overrides the returned content default handle_pytanie
312 # we ignore the result, returning our own
313 super(Uporzadkuj, self).handle_pytanie(element)
314 order_items = element.xpath(".//punkt/@rozw")
316 return u"""<div class="question" data-original="%s" data-no="%s">""" % \
317 (','.join(order_items), self.question_counter), \
320 def handle_punkt(self, element):
321 return """<li class="question-piece" data-pos="%(rozw)s"/>""" \
326 class Luki(Exercise):
327 def find_pieces(self, question):
328 return question.xpath(".//luka")
330 def solution_html(self, piece):
331 return piece.text + ''.join(
332 [etree.tostring(n, encoding=unicode)
335 def handle_pytanie(self, element):
336 qpre, qpost = super(Luki, self).handle_pytanie(element)
338 luki = list(enumerate(self.find_pieces(element)))
342 for (i, luka) in luki:
344 luka_html = self.solution_html(luka)
345 luki_html += u'<span class="draggable question-piece" data-no="%d">%s</span>' % (i, luka_html)
346 self.words_html = '<div class="words">%s</div>' % luki_html
350 def handle_opis(self, element):
351 return '', self.words_html
353 def handle_luka(self, element):
354 self.piece_counter += 1
355 return '<span class="placeholder" data-solution="%d"></span>' % self.piece_counter
359 def find_pieces(self, question):
360 return question.xpath(".//zastap")
362 def solution_html(self, piece):
363 return piece.attrib['rozw']
365 def handle_zastap(self, element):
366 self.piece_counter += 1
367 return '<span class="placeholder zastap question-piece" data-solution="%d">' \
368 % self.piece_counter, '</span>'
371 class Przyporzadkuj(Exercise):
372 def handle_pytanie(self, element):
373 pre, post = super(Przyporzadkuj, self).handle_pytanie(element)
374 minimum = element.attrib.get("min", None)
376 self.options = {"min": int(minimum)}
379 def handle_lista(self, lista):
380 if 'nazwa' in lista.attrib:
382 'data-name': lista.attrib['nazwa'],
385 self.options = {'predicate': True}
386 elif 'cel' in lista.attrib:
388 'data-target': lista.attrib['cel'],
391 self.options = {'subject': True, 'handles': 'uchwyty' in lista.attrib}
394 pre, post = super(Przyporzadkuj, self).handle_lista(lista, attrs)
395 return pre, post + '<br class="clr"/>'
397 def handle_punkt(self, element):
398 if self.options['subject']:
399 self.piece_counter += 1
400 if self.options['handles']:
401 return '<li><span data-solution="%s" data-no="%s" class="question-piece draggable handle add-li">%s</span>' % (element.attrib['rozw'], self.piece_counter, self.piece_counter), '</li>'
403 return '<li data-solution="%s" data-no="%s" class="question-piece draggable">' % (element.attrib['rozw'], self.piece_counter), '</li>'
405 elif self.options['predicate']:
406 if self.options['min']:
407 placeholders = u'<li class="placeholder"/>' * self.options['min']
409 placeholders = u'<li class="placeholder multiple"/>'
410 return '<li data-predicate="%(nazwa)s">' % element.attrib, '<ul class="subjects">' + placeholders + '</ul></li>'
413 return super(Przyporzadkuj, self).handle_punkt(element)
416 class PrawdaFalsz(Exercise):
417 def handle_punkt(self, element):
418 if 'rozw' in element.attrib:
419 return u'''<li data-solution="%s" class="question-piece">
420 <span class="buttons">
421 <a href="#" data-value="true" class="true">Prawda</a>
422 <a href="#" data-value="false" class="false">Fałsz</a>
423 </span>''' % {'prawda': 'true', 'falsz': 'false'}[element.attrib['rozw']], '</li>'
425 return super(PrawdaFalsz, self).handle_punkt(element)
428 class EduModuleFormat(Format):
429 def __init__(self, wldoc, **kwargs):
430 super(EduModuleFormat, self).__init__(wldoc, **kwargs)
433 edumod = EduModule({'provider': self.wldoc.provider, 'urlmapper': self})
435 html = edumod.generate(self.wldoc.edoc.getroot())
437 return IOFile.from_string(html.encode('utf-8'))
439 def url_for_material(self, slug, fmt=None):
440 # No briliant idea for an API here.
442 return "%s.%s" % (slug, fmt)
446 def transform(wldoc, stylesheet='edumed', options=None, flags=None):
447 """Transforms the WL document to XHTML.
449 If output_filename is None, returns an XML,
450 otherwise returns True if file has been written,False if it hasn't.
451 File won't be written if it has no content.
453 edumodfor = EduModuleFormat(wldoc)
454 return edumodfor.build()