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