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 | 25 | # other libs/dependencies |
|
26 | 26 | from jinja2 import Environment, FileSystemLoader |
|
27 | from markdown import markdown | |
|
28 | 27 | |
|
29 | 28 | # IPython imports |
|
30 | 29 | from IPython.config.configurable import Configurable |
@@ -45,7 +44,7 b" JINJA_EXTENSIONS = ['jinja2.ext.loopcontrols']" | |||
|
45 | 44 | |
|
46 | 45 | default_filters = { |
|
47 | 46 | 'indent': indent, |
|
48 | 'markdown': markdown, | |
|
47 | 'markdown': filters.markdown2html, | |
|
49 | 48 | 'ansi2html': filters.ansi2html, |
|
50 | 49 | 'filter_data_type': filters.DataTypeFilter, |
|
51 | 50 | 'get_lines': filters.get_lines, |
@@ -19,16 +19,18 b' from __future__ import print_function' | |||
|
19 | 19 | import sys |
|
20 | 20 | import subprocess |
|
21 | 21 | |
|
22 | from IPython.nbconvert.utils.pandoc import pandoc | |
|
23 | ||
|
22 | 24 | #----------------------------------------------------------------------------- |
|
23 | 25 | # Functions |
|
24 | 26 | #----------------------------------------------------------------------------- |
|
25 | 27 | |
|
26 | 28 | __all__ = [ |
|
29 | 'markdown2html', | |
|
27 | 30 | 'markdown2latex', |
|
28 | 31 | 'markdown2rst' |
|
29 | 32 | ] |
|
30 | 33 | |
|
31 | ||
|
32 | 34 | def markdown2latex(source): |
|
33 | 35 | """Convert a markdown string to LaTeX via pandoc. |
|
34 | 36 | |
@@ -45,18 +47,13 b' def markdown2latex(source):' | |||
|
45 | 47 | out : string |
|
46 | 48 | Output as returned by pandoc. |
|
47 | 49 | """ |
|
48 | p = subprocess.Popen('pandoc -f markdown -t latex'.split(), | |
|
49 | stdin=subprocess.PIPE, stdout=subprocess.PIPE) | |
|
50 | ||
|
51 | out, err = p.communicate(source.encode('utf-8')) | |
|
52 | ||
|
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] | |
|
50 | return pandoc(source, 'markdown', 'latex') | |
|
58 | 51 | |
|
59 | 52 | |
|
53 | def markdown2html(source): | |
|
54 | """Convert a markdown string to HTML via pandoc""" | |
|
55 | return pandoc(source, 'markdown', 'html') | |
|
56 | ||
|
60 | 57 | def markdown2rst(source): |
|
61 | 58 | """Convert a markdown string to LaTeX via pandoc. |
|
62 | 59 | |
@@ -73,13 +70,5 b' def markdown2rst(source):' | |||
|
73 | 70 | out : string |
|
74 | 71 | Output as returned by pandoc. |
|
75 | 72 | """ |
|
76 | p = subprocess.Popen('pandoc -f markdown -t rst'.split(), | |
|
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 | ||
|
85 | return unicode(out, 'utf-8') | |
|
73 | return pandoc(source, 'markdown', 'rst') | |
|
74 |
General Comments 0
You need to be logged in to leave comments.
Login now