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