From 381e40f2b9c87b6a9359f2c79cf4687064625288 Mon Sep 17 00:00:00 2001 From: Bryan Newbold Date: Wed, 30 Aug 2017 21:04:39 -0700 Subject: bootstrap 3 + pelican template repo --- plugins/headerid/README.rst | 16 ++++++++++++++++ plugins/headerid/__init__.py | 1 + plugins/headerid/headerid.py | 31 +++++++++++++++++++++++++++++++ 3 files changed, 48 insertions(+) create mode 100644 plugins/headerid/README.rst create mode 100644 plugins/headerid/__init__.py create mode 100644 plugins/headerid/headerid.py (limited to 'plugins/headerid') diff --git a/plugins/headerid/README.rst b/plugins/headerid/README.rst new file mode 100644 index 0000000..7bfa402 --- /dev/null +++ b/plugins/headerid/README.rst @@ -0,0 +1,16 @@ +Pelican ``headerid`` plugin +=========================== + +This plugin adds an anchor to each heading so you can deep-link to headers. +It is intended for formats such as reStructuredText that do not natively +generate these anchors. + +The ``HEADERID_LINK_CHAR`` config can be set to use a different char from ``*`` +for anchor text. + +For Markdown, this plugin is less relevant since the Python-Markdown library +includes a Table of Contents extension that will generate link anchors. +To enable the ``toc`` extension, add a line similar to the following example +to your Pelican settings file:: + + MD_EXTENSIONS = ["codehilite(css_class=highlight)", "extra", "toc"] diff --git a/plugins/headerid/__init__.py b/plugins/headerid/__init__.py new file mode 100644 index 0000000..423261c --- /dev/null +++ b/plugins/headerid/__init__.py @@ -0,0 +1 @@ +from headerid import * \ No newline at end of file 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('%s' % + (anchor_name, LINK_CHAR)) + PelicanHTMLTranslator.depart_title(self, node) + readers.PelicanHTMLTranslator = HeaderIDPatchedPelicanHTMLTranslator -- cgit v1.2.3