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
18 # def handle_utwor(self, element):
20 # # from pdb import *; set_trace()
21 # v['title'] = element.xpath('//dc:title/text()', namespaces={'dc':DCNS.uri})[0]
26 # <meta charset="utf-8"/>
27 # <title>%(title)s</title>
28 # <link rel="stylesheet" type="text/css" href="/media/static/edumed/edumed.css"/>
29 # <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
30 # <script src="/media/static/edumed/js/edumed.js"></script>
39 def handle_powiesc(self, element):
41 <div class="module" id="book-text">
42 <span class="teacher-toggle">
43 <input type="checkbox" name="teacher-toggle" id="teacher-toggle"/>
44 <label for="teacher-toggle">Pokaż treść dla nauczyciela</label>
50 handle_autor_utworu = tag("span", "author")
51 handle_nazwa_utworu = tag("h1", "title")
52 handle_dzielo_nadrzedne = tag("span", "collection")
53 handle_podtytul = tag("span", "subtitle")
54 handle_naglowek_akt = handle_naglowek_czesc = handle_srodtytul = tag("h2")
55 handle_naglowek_scena = handle_naglowek_rozdzial = tag('h3')
56 handle_naglowek_osoba = handle_naglowek_podrozdzial = tag('h4')
57 handle_akap = handle_akap_dialog = handle_akap_cd = tag('p', 'paragraph')
58 handle_strofa = tag('div', 'stanza')
60 def handle_aktywnosc(self, element):
61 self.activity_counter += 1
64 'activity_counter': self.activity_counter
68 opis = submill.generate(element.xpath('opis')[0])
70 n = element.xpath('wskazowki')
71 if n: wskazowki = submill.generate(n[0])
74 n = element.xpath('pomoce')
76 if n: pomoce = submill.generate(n[0])
79 forma = ''.join(element.xpath('forma/text()'))
81 czas = ''.join(element.xpath('czas/text()'))
83 counter = self.activity_counter
86 <div class="activity">
87 <div class="text">%(counter)d.
92 <p>Czas: %(czas)s min</p>
93 <p>Forma: %(forma)s</p>
96 <div class="clearboth"></div>
100 handle_opis = ifoption(activity=False)(tag('div', 'description'))
101 handle_wskazowki = ifoption(activity=False)(tag('div', ('hints', 'teacher')))
103 @ifoption(activity=False)
104 @tagged('div', 'materials')
105 def handle_pomoce(self, _):
106 return "Pomoce: ", ""
108 def handle_czas(self, *_):
111 def handle_forma(self, *_):
114 def handle_cwiczenie(self, element):
115 excercise_handlers = {
117 'uporzadkuj': Uporzadkuj
120 typ = element.attrib['typ']
121 handler = excercise_handlers[typ](self.options)
122 return handler.generate(element)
125 def handle_lista(self, element):
126 ltype = element.attrib.get('typ', 'punkt')
127 if ltype == 'slowniczek':
128 self.options = {'slowniczek': True}
129 return '<div class="slowniczek">', '</div>'
130 ### robie teraz punkty wyboru
131 listtag = {'num': 'ol',
134 'czytelnia': 'ul'}[ltype]
136 return '<%s class="lista %s">' % (listtag, ltype), '</%s>' % listtag
138 def handle_punkt(self, element):
139 if self.options['slowniczek']:
140 return '<dl>', '</dl>'
142 return '<li>', '</li>'
144 def handle_rdf__RDF(self, _):
145 # ustal w opcjach rzeczy :D
149 class Excercise(EduModule):
150 def __init__(self, *args, **kw):
151 self.question_counter = 0
152 super(Excercise, self).__init__(*args, **kw)
154 def handle_cwiczenie(self, element):
155 self.options = {'excercise': element.attrib['typ']}
156 self.question_counter = 0
157 self.piece_counter = 0
160 <div class="excercise %(typ)s" data-type="%(typ)s">
161 <form action="#" method="POST">
162 """ % element.attrib, \
164 <div class="buttons">
165 <span class="message"></span>
166 <input type="button" class="check" value="sprawdź"/>
167 <input type="button" class="solutions" value="pokaż rozwiązanie"/>
173 def handle_pytanie(self, element):
174 self.question_counter += 1
175 self.piece_counter = 0
176 solution = element.attrib.get('rozw', None)
177 if solution: solution_s = ' data-solution="%s"' % solution
178 else: solution_s = ''
180 return '<div class="question" data-no="%d" %s>' %\
181 (self.question_counter, solution_s), \
185 class Wybor(Excercise):
186 def handle_punkt(self, element):
187 if self.options['excercise'] and element.attrib.get('nazwa', None):
188 qc = self.question_counter
189 self.piece_counter += 1
190 no = self.piece_counter
191 eid = "q%(qc)d_%(no)d" % locals()
193 <li class="question-piece" data-qc="%(qc)d" data-no="%(no)d"><input type="checkbox" name="" id="%(eid)s" /><label for="%(eid)s">
194 """ % locals(), u"</label></li>"
197 return super(Wybor, self).handle_punkt(element)
200 class Uporzadkuj(Excercise):
201 def handle_cwiczenie(self, element):
202 pre, post = super(Uporzadkuj, self).handle_cwiczenie(element)
203 order_items = element.xpath(".//punkt/@rozw")
205 if order_items == []: pdb.set_trace()
207 return pre + u"""<div class="question" data-original="%s">""" % \
208 ','.join(order_items), \
211 def handle_punkt(self, element):
212 return """<li class="question-piece" data-pos="%(rozw)s"/>""" % element.attrib,\
216 class Luki(Excercise):
217 def handle_luka(self, element):
218 return '<input type="text" class="luka" data-solution="%s">' % element.text, \
222 class Zastap(Excercise):
223 def handle_zastap(self, element):
224 return '<span class="zastap" data-solution="%(rozw)s">' % element.attrib, '</span>'
229 def transform(wldoc, stylesheet='edumed', options=None, flags=None):
230 """Transforms the WL document to XHTML.
232 If output_filename is None, returns an XML,
233 otherwise returns True if file has been written,False if it hasn't.
234 File won't be written if it has no content.
237 edumod = EduModule(options)
238 # from pdb import set_trace; set_trace()
239 html = edumod.generate(wldoc.edoc.getroot())
241 return OutputFile.from_string(html.encode('utf-8'))