Add lesson TOC. Also: don't just arbitrary paste data into XML.
authorRadek Czajka <radoslaw.czajka@nowoczesnapolska.org.pl>
Mon, 11 Feb 2013 13:28:12 +0000 (14:28 +0100)
committerRadek Czajka <radoslaw.czajka@nowoczesnapolska.org.pl>
Mon, 11 Feb 2013 13:28:43 +0000 (14:28 +0100)
librarian/pyhtml.py
librarian/xmlutils.py

index 56b472c..302d790 100644 (file)
@@ -5,7 +5,7 @@
 #
 from lxml import etree
 from librarian import IOFile, RDFNS, DCNS, Format
-from xmlutils import Xmill, tag, tagged, ifoption
+from xmlutils import Xmill, tag, tagged, ifoption, tag_open_close
 from librarian import functions
 import re
 import random
@@ -31,11 +31,10 @@ class EduModule(Xmill):
 """, u"</div>"
 
     handle_autor_utworu = tag("span", "author")
-    handle_nazwa_utworu = tag("h1", "title")
     handle_dzielo_nadrzedne = tag("span", "collection")
     handle_podtytul = tag("span", "subtitle")
     handle_naglowek_akt = handle_naglowek_czesc = handle_srodtytul = tag("h2")
-    handle_naglowek_scena = handle_naglowek_rozdzial = tag('h2')
+    handle_naglowek_scena = tag('h2')
     handle_naglowek_osoba = handle_naglowek_podrozdzial = tag('h3')
     handle_akap = handle_akap_dialog = handle_akap_cd = tag('p', 'paragraph')
     handle_strofa = tag('div', 'stanza')
@@ -43,6 +42,21 @@ class EduModule(Xmill):
     handle_tytul_dziela = tag('em', 'title')
     handle_slowo_obce = tag('em', 'foreign')
 
+    def handle_nazwa_utworu(self, element):
+        toc = []
+        for naglowek in element.getparent().findall('.//naglowek_rozdzial'):
+            a = etree.Element("a")
+            a.attrib["href"] = "#" + naglowek.text
+            a.text = naglowek.text
+            atxt = etree.tostring(a, encoding=unicode)
+            toc.append("<li>%s</li>" % atxt)
+        toc = "<ul class='toc'>%s</ul>" % "".join(toc)
+        return "<h1 class='title'>Lekcja: ", "</h1>" + toc
+
+    @tagged("h2")
+    def handle_naglowek_rozdzial(self, element):
+        return "", "".join(tag_open_close("a", name=element.text))
+
     def handle_uwaga(self, _e):
         return None
 
index 37a719b..97a3039 100644 (file)
@@ -117,16 +117,30 @@ class Xmill(object):
         return out
 
 
-def tag(name, classes=None, **attrs):
+def tag_open_close(name_, classes_=None, **attrs):
+    u"""Creates tag beginning and end.
+    
+    >>> tag_open_close("a", "klass", x=u"ą<")
+    (u'<a x="\\u0105&lt;" class="klass">', u'</a>')
+
+    """
+    if classes_:
+        if isinstance(classes_, (tuple, list)): classes_ = ' '.join(classes_)
+        attrs['class'] = classes_
+
+    e = etree.Element(name_)
+    e.text = " "
+    for k, v in attrs.items():
+        e.attrib[k] = v
+    pre, post = etree.tostring(e, encoding=unicode).split(u"> <")
+    return pre + u">", u"<" + post
+
+def tag(name_, classes_=None, **attrs):
     """Returns a handler which wraps node contents in tag `name', with class attribute
     set to `classes' and other attributes according to keyword paramters
     """
-    if classes:
-        if isinstance(classes, (tuple, list)): classes = ' '.join(classes)
-        attrs['class'] = classes
-    a = ''.join([' %s="%s"' % (k,v) for (k,v) in attrs.items()])
     def _hnd(self, element):
-        return "<%s%s>" % (name, a), "</%s>" % name
+        return tag_open_close(name_, classes_, **attrs)
     return _hnd