Skip to content
Permalink
Browse files
Merge pull request #2767 from getnikola/header_permalinks_file_blacklist
Add a file blacklist for header permalinks
  • Loading branch information
Kwpolska committed May 15, 2017
2 parents 8bd9cb5 + e7a8bb1 commit 1d03f5f1e33893b734d0cb06e2ac56e65a8c452d
Showing with 20 additions and 5 deletions.
  1. +4 −0 docs/manual.txt
  2. +4 −1 nikola/conf.py.in
  3. +12 −4 nikola/filters.py
@@ -1928,6 +1928,10 @@ add_header_permalinks
# Include *every* header (not recommended):
# HEADER_PERMALINKS_XPATH_LIST = ['*//{hx}']

You can also use a file blacklist (``HEADER_PERMALINKS_FILE_BLACKLIST``),
useful for some index pages. Paths include the output directory (eg.
``output/index.html``)

You can apply filters to specific posts or pages by using the ``filters`` metadata field:

.. code:: restructuredtext
@@ -590,11 +590,14 @@ GITHUB_COMMIT_SOURCE = True
# ({hx}} is replaced by headers h1 through h6).
# You must change this if you use a custom theme that does not use
# "e-content entry-content" as a class for post and page contents.

# HEADER_PERMALINKS_XPATH_LIST = ['*//div[@class="e-content entry-content"]//{hx}']
# Include *every* header (not recommended):
# HEADER_PERMALINKS_XPATH_LIST = ['*//{hx}']

# File blacklist for header permalinks. Contains output path
# (eg. 'output/index.html')
# HEADER_PERMALINKS_FILE_BLACKLIST = []

# Expert setting! Create a gzipped copy of each generated file. Cheap server-
# side optimization for very high traffic sites or low memory servers.
# GZIP_FILES = False
@@ -400,10 +400,15 @@ def _normalize_html(data):
normalize_html = apply_to_text_file(_normalize_html)


@_ConfigurableFilter(xpath_list='HEADER_PERMALINKS_XPATH_LIST')
@apply_to_text_file
def add_header_permalinks(data, xpath_list=None):
@_ConfigurableFilter(xpath_list='HEADER_PERMALINKS_XPATH_LIST', file_blacklist='HEADER_PERMALINKS_FILE_BLACKLIST')
def add_header_permalinks(fname, xpath_list=None, file_blacklist=None):
"""Post-process HTML via lxml to add header permalinks Sphinx-style."""
# Blacklist requires custom file handling
file_blacklist = file_blacklist or []
if fname in file_blacklist:
return
with io.open(fname, 'r', encoding='utf-8') as inf:
data = inf.read()
doc = lxml.html.document_fromstring(data)
# Get language for slugify
try:
@@ -436,4 +441,7 @@ def add_header_permalinks(data, xpath_list=None):

new_node = lxml.html.fragment_fromstring('<a href="#{0}" class="headerlink" title="Permalink to this heading">¶</a>'.format(hid))
node.append(new_node)
return lxml.html.tostring(doc, encoding="unicode")

data = lxml.html.tostring(doc, encoding="unicode")
with io.open(fname, 'w+', encoding='utf-8') as outf:
outf.write(data)

0 comments on commit 1d03f5f

Please sign in to comment.