##// END OF EJS Templates
Cleanup
Jonathan Frederic -
Show More
@@ -1,137 +1,128 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 19 import os
20 20 import subprocess
21 21 from io import TextIOWrapper, BytesIO
22 22
23 23 # IPython imports
24 24 from IPython.nbconvert.utils.pandoc import pandoc
25 25 from IPython.nbconvert.utils.exceptions import ConversionException
26 from IPython.utils.process import find_cmd, FindCmdError, getoutput
26 from IPython.utils.process import get_output_error_code
27 27 from IPython.utils.py3compat import cast_bytes
28 from IPython.utils.version import check_version
28 29
29 30 #-----------------------------------------------------------------------------
30 31 # Functions
31 32 #-----------------------------------------------------------------------------
32 33 marked = os.path.join(os.path.dirname(__file__), "marked.js")
33 34
34 35 __all__ = [
35 36 'markdown2html',
36 37 'markdown2html_pandoc',
37 38 'markdown2html_marked',
38 39 'markdown2latex',
39 40 'markdown2rst',
40 41 ]
41 42
42 43 class NodeJSMissing(ConversionException):
43 44 """Exception raised when node.js is missing."""
44 45 pass
45 46
46 47 def markdown2latex(source):
47 48 """Convert a markdown string to LaTeX via pandoc.
48 49
49 50 This function will raise an error if pandoc is not installed.
50 51 Any error messages generated by pandoc are printed to stderr.
51 52
52 53 Parameters
53 54 ----------
54 55 source : string
55 56 Input string, assumed to be valid markdown.
56 57
57 58 Returns
58 59 -------
59 60 out : string
60 61 Output as returned by pandoc.
61 62 """
62 63 return pandoc(source, 'markdown', 'latex')
63 64
64 65 def markdown2html_pandoc(source):
65 66 """Convert a markdown string to HTML via pandoc"""
66 67 return pandoc(source, 'markdown', 'html', extra_args=['--mathjax'])
67 68
68 69 def markdown2html_marked(source, encoding='utf-8'):
69 70 """Convert a markdown string to HTML via marked"""
70 71 command = [node_cmd, marked]
71 72 try:
72 73 p = subprocess.Popen(command,
73 74 stdin=subprocess.PIPE, stdout=subprocess.PIPE
74 75 )
75 76 except OSError as e:
76 77 raise NodeJSMissing(
77 78 "The command '%s' returned an error: %s.\n" % (" ".join(command), e) +
78 79 "Please check that Node.js is installed."
79 80 )
80 81 out, _ = p.communicate(cast_bytes(source, encoding))
81 82 out = TextIOWrapper(BytesIO(out), encoding, 'replace').read()
82 83 return out.rstrip('\n')
83 84
84 85 def markdown2rst(source):
85 86 """Convert a markdown string to LaTeX via pandoc.
86 87
87 88 This function will raise an error if pandoc is not installed.
88 89 Any error messages generated by pandoc are printed to stderr.
89 90
90 91 Parameters
91 92 ----------
92 93 source : string
93 94 Input string, assumed to be valid markdown.
94 95
95 96 Returns
96 97 -------
97 98 out : string
98 99 Output as returned by pandoc.
99 100 """
100 101 return pandoc(source, 'markdown', 'rst')
101 102
102 103 def _verify_node(cmd):
103 """Verify that the node command exists and is atleast the minimum supported
104 """Verify that the node command exists and is at least the minimum supported
104 105 version of node.
105 106
106 107 Parameters
107 108 ----------
108 109 cmd : string
109 110 Node command to verify (i.e 'node')."""
110 111 try:
111 find_cmd(node_cmd)
112 except FindCmdError:
112 out, err, return_code = get_output_error_code([cmd, '--version'])
113 except OSError:
114 # Command not found
113 115 return False
114 else:
115 # Remove the version 'v' prefix from the output and strip whitespace.
116 version_str = getoutput(cmd + ' --version').replace('v', '').strip()
117 try:
118 # Make sure the node version is atleast 0.9.12
119 version_numbers = [int(x) for x in version_str.split('.')]
120 if version_numbers[0] > 0 or version_numbers[1] > 9 or (version_numbers[1] == 9 and version_numbers[2] >= 12):
121 return True
122 else:
123 return False
124 except:
125 return False
126
127 # prefer md2html via marked if node.js is available
116 return return_code == 0 and check_version(out.lstrip('v'), '0.9.12')
117
118 # prefer md2html via marked if node.js >= 0.9.12 is available
128 119 # node is called nodejs on debian, so try that first
129 120 node_cmd = 'nodejs'
130 121 if _verify_node(node_cmd):
131 122 markdown2html = markdown2html_marked
132 123 else:
133 124 node_cmd = 'node'
134 125 if _verify_node(node_cmd):
135 126 markdown2html = markdown2html_marked
136 127 else:
137 128 markdown2html = markdown2html_pandoc
General Comments 0
You need to be logged in to leave comments. Login now