diff --git a/IPython/nbconvert/exporters/exporter.py b/IPython/nbconvert/exporters/exporter.py index 7e25156..5ae6296 100755 --- a/IPython/nbconvert/exporters/exporter.py +++ b/IPython/nbconvert/exporters/exporter.py @@ -24,7 +24,6 @@ from copy import deepcopy # other libs/dependencies from jinja2 import Environment, FileSystemLoader -from markdown import markdown # IPython imports from IPython.config.configurable import Configurable @@ -45,7 +44,7 @@ JINJA_EXTENSIONS = ['jinja2.ext.loopcontrols'] default_filters = { 'indent': indent, - 'markdown': markdown, + 'markdown': filters.markdown2html, 'ansi2html': filters.ansi2html, 'filter_data_type': filters.DataTypeFilter, 'get_lines': filters.get_lines, diff --git a/IPython/nbconvert/filters/markdown.py b/IPython/nbconvert/filters/markdown.py index 85669b0..eb842bd 100755 --- a/IPython/nbconvert/filters/markdown.py +++ b/IPython/nbconvert/filters/markdown.py @@ -19,16 +19,18 @@ from __future__ import print_function import sys import subprocess +from IPython.nbconvert.utils.pandoc import pandoc + #----------------------------------------------------------------------------- # Functions #----------------------------------------------------------------------------- __all__ = [ + 'markdown2html', 'markdown2latex', 'markdown2rst' ] - def markdown2latex(source): """Convert a markdown string to LaTeX via pandoc. @@ -45,18 +47,13 @@ def markdown2latex(source): out : string Output as returned by pandoc. """ - p = subprocess.Popen('pandoc -f markdown -t latex'.split(), - stdin=subprocess.PIPE, stdout=subprocess.PIPE) - - out, err = p.communicate(source.encode('utf-8')) - - if err: - print(err, file=sys.stderr) - #print('*'*20+'\n', out, '\n'+'*'*20) # dbg - - return unicode(out, 'utf-8')[:-1] + return pandoc(source, 'markdown', 'latex') +def markdown2html(source): + """Convert a markdown string to HTML via pandoc""" + return pandoc(source, 'markdown', 'html') + def markdown2rst(source): """Convert a markdown string to LaTeX via pandoc. @@ -73,13 +70,5 @@ def markdown2rst(source): out : string Output as returned by pandoc. """ - p = subprocess.Popen('pandoc -f markdown -t rst'.split(), - stdin=subprocess.PIPE, stdout=subprocess.PIPE) - - out, err = p.communicate(source.encode('utf-8')) - - if err: - print(err, file=sys.stderr) - #print('*'*20+'\n', out, '\n'+'*'*20) # dbg - - return unicode(out, 'utf-8') + return pandoc(source, 'markdown', 'rst') + diff --git a/IPython/nbconvert/utils/pandoc.py b/IPython/nbconvert/utils/pandoc.py new file mode 100644 index 0000000..c914cee --- /dev/null +++ b/IPython/nbconvert/utils/pandoc.py @@ -0,0 +1,53 @@ +"""Utility for calling pandoc""" +#----------------------------------------------------------------------------- +# Copyright (c) 2013 the IPython Development Team. +# +# Distributed under the terms of the Modified BSD License. +# +# The full license is in the file COPYING.txt, distributed with this software. +#----------------------------------------------------------------------------- + +#----------------------------------------------------------------------------- +# Imports +#----------------------------------------------------------------------------- + +from __future__ import print_function + +# Stdlib imports +import sys +import subprocess + +# IPython imports +from IPython.utils.py3compat import cast_bytes + +#----------------------------------------------------------------------------- +# Classes and functions +#----------------------------------------------------------------------------- + +def pandoc(source, fmt, to, encoding='utf-8'): + """Convert an input string in format `from` to format `to` via pandoc. + + This function will raise an error if pandoc is not installed. + Any error messages generated by pandoc are printed to stderr. + + Parameters + ---------- + source : string + Input string, assumed to be valid format `from`. + fmt : string + The name of the input format (markdown, etc.) + to : string + The name of the output format (html, etc.) + + Returns + ------- + out : unicode + Output as returned by pandoc. + """ + p = subprocess.Popen(['pandoc', '-f', fmt, '-t', to], + stdin=subprocess.PIPE, stdout=subprocess.PIPE + ) + out, _ = p.communicate(cast_bytes(source, encoding)) + out = out.decode(encoding, 'replace') + return out[:-1] +