markdown.py
79 lines
| 2.3 KiB
| text/x-python
|
PythonLexer
Jonathan Frederic
|
r10676 | """Markdown filters | ||
This file contains a collection of utility filters for dealing with | ||||
markdown within Jinja templates. | ||||
Jonathan Frederic
|
r10436 | """ | ||
#----------------------------------------------------------------------------- | ||||
# 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 | ||||
Jonathan Frederic
|
r10485 | import sys | ||
Jonathan Frederic
|
r10436 | import subprocess | ||
#----------------------------------------------------------------------------- | ||||
# Functions | ||||
#----------------------------------------------------------------------------- | ||||
Jonathan Frederic
|
r10485 | |||
Jonathan Frederic
|
r10676 | def markdown2latex(source): | ||
Jonathan Frederic
|
r10436 | """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 | ||||
---------- | ||||
Jonathan Frederic
|
r10676 | source : string | ||
Jonathan Frederic
|
r10436 | 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) | ||||
Jonathan Frederic
|
r10676 | |||
out, err = p.communicate(source.encode('utf-8')) | ||||
Jonathan Frederic
|
r10436 | if err: | ||
print(err, file=sys.stderr) | ||||
#print('*'*20+'\n', out, '\n'+'*'*20) # dbg | ||||
Jonathan Frederic
|
r10676 | |||
Bussonnier Matthias
|
r10881 | return unicode(out, 'utf-8')[:-1] | ||
Jonathan Frederic
|
r10436 | |||
Jonathan Frederic
|
r10676 | def markdown2rst(source): | ||
Jonathan Frederic
|
r10436 | """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 | ||||
---------- | ||||
Jonathan Frederic
|
r10676 | source : string | ||
Jonathan Frederic
|
r10436 | 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) | ||||
Jonathan Frederic
|
r10676 | |||
out, err = p.communicate(source.encode('utf-8')) | ||||
Jonathan Frederic
|
r10436 | if err: | ||
print(err, file=sys.stderr) | ||||
#print('*'*20+'\n', out, '\n'+'*'*20) # dbg | ||||
Jonathan Frederic
|
r10676 | |||
return unicode(out, 'utf-8') | ||||