From fa5d4c0a64fe04ab409467aa454013db56188aa6 2013-07-09 16:40:19 From: MinRK Date: 2013-07-09 16:40:19 Subject: [PATCH] add markdown2html filter uses pandoc instead of Python markdown for better consistency. Also eliminates one dependency. --- 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') +