X-Git-Url: https://git.mdrn.pl/librarian.git/blobdiff_plain/8c5c356ee7710262751d1a629aaa80e272f71918..aeed223c57f453d068216f86941b7de963190536:/librarian/pyhtml.py diff --git a/librarian/pyhtml.py b/librarian/pyhtml.py index 5d4dc50..d3314cc 100644 --- a/librarian/pyhtml.py +++ b/librarian/pyhtml.py @@ -12,30 +12,7 @@ class EduModule(Xmill): def __init__(self, *args): super(EduModule, self).__init__(*args) self.activity_counter = 0 - self.question_counter = 0 - - def handle_utwor(self, element): - v = {} -# from pdb import *; set_trace() - v['title'] = element.xpath('//dc:title/text()', namespaces={'dc':DCNS.uri})[0] - return u""" - - - - -%(title)s - - - - - -""" % v, u""" - - - -""" - def handle_powiesc(self, element): return u"""
@@ -110,17 +87,70 @@ class EduModule(Xmill): def handle_forma(self, *_): return + + def handle_cwiczenie(self, element): + excercise_handlers = { + 'wybor': Wybor, + 'uporzadkuj': Uporzadkuj, + 'luki': Luki, + 'zastap': Zastap, + 'przyporzadkuj': Przyporzadkuj, + 'prawdafalsz': PrawdaFalsz + } + typ = element.attrib['typ'] + handler = excercise_handlers[typ](self.options) + return handler.generate(element) + + # Lists + def handle_lista(self, element, attrs={}): + ltype = element.attrib.get('typ', 'punkt') + if ltype == 'slowniczek': + self.options = {'slowniczek': True} + return '
', '
' +### robie teraz punkty wyboru + listtag = {'num': 'ol', + 'punkt': 'ul', + 'alfa': 'ul', + 'czytelnia': 'ul'}[ltype] + + classes = attrs.get('class', '') + if classes: del attrs['class'] + + attrs_s = ' '.join(['%s="%s"' % kv for kv in attrs.items()]) + if attrs_s: attrs_s = ' ' + attrs_s + + return '<%s class="lista %s %s"%s>' % (listtag, ltype, classes, attrs_s), '' % listtag + + def handle_punkt(self, element): + if self.options['slowniczek']: + return '
', '
' + else: + return '
  • ', '
  • ' + + def handle_rdf__RDF(self, _): + # ustal w opcjach rzeczy :D + return + + +class Excercise(EduModule): + def __init__(self, *args, **kw): + self.question_counter = 0 + super(Excercise, self).__init__(*args, **kw) + + def handle_rozw_kom(self, element): + return None + def handle_cwiczenie(self, element): self.options = {'excercise': element.attrib['typ']} self.question_counter = 0 self.piece_counter = 0 - return u""" + pre = u"""
    -""" % element.attrib, \ -u""" +""" % element.attrib + post = u"""
    @@ -129,8 +159,16 @@ u"""
    """ + # Add a single tag if it's not there + if not element.xpath(".//pytanie"): + qpre, qpost = self.handle_pytanie(element) + pre = pre + qpre + post = qpost + post + return pre, post def handle_pytanie(self, element): + """This will handle element, when there is no + """ self.question_counter += 1 self.piece_counter = 0 solution = element.attrib.get('rozw', None) @@ -139,40 +177,98 @@ u""" return '
    ' %\ (self.question_counter, solution_s), \ - "
    " + "
    " - # Lists - def handle_lista(self, element): - ltype = element.attrib.get('typ', 'punkt') - if ltype == 'slowniczek': - self.options = {'slowniczek': True} - return '
    ', '
    ' -### robie teraz punkty wyboru - listtag = {'num': 'ol', - 'punkt': 'ul', - 'alfa': 'ul', - 'czytelnia': 'ul'}[ltype] - - return '<%s class="lista %s">' % (listtag, ltype), '' % listtag +class Wybor(Excercise): def handle_punkt(self, element): - if self.options['excercise'] and element.attrib['nazwa']: + if self.options['excercise'] and element.attrib.get('nazwa', None): qc = self.question_counter self.piece_counter += 1 no = self.piece_counter - + eid = "q%(qc)d_%(no)d" % locals() + aname = element.attrib.get('nazwa', None) return u""" -
  • -""" % locals(), u"
  • " +
  • + +
  • " - elif self.options['slowniczek']: - return '
    ', '
    ' else: - return '
  • ', '
  • ' + return super(Wybor, self).handle_punkt(element) - def handle_rdf__RDF(self, _): - # ustal w opcjach rzeczy :D - return + +class Uporzadkuj(Excercise): + def handle_pytanie(self, element): + """ +Overrides the returned content default handle_pytanie + """ + # we ignore the result, returning our own + super(Uporzadkuj, self).handle_pytanie(element) + order_items = element.xpath(".//punkt/@rozw") + + return u"""
    """ % \ + (','.join(order_items), self.question_counter), \ + u"""
    """ + + def handle_punkt(self, element): + return """
  • """ \ + % element.attrib,\ + "
  • " + + +class Luki(Excercise): + def handle_luka(self, element): + return '' % element.text + + +class Zastap(Excercise): + def handle_zastap(self, element): + return '' % element.attrib, '' + + +class Przyporzadkuj(Excercise): + def handle_lista(self, lista): + print "in lista %s %s" % (lista.attrib, self.options) + if 'nazwa' in lista.attrib: + attrs = { + 'data-name': lista.attrib['nazwa'], + 'class': 'predicate' + } + self.options = {'predicate': True} + elif 'cel' in lista.attrib: + attrs = { + 'data-target': lista.attrib['cel'], + 'class': 'subject' + } + self.options = {'subject': True} + else: + attrs = {} + pre, post = super(Przyporzadkuj, self).handle_lista(lista, attrs) + return pre, post + '
    ' + + def handle_punkt(self, element): + print "in punkt %s %s" % (element.attrib, self.options) + + if self.options['subject']: + return '
  • ' % element.attrib, '
  • ' + elif self.options['predicate']: + print etree.tostring(element, encoding=unicode) + return '
  • ' % element.attrib, '
  • ' + else: + return super(Przyporzadkuj, self).handle_punkt(element) + + +class PrawdaFalsz(Excercise): + def handle_punkt(self, element): + if 'rozw' in element.attrib: + return u'''
  • + + Prawda + Fałsz + ''' % {'prawda': 'true', 'falsz': 'false'}[element.attrib['rozw']], '
  • ' + else: + return super(PrawdaFalsz, self).handle_punkt(element) def transform(wldoc, stylesheet='edumed', options=None, flags=None): @@ -182,7 +278,6 @@ def transform(wldoc, stylesheet='edumed', options=None, flags=None): otherwise returns True if file has been written,False if it hasn't. File won't be written if it has no content. """ - edumod = EduModule(options) # from pdb import set_trace; set_trace() html = edumod.generate(wldoc.edoc.getroot())