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