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