瀏覽代碼

Add headerid plugin.

Ionel Cristian Mărieș 10 年之前
父節點
當前提交
64e96e1de3
共有 3 個文件被更改,包括 24 次插入0 次删除
  1. 4 0
      headerid/README.rst
  2. 1 0
      headerid/__init__.py
  3. 19 0
      headerid/headerid.py

+ 4 - 0
headerid/README.rst

@@ -0,0 +1,4 @@
+Pelican ``headerid`` plugin
+===========================
+
+This plugin adds an anchor to each heading so you can deeplink to headers.

+ 1 - 0
headerid/__init__.py

@@ -0,0 +1 @@
+from headerid import *

+ 19 - 0
headerid/headerid.py

@@ -0,0 +1,19 @@
+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