Browse Source

Merge pull request #418 from ionelmc/headerid

Add headerid plugin for reStructuredText
Justin Mayer 10 years ago
parent
commit
ab8354e6f3
4 changed files with 26 additions and 0 deletions
  1. 2 0
      Readme.rst
  2. 4 0
      headerid/README.rst
  3. 1 0
      headerid/__init__.py
  4. 19 0
      headerid/headerid.py

+ 2 - 0
Readme.rst

@@ -82,6 +82,8 @@ Gravatar                  Assigns the ``author_gravatar`` variable to the Gravat
 
 
 Gzip cache                Enables certain web servers (e.g., Nginx) to use a static cache of gzip-compressed files to prevent the server from compressing files during an HTTP call
 Gzip cache                Enables certain web servers (e.g., Nginx) to use a static cache of gzip-compressed files to prevent the server from compressing files during an HTTP call
 
 
+Headerid                  This plugin adds an anchor to each heading so you can deeplink to headers in reStructuredText articles.
+
 HTML entities             Allows you to enter HTML entities such as ©, <, • inline in a RST document
 HTML entities             Allows you to enter HTML entities such as ©, <, • inline in a RST document
 
 
 HTML tags for rST         Allows you to use HTML tags from within reST documents
 HTML tags for rST         Allows you to use HTML tags from within reST documents

+ 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