##// END OF EJS Templates
Merge pull request #3582 from minrk/nomarkdown...
Matthias Bussonnier -
r11274:50c2fe2c merge
parent child Browse files
Show More
@@ -0,0 +1,53 b''
1 """Utility for calling pandoc"""
2 #-----------------------------------------------------------------------------
3 # Copyright (c) 2013 the IPython Development Team.
4 #
5 # Distributed under the terms of the Modified BSD License.
6 #
7 # The full license is in the file COPYING.txt, distributed with this software.
8 #-----------------------------------------------------------------------------
9
10 #-----------------------------------------------------------------------------
11 # Imports
12 #-----------------------------------------------------------------------------
13
14 from __future__ import print_function
15
16 # Stdlib imports
17 import sys
18 import subprocess
19
20 # IPython imports
21 from IPython.utils.py3compat import cast_bytes
22
23 #-----------------------------------------------------------------------------
24 # Classes and functions
25 #-----------------------------------------------------------------------------
26
27 def pandoc(source, fmt, to, encoding='utf-8'):
28 """Convert an input string in format `from` to format `to` via pandoc.
29
30 This function will raise an error if pandoc is not installed.
31 Any error messages generated by pandoc are printed to stderr.
32
33 Parameters
34 ----------
35 source : string
36 Input string, assumed to be valid format `from`.
37 fmt : string
38 The name of the input format (markdown, etc.)
39 to : string
40 The name of the output format (html, etc.)
41
42 Returns
43 -------
44 out : unicode
45 Output as returned by pandoc.
46 """
47 p = subprocess.Popen(['pandoc', '-f', fmt, '-t', to],
48 stdin=subprocess.PIPE, stdout=subprocess.PIPE
49 )
50 out, _ = p.communicate(cast_bytes(source, encoding))
51 out = out.decode(encoding, 'replace')
52 return out[:-1]
53
@@ -24,7 +24,6 b' from copy import deepcopy'
24
24
25 # other libs/dependencies
25 # other libs/dependencies
26 from jinja2 import Environment, FileSystemLoader
26 from jinja2 import Environment, FileSystemLoader
27 from markdown import markdown
28
27
29 # IPython imports
28 # IPython imports
30 from IPython.config.configurable import Configurable
29 from IPython.config.configurable import Configurable
@@ -45,7 +44,7 b" JINJA_EXTENSIONS = ['jinja2.ext.loopcontrols']"
45
44
46 default_filters = {
45 default_filters = {
47 'indent': indent,
46 'indent': indent,
48 'markdown': markdown,
47 'markdown': filters.markdown2html,
49 'ansi2html': filters.ansi2html,
48 'ansi2html': filters.ansi2html,
50 'filter_data_type': filters.DataTypeFilter,
49 'filter_data_type': filters.DataTypeFilter,
51 'get_lines': filters.get_lines,
50 'get_lines': filters.get_lines,
@@ -19,16 +19,18 b' from __future__ import print_function'
19 import sys
19 import sys
20 import subprocess
20 import subprocess
21
21
22 from IPython.nbconvert.utils.pandoc import pandoc
23
22 #-----------------------------------------------------------------------------
24 #-----------------------------------------------------------------------------
23 # Functions
25 # Functions
24 #-----------------------------------------------------------------------------
26 #-----------------------------------------------------------------------------
25
27
26 __all__ = [
28 __all__ = [
29 'markdown2html',
27 'markdown2latex',
30 'markdown2latex',
28 'markdown2rst'
31 'markdown2rst'
29 ]
32 ]
30
33
31
32 def markdown2latex(source):
34 def markdown2latex(source):
33 """Convert a markdown string to LaTeX via pandoc.
35 """Convert a markdown string to LaTeX via pandoc.
34
36
@@ -45,17 +47,12 b' def markdown2latex(source):'
45 out : string
47 out : string
46 Output as returned by pandoc.
48 Output as returned by pandoc.
47 """
49 """
48 p = subprocess.Popen('pandoc -f markdown -t latex'.split(),
50 return pandoc(source, 'markdown', 'latex')
49 stdin=subprocess.PIPE, stdout=subprocess.PIPE)
50
51 out, err = p.communicate(source.encode('utf-8'))
52
51
53 if err:
54 print(err, file=sys.stderr)
55 #print('*'*20+'\n', out, '\n'+'*'*20) # dbg
56
57 return unicode(out, 'utf-8')[:-1]
58
52
53 def markdown2html(source):
54 """Convert a markdown string to HTML via pandoc"""
55 return pandoc(source, 'markdown', 'html')
59
56
60 def markdown2rst(source):
57 def markdown2rst(source):
61 """Convert a markdown string to LaTeX via pandoc.
58 """Convert a markdown string to LaTeX via pandoc.
@@ -73,13 +70,5 b' def markdown2rst(source):'
73 out : string
70 out : string
74 Output as returned by pandoc.
71 Output as returned by pandoc.
75 """
72 """
76 p = subprocess.Popen('pandoc -f markdown -t rst'.split(),
73 return pandoc(source, 'markdown', 'rst')
77 stdin=subprocess.PIPE, stdout=subprocess.PIPE)
78
79 out, err = p.communicate(source.encode('utf-8'))
80
81 if err:
82 print(err, file=sys.stderr)
83 #print('*'*20+'\n', out, '\n'+'*'*20) # dbg
84
74
85 return unicode(out, 'utf-8')
General Comments 0
You need to be logged in to leave comments. Login now