simple_footnotes.py 4.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*- #
  3. from pelican import signals
  4. import html5lib
  5. RAW_FOOTNOTE_CONTAINERS = ["code"]
  6. def getText(node, recursive = False):
  7. """Get all the text associated with this node.
  8. With recursive == True, all text from child nodes is retrieved."""
  9. L = [u'']
  10. for n in node.childNodes:
  11. if n.nodeType in (node.TEXT_NODE, node.CDATA_SECTION_NODE):
  12. L.append(n.data)
  13. else:
  14. if not recursive:
  15. return None
  16. L.append(getText(n) )
  17. return u''.join(L)
  18. def sequence_gen(genlist):
  19. for gen in genlist:
  20. for elem in gen:
  21. yield elem
  22. def parse_for_footnotes(article_or_page_generator):
  23. all_content = [
  24. getattr(article_or_page_generator, attr, None) \
  25. for attr in [u'articles',u'drafts',u'pages'] ]
  26. all_content = [ x for x in all_content if x is not None ]
  27. for article in sequence_gen(all_content):
  28. if u"[ref]" in article._content and u"[/ref]" in article._content:
  29. content = article._content.replace(u"[ref]", u"<x-simple-footnote>").replace(u"[/ref]", u"</x-simple-footnote>")
  30. parser = html5lib.HTMLParser(tree=html5lib.getTreeBuilder(u"dom"))
  31. dom = parser.parse(content)
  32. endnotes = []
  33. count = 0
  34. for footnote in dom.getElementsByTagName(u"x-simple-footnote"):
  35. pn = footnote
  36. leavealone = False
  37. while pn:
  38. if pn.nodeName in RAW_FOOTNOTE_CONTAINERS:
  39. leavealone = True
  40. break
  41. pn = pn.parentNode
  42. if leavealone:
  43. continue
  44. count += 1
  45. fnid = u"sf-%s-%s" % (article.slug, count)
  46. fnbackid = u"%s-back" % (fnid,)
  47. endnotes.append((footnote, fnid, fnbackid))
  48. number = dom.createElement(u"sup")
  49. number.setAttribute(u"id", fnbackid)
  50. numbera = dom.createElement(u"a")
  51. numbera.setAttribute(u"href", u"#%s" % fnid)
  52. numbera.setAttribute(u"class", u"simple-footnote")
  53. numbera.appendChild(dom.createTextNode(unicode(count)))
  54. txt = getText(footnote, recursive=True).replace(u"\n", u" ")
  55. numbera.setAttribute(u"title", txt)
  56. number.appendChild(numbera)
  57. footnote.parentNode.insertBefore(number, footnote)
  58. if endnotes:
  59. ol = dom.createElement(u"ol")
  60. ol.setAttribute(u"class", u"simple-footnotes")
  61. for e, fnid, fnbackid in endnotes:
  62. li = dom.createElement(u"li")
  63. li.setAttribute(u"id", fnid)
  64. while e.firstChild:
  65. li.appendChild(e.firstChild)
  66. backlink = dom.createElement(u"a")
  67. backlink.setAttribute(u"href", u"#%s" % fnbackid)
  68. backlink.setAttribute(u"class", u"simple-footnote-back")
  69. backlink.appendChild(dom.createTextNode(u'\u21a9'))
  70. li.appendChild(dom.createTextNode(u" "))
  71. li.appendChild(backlink)
  72. ol.appendChild(li)
  73. e.parentNode.removeChild(e)
  74. dom.getElementsByTagName(u"body")[0].appendChild(ol)
  75. s = html5lib.serializer.HTMLSerializer(omit_optional_tags=False, quote_attr_values='legacy')
  76. output_generator = s.serialize(html5lib.treewalkers.getTreeWalker(u"dom")(dom.getElementsByTagName(u"body")[0]))
  77. article._content = u"".join(list(output_generator)).replace(
  78. u"<x-simple-footnote>", u"[ref]").replace(u"</x-simple-footnote>", u"[/ref]").replace(
  79. u"<body>", u"").replace(u"</body>", u"")
  80. def register():
  81. signals.article_generator_finalized.connect(parse_for_footnotes)
  82. signals.page_generator_finalized.connect(parse_for_footnotes)