##// END OF EJS Templates
urlencode images for rst files...
Paul Ivanov -
Show More
@@ -1,139 +1,139
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 os
19 import os
20 import subprocess
20 import subprocess
21 import warnings
21 import warnings
22 from io import TextIOWrapper, BytesIO
22 from io import TextIOWrapper, BytesIO
23
23
24 # IPython imports
24 # IPython imports
25 from IPython.nbconvert.utils.pandoc import pandoc
25 from IPython.nbconvert.utils.pandoc import pandoc
26 from IPython.nbconvert.utils.exceptions import ConversionException
26 from IPython.nbconvert.utils.exceptions import ConversionException
27 from IPython.utils.process import get_output_error_code
27 from IPython.utils.process import get_output_error_code
28 from IPython.utils.py3compat import cast_bytes
28 from IPython.utils.py3compat import cast_bytes
29 from IPython.utils.version import check_version
29 from IPython.utils.version import check_version
30
30
31 #-----------------------------------------------------------------------------
31 #-----------------------------------------------------------------------------
32 # Functions
32 # Functions
33 #-----------------------------------------------------------------------------
33 #-----------------------------------------------------------------------------
34 marked = os.path.join(os.path.dirname(__file__), "marked.js")
34 marked = os.path.join(os.path.dirname(__file__), "marked.js")
35 _node = None
35 _node = None
36
36
37 __all__ = [
37 __all__ = [
38 'markdown2html',
38 'markdown2html',
39 'markdown2html_pandoc',
39 'markdown2html_pandoc',
40 'markdown2html_marked',
40 'markdown2html_marked',
41 'markdown2latex',
41 'markdown2latex',
42 'markdown2rst',
42 'markdown2rst',
43 ]
43 ]
44
44
45 class NodeJSMissing(ConversionException):
45 class NodeJSMissing(ConversionException):
46 """Exception raised when node.js is missing."""
46 """Exception raised when node.js is missing."""
47 pass
47 pass
48
48
49 def markdown2latex(source):
49 def markdown2latex(source):
50 """Convert a markdown string to LaTeX via pandoc.
50 """Convert a markdown string to LaTeX via pandoc.
51
51
52 This function will raise an error if pandoc is not installed.
52 This function will raise an error if pandoc is not installed.
53 Any error messages generated by pandoc are printed to stderr.
53 Any error messages generated by pandoc are printed to stderr.
54
54
55 Parameters
55 Parameters
56 ----------
56 ----------
57 source : string
57 source : string
58 Input string, assumed to be valid markdown.
58 Input string, assumed to be valid markdown.
59
59
60 Returns
60 Returns
61 -------
61 -------
62 out : string
62 out : string
63 Output as returned by pandoc.
63 Output as returned by pandoc.
64 """
64 """
65 return pandoc(source, 'markdown', 'latex')
65 return pandoc(source, 'markdown', 'latex')
66
66
67 def markdown2html(source):
67 def markdown2html(source):
68 """Convert a markdown string to HTML"""
68 """Convert a markdown string to HTML"""
69 global _node
69 global _node
70 if _node is None:
70 if _node is None:
71 # prefer md2html via marked if node.js >= 0.9.12 is available
71 # prefer md2html via marked if node.js >= 0.9.12 is available
72 # node is called nodejs on debian, so try that first
72 # node is called nodejs on debian, so try that first
73 _node = 'nodejs'
73 _node = 'nodejs'
74 if not _verify_node(_node):
74 if not _verify_node(_node):
75 _node = 'node'
75 _node = 'node'
76 if not _verify_node(_node):
76 if not _verify_node(_node):
77 warnings.warn( "Node.js 0.9.12 or later wasn't found.\n" +
77 warnings.warn( "Node.js 0.9.12 or later wasn't found.\n" +
78 "Nbconvert will try to use Pandoc instead.")
78 "Nbconvert will try to use Pandoc instead.")
79 _node = False
79 _node = False
80 if _node:
80 if _node:
81 return markdown2html_marked(source)
81 return markdown2html_marked(source)
82 else:
82 else:
83 return markdown2html_pandoc(source)
83 return markdown2html_pandoc(source)
84
84
85 def markdown2html_pandoc(source):
85 def markdown2html_pandoc(source):
86 """Convert a markdown string to HTML via pandoc"""
86 """Convert a markdown string to HTML via pandoc"""
87 return pandoc(source, 'markdown', 'html', extra_args=['--mathjax'])
87 return pandoc(source, 'markdown', 'html', extra_args=['--mathjax'])
88
88
89 def markdown2html_marked(source, encoding='utf-8'):
89 def markdown2html_marked(source, encoding='utf-8'):
90 """Convert a markdown string to HTML via marked"""
90 """Convert a markdown string to HTML via marked"""
91 command = [_node, marked]
91 command = [_node, marked]
92 try:
92 try:
93 p = subprocess.Popen(command,
93 p = subprocess.Popen(command,
94 stdin=subprocess.PIPE, stdout=subprocess.PIPE
94 stdin=subprocess.PIPE, stdout=subprocess.PIPE
95 )
95 )
96 except OSError as e:
96 except OSError as e:
97 raise NodeJSMissing(
97 raise NodeJSMissing(
98 "The command '%s' returned an error: %s.\n" % (" ".join(command), e) +
98 "The command '%s' returned an error: %s.\n" % (" ".join(command), e) +
99 "Please check that Node.js is installed."
99 "Please check that Node.js is installed."
100 )
100 )
101 out, _ = p.communicate(cast_bytes(source, encoding))
101 out, _ = p.communicate(cast_bytes(source, encoding))
102 out = TextIOWrapper(BytesIO(out), encoding, 'replace').read()
102 out = TextIOWrapper(BytesIO(out), encoding, 'replace').read()
103 return out.rstrip('\n')
103 return out.rstrip('\n')
104
104
105 def markdown2rst(source):
105 def markdown2rst(source):
106 """Convert a markdown string to LaTeX via pandoc.
106 """Convert a markdown string to ReST via pandoc.
107
107
108 This function will raise an error if pandoc is not installed.
108 This function will raise an error if pandoc is not installed.
109 Any error messages generated by pandoc are printed to stderr.
109 Any error messages generated by pandoc are printed to stderr.
110
110
111 Parameters
111 Parameters
112 ----------
112 ----------
113 source : string
113 source : string
114 Input string, assumed to be valid markdown.
114 Input string, assumed to be valid markdown.
115
115
116 Returns
116 Returns
117 -------
117 -------
118 out : string
118 out : string
119 Output as returned by pandoc.
119 Output as returned by pandoc.
120 """
120 """
121 return pandoc(source, 'markdown', 'rst')
121 return pandoc(source, 'markdown', 'rst')
122
122
123 def _verify_node(cmd):
123 def _verify_node(cmd):
124 """Verify that the node command exists and is at least the minimum supported
124 """Verify that the node command exists and is at least the minimum supported
125 version of node.
125 version of node.
126
126
127 Parameters
127 Parameters
128 ----------
128 ----------
129 cmd : string
129 cmd : string
130 Node command to verify (i.e 'node')."""
130 Node command to verify (i.e 'node')."""
131 try:
131 try:
132 out, err, return_code = get_output_error_code([cmd, '--version'])
132 out, err, return_code = get_output_error_code([cmd, '--version'])
133 except OSError:
133 except OSError:
134 # Command not found
134 # Command not found
135 return False
135 return False
136 if return_code:
136 if return_code:
137 # Command error
137 # Command error
138 return False
138 return False
139 return check_version(out.lstrip('v'), '0.9.12')
139 return check_version(out.lstrip('v'), '0.9.12')
@@ -1,80 +1,80
1 {%- extends 'display_priority.tpl' -%}
1 {%- extends 'display_priority.tpl' -%}
2
2
3
3
4 {% block in_prompt %}
4 {% block in_prompt %}
5 {% endblock in_prompt %}
5 {% endblock in_prompt %}
6
6
7 {% block output_prompt %}
7 {% block output_prompt %}
8 {% endblock output_prompt %}
8 {% endblock output_prompt %}
9
9
10 {% block input %}
10 {% block input %}
11 {%- if not cell.input.isspace() -%}
11 {%- if not cell.input.isspace() -%}
12 .. code:: python
12 .. code:: python
13
13
14 {{ cell.input | indent}}
14 {{ cell.input | indent}}
15 {%- endif -%}
15 {%- endif -%}
16 {% endblock input %}
16 {% endblock input %}
17
17
18 {% block pyerr %}
18 {% block pyerr %}
19 ::
19 ::
20
20
21 {{ super() }}
21 {{ super() }}
22 {% endblock pyerr %}
22 {% endblock pyerr %}
23
23
24 {% block traceback_line %}
24 {% block traceback_line %}
25 {{ line | indent | strip_ansi }}
25 {{ line | indent | strip_ansi }}
26 {% endblock traceback_line %}
26 {% endblock traceback_line %}
27
27
28 {% block pyout %}
28 {% block pyout %}
29 {% block data_priority scoped %}
29 {% block data_priority scoped %}
30 {{ super() }}
30 {{ super() }}
31 {% endblock %}
31 {% endblock %}
32 {% endblock pyout %}
32 {% endblock pyout %}
33
33
34 {% block stream %}
34 {% block stream %}
35 .. parsed-literal::
35 .. parsed-literal::
36
36
37 {{ output.text | indent }}
37 {{ output.text | indent }}
38 {% endblock stream %}
38 {% endblock stream %}
39
39
40 {% block data_svg %}
40 {% block data_svg %}
41 .. image:: {{ output.svg_filename }}
41 .. image:: {{ output.svg_filename|urlencode }}
42 {% endblock data_svg %}
42 {% endblock data_svg %}
43
43
44 {% block data_png %}
44 {% block data_png %}
45 .. image:: {{ output.png_filename }}
45 .. image:: {{ output.png_filename|urlencode }}
46 {% endblock data_png %}
46 {% endblock data_png %}
47
47
48 {% block data_jpg %}
48 {% block data_jpg %}
49 .. image:: {{ output.jpeg_filename }}
49 .. image:: {{ output.jpeg_filename|urlencode }}
50 {% endblock data_jpg %}
50 {% endblock data_jpg %}
51
51
52 {% block data_latex %}
52 {% block data_latex %}
53 .. math::
53 .. math::
54
54
55 {{ output.latex | strip_dollars | indent }}
55 {{ output.latex | strip_dollars | indent }}
56 {% endblock data_latex %}
56 {% endblock data_latex %}
57
57
58 {% block data_text scoped %}
58 {% block data_text scoped %}
59 .. parsed-literal::
59 .. parsed-literal::
60
60
61 {{ output.text | indent }}
61 {{ output.text | indent }}
62 {% endblock data_text %}
62 {% endblock data_text %}
63
63
64 {% block data_html scoped %}
64 {% block data_html scoped %}
65 .. raw:: html
65 .. raw:: html
66
66
67 {{ output.html | indent }}
67 {{ output.html | indent }}
68 {% endblock data_html %}
68 {% endblock data_html %}
69
69
70 {% block markdowncell scoped %}
70 {% block markdowncell scoped %}
71 {{ cell.source | markdown2rst }}
71 {{ cell.source | markdown2rst }}
72 {% endblock markdowncell %}
72 {% endblock markdowncell %}
73
73
74 {% block headingcell scoped %}
74 {% block headingcell scoped %}
75 {{ ("#" * cell.level + cell.source) | replace('\n', ' ') | markdown2rst }}
75 {{ ("#" * cell.level + cell.source) | replace('\n', ' ') | markdown2rst }}
76 {% endblock headingcell %}
76 {% endblock headingcell %}
77
77
78 {% block unknowncell scoped %}
78 {% block unknowncell scoped %}
79 unknown type {{cell.type}}
79 unknown type {{cell.type}}
80 {% endblock unknowncell %}
80 {% endblock unknowncell %}
General Comments 0
You need to be logged in to leave comments. Login now