|
|
"""Markdown filters
|
|
|
This file contains a collection of utility filters for dealing with
|
|
|
markdown within Jinja templates.
|
|
|
"""
|
|
|
#-----------------------------------------------------------------------------
|
|
|
# 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
|
|
|
|
|
|
#-----------------------------------------------------------------------------
|
|
|
# Functions
|
|
|
#-----------------------------------------------------------------------------
|
|
|
|
|
|
def markdown2latex(source):
|
|
|
"""Convert a markdown string to LaTeX 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 markdown.
|
|
|
|
|
|
Returns
|
|
|
-------
|
|
|
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]
|
|
|
|
|
|
|
|
|
def markdown2rst(source):
|
|
|
"""Convert a markdown string to LaTeX 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 markdown.
|
|
|
|
|
|
Returns
|
|
|
-------
|
|
|
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')
|
|
|
|