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