Show More
@@ -1,113 +1,113 | |||
|
1 | 1 | """String filters. |
|
2 | 2 | |
|
3 | 3 | Contains a collection of useful string manipulation filters for use in Jinja |
|
4 | 4 | templates. |
|
5 | 5 | """ |
|
6 | 6 | #----------------------------------------------------------------------------- |
|
7 | 7 | # Copyright (c) 2013, the IPython Development Team. |
|
8 | 8 | # |
|
9 | 9 | # Distributed under the terms of the Modified BSD License. |
|
10 | 10 | # |
|
11 | 11 | # The full license is in the file COPYING.txt, distributed with this software. |
|
12 | 12 | #----------------------------------------------------------------------------- |
|
13 | 13 | |
|
14 | 14 | #----------------------------------------------------------------------------- |
|
15 | 15 | # Imports |
|
16 | 16 | #----------------------------------------------------------------------------- |
|
17 | 17 | |
|
18 | 18 | # Our own imports |
|
19 | 19 | import textwrap |
|
20 | 20 | |
|
21 | 21 | #----------------------------------------------------------------------------- |
|
22 | 22 | # Functions |
|
23 | 23 | #----------------------------------------------------------------------------- |
|
24 | 24 | |
|
25 | 25 | __all__ = [ |
|
26 | 26 | 'wrap', |
|
27 | 27 | 'strip_dollars', |
|
28 | 28 | 'rm_fake', |
|
29 | 29 | 'python_comment', |
|
30 | 30 | 'get_lines' |
|
31 | 31 | ] |
|
32 | 32 | |
|
33 | 33 | |
|
34 | 34 | def wrap(text, width=100): |
|
35 | 35 | """ |
|
36 | 36 | Intelligently wrap text. |
|
37 | 37 | Wrap text without breaking words if possible. |
|
38 | 38 | |
|
39 | 39 | Parameters |
|
40 | 40 | ---------- |
|
41 | 41 | text : str |
|
42 | 42 | Text to wrap. |
|
43 | 43 | width : int, optional |
|
44 | 44 | Number of characters to wrap to, default 100. |
|
45 | 45 | """ |
|
46 | 46 | |
|
47 | 47 | split_text = text.split('\n') |
|
48 | 48 | wrp = map(lambda x:textwrap.wrap(x,width), split_text) |
|
49 | 49 | wrpd = map('\n'.join, wrp) |
|
50 | 50 | return '\n'.join(wrpd) |
|
51 | 51 | |
|
52 | 52 | |
|
53 | 53 | def strip_dollars(text): |
|
54 | 54 | """ |
|
55 | 55 | Remove all dollar symbols from text |
|
56 | 56 | |
|
57 | 57 | Parameters |
|
58 | 58 | ---------- |
|
59 | 59 | text : str |
|
60 | 60 | Text to remove dollars from |
|
61 | 61 | """ |
|
62 | 62 | |
|
63 | 63 | return text.strip('$') |
|
64 | 64 | |
|
65 | 65 | |
|
66 | 66 | def rm_fake(text): |
|
67 | 67 | """ |
|
68 |
Remove all occurrences of ' |
|
|
68 | Remove all occurrences of 'files/' from text | |
|
69 | 69 | |
|
70 | 70 | Parameters |
|
71 | 71 | ---------- |
|
72 | 72 | text : str |
|
73 |
Text to remove ' |
|
|
73 | Text to remove 'files/' from | |
|
74 | 74 | """ |
|
75 |
return text.replace(' |
|
|
75 | return text.replace('files/', '') | |
|
76 | 76 | |
|
77 | 77 | |
|
78 | 78 | def python_comment(text): |
|
79 | 79 | """ |
|
80 | 80 | Build a Python comment line from input text. |
|
81 | 81 | |
|
82 | 82 | Parameters |
|
83 | 83 | ---------- |
|
84 | 84 | text : str |
|
85 | 85 | Text to comment out. |
|
86 | 86 | """ |
|
87 | 87 | |
|
88 | 88 | #Replace line breaks with line breaks and comment symbols. |
|
89 | 89 | #Also add a comment symbol at the beginning to comment out |
|
90 | 90 | #the first line. |
|
91 | 91 | return '# '+'\n# '.join(text.split('\n')) |
|
92 | 92 | |
|
93 | 93 | |
|
94 | 94 | def get_lines(text, start=None,end=None): |
|
95 | 95 | """ |
|
96 | 96 | Split the input text into separate lines and then return the |
|
97 | 97 | lines that the caller is interested in. |
|
98 | 98 | |
|
99 | 99 | Parameters |
|
100 | 100 | ---------- |
|
101 | 101 | text : str |
|
102 | 102 | Text to parse lines from. |
|
103 | 103 | start : int, optional |
|
104 | 104 | First line to grab from. |
|
105 | 105 | end : int, optional |
|
106 | 106 | Last line to grab from. |
|
107 | 107 | """ |
|
108 | 108 | |
|
109 | 109 | # Split the input into lines. |
|
110 | 110 | lines = text.split("\n") |
|
111 | 111 | |
|
112 | 112 | # Return the right lines. |
|
113 | 113 | return "\n".join(lines[start:end]) #re-join |
General Comments 0
You need to be logged in to leave comments.
Login now