Show More
@@ -1,207 +1,207 b'' | |||||
1 | # coding: utf-8 |
|
1 | # coding: utf-8 | |
2 | """String filters. |
|
2 | """String filters. | |
3 |
|
3 | |||
4 | Contains a collection of useful string manipulation filters for use in Jinja |
|
4 | Contains a collection of useful string manipulation filters for use in Jinja | |
5 | templates. |
|
5 | templates. | |
6 | """ |
|
6 | """ | |
7 | #----------------------------------------------------------------------------- |
|
7 | #----------------------------------------------------------------------------- | |
8 | # Copyright (c) 2013, the IPython Development Team. |
|
8 | # Copyright (c) 2013, the IPython Development Team. | |
9 | # |
|
9 | # | |
10 | # Distributed under the terms of the Modified BSD License. |
|
10 | # Distributed under the terms of the Modified BSD License. | |
11 | # |
|
11 | # | |
12 | # The full license is in the file COPYING.txt, distributed with this software. |
|
12 | # The full license is in the file COPYING.txt, distributed with this software. | |
13 | #----------------------------------------------------------------------------- |
|
13 | #----------------------------------------------------------------------------- | |
14 |
|
14 | |||
15 | #----------------------------------------------------------------------------- |
|
15 | #----------------------------------------------------------------------------- | |
16 | # Imports |
|
16 | # Imports | |
17 | #----------------------------------------------------------------------------- |
|
17 | #----------------------------------------------------------------------------- | |
18 |
|
18 | |||
19 | import os |
|
19 | import os | |
20 | import re |
|
20 | import re | |
21 | import textwrap |
|
21 | import textwrap | |
22 | try: |
|
22 | try: | |
23 | from urllib.parse import quote # Py 3 |
|
23 | from urllib.parse import quote # Py 3 | |
24 | except ImportError: |
|
24 | except ImportError: | |
25 | from urllib2 import quote # Py 2 |
|
25 | from urllib2 import quote # Py 2 | |
26 | from xml.etree import ElementTree |
|
26 | from xml.etree import ElementTree | |
27 |
|
27 | |||
28 | from IPython.core.interactiveshell import InteractiveShell |
|
28 | from IPython.core.interactiveshell import InteractiveShell | |
29 | from IPython.utils import py3compat |
|
29 | from IPython.utils import py3compat | |
30 |
|
30 | |||
31 | #----------------------------------------------------------------------------- |
|
31 | #----------------------------------------------------------------------------- | |
32 | # Functions |
|
32 | # Functions | |
33 | #----------------------------------------------------------------------------- |
|
33 | #----------------------------------------------------------------------------- | |
34 |
|
34 | |||
35 | __all__ = [ |
|
35 | __all__ = [ | |
36 | 'wrap_text', |
|
36 | 'wrap_text', | |
37 | 'html2text', |
|
37 | 'html2text', | |
38 | 'add_anchor', |
|
38 | 'add_anchor', | |
39 | 'strip_dollars', |
|
39 | 'strip_dollars', | |
40 | 'strip_files_prefix', |
|
40 | 'strip_files_prefix', | |
41 | 'comment_lines', |
|
41 | 'comment_lines', | |
42 | 'get_lines', |
|
42 | 'get_lines', | |
43 | 'ipython2python', |
|
43 | 'ipython2python', | |
44 | 'posix_path', |
|
44 | 'posix_path', | |
45 | 'path2url', |
|
45 | 'path2url', | |
46 | 'add_prompts' |
|
46 | 'add_prompts' | |
47 | ] |
|
47 | ] | |
48 |
|
48 | |||
49 |
|
49 | |||
50 | def wrap_text(text, width=100): |
|
50 | def wrap_text(text, width=100): | |
51 | """ |
|
51 | """ | |
52 | Intelligently wrap text. |
|
52 | Intelligently wrap text. | |
53 | Wrap text without breaking words if possible. |
|
53 | Wrap text without breaking words if possible. | |
54 |
|
54 | |||
55 | Parameters |
|
55 | Parameters | |
56 | ---------- |
|
56 | ---------- | |
57 | text : str |
|
57 | text : str | |
58 | Text to wrap. |
|
58 | Text to wrap. | |
59 | width : int, optional |
|
59 | width : int, optional | |
60 | Number of characters to wrap to, default 100. |
|
60 | Number of characters to wrap to, default 100. | |
61 | """ |
|
61 | """ | |
62 |
|
62 | |||
63 | split_text = text.split('\n') |
|
63 | split_text = text.split('\n') | |
64 | wrp = map(lambda x:textwrap.wrap(x,width), split_text) |
|
64 | wrp = map(lambda x:textwrap.wrap(x,width), split_text) | |
65 | wrpd = map('\n'.join, wrp) |
|
65 | wrpd = map('\n'.join, wrp) | |
66 | return '\n'.join(wrpd) |
|
66 | return '\n'.join(wrpd) | |
67 |
|
67 | |||
68 |
|
68 | |||
69 | def html2text(element): |
|
69 | def html2text(element): | |
70 | """extract inner text from html |
|
70 | """extract inner text from html | |
71 |
|
71 | |||
72 | Analog of jQuery's $(element).text() |
|
72 | Analog of jQuery's $(element).text() | |
73 | """ |
|
73 | """ | |
74 | if isinstance(element, py3compat.string_types): |
|
74 | if isinstance(element, py3compat.string_types): | |
75 | element = ElementTree.fromstring(element) |
|
75 | element = ElementTree.fromstring(element) | |
76 |
|
76 | |||
77 | text = element.text or "" |
|
77 | text = element.text or "" | |
78 | for child in element: |
|
78 | for child in element: | |
79 | text += html2text(child) |
|
79 | text += html2text(child) | |
80 | text += (element.tail or "") |
|
80 | text += (element.tail or "") | |
81 | return text |
|
81 | return text | |
82 |
|
82 | |||
83 |
|
83 | |||
84 | def add_anchor(html): |
|
84 | def add_anchor(html): | |
85 | """Add an anchor-link to an html header tag |
|
85 | """Add an anchor-link to an html header tag | |
86 |
|
86 | |||
87 | For use in heading cells |
|
87 | For use in heading cells | |
88 | """ |
|
88 | """ | |
89 | h = ElementTree.fromstring(py3compat.cast_bytes_py2(html, encoding='utf-8')) |
|
89 | h = ElementTree.fromstring(py3compat.cast_bytes_py2(html, encoding='utf-8')) | |
90 | link = html2text(h).replace(' ', '-') |
|
90 | link = html2text(h).replace(' ', '-') | |
91 | h.set('id', link) |
|
91 | h.set('id', link) | |
92 | a = ElementTree.Element("a", {"class" : "anchor-link", "href" : "#" + link}) |
|
92 | a = ElementTree.Element("a", {"class" : "anchor-link", "href" : "#" + link}) | |
93 | a.text = u'ΒΆ' |
|
93 | a.text = u'ΒΆ' | |
94 | h.append(a) |
|
94 | h.append(a) | |
95 |
|
95 | |||
96 | # Known issue of Python3.x, ElementTree.tostring() returns a byte string |
|
96 | # Known issue of Python3.x, ElementTree.tostring() returns a byte string | |
97 | # instead of a text string. See issue http://bugs.python.org/issue10942 |
|
97 | # instead of a text string. See issue http://bugs.python.org/issue10942 | |
98 | # Workaround is to make sure the bytes are casted to a string. |
|
98 | # Workaround is to make sure the bytes are casted to a string. | |
99 | return py3compat.decode(ElementTree.tostring(h), 'utf-8') |
|
99 | return py3compat.decode(ElementTree.tostring(h), 'utf-8') | |
100 |
|
100 | |||
101 |
|
101 | |||
102 | def add_prompts(code, first='>>> ', cont='... '): |
|
102 | def add_prompts(code, first='>>> ', cont='... '): | |
103 | """Add prompts to code snippets""" |
|
103 | """Add prompts to code snippets""" | |
104 | new_code = [] |
|
104 | new_code = [] | |
105 | code_list = code.split('\n') |
|
105 | code_list = code.split('\n') | |
106 | new_code.append(first + code_list[0]) |
|
106 | new_code.append(first + code_list[0]) | |
107 | for line in code_list[1:]: |
|
107 | for line in code_list[1:]: | |
108 | new_code.append(cont + line) |
|
108 | new_code.append(cont + line) | |
109 | return '\n'.join(new_code) |
|
109 | return '\n'.join(new_code) | |
110 |
|
110 | |||
111 |
|
111 | |||
112 | def strip_dollars(text): |
|
112 | def strip_dollars(text): | |
113 | """ |
|
113 | """ | |
114 | Remove all dollar symbols from text |
|
114 | Remove all dollar symbols from text | |
115 |
|
115 | |||
116 | Parameters |
|
116 | Parameters | |
117 | ---------- |
|
117 | ---------- | |
118 | text : str |
|
118 | text : str | |
119 | Text to remove dollars from |
|
119 | Text to remove dollars from | |
120 | """ |
|
120 | """ | |
121 |
|
121 | |||
122 | return text.strip('$') |
|
122 | return text.strip('$') | |
123 |
|
123 | |||
124 |
|
124 | |||
125 | files_url_pattern = re.compile(r'(src|href)\=([\'"]?)files/') |
|
125 | files_url_pattern = re.compile(r'(src|href)\=([\'"]?)/?files/') | |
126 | markdown_url_pattern = re.compile(r'!\[(?P<caption>.*?)\]\(/?files/(?P<location>.*?)\)') |
|
126 | markdown_url_pattern = re.compile(r'(!?)\[(?P<caption>.*?)\]\(/?files/(?P<location>.*?)\)') | |
127 |
|
127 | |||
128 | def strip_files_prefix(text): |
|
128 | def strip_files_prefix(text): | |
129 | """ |
|
129 | """ | |
130 | Fix all fake URLs that start with `files/`, stripping out the `files/` prefix. |
|
130 | Fix all fake URLs that start with `files/`, stripping out the `files/` prefix. | |
131 | Applies to both urls (for html) and relative paths (for markdown paths). |
|
131 | Applies to both urls (for html) and relative paths (for markdown paths). | |
132 |
|
132 | |||
133 | Parameters |
|
133 | Parameters | |
134 | ---------- |
|
134 | ---------- | |
135 | text : str |
|
135 | text : str | |
136 | Text in which to replace 'src="files/real...' with 'src="real...' |
|
136 | Text in which to replace 'src="files/real...' with 'src="real...' | |
137 | """ |
|
137 | """ | |
138 | cleaned_text = files_url_pattern.sub(r"\1=\2", text) |
|
138 | cleaned_text = files_url_pattern.sub(r"\1=\2", text) | |
139 |
cleaned_text = markdown_url_pattern.sub(r' |
|
139 | cleaned_text = markdown_url_pattern.sub(r'\1[\2](\3)', cleaned_text) | |
140 | return cleaned_text |
|
140 | return cleaned_text | |
141 |
|
141 | |||
142 |
|
142 | |||
143 | def comment_lines(text, prefix='# '): |
|
143 | def comment_lines(text, prefix='# '): | |
144 | """ |
|
144 | """ | |
145 | Build a Python comment line from input text. |
|
145 | Build a Python comment line from input text. | |
146 |
|
146 | |||
147 | Parameters |
|
147 | Parameters | |
148 | ---------- |
|
148 | ---------- | |
149 | text : str |
|
149 | text : str | |
150 | Text to comment out. |
|
150 | Text to comment out. | |
151 | prefix : str |
|
151 | prefix : str | |
152 | Character to append to the start of each line. |
|
152 | Character to append to the start of each line. | |
153 | """ |
|
153 | """ | |
154 |
|
154 | |||
155 | #Replace line breaks with line breaks and comment symbols. |
|
155 | #Replace line breaks with line breaks and comment symbols. | |
156 | #Also add a comment symbol at the beginning to comment out |
|
156 | #Also add a comment symbol at the beginning to comment out | |
157 | #the first line. |
|
157 | #the first line. | |
158 | return prefix + ('\n'+prefix).join(text.split('\n')) |
|
158 | return prefix + ('\n'+prefix).join(text.split('\n')) | |
159 |
|
159 | |||
160 |
|
160 | |||
161 | def get_lines(text, start=None,end=None): |
|
161 | def get_lines(text, start=None,end=None): | |
162 | """ |
|
162 | """ | |
163 | Split the input text into separate lines and then return the |
|
163 | Split the input text into separate lines and then return the | |
164 | lines that the caller is interested in. |
|
164 | lines that the caller is interested in. | |
165 |
|
165 | |||
166 | Parameters |
|
166 | Parameters | |
167 | ---------- |
|
167 | ---------- | |
168 | text : str |
|
168 | text : str | |
169 | Text to parse lines from. |
|
169 | Text to parse lines from. | |
170 | start : int, optional |
|
170 | start : int, optional | |
171 | First line to grab from. |
|
171 | First line to grab from. | |
172 | end : int, optional |
|
172 | end : int, optional | |
173 | Last line to grab from. |
|
173 | Last line to grab from. | |
174 | """ |
|
174 | """ | |
175 |
|
175 | |||
176 | # Split the input into lines. |
|
176 | # Split the input into lines. | |
177 | lines = text.split("\n") |
|
177 | lines = text.split("\n") | |
178 |
|
178 | |||
179 | # Return the right lines. |
|
179 | # Return the right lines. | |
180 | return "\n".join(lines[start:end]) #re-join |
|
180 | return "\n".join(lines[start:end]) #re-join | |
181 |
|
181 | |||
182 | def ipython2python(code): |
|
182 | def ipython2python(code): | |
183 | """Transform IPython syntax to pure Python syntax |
|
183 | """Transform IPython syntax to pure Python syntax | |
184 |
|
184 | |||
185 | Parameters |
|
185 | Parameters | |
186 | ---------- |
|
186 | ---------- | |
187 |
|
187 | |||
188 | code : str |
|
188 | code : str | |
189 | IPython code, to be transformed to pure Python |
|
189 | IPython code, to be transformed to pure Python | |
190 | """ |
|
190 | """ | |
191 | shell = InteractiveShell.instance() |
|
191 | shell = InteractiveShell.instance() | |
192 | return shell.input_transformer_manager.transform_cell(code) |
|
192 | return shell.input_transformer_manager.transform_cell(code) | |
193 |
|
193 | |||
194 | def posix_path(path): |
|
194 | def posix_path(path): | |
195 | """Turn a path into posix-style path/to/etc |
|
195 | """Turn a path into posix-style path/to/etc | |
196 |
|
196 | |||
197 | Mainly for use in latex on Windows, |
|
197 | Mainly for use in latex on Windows, | |
198 | where native Windows paths are not allowed. |
|
198 | where native Windows paths are not allowed. | |
199 | """ |
|
199 | """ | |
200 | if os.path.sep != '/': |
|
200 | if os.path.sep != '/': | |
201 | return path.replace(os.path.sep, '/') |
|
201 | return path.replace(os.path.sep, '/') | |
202 | return path |
|
202 | return path | |
203 |
|
203 | |||
204 | def path2url(path): |
|
204 | def path2url(path): | |
205 | """Turn a file path into a URL""" |
|
205 | """Turn a file path into a URL""" | |
206 | parts = path.split(os.path.sep) |
|
206 | parts = path.split(os.path.sep) | |
207 | return '/'.join(quote(part) for part in parts) |
|
207 | return '/'.join(quote(part) for part in parts) |
General Comments 0
You need to be logged in to leave comments.
Login now