##// END OF EJS Templates
Fix failure on Py2.6
Jonathan Frederic -
Show More
@@ -1,151 +1,155 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 re
19 import re
20 import textwrap
20 import textwrap
21 from xml.etree import ElementTree
21 from xml.etree import ElementTree
22
22
23 from IPython.utils import py3compat
23 from IPython.utils import py3compat
24
24
25 #-----------------------------------------------------------------------------
25 #-----------------------------------------------------------------------------
26 # Functions
26 # Functions
27 #-----------------------------------------------------------------------------
27 #-----------------------------------------------------------------------------
28
28
29 __all__ = [
29 __all__ = [
30 'wrap',
30 'wrap',
31 'html_text',
31 'html_text',
32 'add_anchor',
32 'add_anchor',
33 'strip_dollars',
33 'strip_dollars',
34 'rm_fake',
34 'rm_fake',
35 'python_comment',
35 'python_comment',
36 'get_lines'
36 'get_lines'
37 ]
37 ]
38
38
39
39
40 def wrap(text, width=100):
40 def wrap(text, width=100):
41 """
41 """
42 Intelligently wrap text.
42 Intelligently wrap text.
43 Wrap text without breaking words if possible.
43 Wrap text without breaking words if possible.
44
44
45 Parameters
45 Parameters
46 ----------
46 ----------
47 text : str
47 text : str
48 Text to wrap.
48 Text to wrap.
49 width : int, optional
49 width : int, optional
50 Number of characters to wrap to, default 100.
50 Number of characters to wrap to, default 100.
51 """
51 """
52
52
53 split_text = text.split('\n')
53 split_text = text.split('\n')
54 wrp = map(lambda x:textwrap.wrap(x,width), split_text)
54 wrp = map(lambda x:textwrap.wrap(x,width), split_text)
55 wrpd = map('\n'.join, wrp)
55 wrpd = map('\n'.join, wrp)
56 return '\n'.join(wrpd)
56 return '\n'.join(wrpd)
57
57
58
58
59 def html_text(element):
59 def html_text(element):
60 """extract inner text from html
60 """extract inner text from html
61
61
62 Analog of jQuery's $(element).text()
62 Analog of jQuery's $(element).text()
63 """
63 """
64 if not isinstance(element, (ElementTree.ElementTree, ElementTree.Element)):
64 instance_element_tree = isinstance(ElementTree.ElementTree, Type) and
65 isinstance(element, (ElementTree.ElementTree))
66 instance_element = isinstance(ElementTree.Element, Type) and
67 isinstance(element, (ElementTree.Element))
68 if not (instance_element or instance_element_tree):
65 element = ElementTree.fromstring(element)
69 element = ElementTree.fromstring(element)
66
70
67 text = element.text or ""
71 text = element.text or ""
68 for child in element:
72 for child in element:
69 text += html_text(child)
73 text += html_text(child)
70 text += (element.tail or "")
74 text += (element.tail or "")
71 return text
75 return text
72
76
73
77
74 def add_anchor(html):
78 def add_anchor(html):
75 """Add an anchor-link to an html header tag
79 """Add an anchor-link to an html header tag
76
80
77 For use in heading cells
81 For use in heading cells
78 """
82 """
79 h = ElementTree.fromstring(py3compat.cast_bytes_py2(html))
83 h = ElementTree.fromstring(py3compat.cast_bytes_py2(html))
80 link = html_text(h).replace(' ', '-')
84 link = html_text(h).replace(' ', '-')
81 h.set('id', link)
85 h.set('id', link)
82 a = ElementTree.Element("a", {"class" : "anchor-link", "href" : "#" + link})
86 a = ElementTree.Element("a", {"class" : "anchor-link", "href" : "#" + link})
83 a.text = u'ΒΆ'
87 a.text = u'ΒΆ'
84 h.append(a)
88 h.append(a)
85 return ElementTree.tostring(h)
89 return ElementTree.tostring(h)
86
90
87
91
88 def strip_dollars(text):
92 def strip_dollars(text):
89 """
93 """
90 Remove all dollar symbols from text
94 Remove all dollar symbols from text
91
95
92 Parameters
96 Parameters
93 ----------
97 ----------
94 text : str
98 text : str
95 Text to remove dollars from
99 Text to remove dollars from
96 """
100 """
97
101
98 return text.strip('$')
102 return text.strip('$')
99
103
100
104
101 files_url_pattern = re.compile(r'(src|href)\=([\'"]?)files/')
105 files_url_pattern = re.compile(r'(src|href)\=([\'"]?)files/')
102
106
103 def rm_fake(text):
107 def rm_fake(text):
104 """
108 """
105 Fix all fake URLs that start with `files/`,
109 Fix all fake URLs that start with `files/`,
106 stripping out the `files/` prefix.
110 stripping out the `files/` prefix.
107
111
108 Parameters
112 Parameters
109 ----------
113 ----------
110 text : str
114 text : str
111 Text in which to replace 'src="files/real...' with 'src="real...'
115 Text in which to replace 'src="files/real...' with 'src="real...'
112 """
116 """
113 return files_url_pattern.sub(r"\1=\2", text)
117 return files_url_pattern.sub(r"\1=\2", text)
114
118
115
119
116 def python_comment(text):
120 def python_comment(text):
117 """
121 """
118 Build a Python comment line from input text.
122 Build a Python comment line from input text.
119
123
120 Parameters
124 Parameters
121 ----------
125 ----------
122 text : str
126 text : str
123 Text to comment out.
127 Text to comment out.
124 """
128 """
125
129
126 #Replace line breaks with line breaks and comment symbols.
130 #Replace line breaks with line breaks and comment symbols.
127 #Also add a comment symbol at the beginning to comment out
131 #Also add a comment symbol at the beginning to comment out
128 #the first line.
132 #the first line.
129 return '# '+'\n# '.join(text.split('\n'))
133 return '# '+'\n# '.join(text.split('\n'))
130
134
131
135
132 def get_lines(text, start=None,end=None):
136 def get_lines(text, start=None,end=None):
133 """
137 """
134 Split the input text into separate lines and then return the
138 Split the input text into separate lines and then return the
135 lines that the caller is interested in.
139 lines that the caller is interested in.
136
140
137 Parameters
141 Parameters
138 ----------
142 ----------
139 text : str
143 text : str
140 Text to parse lines from.
144 Text to parse lines from.
141 start : int, optional
145 start : int, optional
142 First line to grab from.
146 First line to grab from.
143 end : int, optional
147 end : int, optional
144 Last line to grab from.
148 Last line to grab from.
145 """
149 """
146
150
147 # Split the input into lines.
151 # Split the input into lines.
148 lines = text.split("\n")
152 lines = text.split("\n")
149
153
150 # Return the right lines.
154 # Return the right lines.
151 return "\n".join(lines[start:end]) #re-join
155 return "\n".join(lines[start:end]) #re-join
General Comments 0
You need to be logged in to leave comments. Login now