From 4173cdb48bf4dbfe63d67d172064f0d1980559b4 2014-07-17 17:04:12 From: Thomas Kluyver Date: 2014-07-17 17:04:12 Subject: [PATCH] Make mistune a requirement for nbconvert --- diff --git a/.travis.yml b/.travis.yml index e03027c..8ffacd6 100644 --- a/.travis.yml +++ b/.travis.yml @@ -16,8 +16,7 @@ before_install: - time sudo apt-get install pandoc casperjs nodejs libzmq3-dev # pin tornado < 4 for js tests while phantom is on super old webkit - if [[ $GROUP == 'js' ]]; then pip install 'tornado<4'; fi - - time pip install -f https://nipy.bic.berkeley.edu/wheelhouse/travis jinja2 sphinx pygments tornado requests mock pyzmq jsonschema jsonpointer - - time npm install -g requirejs jquery + - time pip install -f https://nipy.bic.berkeley.edu/wheelhouse/travis jinja2 sphinx pygments tornado requests mock pyzmq jsonschema jsonpointer mistune install: - time python setup.py install -q script: diff --git a/IPython/nbconvert/filters/markdown.py b/IPython/nbconvert/filters/markdown.py index d13417c..5cbd62a 100755 --- a/IPython/nbconvert/filters/markdown.py +++ b/IPython/nbconvert/filters/markdown.py @@ -18,13 +18,12 @@ from __future__ import print_function # Stdlib imports import os import subprocess -import warnings from io import TextIOWrapper, BytesIO -try: - import mistune -except ImportError: - mistune = None +import mistune +from pygments import highlight +from pygments.lexers import get_lexer_by_name +from pygments.formatters import HtmlFormatter # IPython imports from IPython.nbconvert.utils.pandoc import pandoc @@ -70,50 +69,37 @@ def markdown2latex(source): """ return pandoc(source, 'markdown', 'latex') -def markdown2html(source): - """Convert a markdown string to HTML""" - global _node - if _node is None: - # prefer md2html via marked if node.js >= 0.9.12 is available - # node is called nodejs on debian, so try that first - _node = 'nodejs' - if not _verify_node(_node): - _node = 'node' - if not _verify_node(_node): - warnings.warn( "Node.js 0.9.12 or later wasn't found.\n" + - "Nbconvert will try to use Pandoc instead.") - _node = False - if _node: - return markdown2html_marked(source) - if mistune is not None: - return markdown2html_mistune(source) - else: - return markdown2html_pandoc(source) - -if mistune is not None: - from pygments import highlight - from pygments.lexers import get_lexer_by_name - from pygments.formatters import HtmlFormatter - - class MyRenderer(mistune.Renderer): - def block_code(self, code, lang): - 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) + +class MyRenderer(mistune.Renderer): + def block_code(self, code, lang): + 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) def markdown2html_mistune(source): + """Convert a markdown string to HTML using mistune""" return mistune.Markdown(renderer=MyRenderer()).render(source) def markdown2html_pandoc(source): """Convert a markdown string to HTML via pandoc""" return pandoc(source, 'markdown', 'html', extra_args=['--mathjax']) +def _find_nodejs(): + global _node + if _node is None: + # prefer md2html via marked if node.js >= 0.9.12 is available + # node is called nodejs on debian, so try that first + _node = 'nodejs' + if not _verify_node(_node): + _node = 'node' + return _node + def markdown2html_marked(source, encoding='utf-8'): """Convert a markdown string to HTML via marked""" - command = [_node, marked] + command = [_find_nodejs(), marked] try: p = subprocess.Popen(command, stdin=subprocess.PIPE, stdout=subprocess.PIPE @@ -127,6 +113,9 @@ def markdown2html_marked(source, encoding='utf-8'): out = TextIOWrapper(BytesIO(out), encoding, 'replace').read() return out.rstrip('\n') +# The mistune renderer is the default, because it's simple to depend on it +markdown2html = markdown2html_mistune + def markdown2rst(source): """Convert a markdown string to ReST via pandoc. diff --git a/setup.py b/setup.py index 4f69925..d704a66 100755 --- a/setup.py +++ b/setup.py @@ -274,7 +274,7 @@ extras_require = dict( terminal = [], nbformat = ['jsonschema>=2.0', 'jsonpointer>=1.3'], notebook = ['tornado>=3.1', 'pyzmq>=2.1.11', 'jinja2'], - nbconvert = ['pygments', 'jinja2', 'Sphinx>=0.3'] + nbconvert = ['pygments', 'jinja2', 'Sphinx>=0.3', 'mistune'] ) if sys.version_info < (3, 3):