Show More
@@ -1,74 +1,106 b'' | |||
|
1 | 1 | """Markdown filters |
|
2 | 2 | This file contains a collection of utility filters for dealing with |
|
3 | 3 | markdown within Jinja templates. |
|
4 | 4 | """ |
|
5 | 5 | #----------------------------------------------------------------------------- |
|
6 | 6 | # Copyright (c) 2013, the IPython Development Team. |
|
7 | 7 | # |
|
8 | 8 | # Distributed under the terms of the Modified BSD License. |
|
9 | 9 | # |
|
10 | 10 | # The full license is in the file COPYING.txt, distributed with this software. |
|
11 | 11 | #----------------------------------------------------------------------------- |
|
12 | 12 | |
|
13 | 13 | #----------------------------------------------------------------------------- |
|
14 | 14 | # Imports |
|
15 | 15 | #----------------------------------------------------------------------------- |
|
16 | 16 | from __future__ import print_function |
|
17 | 17 | |
|
18 | 18 | # Stdlib imports |
|
19 | import sys | |
|
20 | 19 | import subprocess |
|
20 | from io import TextIOWrapper, BytesIO | |
|
21 | 21 | |
|
22 | # IPython imports | |
|
22 | 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 | 29 | # Functions |
|
26 | 30 | #----------------------------------------------------------------------------- |
|
27 | 31 | |
|
28 | 32 | __all__ = [ |
|
29 | 33 | 'markdown2html', |
|
34 | 'markdown2html_pandoc', | |
|
35 | 'markdown2html_marked', | |
|
30 | 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 | 44 | def markdown2latex(source): |
|
35 | 45 | """Convert a markdown string to LaTeX via pandoc. |
|
36 | 46 | |
|
37 | 47 | This function will raise an error if pandoc is not installed. |
|
38 | 48 | Any error messages generated by pandoc are printed to stderr. |
|
39 | 49 | |
|
40 | 50 | Parameters |
|
41 | 51 | ---------- |
|
42 | 52 | source : string |
|
43 | 53 | Input string, assumed to be valid markdown. |
|
44 | 54 | |
|
45 | 55 | Returns |
|
46 | 56 | ------- |
|
47 | 57 | out : string |
|
48 | 58 | Output as returned by pandoc. |
|
49 | 59 | """ |
|
50 | 60 | return pandoc(source, 'markdown', 'latex') |
|
51 | 61 | |
|
52 | ||
|
53 | def markdown2html(source): | |
|
62 | def markdown2html_pandoc(source): | |
|
54 | 63 | """Convert a markdown string to HTML via pandoc""" |
|
55 | 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 | 83 | def markdown2rst(source): |
|
58 | 84 | """Convert a markdown string to LaTeX via pandoc. |
|
59 | 85 | |
|
60 | 86 | This function will raise an error if pandoc is not installed. |
|
61 | 87 | Any error messages generated by pandoc are printed to stderr. |
|
62 | 88 | |
|
63 | 89 | Parameters |
|
64 | 90 | ---------- |
|
65 | 91 | source : string |
|
66 | 92 | Input string, assumed to be valid markdown. |
|
67 | 93 | |
|
68 | 94 | Returns |
|
69 | 95 | ------- |
|
70 | 96 | out : string |
|
71 | 97 | Output as returned by pandoc. |
|
72 | 98 | """ |
|
73 | 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