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
15 class EduModule(Xmill):
16 def __init__(self, options=None):
17 super(EduModule, self).__init__(options)
18 self.activity_counter = 0
19 self.register_text_filter(functions.substitute_entities)
21 def handle_powiesc(self, element):
23 <div class="module" id="book-text">
24 <!-- <span class="teacher-toggle">
25 <input type="checkbox" name="teacher-toggle" id="teacher-toggle"/>
26 <label for="teacher-toggle">Pokaż treść dla nauczyciela</label>
31 handle_autor_utworu = tag("span", "author")
32 handle_nazwa_utworu = tag("h1", "title")
33 handle_dzielo_nadrzedne = tag("span", "collection")
34 handle_podtytul = tag("span", "subtitle")
35 handle_naglowek_akt = handle_naglowek_czesc = handle_srodtytul = tag("h2")
36 handle_naglowek_scena = handle_naglowek_rozdzial = tag('h2')
37 handle_naglowek_osoba = handle_naglowek_podrozdzial = tag('h3')
38 handle_akap = handle_akap_dialog = handle_akap_cd = tag('p', 'paragraph')
39 handle_strofa = tag('div', 'stanza')
40 handle_wyroznienie = tag('em')
41 handle_tytul_dziela = tag('em', 'title')
42 handle_slowo_obce = tag('em', 'foreign')
44 def handle_aktywnosc(self, element):
45 self.activity_counter += 1
48 'activity_counter': self.activity_counter,
50 submill = EduModule(dict(self.options.items() + {'sub_gen': True}.items()))
52 opis = submill.generate(element.xpath('opis')[0])
54 n = element.xpath('wskazowki')
55 if n: wskazowki = submill.generate(n[0])
58 n = element.xpath('pomoce')
60 if n: pomoce = submill.generate(n[0])
63 forma = ''.join(element.xpath('forma/text()'))
65 czas = ''.join(element.xpath('czas/text()'))
67 counter = self.activity_counter
70 <div class="activity">
72 <span class="act_counter">%(counter)d.</span>
77 <section class="infobox time"><h1>Czas</h1><p>%(czas)s min</p></section>
78 <section class="infobox kind"><h1>Forma</h1><p>%(forma)s</p></section>
81 <div class="clearboth"></div>
85 handle_opis = ifoption(sub_gen=True)(tag('div', 'description'))
86 handle_wskazowki = ifoption(sub_gen=True)(tag('div', ('hints', 'teacher')))
88 @ifoption(sub_gen=True)
89 @tagged('section', 'infobox materials')
90 def handle_pomoce(self, _):
91 return """<h1>Pomoce</h1>""", ""
93 def handle_czas(self, *_):
96 def handle_forma(self, *_):
99 def handle_cwiczenie(self, element):
100 exercise_handlers = {
102 'uporzadkuj': Uporzadkuj,
105 'przyporzadkuj': Przyporzadkuj,
106 'prawdafalsz': PrawdaFalsz
109 typ = element.attrib['typ']
110 handler = exercise_handlers[typ](self.options)
111 return handler.generate(element)
114 def handle_lista(self, element, attrs={}):
115 ltype = element.attrib.get('typ', 'punkt')
116 if ltype == 'slowniczek':
117 surl = element.attrib.get('href', None)
120 sxml = etree.fromstring(self.options['provider'].by_uri(surl).get_string())
121 self.options = {'slowniczek': True, 'slowniczek_xml': sxml }
122 return '<div class="slowniczek">', '</div>'
124 listtag = {'num': 'ol',
127 'czytelnia': 'ul'}[ltype]
129 classes = attrs.get('class', '')
130 if classes: del attrs['class']
132 attrs_s = ' '.join(['%s="%s"' % kv for kv in attrs.items()])
133 if attrs_s: attrs_s = ' ' + attrs_s
135 return '<%s class="lista %s %s"%s>' % (listtag, ltype, classes, attrs_s), '</%s>' % listtag
137 def handle_punkt(self, element):
138 if self.options['slowniczek']:
139 return '<dl>', '</dl>'
141 return '<li>', '</li>'
143 def handle_definiendum(self, element):
144 nxt = element.getnext()
147 # let's pull definiens from another document
148 if self.options['slowniczek_xml'] and (not nxt or nxt.tag != 'definiens'):
149 sxml = self.options['slowniczek_xml']
150 assert element.text != ''
151 defloc = sxml.xpath("//definiendum[text()='%s']" % element.text)
153 definiens = defloc[0].getnext()
154 if definiens.tag == 'definiens':
155 subgen = EduModule(self.options)
156 definiens_s = subgen.generate(definiens)
158 return u"<dt>", u"</dt>" + definiens_s
160 def handle_definiens(self, element):
161 return u"<dd>", u"</dd>"
163 def handle_podpis(self, element):
164 return u"""<div class="caption">""", u"</div>"
166 def handle_tabela(self, element):
167 has_frames = int(element.attrib.get("ramki", "0"))
168 if has_frames: frames_c = "framed"
170 return u"""<table class="%s">""" % frames_c, u"</table>"
172 def handle_wiersz(self, element):
173 return u"<tr>", u"</tr>"
175 def handle_kol(self, element):
176 return u"<td>", u"</td>"
178 def handle_rdf__RDF(self, _):
179 # ustal w opcjach rzeczy :D
182 def handle_link(self, element):
183 if 'url' in element.attrib:
184 return tag('a', href=element.attrib['url'])(self, element)
185 elif 'material' in element.attrib:
186 formats = re.split(r"[, ]+", Element.attrib.get('format', 'odt'))
189 fmt_links.append(u'<a href="%s">%s</a>' % (self.options['urlmapper'].url_for_material(element.attrib['material'], f), f.upper()))
191 return u"", u' (%s)' % u' '.join(fmt_links)
194 class Exercise(EduModule):
195 def __init__(self, *args, **kw):
196 self.question_counter = 0
197 super(Exercise, self).__init__(*args, **kw)
199 handle_opis = tag('div', 'description')
201 def handle_rozw_kom(self, element):
202 return u"""<div style="display:none" class="comment">""", u"""</div>"""
204 def handle_cwiczenie(self, element):
205 self.options = {'exercise': element.attrib['typ']}
206 self.question_counter = 0
207 self.piece_counter = 0
210 <div class="exercise %(typ)s" data-type="%(typ)s">
211 <form action="#" method="POST">
214 <div class="buttons">
215 <span class="message"></span>
216 <input type="button" class="check" value="sprawdź"/>
217 <input type="button" class="retry" style="display:none" value="spróbuj ponownie"/>
218 <input type="button" class="solutions" value="pokaż rozwiązanie"/>
219 <input type="button" class="reset" value="reset"/>
224 # Add a single <pytanie> tag if it's not there
225 if not element.xpath(".//pytanie"):
226 qpre, qpost = self.handle_pytanie(element)
231 def handle_pytanie(self, element):
232 """This will handle <cwiczenie> element, when there is no <pytanie>
235 self.question_counter += 1
236 self.piece_counter = 0
237 solution = element.attrib.get('rozw', None)
238 if solution: solution_s = ' data-solution="%s"' % solution
239 else: solution_s = ''
241 handles = element.attrib.get('uchwyty', None)
243 add_class += ' handles handles-%s' % handles
244 self.options = {'handles': handles}
246 minimum = element.attrib.get('min', None)
247 if minimum: minimum_s = ' data-minimum="%d"' % int(minimum)
250 return '<div class="question%s" data-no="%d" %s>' %\
251 (add_class, self.question_counter, solution_s + minimum_s), \
255 class Wybor(Exercise):
256 def handle_cwiczenie(self, element):
257 pre, post = super(Wybor, self).handle_cwiczenie(element)
258 is_single_choice = True
259 for p in element.xpath(".//pytanie"):
260 solutions = re.split(r"[, ]+", p.attrib['rozw'])
261 if len(solutions) != 1:
262 is_single_choice = False
264 self.options = {'single': is_single_choice}
267 def handle_punkt(self, element):
268 if self.options['exercise'] and element.attrib.get('nazwa', None):
269 qc = self.question_counter
270 self.piece_counter += 1
271 no = self.piece_counter
272 eid = "q%(qc)d_%(no)d" % locals()
273 aname = element.attrib.get('nazwa', None)
274 if self.options['single']:
276 <li class="question-piece" data-qc="%(qc)d" data-no="%(no)d" data-name="%(aname)s">
277 <input type="radio" name="q%(qc)d" id="%(eid)s" value="%(aname)s" />
278 <label for="%(eid)s">
279 """ % locals(), u"</label></li>"
282 <li class="question-piece" data-qc="%(qc)d" data-no="%(no)d" data-name="%(aname)s">
283 <input type="checkbox" name="%(eid)s" id="%(eid)s" />
284 <label for="%(eid)s">
285 """ % locals(), u"</label></li>"
288 return super(Wybor, self).handle_punkt(element)
291 class Uporzadkuj(Exercise):
292 def handle_pytanie(self, element):
294 Overrides the returned content default handle_pytanie
296 # we ignore the result, returning our own
297 super(Uporzadkuj, self).handle_pytanie(element)
298 order_items = element.xpath(".//punkt/@rozw")
300 return u"""<div class="question" data-original="%s" data-no="%s">""" % \
301 (','.join(order_items), self.question_counter), \
304 def handle_punkt(self, element):
305 return """<li class="question-piece" data-pos="%(rozw)s"/>""" \
310 class Luki(Exercise):
311 def find_pieces(self, question):
312 return question.xpath(".//luka")
314 def solution_html(self, piece):
315 return piece.text + ''.join(
316 [etree.tostring(n, encoding=unicode)
319 def handle_pytanie(self, element):
320 qpre, qpost = super(Luki, self).handle_pytanie(element)
322 luki = list(enumerate(self.find_pieces(element)))
326 for (i, luka) in luki:
328 luka_html = self.solution_html(luka)
329 luki_html += u'<span class="draggable question-piece" data-no="%d">%s</span>' % (i, luka_html)
330 self.words_html = '<div class="words">%s</div>' % luki_html
334 def handle_opis(self, element):
335 return '', self.words_html
337 def handle_luka(self, element):
338 self.piece_counter += 1
339 return '<span class="placeholder" data-solution="%d"></span>' % self.piece_counter
343 def find_pieces(self, question):
344 return question.xpath(".//zastap")
346 def solution_html(self, piece):
347 return piece.attrib['rozw']
349 def handle_zastap(self, element):
350 self.piece_counter += 1
351 return '<span class="placeholder zastap question-piece" data-solution="%d">' \
352 % self.piece_counter, '</span>'
355 class Przyporzadkuj(Exercise):
356 def handle_pytanie(self, element):
357 pre, post = super(Przyporzadkuj, self).handle_pytanie(element)
358 minimum = element.attrib.get("min", None)
360 self.options = {"min": int(minimum)}
363 def handle_lista(self, lista):
364 if 'nazwa' in lista.attrib:
366 'data-name': lista.attrib['nazwa'],
369 self.options = {'predicate': True}
370 elif 'cel' in lista.attrib:
372 'data-target': lista.attrib['cel'],
375 self.options = {'subject': True, 'handles': 'uchwyty' in lista.attrib}
378 pre, post = super(Przyporzadkuj, self).handle_lista(lista, attrs)
379 return pre, post + '<br class="clr"/>'
381 def handle_punkt(self, element):
382 if self.options['subject']:
383 self.piece_counter += 1
384 if self.options['handles']:
385 return '<li><span data-solution="%s" data-no="%s" class="question-piece draggable handle">%s</span>' % (element.attrib['rozw'], self.piece_counter, self.piece_counter), '</li>'
387 return '<li data-solution="%s" data-no="%s" class="question-piece draggable">' % (element.attrib['rozw'], self.piece_counter), '</li>'
389 elif self.options['predicate']:
390 if self.options['min']:
391 placeholders = u'<li class="placeholder"/>' * self.options['min']
393 placeholders = u'<li class="placeholder multiple"/>'
394 return '<li data-predicate="%(nazwa)s">' % element.attrib, '<ul class="subjects">' + placeholders + '</ul></li>'
397 return super(Przyporzadkuj, self).handle_punkt(element)
400 class PrawdaFalsz(Exercise):
401 def handle_punkt(self, element):
402 if 'rozw' in element.attrib:
403 return u'''<li data-solution="%s" class="question-piece">
404 <span class="buttons">
405 <a href="#" data-value="true" class="true">Prawda</a>
406 <a href="#" data-value="false" class="false">Fałsz</a>
407 </span>''' % {'prawda': 'true', 'falsz': 'false'}[element.attrib['rozw']], '</li>'
409 return super(PrawdaFalsz, self).handle_punkt(element)
412 class EduModuleFormat(Format):
413 def __init__(self, wldoc, **kwargs):
414 super(EduModuleFormat, self).__init__(wldoc, **kwargs)
417 edumod = EduModule({'provider': self.wldoc.provider, 'urlmapper': self})
419 html = edumod.generate(self.wldoc.edoc.getroot())
421 return IOFile.from_string(html.encode('utf-8'))
423 def url_for_material(self, slug, fmt=None):
424 # No briliant idea for an API here.
426 return "%s.%s" % (slug, fmt)
430 def transform(wldoc, stylesheet='edumed', options=None, flags=None):
431 """Transforms the WL document to XHTML.
433 If output_filename is None, returns an XML,
434 otherwise returns True if file has been written,False if it hasn't.
435 File won't be written if it has no content.
437 edumodfor = EduModuleFormat(wldoc)
438 return edumodfor.build()