##// END OF EJS Templates
Merge pull request #5210 from minrk/pyreadline...
Merge pull request #5210 from minrk/pyreadline fix pyreadline import in rlineimpl

File last commit:

r15437:4cc75cc2
r15452:132d6299 merge
Show More
markdown.py
116 lines | 3.5 KiB | text/x-python | PythonLexer
Jonathan Frederic
Cleanup and refactor of filters
r10676 """Markdown filters
This file contains a collection of utility filters for dealing with
markdown within Jinja templates.
Jonathan Frederic
Transformer refactor
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
MinRK
add marked.js entry point for md2html in nbconvert...
r14206 import os
Jonathan Frederic
Transformer refactor
r10436 import subprocess
MinRK
prefer marked to pandoc for markdown2html...
r14203 from io import TextIOWrapper, BytesIO
Jonathan Frederic
Transformer refactor
r10436
MinRK
prefer marked to pandoc for markdown2html...
r14203 # IPython imports
MinRK
add markdown2html filter...
r11268 from IPython.nbconvert.utils.pandoc import pandoc
MinRK
prefer marked to pandoc for markdown2html...
r14203 from IPython.nbconvert.utils.exceptions import ConversionException
from IPython.utils.process import find_cmd, FindCmdError
from IPython.utils.py3compat import cast_bytes
MinRK
add markdown2html filter...
r11268
Jonathan Frederic
Transformer refactor
r10436 #-----------------------------------------------------------------------------
# Functions
#-----------------------------------------------------------------------------
MinRK
add marked.js entry point for md2html in nbconvert...
r14206 marked = os.path.join(os.path.dirname(__file__), "marked.js")
Jonathan Frederic
Post code-review, extended refactor.
r10485
Brian E. Granger
Fixing import logic.
r11088 __all__ = [
MinRK
add markdown2html filter...
r11268 'markdown2html',
MinRK
prefer marked to pandoc for markdown2html...
r14203 'markdown2html_pandoc',
'markdown2html_marked',
Brian E. Granger
Fixing import logic.
r11088 'markdown2latex',
MinRK
prefer marked to pandoc for markdown2html...
r14203 'markdown2rst',
Brian E. Granger
Fixing import logic.
r11088 ]
MinRK
add marked.js entry point for md2html in nbconvert...
r14206 class NodeJSMissing(ConversionException):
"""Exception raised when node.js is missing."""
MinRK
prefer marked to pandoc for markdown2html...
r14203 pass
Jonathan Frederic
Cleanup and refactor of filters
r10676 def markdown2latex(source):
Jonathan Frederic
Transformer refactor
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
Cleanup and refactor of filters
r10676 source : string
Jonathan Frederic
Transformer refactor
r10436 Input string, assumed to be valid markdown.
Returns
-------
out : string
Output as returned by pandoc.
"""
MinRK
add markdown2html filter...
r11268 return pandoc(source, 'markdown', 'latex')
Jonathan Frederic
Transformer refactor
r10436
MinRK
prefer marked to pandoc for markdown2html...
r14203 def markdown2html_pandoc(source):
MinRK
add markdown2html filter...
r11268 """Convert a markdown string to HTML via pandoc"""
MinRK
use mathjax-style for math passing through pandoc to html
r11294 return pandoc(source, 'markdown', 'html', extra_args=['--mathjax'])
MinRK
add markdown2html filter...
r11268
MinRK
prefer marked to pandoc for markdown2html...
r14203 def markdown2html_marked(source, encoding='utf-8'):
"""Convert a markdown string to HTML via marked"""
MinRK
handle nodejs executable on debian...
r15437 command = [node_cmd, marked]
MinRK
prefer marked to pandoc for markdown2html...
r14203 try:
p = subprocess.Popen(command,
stdin=subprocess.PIPE, stdout=subprocess.PIPE
)
except OSError as e:
MinRK
add marked.js entry point for md2html in nbconvert...
r14206 raise NodeJSMissing(
MinRK
prefer marked to pandoc for markdown2html...
r14203 "The command '%s' returned an error: %s.\n" % (" ".join(command), e) +
MinRK
add marked.js entry point for md2html in nbconvert...
r14206 "Please check that Node.js is installed."
MinRK
prefer marked to pandoc for markdown2html...
r14203 )
out, _ = p.communicate(cast_bytes(source, encoding))
out = TextIOWrapper(BytesIO(out), encoding, 'replace').read()
return out.rstrip('\n')
Jonathan Frederic
Cleanup and refactor of filters
r10676 def markdown2rst(source):
Jonathan Frederic
Transformer refactor
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
Cleanup and refactor of filters
r10676 source : string
Jonathan Frederic
Transformer refactor
r10436 Input string, assumed to be valid markdown.
Returns
-------
out : string
Output as returned by pandoc.
"""
MinRK
add markdown2html filter...
r11268 return pandoc(source, 'markdown', 'rst')
MinRK
handle nodejs executable on debian...
r15437 # prefer md2html via marked if node.js is available
# node is called nodejs on debian, so try that first
node_cmd = 'nodejs'
MinRK
prefer marked to pandoc for markdown2html...
r14203 try:
MinRK
handle nodejs executable on debian...
r15437 find_cmd(node_cmd)
MinRK
prefer marked to pandoc for markdown2html...
r14203 except FindCmdError:
MinRK
handle nodejs executable on debian...
r15437 node_cmd = 'node'
try:
find_cmd(node_cmd)
except FindCmdError:
markdown2html = markdown2html_pandoc
else:
markdown2html = markdown2html_marked
MinRK
prefer marked to pandoc for markdown2html...
r14203 else:
markdown2html = markdown2html_marked