Show More
@@ -1,74 +1,106 b'' | |||||
1 | """Markdown filters |
|
1 | """Markdown filters | |
2 | This file contains a collection of utility filters for dealing with |
|
2 | This file contains a collection of utility filters for dealing with | |
3 | markdown within Jinja templates. |
|
3 | markdown within Jinja templates. | |
4 | """ |
|
4 | """ | |
5 | #----------------------------------------------------------------------------- |
|
5 | #----------------------------------------------------------------------------- | |
6 | # Copyright (c) 2013, the IPython Development Team. |
|
6 | # Copyright (c) 2013, the IPython Development Team. | |
7 | # |
|
7 | # | |
8 | # Distributed under the terms of the Modified BSD License. |
|
8 | # Distributed under the terms of the Modified BSD License. | |
9 | # |
|
9 | # | |
10 | # The full license is in the file COPYING.txt, distributed with this software. |
|
10 | # The full license is in the file COPYING.txt, distributed with this software. | |
11 | #----------------------------------------------------------------------------- |
|
11 | #----------------------------------------------------------------------------- | |
12 |
|
12 | |||
13 | #----------------------------------------------------------------------------- |
|
13 | #----------------------------------------------------------------------------- | |
14 | # Imports |
|
14 | # Imports | |
15 | #----------------------------------------------------------------------------- |
|
15 | #----------------------------------------------------------------------------- | |
16 | from __future__ import print_function |
|
16 | from __future__ import print_function | |
17 |
|
17 | |||
18 | # Stdlib imports |
|
18 | # Stdlib imports | |
19 | import sys |
|
|||
20 | import subprocess |
|
19 | import subprocess | |
|
20 | from io import TextIOWrapper, BytesIO | |||
21 |
|
21 | |||
|
22 | # IPython imports | |||
22 | from IPython.nbconvert.utils.pandoc import pandoc |
|
23 | from IPython.nbconvert.utils.pandoc import pandoc | |
|
24 | from IPython.nbconvert.utils.exceptions import ConversionException | |||
|
25 | from IPython.utils.process import find_cmd, FindCmdError | |||
|
26 | from IPython.utils.py3compat import cast_bytes | |||
23 |
|
27 | |||
24 | #----------------------------------------------------------------------------- |
|
28 | #----------------------------------------------------------------------------- | |
25 | # Functions |
|
29 | # Functions | |
26 | #----------------------------------------------------------------------------- |
|
30 | #----------------------------------------------------------------------------- | |
27 |
|
31 | |||
28 | __all__ = [ |
|
32 | __all__ = [ | |
29 | 'markdown2html', |
|
33 | 'markdown2html', | |
|
34 | 'markdown2html_pandoc', | |||
|
35 | 'markdown2html_marked', | |||
30 | 'markdown2latex', |
|
36 | 'markdown2latex', | |
31 | 'markdown2rst' |
|
37 | 'markdown2rst', | |
32 | ] |
|
38 | ] | |
33 |
|
39 | |||
|
40 | class MarkedMissing(ConversionException): | |||
|
41 | """Exception raised when Marked is missing.""" | |||
|
42 | pass | |||
|
43 | ||||
34 | def markdown2latex(source): |
|
44 | def markdown2latex(source): | |
35 | """Convert a markdown string to LaTeX via pandoc. |
|
45 | """Convert a markdown string to LaTeX via pandoc. | |
36 |
|
46 | |||
37 | This function will raise an error if pandoc is not installed. |
|
47 | This function will raise an error if pandoc is not installed. | |
38 | Any error messages generated by pandoc are printed to stderr. |
|
48 | Any error messages generated by pandoc are printed to stderr. | |
39 |
|
49 | |||
40 | Parameters |
|
50 | Parameters | |
41 | ---------- |
|
51 | ---------- | |
42 | source : string |
|
52 | source : string | |
43 | Input string, assumed to be valid markdown. |
|
53 | Input string, assumed to be valid markdown. | |
44 |
|
54 | |||
45 | Returns |
|
55 | Returns | |
46 | ------- |
|
56 | ------- | |
47 | out : string |
|
57 | out : string | |
48 | Output as returned by pandoc. |
|
58 | Output as returned by pandoc. | |
49 | """ |
|
59 | """ | |
50 | return pandoc(source, 'markdown', 'latex') |
|
60 | return pandoc(source, 'markdown', 'latex') | |
51 |
|
61 | |||
52 |
|
62 | def markdown2html_pandoc(source): | ||
53 | def markdown2html(source): |
|
|||
54 | """Convert a markdown string to HTML via pandoc""" |
|
63 | """Convert a markdown string to HTML via pandoc""" | |
55 | return pandoc(source, 'markdown', 'html', extra_args=['--mathjax']) |
|
64 | return pandoc(source, 'markdown', 'html', extra_args=['--mathjax']) | |
56 |
|
65 | |||
|
66 | def markdown2html_marked(source, encoding='utf-8'): | |||
|
67 | """Convert a markdown string to HTML via marked""" | |||
|
68 | command = ['marked', '--gfm', '--tables'] | |||
|
69 | try: | |||
|
70 | p = subprocess.Popen(command, | |||
|
71 | stdin=subprocess.PIPE, stdout=subprocess.PIPE | |||
|
72 | ) | |||
|
73 | except OSError as e: | |||
|
74 | raise MarkedMissing( | |||
|
75 | "The command '%s' returned an error: %s.\n" % (" ".join(command), e) + | |||
|
76 | "Please check that marked is installed:\n" + | |||
|
77 | " npm install -g marked" | |||
|
78 | ) | |||
|
79 | out, _ = p.communicate(cast_bytes(source, encoding)) | |||
|
80 | out = TextIOWrapper(BytesIO(out), encoding, 'replace').read() | |||
|
81 | return out.rstrip('\n') | |||
|
82 | ||||
57 | def markdown2rst(source): |
|
83 | def markdown2rst(source): | |
58 | """Convert a markdown string to LaTeX via pandoc. |
|
84 | """Convert a markdown string to LaTeX via pandoc. | |
59 |
|
85 | |||
60 | This function will raise an error if pandoc is not installed. |
|
86 | This function will raise an error if pandoc is not installed. | |
61 | Any error messages generated by pandoc are printed to stderr. |
|
87 | Any error messages generated by pandoc are printed to stderr. | |
62 |
|
88 | |||
63 | Parameters |
|
89 | Parameters | |
64 | ---------- |
|
90 | ---------- | |
65 | source : string |
|
91 | source : string | |
66 | Input string, assumed to be valid markdown. |
|
92 | Input string, assumed to be valid markdown. | |
67 |
|
93 | |||
68 | Returns |
|
94 | Returns | |
69 | ------- |
|
95 | ------- | |
70 | out : string |
|
96 | out : string | |
71 | Output as returned by pandoc. |
|
97 | Output as returned by pandoc. | |
72 | """ |
|
98 | """ | |
73 | return pandoc(source, 'markdown', 'rst') |
|
99 | return pandoc(source, 'markdown', 'rst') | |
74 |
|
100 | |||
|
101 | try: | |||
|
102 | find_cmd('marked') | |||
|
103 | except FindCmdError: | |||
|
104 | markdown2html = markdown2html_pandoc | |||
|
105 | else: | |||
|
106 | markdown2html = markdown2html_marked |
General Comments 0
You need to be logged in to leave comments.
Login now