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