headerid.py 1.1 KB

1234567891011121314151617181920212223242526272829303132
  1. from pelican import readers
  2. from pelican.readers import PelicanHTMLTranslator
  3. from pelican import signals
  4. from docutils import nodes
  5. LINK_CHAR = '*'
  6. def init_headerid(sender):
  7. global LINK_CHAR
  8. char = sender.settings.get('HEADERID_LINK_CHAR')
  9. if char:
  10. LINK_CHAR = char
  11. def register():
  12. signals.initialized.connect(init_headerid)
  13. class HeaderIDPatchedPelicanHTMLTranslator(PelicanHTMLTranslator):
  14. def depart_title(self, node):
  15. close_tag = self.context[-1]
  16. parent = node.parent
  17. if isinstance(parent, nodes.section) and parent.hasattr('ids') and parent['ids']:
  18. anchor_name = parent['ids'][0]
  19. # add permalink anchor
  20. if close_tag.startswith('</h'):
  21. self.body.append(
  22. '<a class="headerlink" href="#%s" title="Permalink to this headline">%s</a>' %
  23. (anchor_name, LINK_CHAR))
  24. PelicanHTMLTranslator.depart_title(self, node)
  25. readers.PelicanHTMLTranslator = HeaderIDPatchedPelicanHTMLTranslator