summaryrefslogtreecommitdiffstats
path: root/plugins/headerid/headerid.py
diff options
context:
space:
mode:
authorBryan Newbold <bnewbold@archive.org>2017-08-30 21:04:39 -0700
committerBryan Newbold <bnewbold@archive.org>2017-08-30 21:10:40 -0700
commit381e40f2b9c87b6a9359f2c79cf4687064625288 (patch)
tree5542cd9ae24ac604958f09686c9b53923452d459 /plugins/headerid/headerid.py
downloadarchive3k.org-381e40f2b9c87b6a9359f2c79cf4687064625288.tar.gz
archive3k.org-381e40f2b9c87b6a9359f2c79cf4687064625288.zip
bootstrap 3 + pelican template repo
Diffstat (limited to 'plugins/headerid/headerid.py')
-rw-r--r--plugins/headerid/headerid.py31
1 files changed, 31 insertions, 0 deletions
diff --git a/plugins/headerid/headerid.py b/plugins/headerid/headerid.py
new file mode 100644
index 0000000..ee9d265
--- /dev/null
+++ b/plugins/headerid/headerid.py
@@ -0,0 +1,31 @@
+from pelican import readers
+from pelican.readers import PelicanHTMLTranslator
+from pelican import signals
+from docutils import nodes
+
+LINK_CHAR = '*'
+
+
+def init_headerid(sender):
+ global LINK_CHAR
+ char = sender.settings.get('HEADERID_LINK_CHAR')
+ if char:
+ LINK_CHAR = char
+
+def register():
+ signals.initialized.connect(init_headerid)
+
+
+ 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">%s</a>' %
+ (anchor_name, LINK_CHAR))
+ PelicanHTMLTranslator.depart_title(self, node)
+ readers.PelicanHTMLTranslator = HeaderIDPatchedPelicanHTMLTranslator