##// END OF EJS Templates
Make mistune a requirement for nbconvert
Thomas Kluyver -
Show More
@@ -16,8 +16,7 b' before_install:'
16 - time sudo apt-get install pandoc casperjs nodejs libzmq3-dev
16 - time sudo apt-get install pandoc casperjs nodejs libzmq3-dev
17 # pin tornado < 4 for js tests while phantom is on super old webkit
17 # pin tornado < 4 for js tests while phantom is on super old webkit
18 - if [[ $GROUP == 'js' ]]; then pip install 'tornado<4'; fi
18 - if [[ $GROUP == 'js' ]]; then pip install 'tornado<4'; fi
19 - time pip install -f https://nipy.bic.berkeley.edu/wheelhouse/travis jinja2 sphinx pygments tornado requests mock pyzmq jsonschema jsonpointer
19 - time pip install -f https://nipy.bic.berkeley.edu/wheelhouse/travis jinja2 sphinx pygments tornado requests mock pyzmq jsonschema jsonpointer mistune
20 - time npm install -g requirejs jquery
21 install:
20 install:
22 - time python setup.py install -q
21 - time python setup.py install -q
23 script:
22 script:
@@ -18,13 +18,12 b' from __future__ import print_function'
18 # Stdlib imports
18 # Stdlib imports
19 import os
19 import os
20 import subprocess
20 import subprocess
21 import warnings
22 from io import TextIOWrapper, BytesIO
21 from io import TextIOWrapper, BytesIO
23
22
24 try:
23 import mistune
25 import mistune
24 from pygments import highlight
26 except ImportError:
25 from pygments.lexers import get_lexer_by_name
27 mistune = None
26 from pygments.formatters import HtmlFormatter
28
27
29 # IPython imports
28 # IPython imports
30 from IPython.nbconvert.utils.pandoc import pandoc
29 from IPython.nbconvert.utils.pandoc import pandoc
@@ -70,50 +69,37 b' def markdown2latex(source):'
70 """
69 """
71 return pandoc(source, 'markdown', 'latex')
70 return pandoc(source, 'markdown', 'latex')
72
71
73 def markdown2html(source):
72
74 """Convert a markdown string to HTML"""
73 class MyRenderer(mistune.Renderer):
75 global _node
74 def block_code(self, code, lang):
76 if _node is None:
75 if not lang:
77 # prefer md2html via marked if node.js >= 0.9.12 is available
76 return '\n<pre><code>%s</code></pre>\n' % \
78 # node is called nodejs on debian, so try that first
77 mistune.escape(code)
79 _node = 'nodejs'
78 lexer = get_lexer_by_name(lang, stripall=True)
80 if not _verify_node(_node):
79 formatter = HtmlFormatter()
81 _node = 'node'
80 return highlight(code, lexer, formatter)
82 if not _verify_node(_node):
83 warnings.warn( "Node.js 0.9.12 or later wasn't found.\n" +
84 "Nbconvert will try to use Pandoc instead.")
85 _node = False
86 if _node:
87 return markdown2html_marked(source)
88 if mistune is not None:
89 return markdown2html_mistune(source)
90 else:
91 return markdown2html_pandoc(source)
92
93 if mistune is not None:
94 from pygments import highlight
95 from pygments.lexers import get_lexer_by_name
96 from pygments.formatters import HtmlFormatter
97
98 class MyRenderer(mistune.Renderer):
99 def block_code(self, code, lang):
100 if not lang:
101 return '\n<pre><code>%s</code></pre>\n' % \
102 mistune.escape(code)
103 lexer = get_lexer_by_name(lang, stripall=True)
104 formatter = HtmlFormatter()
105 return highlight(code, lexer, formatter)
106
81
107 def markdown2html_mistune(source):
82 def markdown2html_mistune(source):
83 """Convert a markdown string to HTML using mistune"""
108 return mistune.Markdown(renderer=MyRenderer()).render(source)
84 return mistune.Markdown(renderer=MyRenderer()).render(source)
109
85
110 def markdown2html_pandoc(source):
86 def markdown2html_pandoc(source):
111 """Convert a markdown string to HTML via pandoc"""
87 """Convert a markdown string to HTML via pandoc"""
112 return pandoc(source, 'markdown', 'html', extra_args=['--mathjax'])
88 return pandoc(source, 'markdown', 'html', extra_args=['--mathjax'])
113
89
90 def _find_nodejs():
91 global _node
92 if _node is None:
93 # prefer md2html via marked if node.js >= 0.9.12 is available
94 # node is called nodejs on debian, so try that first
95 _node = 'nodejs'
96 if not _verify_node(_node):
97 _node = 'node'
98 return _node
99
114 def markdown2html_marked(source, encoding='utf-8'):
100 def markdown2html_marked(source, encoding='utf-8'):
115 """Convert a markdown string to HTML via marked"""
101 """Convert a markdown string to HTML via marked"""
116 command = [_node, marked]
102 command = [_find_nodejs(), marked]
117 try:
103 try:
118 p = subprocess.Popen(command,
104 p = subprocess.Popen(command,
119 stdin=subprocess.PIPE, stdout=subprocess.PIPE
105 stdin=subprocess.PIPE, stdout=subprocess.PIPE
@@ -127,6 +113,9 b" def markdown2html_marked(source, encoding='utf-8'):"
127 out = TextIOWrapper(BytesIO(out), encoding, 'replace').read()
113 out = TextIOWrapper(BytesIO(out), encoding, 'replace').read()
128 return out.rstrip('\n')
114 return out.rstrip('\n')
129
115
116 # The mistune renderer is the default, because it's simple to depend on it
117 markdown2html = markdown2html_mistune
118
130 def markdown2rst(source):
119 def markdown2rst(source):
131 """Convert a markdown string to ReST via pandoc.
120 """Convert a markdown string to ReST via pandoc.
132
121
@@ -274,7 +274,7 b' extras_require = dict('
274 terminal = [],
274 terminal = [],
275 nbformat = ['jsonschema>=2.0', 'jsonpointer>=1.3'],
275 nbformat = ['jsonschema>=2.0', 'jsonpointer>=1.3'],
276 notebook = ['tornado>=3.1', 'pyzmq>=2.1.11', 'jinja2'],
276 notebook = ['tornado>=3.1', 'pyzmq>=2.1.11', 'jinja2'],
277 nbconvert = ['pygments', 'jinja2', 'Sphinx>=0.3']
277 nbconvert = ['pygments', 'jinja2', 'Sphinx>=0.3', 'mistune']
278 )
278 )
279
279
280 if sys.version_info < (3, 3):
280 if sys.version_info < (3, 3):
General Comments 0
You need to be logged in to leave comments. Login now