From 3ffce80e2f1c58f741cd6a3d31aa86199e8874f1 Mon Sep 17 00:00:00 2001 From: Radek Czajka Date: Mon, 11 Feb 2013 14:28:12 +0100 Subject: [PATCH] Add lesson TOC. Also: don't just arbitrary paste data into XML. --- librarian/pyhtml.py | 20 +++++++++++++++++--- librarian/xmlutils.py | 26 ++++++++++++++++++++------ 2 files changed, 37 insertions(+), 9 deletions(-) diff --git a/librarian/pyhtml.py b/librarian/pyhtml.py index 56b472c..302d790 100644 --- a/librarian/pyhtml.py +++ b/librarian/pyhtml.py @@ -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"" 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("
  • %s
  • " % atxt) + toc = "" % "".join(toc) + return "

    Lekcja: ", "

    " + 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 diff --git a/librarian/xmlutils.py b/librarian/xmlutils.py index 37a719b..97a3039 100644 --- a/librarian/xmlutils.py +++ b/librarian/xmlutils.py @@ -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'', u'') + + """ + 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), "" % name + return tag_open_close(name_, classes_, **attrs) return _hnd -- 2.20.1