don't pass self.
[librarian.git] / librarian / pyhtml.py
1 # -*- coding: utf-8 -*-
2 #
3 # This file is part of Librarian, licensed under GNU Affero GPLv3 or later.
4 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
5 #
6 from lxml import etree
7 from librarian import OutputFile, RDFNS, DCNS
8 from xmlutils import Xmill, tag, tagged, ifoption
9  
10
11 class EduModule(Xmill):
12     def __init__(self, *args):
13         super(EduModule, self).__init__(*args)
14         self.activity_counter = 0
15
16
17 #     def handle_utwor(self, element):
18 #         v = {}
19 # #        from pdb import *; set_trace()
20 #         v['title'] = element.xpath('//dc:title/text()', namespaces={'dc':DCNS.uri})[0]
21 #         return u"""
22 # <!DOCTYPE html>
23 # <html>
24 # <head>
25 # <meta charset="utf-8"/>
26 # <title>%(title)s</title>
27 # <link rel="stylesheet" type="text/css" href="/media/static/edumed/edumed.css"/>
28 # <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
29 # <script src="/media/static/edumed/js/edumed.js"></script>
30 # </head>
31 # <body>
32 # """ % v, u"""
33 # </body>
34 # </html>
35 # """
36
37     
38     def handle_powiesc(self, element):
39         return u"""
40 <div class="module" id="book-text">
41  <span class="teacher-toggle">
42   <input type="checkbox" name="teacher-toggle" id="teacher-toggle"/>
43   <label for="teacher-toggle">Pokaż treść dla nauczyciela</label>
44  </span>
45
46 """, u"</div>"
47
48
49     handle_autor_utworu = tag("span", "author")
50     handle_nazwa_utworu = tag("h1", "title")
51     handle_dzielo_nadrzedne = tag("span", "collection")
52     handle_podtytul = tag("span", "subtitle")
53     handle_naglowek_akt = handle_naglowek_czesc = handle_srodtytul = tag("h2")
54     handle_naglowek_scena = handle_naglowek_rozdzial = tag('h3')
55     handle_naglowek_osoba = handle_naglowek_podrozdzial = tag('h4')
56     handle_akap = handle_akap_dialog = handle_akap_cd = tag('p', 'paragraph')
57     handle_strofa = tag('div', 'stanza')
58
59     def handle_aktywnosc(self, element):
60         self.activity_counter += 1
61         self.options = {
62             'activity': True, 
63             'activity_counter': self.activity_counter
64             }
65         submill = EduModule()
66
67         opis = submill.generate(element.xpath('opis')[0])
68
69         n = element.xpath('wskazowki')
70         if n: wskazowki = submill.generate(n[0])
71
72         else: wskazowki = ''
73         n = element.xpath('pomoce')
74
75         if n: pomoce = submill.generate(n[0])
76         else: pomoce = ''
77
78         forma = ''.join(element.xpath('forma/text()'))
79
80         czas = ''.join(element.xpath('czas/text()'))
81
82         counter = self.activity_counter
83
84         return u"""
85 <div class="activity">
86  <div class="text">%(counter)d. 
87   %(opis)s
88   %(wskazowki)s
89  </div>
90  <div class="info">
91   <p>Czas: %(czas)s min</p>
92   <p>Forma: %(forma)s</p>
93   %(pomoce)s
94  </div>
95  <div class="clearboth"></div>
96 </div>
97 """ % locals()
98
99     handle_opis = ifoption(activity=False)(tag('div', 'description'))
100     handle_wskazowki = ifoption(activity=False)(tag('div', ('hints', 'teacher')))
101     
102     @ifoption(activity=False)
103     @tagged('div', 'materials')
104     def handle_pomoce(self, _):
105         return "Pomoce: ", ""
106     
107     def handle_czas(self, *_):
108         return
109
110     def handle_forma(self, *_):
111         return
112             
113     def handle_cwiczenie(self, element):
114         excercise_handlers = {
115             'wybor': Wybor,
116             'uporzadkuj': Uporzadkuj
117             }
118         
119         typ = element.attrib['typ']
120         handler = excercise_handlers[typ](self.options)
121         return handler.generate(element)
122
123     # Lists
124     def handle_lista(self, element):
125         ltype = element.attrib.get('typ', 'punkt')
126         if ltype == 'slowniczek':
127             self.options = {'slowniczek': True}
128             return '<div class="slowniczek">', '</div>'
129 ### robie teraz punkty wyboru
130         listtag = {'num': 'ol', 
131                'punkt': 'ul', 
132                'alfa': 'ul', 
133                'czytelnia': 'ul'}[ltype]
134
135         return '<%s class="lista %s">' % (listtag, ltype), '</%s>' % listtag
136
137     def handle_punkt(self, element):
138         if self.options['slowniczek']:
139             return '<dl>', '</dl>'
140         else:
141             return '<li>', '</li>'
142
143     def handle_rdf__RDF(self, _):
144         # ustal w opcjach  rzeczy :D
145         return 
146
147
148 class Excercise(EduModule):
149     def __init__(self, *args, **kw):
150         self.question_counter = 0
151         super(Excercise, self).__init__(*args, **kw)
152
153     def handle_cwiczenie(self, element):
154         self.options = {'excercise': element.attrib['typ']}
155         self.question_counter = 0
156         self.piece_counter = 0
157
158         return u"""
159 <div class="excercise %(typ)s" data-type="%(typ)s">
160 <form action="#" method="POST">
161 """ % element.attrib, \
162 u"""
163 <div class="buttons">
164 <span class="message"></span>
165 <input type="button" class="check" value="sprawdź"/>
166 <input type="button" class="solutions" value="pokaż rozwiązanie"/>
167 </div>
168 </form>
169 </div>
170 """
171  
172     def handle_pytanie(self, element):
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 = ''
178
179         return '<div class="question" data-no="%d" %s>' %\
180             (self.question_counter, solution_s), \
181     "</div>"    
182
183
184 class Wybor(Excercise):
185     def handle_punkt(self, element):
186         if self.options['excercise'] and element.attrib.get('nazwa', None):
187             qc = self.question_counter
188             self.piece_counter += 1
189             no = self.piece_counter
190
191             return u"""
192 <li class="question-piece" data-qc="%(qc)d" data-no="%(no)d"><input type="checkbox" name="q%(qc)d_%(no)d"/>
193 """ % locals(), u"</li>"
194
195         else:
196             return super(Wybor, self).handle_punkt(element)
197
198
199 class Uporzadkuj(Excercise):
200     def handle_cwiczenie(self, element):
201         pre, post = super(Uporzadkuj, self).handle_cwiczenie(element)
202         order_items = element.xpath(".//punkt/@rozw")
203         import pdb
204         if order_items == []: pdb.set_trace()
205
206         return pre + u"""<div class="question" data-solution="%s">""" % \
207             ','.join(order_items), \
208             u"""</div>""" + post
209     
210     def handle_punkt(self, element):
211         return """<li class="question-piece" data-pos="%(rozw)s"/>""" % element.attrib,\
212             "</li>"
213
214
215 def transform(wldoc, stylesheet='edumed', options=None, flags=None):
216     """Transforms the WL document to XHTML.
217
218     If output_filename is None, returns an XML,
219     otherwise returns True if file has been written,False if it hasn't.
220     File won't be written if it has no content.
221     """
222     
223     edumod = EduModule(options)
224 #    from pdb import set_trace; set_trace()
225     html = edumod.generate(wldoc.edoc.getroot())
226
227     return OutputFile.from_string(html.encode('utf-8'))