New python html generator
[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         self.question_counter = 0
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="master.book.css"/>
28 <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
29 <script src="edumed.js"></script>
30 </head>
31 <body>
32 """ % v, u"""
33 </body>
34 </html>
35
36 """
37
38     
39     def handle_powiesc(self, element):
40         return u"""
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>
45  </span>
46
47 """, u"</div>"
48
49
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')
59
60     def handle_aktywnosc(self, element):
61         self.activity_counter += 1
62         self.options = {
63             'activity': True, 
64             'activity_counter': self.activity_counter
65             }
66         submill = EduModule()
67
68         opis = submill.generate(element.xpath('opis')[0])
69
70         n = element.xpath('wskazowki')
71         if n: wskazowki = submill.generate(n[0])
72
73         else: wskazowki = ''
74         n = element.xpath('pomoce')
75
76         if n: pomoce = submill.generate(n[0])
77         else: pomoce = ''
78
79         forma = ''.join(element.xpath('forma/text()'))
80
81         czas = ''.join(element.xpath('czas/text()'))
82
83         counter = self.activity_counter
84
85         return u"""
86 <div class="activity">
87  <div class="text">%(counter)d. 
88   %(opis)s
89   %(wskazowki)s
90  </div>
91  <div class="info">
92   <p>Czas: %(czas)s min</p>
93   <p>Forma: %(forma)s</p>
94   %(pomoce)s
95  </div>
96  <div class="clearboth"></div>
97 </div>
98 """ % locals()
99
100     handle_opis = ifoption(activity=False)(tag('div', 'description'))
101     handle_wskazowki = ifoption(activity=False)(tag('div', ('hints', 'teacher')))
102     
103     @ifoption(activity=False)
104     @tagged('div', 'materials')
105     def handle_pomoce(self, _):
106         return "Pomoce: ", ""
107     
108     def handle_czas(self, *_):
109         return
110
111     def handle_forma(self, *_):
112         return
113         
114     def handle_cwiczenie(self, element):
115         self.options = {'excercise': element.attrib['typ']}
116         self.question_counter = 0
117         self.piece_counter = 0
118
119         return u"""
120 <div class="excercise %(typ)s" data-type="%(typ)s">
121 <form action="#" method="POST">
122 """ % element.attrib, \
123 u"""
124 <div class="buttons">
125 <span class="message"></span>
126 <input type="button" class="check" value="sprawdź"/>
127 <input type="button" class="solutions" value="pokaż rozwiązanie"/>
128 </div>
129 </form>
130 </div>
131 """
132  
133     def handle_pytanie(self, element):
134         self.question_counter += 1
135         self.piece_counter = 0
136         solution = element.attrib.get('rozw', None)
137         if solution: solution_s = ' data-solution="%s"' % solution
138         else: solution_s = ''
139
140         return '<div class="question" data-no="%d" %s>' %\
141             (self.question_counter, solution_s), \
142     "</div>"    
143
144     # Lists
145     def handle_lista(self, element):
146         ltype = element.attrib.get('typ', 'punkt')
147         if ltype == 'slowniczek':
148             self.options = {'slowniczek': True}
149             return '<div class="slowniczek">', '</div>'
150 ### robie teraz punkty wyboru
151         listtag = {'num': 'ol', 
152                'punkt': 'ul', 
153                'alfa': 'ul', 
154                'czytelnia': 'ul'}[ltype]
155
156         return '<%s class="lista %s">' % (listtag, ltype), '</%s>' % listtag
157
158     def handle_punkt(self, element):
159         if self.options['excercise'] and element.attrib['nazwa']:
160             qc = self.question_counter
161             self.piece_counter += 1
162             no = self.piece_counter
163
164             return u"""
165 <li class="question-piece" data-qc="%(qc)d" data-no="%(no)d"><input type="checkbox" name="q%(qc)d_%(no)d"/>
166 """ % locals(), u"</li>"
167
168         elif self.options['slowniczek']:
169             return '<dl>', '</dl>'
170         else:
171             return '<li>', '</li>'
172
173     def handle_rdf__RDF(self, _):
174         # ustal w opcjach  rzeczy :D
175         return 
176
177
178 def transform(wldoc, stylesheet='edumed', options=None, flags=None):
179     """Transforms the WL document to XHTML.
180
181     If output_filename is None, returns an XML,
182     otherwise returns True if file has been written,False if it hasn't.
183     File won't be written if it has no content.
184     """
185     
186     edumod = EduModule(options)
187 #    from pdb import set_trace; set_trace()
188     html = edumod.generate(wldoc.edoc.getroot())
189
190     return OutputFile.from_string(html.encode('utf-8'))