Skip to content

Instantly share code, notes, and snippets.

@ionelmc ionelmc/headerid.py
Last active Mar 17, 2018

Embed
What would you like to do?
Pelican plugin to add permalinks (headerid) to every section header when using reStructuredText. You can see it in action at http://blog.ionelmc.ro
from pelican import readers
from pelican.readers import PelicanHTMLTranslator
from pelican import signals
from docutils import nodes
def register():
class HeaderIDPatchedPelicanHTMLTranslator(PelicanHTMLTranslator):
def depart_title(self, node):
close_tag = self.context[-1]
parent = node.parent
if isinstance(parent, nodes.section) and parent.hasattr('ids') and parent['ids']:
anchor_name = parent['ids'][0]
# add permalink anchor
if close_tag.startswith('</h'):
self.body.append(
'<a class="headerlink" href="#%s" title="Permalink to this headline">*</a>' % anchor_name
)
PelicanHTMLTranslator.depart_title(self, node)
readers.PelicanHTMLTranslator = HeaderIDPatchedPelicanHTMLTranslator
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.