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 OutputFile, RDFNS, DCNS
8 from xmlutils import Xmill, tag, tagged, ifoption
11 class EduModule(Xmill):
12 def __init__(self, *args):
13 super(EduModule, self).__init__(*args)
14 self.activity_counter = 0
16 def handle_powiesc(self, element):
18 <div class="module" id="book-text">
19 <span class="teacher-toggle">
20 <input type="checkbox" name="teacher-toggle" id="teacher-toggle"/>
21 <label for="teacher-toggle">Pokaż treść dla nauczyciela</label>
27 handle_autor_utworu = tag("span", "author")
28 handle_nazwa_utworu = tag("h1", "title")
29 handle_dzielo_nadrzedne = tag("span", "collection")
30 handle_podtytul = tag("span", "subtitle")
31 handle_naglowek_akt = handle_naglowek_czesc = handle_srodtytul = tag("h2")
32 handle_naglowek_scena = handle_naglowek_rozdzial = tag('h3')
33 handle_naglowek_osoba = handle_naglowek_podrozdzial = tag('h4')
34 handle_akap = handle_akap_dialog = handle_akap_cd = tag('p', 'paragraph')
35 handle_strofa = tag('div', 'stanza')
37 def handle_aktywnosc(self, element):
38 self.activity_counter += 1
41 'activity_counter': self.activity_counter
45 opis = submill.generate(element.xpath('opis')[0])
47 n = element.xpath('wskazowki')
48 if n: wskazowki = submill.generate(n[0])
51 n = element.xpath('pomoce')
53 if n: pomoce = submill.generate(n[0])
56 forma = ''.join(element.xpath('forma/text()'))
58 czas = ''.join(element.xpath('czas/text()'))
60 counter = self.activity_counter
63 <div class="activity">
64 <div class="text">%(counter)d.
69 <p>Czas: %(czas)s min</p>
70 <p>Forma: %(forma)s</p>
73 <div class="clearboth"></div>
77 handle_opis = ifoption(activity=False)(tag('div', 'description'))
78 handle_wskazowki = ifoption(activity=False)(tag('div', ('hints', 'teacher')))
80 @ifoption(activity=False)
81 @tagged('div', 'materials')
82 def handle_pomoce(self, _):
85 def handle_czas(self, *_):
88 def handle_forma(self, *_):
91 def handle_cwiczenie(self, element):
92 excercise_handlers = {
94 'uporzadkuj': Uporzadkuj,
97 'przyporzadkuj': Przyporzadkuj,
98 'prawdafalsz': PrawdaFalsz
101 typ = element.attrib['typ']
102 handler = excercise_handlers[typ](self.options)
103 return handler.generate(element)
106 def handle_lista(self, element, attrs={}):
107 ltype = element.attrib.get('typ', 'punkt')
108 if ltype == 'slowniczek':
109 self.options = {'slowniczek': True}
110 return '<div class="slowniczek">', '</div>'
111 ### robie teraz punkty wyboru
112 listtag = {'num': 'ol',
115 'czytelnia': 'ul'}[ltype]
117 classes = attrs.get('class', '')
118 if classes: del attrs['class']
120 attrs_s = ' '.join(['%s="%s"' % kv for kv in attrs.items()])
121 if attrs_s: attrs_s = ' ' + attrs_s
123 return '<%s class="lista %s %s"%s>' % (listtag, ltype, classes, attrs_s), '</%s>' % listtag
125 def handle_punkt(self, element):
126 if self.options['slowniczek']:
127 return '<dl>', '</dl>'
129 return '<li>', '</li>'
131 def handle_rdf__RDF(self, _):
132 # ustal w opcjach rzeczy :D
136 class Excercise(EduModule):
137 def __init__(self, *args, **kw):
138 self.question_counter = 0
139 super(Excercise, self).__init__(*args, **kw)
141 def handle_rozw_kom(self, element):
144 def handle_cwiczenie(self, element):
145 self.options = {'excercise': element.attrib['typ']}
146 self.question_counter = 0
147 self.piece_counter = 0
150 <div class="excercise %(typ)s" data-type="%(typ)s">
151 <form action="#" method="POST">
154 <div class="buttons">
155 <span class="message"></span>
156 <input type="button" class="check" value="sprawdź"/>
157 <input type="button" class="solutions" value="pokaż rozwiązanie"/>
162 # Add a single <pytanie> tag if it's not there
163 if not element.xpath(".//pytanie"):
164 qpre, qpost = self.handle_pytanie(element)
169 def handle_pytanie(self, element):
170 """This will handle <cwiczenie> element, when there is no <pytanie>
173 self.question_counter += 1
174 self.piece_counter = 0
175 solution = element.attrib.get('rozw', None)
176 if solution: solution_s = ' data-solution="%s"' % solution
177 else: solution_s = ''
179 handles = element.attrib.get('uchwyty', None)
181 add_class += ' handles handles-%s' % handles
182 self.options = {'handles': handles}
185 return '<div class="question%s" data-no="%d" %s>' %\
186 (add_class, self.question_counter, solution_s), \
190 class Wybor(Excercise):
191 def handle_punkt(self, element):
192 if self.options['excercise'] and element.attrib.get('nazwa', None):
193 qc = self.question_counter
194 self.piece_counter += 1
195 no = self.piece_counter
196 eid = "q%(qc)d_%(no)d" % locals()
197 aname = element.attrib.get('nazwa', None)
199 <li class="question-piece" data-qc="%(qc)d" data-no="%(no)d" data-name="%(aname)s">
200 <input type="checkbox" name="" id="%(eid)s" />
201 <label for="%(eid)s">
202 """ % locals(), u"</label></li>"
205 return super(Wybor, self).handle_punkt(element)
208 class Uporzadkuj(Excercise):
209 def handle_pytanie(self, element):
211 Overrides the returned content default handle_pytanie
213 # we ignore the result, returning our own
214 super(Uporzadkuj, self).handle_pytanie(element)
215 order_items = element.xpath(".//punkt/@rozw")
217 return u"""<div class="question" data-original="%s" data-no="%s">""" % \
218 (','.join(order_items), self.question_counter), \
221 def handle_punkt(self, element):
222 return """<li class="question-piece" data-pos="%(rozw)s"/>""" \
227 class Luki(Excercise):
228 def find_pieces(self, question):
229 return question.xpath("//luka")
231 def solution_html(self, piece):
232 return piece.text + ''.join(
233 [etree.tostring(n, encoding=unicode)
236 def handle_pytanie(self, element):
237 qpre, qpost = super(Luki, self).handle_pytanie(element)
239 luki = list(enumerate(self.find_pieces(element)))
243 for (i, luka) in luki:
245 luka_html = self.solution_html(luka)
246 luki_html += u'<span class="draggable question-piece" data-no="%d">%s</span>' % (i, luka_html)
247 self.words_html = '<div class="words">%s</div>' % luki_html
251 def handle_opis(self, element):
252 pre, post = super(Luki, self).handle_opis(element)
253 return pre, self.words_html + post
255 def handle_luka(self, element):
256 self.piece_counter += 1
257 return '<span class="placeholder" data-solution="%d"></span>' % self.piece_counter
261 def find_pieces(self, question):
262 return question.xpath("//zastap")
264 def solution_html(self, piece):
265 return piece.attrib['rozw']
267 def handle_zastap(self, element):
268 self.piece_counter += 1
269 return '<span class="placeholder zastap question-piece" data-solution="%d">' \
270 % self.piece_counter, '</span>'
273 class Przyporzadkuj(Excercise):
274 def handle_lista(self, lista):
275 print "in lista %s %s" % (lista.attrib, self.options)
276 if 'nazwa' in lista.attrib:
278 'data-name': lista.attrib['nazwa'],
281 self.options = {'predicate': True}
282 elif 'cel' in lista.attrib:
284 'data-target': lista.attrib['cel'],
287 self.options = {'subject': True}
290 pre, post = super(Przyporzadkuj, self).handle_lista(lista, attrs)
291 return pre, post + '<br class="clr"/>'
293 def handle_punkt(self, element):
294 print "in punkt %s %s" % (element.attrib, self.options)
296 if self.options['subject']:
297 self.piece_counter += 1
298 if self.options['handles']:
299 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>'
301 return '<li data-solution="%s" data-no="%s" class="question-piece draggable">' % (element.attrib['rozw'], self.piece_counter), '</li>'
303 elif self.options['predicate']:
304 print etree.tostring(element, encoding=unicode)
305 placeholders = u'<li class="placeholder multiple"/>'
306 return '<li data-predicate="%(nazwa)s">' % element.attrib, '<ul class="subjects">' + placeholders + '</ul></li>'
309 return super(Przyporzadkuj, self).handle_punkt(element)
312 class PrawdaFalsz(Excercise):
313 def handle_punkt(self, element):
314 if 'rozw' in element.attrib:
315 return u'''<li data-solution="%s" class="question-piece">
316 <span class="buttons">
317 <a href="#" data-value="true" class="true">Prawda</a>
318 <a href="#" data-value="false" class="false">Fałsz</a>
319 </span>''' % {'prawda': 'true', 'falsz': 'false'}[element.attrib['rozw']], '</li>'
321 return super(PrawdaFalsz, self).handle_punkt(element)
324 def transform(wldoc, stylesheet='edumed', options=None, flags=None):
325 """Transforms the WL document to XHTML.
327 If output_filename is None, returns an XML,
328 otherwise returns True if file has been written,False if it hasn't.
329 File won't be written if it has no content.
331 edumod = EduModule(options)
332 # from pdb import set_trace; set_trace()
333 html = edumod.generate(wldoc.edoc.getroot())
335 return OutputFile.from_string(html.encode('utf-8'))