Kaynağa Gözat

Merge pull request #864 from linevich/simple_footnotes_python3

[Simple Footnotes] Fix for error: "NameError: name 'unicode' is not defined" (Python3)
Justin Mayer 7 yıl önce
ebeveyn
işleme
15a8530e72
1 değiştirilmiş dosya ile 14 ekleme ve 9 silme
  1. 14 9
      simple_footnotes/simple_footnotes.py

+ 14 - 9
simple_footnotes/simple_footnotes.py

@@ -3,10 +3,12 @@
 
 from pelican import signals
 import html5lib
+import six
 
 RAW_FOOTNOTE_CONTAINERS = ["code"]
 
-def getText(node, recursive = False):
+
+def getText(node, recursive=False):
     """Get all the text associated with this node.
        With recursive == True, all text from child nodes is retrieved."""
     L = [u'']
@@ -16,9 +18,10 @@ def getText(node, recursive = False):
         else:
             if not recursive:
                 return None
-        L.append(getText(n) )
+        L.append(getText(n))
     return u''.join(L)
 
+
 def sequence_gen(genlist):
     for gen in genlist:
         for elem in gen:
@@ -27,12 +30,13 @@ def sequence_gen(genlist):
 
 def parse_for_footnotes(article_or_page_generator):
     all_content = [
-      getattr(article_or_page_generator, attr, None) \
-      for attr in [u'articles',u'drafts',u'pages'] ]
-    all_content = [ x for x in all_content if x is not None ]
+        getattr(article_or_page_generator, attr, None) \
+        for attr in [u'articles', u'drafts', u'pages']]
+    all_content = [x for x in all_content if x is not None]
     for article in sequence_gen(all_content):
         if u"[ref]" in article._content and u"[/ref]" in article._content:
-            content = article._content.replace(u"[ref]", u"<x-simple-footnote>").replace(u"[/ref]", u"</x-simple-footnote>")
+            content = article._content.replace(u"[ref]", u"<x-simple-footnote>").replace(u"[/ref]",
+                                                                                         u"</x-simple-footnote>")
             parser = html5lib.HTMLParser(tree=html5lib.getTreeBuilder(u"dom"))
             dom = parser.parse(content)
             endnotes = []
@@ -56,7 +60,7 @@ def parse_for_footnotes(article_or_page_generator):
                 numbera = dom.createElement(u"a")
                 numbera.setAttribute(u"href", u"#%s" % fnid)
                 numbera.setAttribute(u"class", u"simple-footnote")
-                numbera.appendChild(dom.createTextNode(unicode(count)))
+                numbera.appendChild(dom.createTextNode(six.text_type(count)))
                 txt = getText(footnote, recursive=True).replace(u"\n", u" ")
                 numbera.setAttribute(u"title", txt)
                 number.appendChild(numbera)
@@ -79,8 +83,9 @@ def parse_for_footnotes(article_or_page_generator):
                     e.parentNode.removeChild(e)
                 dom.getElementsByTagName(u"body")[0].appendChild(ol)
                 s = html5lib.serializer.HTMLSerializer(omit_optional_tags=False, quote_attr_values='legacy')
-                output_generator = s.serialize(html5lib.treewalkers.getTreeWalker(u"dom")(dom.getElementsByTagName(u"body")[0]))
-                article._content =  u"".join(list(output_generator)).replace(
+                output_generator = s.serialize(
+                    html5lib.treewalkers.getTreeWalker(u"dom")(dom.getElementsByTagName(u"body")[0]))
+                article._content = u"".join(list(output_generator)).replace(
                     u"<x-simple-footnote>", u"[ref]").replace(u"</x-simple-footnote>", u"[/ref]").replace(
                     u"<body>", u"").replace(u"</body>", u"")