headerid.py 867 B

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