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