Skip to content

Commit

Permalink
Merge pull request #877 from danielfrg/overwrite-highlight_code-filter
Browse files Browse the repository at this point in the history
Add option to overwrite the highlight_code filter
  • Loading branch information
MSeal authored Oct 31, 2018
2 parents 6dd08eb + b61090c commit eb3b0a7
Show file tree
Hide file tree
Showing 11 changed files with 201 additions and 9 deletions.
5 changes: 3 additions & 2 deletions nbconvert/exporters/html.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,9 @@ def process_attachments(self, nb, output):
def from_notebook_node(self, nb, resources=None, **kw):
langinfo = nb.metadata.get('language_info', {})
lexer = langinfo.get('pygments_lexer', langinfo.get('name', None))
self.register_filter('highlight_code',
Highlight2HTML(pygments_lexer=lexer, parent=self))
highlight_code = self.filters.get('highlight_code', Highlight2HTML(pygments_lexer=lexer, parent=self))
self.register_filter('highlight_code', highlight_code)

output, resources = super(HTMLExporter, self).from_notebook_node(nb, resources, **kw)
att_output = self.process_attachments(nb, output)
return att_output, resources
5 changes: 3 additions & 2 deletions nbconvert/exporters/latex.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,9 @@ def default_config(self):
def from_notebook_node(self, nb, resources=None, **kw):
langinfo = nb.metadata.get('language_info', {})
lexer = langinfo.get('pygments_lexer', langinfo.get('name', None))
self.register_filter('highlight_code',
Highlight2Latex(pygments_lexer=lexer, parent=self))
highlight_code = self.filters.get('highlight_code', Highlight2Latex(pygments_lexer=lexer, parent=self))
self.register_filter('highlight_code', highlight_code)

return super(LatexExporter, self).from_notebook_node(nb, resources, **kw)

def _create_environment(self):
Expand Down
15 changes: 15 additions & 0 deletions nbconvert/exporters/tests/test_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,3 +119,18 @@ def test_attachments(self):
result = check_for_png.search(output)
self.assertTrue(result.group(0).strip().startswith('<img src="data:image/png;base64, iVBOR'))
self.assertTrue(result.group(1).strip().startswith('alt="python.png"'))

def test_custom_filter_highlight_code(self):
# Overwriting filters takes place at: Exporter.from_notebook_node
nb = v4.new_notebook()
nb.cells.append(v4.new_code_cell("some_text"))

def custom_highlight_code(source, language="python", metadata=None):
return source + " ADDED_TEXT"

filters = {
"highlight_code": custom_highlight_code
}
(output, resources) = HTMLExporter(template_file='basic', filters=filters).from_notebook_node(nb)
self.assertTrue("ADDED_TEXT" in output)

14 changes: 14 additions & 0 deletions nbconvert/exporters/tests/test_latex.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,3 +147,17 @@ class MyExporter(LatexExporter):
exporter = MyExporter(extra_loaders=[my_loader_tplx])
nb = v4.new_notebook()
out, resources = exporter.from_notebook_node(nb)

def test_custom_filter_highlight_code(self):
# Overwriting filters takes place at: Exporter.from_notebook_node
nb = v4.new_notebook()
nb.cells.append(v4.new_code_cell("some_text"))

def custom_highlight_code(source, language="python", metadata=None, strip_verbatim=False):
return source + " ADDED_TEXT"

filters = {
"highlight_code": custom_highlight_code
}
(output, resources) = LatexExporter(filters=filters).from_notebook_node(nb)
self.assertTrue("ADDED_TEXT" in output)
49 changes: 49 additions & 0 deletions nbconvert/preprocessors/tests/files/Interrupt-IPY6.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false
},
"outputs": [
{
"ename": "KeyboardInterrupt",
"evalue": "",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mKeyboardInterrupt\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-1-31d18a52bf41>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0;32mwhile\u001b[0m \u001b[0;32mTrue\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0;32mcontinue\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mKeyboardInterrupt\u001b[0m: "
]
}
],
"source": [
"while True: continue"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"done\n"
]
}
],
"source": [
"print(\"done\")"
]
}
],
"metadata": {},
"nbformat": 4,
"nbformat_minor": 0
}
2 changes: 1 addition & 1 deletion nbconvert/preprocessors/tests/files/Interrupt.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mKeyboardInterrupt\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-1-31d18a52bf41>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0;32mwhile\u001b[0m \u001b[0;32mTrue\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0;32mcontinue\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;32m<ipython-input-1-31d18a52bf41>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0;32mwhile\u001b[0m \u001b[0;32mTrue\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0;32mcontinue\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mKeyboardInterrupt\u001b[0m: "
]
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"tags": [
"raises-exception"
]
},
"outputs": [
{
"ename": "Exception",
"evalue": "message",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mException\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-1-644b5753a261>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;31m# üñîçø∂é\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0;32mraise\u001b[0m \u001b[0mException\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"message\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mException\u001b[0m: message"
]
}
],
"source": [
"# üñîçø∂é\n",
"raise Exception(\"message\")"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"ok\n"
]
}
],
"source": [
"print('ok')"
]
}
],
"metadata": {},
"nbformat": 4,
"nbformat_minor": 1
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mException\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-1-644b5753a261>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;31m# üñîçø∂é\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0;32mraise\u001b[0m \u001b[0mException\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"message\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;32m<ipython-input-1-644b5753a261>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;31m# üñîçø∂é\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0;32mraise\u001b[0m \u001b[0mException\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"message\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mException\u001b[0m: message"
]
}
Expand Down
50 changes: 50 additions & 0 deletions nbconvert/preprocessors/tests/files/Skip Exceptions-IPY6.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false
},
"outputs": [
{
"ename": "Exception",
"evalue": "message",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mException\u001b[0m Traceback (most recent call last)",
"\u001b[1;32m<ipython-input-1-9abc744909d9>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[1;31m# üñîçø∂é\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[1;32mraise\u001b[0m \u001b[0mException\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"message\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[1;31mException\u001b[0m: message"
]
}
],
"source": [
"# üñîçø∂é\n",
"raise Exception(\"message\")"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"ok\n"
]
}
],
"source": [
"print('ok')"
]
}
],
"metadata": {},
"nbformat": 4,
"nbformat_minor": 0
}
2 changes: 1 addition & 1 deletion nbconvert/preprocessors/tests/files/Skip Exceptions.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mException\u001b[0m Traceback (most recent call last)",
"\u001b[1;32m<ipython-input-1-9abc744909d9>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[1;31m# üñîçø∂é\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[1;32mraise\u001b[0m \u001b[0mException\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"message\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[1;32m<ipython-input-1-9abc744909d9>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[1;31m# üñîçø∂é\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[1;32mraise\u001b[0m \u001b[0mException\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"message\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[1;31mException\u001b[0m: message"
]
}
Expand Down
16 changes: 14 additions & 2 deletions nbconvert/preprocessors/tests/test_execute.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from .base import PreprocessorTestsBase
from ..execute import ExecutePreprocessor, CellExecutionError, executenb

import IPython
from nbconvert.filters import strip_ansi
from testpath import modified_env
from ipython_genutils.py3compat import string_types
Expand Down Expand Up @@ -124,11 +125,22 @@ def test_run_notebooks(self):
input_files = glob.glob(os.path.join(current_dir, 'files', '*.ipynb'))
shared_opts = dict(kernel_name="python")
for filename in input_files:
# There is some slight differences between the output in IPython 6 and IPython 7.
IPY_MAJOR = IPython.version_info[0]
if os.path.basename(filename).endswith("-IPY6.ipynb"):
print(filename, IPY_MAJOR)
if IPY_MAJOR >= 7:
continue
elif os.path.basename(filename) in ("Interrupt.ipynb", "Skip Exceptions with Cell Tags.ipynb", "Skip Exceptions.ipynb"):
if IPY_MAJOR < 7:
continue

# Special arguments for the notebooks
if os.path.basename(filename) == "Disable Stdin.ipynb":
continue
elif os.path.basename(filename) == "Interrupt.ipynb":
elif os.path.basename(filename) in ("Interrupt.ipynb", "Interrupt-IPY6.ipynb"):
opts = dict(timeout=1, interrupt_on_timeout=True, allow_errors=True)
elif os.path.basename(filename) == "Skip Exceptions.ipynb":
elif os.path.basename(filename) in ("Skip Exceptions.ipynb", "Skip Exceptions-IPY6.ipynb"):
opts = dict(allow_errors=True)
else:
opts = dict()
Expand Down

0 comments on commit eb3b0a7

Please sign in to comment.