Skip to content

Commit

Permalink
Alternative way to make anchor_link_text configurable
Browse files Browse the repository at this point in the history
Closes jupytergh-412, alternative to jupytergh-520
  • Loading branch information
takluyver committed Jan 29, 2017
1 parent 605bac3 commit 8f2646e
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
17 changes: 16 additions & 1 deletion nbconvert/exporters/html.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@

import os

from traitlets import default
from traitlets import default, Unicode
from traitlets.config import Config

from nbconvert.filters.highlight import Highlight2HTML
from nbconvert.filters.markdown_mistune import IPythonRenderer, MarkdownWithMath

from .templateexporter import TemplateExporter

Expand All @@ -21,6 +22,9 @@ class HTMLExporter(TemplateExporter):
filters, just change the 'template_file' config option.
"""

anchor_link_text = Unicode(u'¶',
help="The text used as the text for anchor links.").tag(config=True)

@default('file_extension')
def _file_extension_default(self):
return '.html'
Expand Down Expand Up @@ -61,6 +65,17 @@ def default_config(self):
c.merge(super(HTMLExporter,self).default_config)
return c

def markdown2html(self, source):
"""Markdown to HTML filter respecting the anchor_link_text setting"""
renderer = IPythonRenderer(escape=False,
anchor_link_text=self.anchor_link_text)
return MarkdownWithMath(renderer=renderer).render(source)

def default_filters(self):
for pair in super(HTMLExporter, self).default_filters():
yield pair
yield ('markdown2html', self.markdown2html)

def from_notebook_node(self, nb, resources=None, **kw):
langinfo = nb.metadata.get('language_info', {})
lexer = langinfo.get('pygments_lexer', langinfo.get('name', None))
Expand Down
3 changes: 2 additions & 1 deletion nbconvert/filters/markdown_mistune.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,8 @@ def block_code(self, code, lang):

def header(self, text, level, raw=None):
html = super(IPythonRenderer, self).header(text, level, raw=raw)
return add_anchor(html)
anchor_link_text = self.options.get('anchor_link_text', u'¶')
return add_anchor(html, anchor_link_text=anchor_link_text)

# Pass math through unaltered - mathjax does the rendering in the browser
def block_math(self, text):
Expand Down
4 changes: 2 additions & 2 deletions nbconvert/filters/strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def _convert_header_id(header_contents):
"""
return header_contents.replace(' ', '-')

def add_anchor(html):
def add_anchor(html, anchor_link_text=u'¶'):
"""Add an id and an anchor-link to an html header
For use on markdown headings
Expand All @@ -99,7 +99,7 @@ def add_anchor(html):
link = _convert_header_id(html2text(h))
h.set('id', link)
a = ElementTree.Element("a", {"class" : "anchor-link", "href" : "#" + link})
a.text = u'¶'
a.text = anchor_link_text
h.append(a)

# Known issue of Python3.x, ElementTree.tostring() returns a byte string
Expand Down

0 comments on commit 8f2646e

Please sign in to comment.