Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
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 25f737c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
29 changes: 24 additions & 5 deletions nikola/filters.py
Expand Up @@ -33,7 +33,6 @@
import shutil
import subprocess
import tempfile
import uuid
import shlex

import lxml
Expand All @@ -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):
Expand Down Expand Up @@ -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")
4 changes: 3 additions & 1 deletion nikola/utils.py
Expand Up @@ -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


Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit 25f737c

Please sign in to comment.