From f239f6fd223afd3d9c7320a69f4dd319ca382fd3 2014-07-17 17:06:10 From: Thomas Kluyver Date: 2014-07-17 17:06:10 Subject: [PATCH] Fix code block rendering when lexer not known --- diff --git a/IPython/nbconvert/filters/markdown.py b/IPython/nbconvert/filters/markdown.py index fe345a5..e7e0154 100755 --- a/IPython/nbconvert/filters/markdown.py +++ b/IPython/nbconvert/filters/markdown.py @@ -25,6 +25,7 @@ import mistune from pygments import highlight from pygments.lexers import get_lexer_by_name from pygments.formatters import HtmlFormatter +from pygments.util import ClassNotFound # IPython imports from IPython.nbconvert.utils.pandoc import pandoc @@ -118,10 +119,17 @@ class MarkdownWithMath(mistune.Markdown): class IPythonRenderer(mistune.Renderer): def block_code(self, code, lang): + if lang: + try: + lexer = get_lexer_by_name(lang, stripall=True) + except ClassNotFound: + code = lang + '\n' + code + lang = None + if not lang: return '\n
%s
\n' % \ mistune.escape(code) - lexer = get_lexer_by_name(lang, stripall=True) + formatter = HtmlFormatter() return highlight(code, lexer, formatter)