Skip to content
Permalink
Browse files
add_header_permalinks: use existing IDs or slugify titles
Signed-off-by: Chris Warrick <kwpolska@gmail.com>
  • Loading branch information
Kwpolska committed May 6, 2017
1 parent 461e3b0 commit 25f737c4a43f87ee09736a4e240bc34dc9e76c83
Showing with 27 additions and 6 deletions.
  1. +24 −5 nikola/filters.py
  2. +3 −1 nikola/utils.py
@@ -33,7 +33,6 @@
import shutil
import subprocess
import tempfile
import uuid
import shlex

import lxml
@@ -43,7 +42,7 @@
typo = None # NOQA
import requests

from .utils import req_missing, LOGGER
from .utils import req_missing, LOGGER, slugify


class _ConfigurableFilter(object):
@@ -398,9 +397,29 @@ def _normalize_html(data):
def add_header_permalinks(data):
"""Post-process HTML via lxml to add header permalinks Sphinx-style."""
doc = lxml.html.document_fromstring(data)
for h in ['h1', 'h2', 'h3', 'h4']:
nodes = doc.findall('*//%s' % h)
# Get language for slugify
try:
lang = doc.attrib['lang'] # <html lang="…">
except KeyError:
# Circular import workaround (utils imports filters)
from nikola.utils import LocaleBorg
lang = LocaleBorg().current_lang
for h in ['h1', 'h2', 'h3', 'h4', 'h5', 'h6']:
nodes = doc.findall('*//' + h)
for node in nodes:
new_node = lxml.html.fragment_fromstring('<a id="{0}" href="#{0}" class="headerlink" title="Permalink to this headline">&nbsp;&pi;</a>'.format(uuid.uuid4()))
parent = node.getparent()
if 'id' in node.attrib:
hid = node.attrib['id']
elif 'id' in parent.attrib:
# docutils: <div> has an ID and contains the header
hid = parent.attrib['id']
else:
# Using force-mode, because not every character can appear in a
# HTML id
node.attrib['id'] = slugify(node.text_content(), lang, True)
hid = node.attrib['id']
# TODO: ignore post titles, site title (?) — configurable blacklist?
# TODO: deduplication (another filter)
new_node = lxml.html.fragment_fromstring('&nbsp;<a href="#{0}" class="headerlink" title="Permalink to this heading">¶</a>'.format(hid))
node.append(new_node)
return lxml.html.tostring(doc, encoding="unicode")
@@ -218,7 +218,6 @@ def req_missing(names, purpose, python=True, optional=False):
return msg


from nikola import filters as task_filters # NOQA
ENCODING = sys.getfilesystemencoding() or sys.stdin.encoding


@@ -902,6 +901,9 @@ def current_time(tzinfo=None):
return dt


from nikola import filters as task_filters # NOQA


def apply_filters(task, filters, skip_ext=None):
"""Apply filters to a task.

0 comments on commit 25f737c

Please sign in to comment.