From 515398ba21b6ac3c0db230a465cfc8528db8d10e Mon Sep 17 00:00:00 2001 From: Radek Czajka Date: Wed, 20 Feb 2013 13:53:08 +0100 Subject: [PATCH] slugify anchors, fix some minor html issues --- librarian/pyhtml.py | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/librarian/pyhtml.py b/librarian/pyhtml.py index b4d8557..1f1f3df 100644 --- a/librarian/pyhtml.py +++ b/librarian/pyhtml.py @@ -13,6 +13,17 @@ from copy import deepcopy IMAGE_THUMB_WIDTH = 300 +try: + from fnpdjango.utils.text.slughifi import slughifi + def naglowek_to_anchor(naglowek): + return slughifi(naglowek.text) +except ImportError: + from urllib import quote + def naglowek_to_anchor(naglowek): + return quote(re.sub(r" +", " ", naglowek.text.strip())) + + + class EduModule(Xmill): def __init__(self, options=None): super(EduModule, self).__init__(options) @@ -54,25 +65,22 @@ class EduModule(Xmill): handle_tytul_dziela = tag('em', 'title') handle_slowo_obce = tag('em', 'foreign') - def naglowek_to_anchor(self, naglowek): - return re.sub(r" +", " ", naglowek.text.strip()) - def handle_nazwa_utworu(self, element): toc = [] for naglowek in element.getparent().findall('.//naglowek_rozdzial'): a = etree.Element("a") - a.attrib["href"] = "#" + self.naglowek_to_anchor(naglowek) + a.attrib["href"] = "#" + naglowek_to_anchor(naglowek) a.text = naglowek.text atxt = etree.tostring(a, encoding=unicode) toc.append("
  • %s
  • " % atxt) toc = "" % "".join(toc) add_header = "Lekcja: " if self.options['wldoc'].book_info.type in ('course', 'synthetic') else '' - return "

    %s" % add_header, "

    " + toc + return "

    %s" % add_header, "

    " + toc def handle_naglowek_rozdzial(self, element): return_to_top = u"wróć do spisu treści" - anchor = "".join(tag_open_close("a", name=self.naglowek_to_anchor(element))) - return return_to_top + "

    ", anchor + "

    " + pre, post = tag_open_close("h2", id=naglowek_to_anchor(element)) + return return_to_top + pre, post def handle_uwaga(self, _e): return None @@ -442,7 +450,7 @@ Overrides the returned content default handle_pytanie u"""""" def handle_punkt(self, element): - return """
  • """ \ + return """
  • """ \ % element.attrib,\ "
  • " @@ -549,9 +557,9 @@ class Przyporzadkuj(Exercise): elif self.options['predicate']: if self.options['min']: - placeholders = u'
  • ' * self.options['min'] + placeholders = u'
  • ' * self.options['min'] else: - placeholders = u'
  • ' + placeholders = u'
  • ' return '
  • ' % element.attrib, '
  • ' else: -- 2.20.1