Show More
@@ -8,11 +8,9 b'' | |||||
8 | import cgi, re, os, time, urllib |
|
8 | import cgi, re, os, time, urllib | |
9 | import encoding, node, util |
|
9 | import encoding, node, util | |
10 |
|
10 | |||
11 | def stringify(thing): |
|
11 | def addbreaks(text): | |
12 | '''turn nested template iterator into string.''' |
|
12 | '''replace raw newlines with xhtml line breaks.''' | |
13 | if hasattr(thing, '__iter__') and not isinstance(thing, str): |
|
13 | return text.replace('\n', '<br/>\n') | |
14 | return "".join([stringify(t) for t in thing if t is not None]) |
|
|||
15 | return str(thing) |
|
|||
16 |
|
14 | |||
17 | agescales = [("year", 3600 * 24 * 365), |
|
15 | agescales = [("year", 3600 * 24 * 365), | |
18 | ("month", 3600 * 24 * 30), |
|
16 | ("month", 3600 * 24 * 30), | |
@@ -46,6 +44,17 b' def age(date):' | |||||
46 | if n >= 2 or s == 1: |
|
44 | if n >= 2 or s == 1: | |
47 | return '%s ago' % fmt(t, n) |
|
45 | return '%s ago' % fmt(t, n) | |
48 |
|
46 | |||
|
47 | def domain(author): | |||
|
48 | '''get domain of author, or empty string if none.''' | |||
|
49 | f = author.find('@') | |||
|
50 | if f == -1: | |||
|
51 | return '' | |||
|
52 | author = author[f + 1:] | |||
|
53 | f = author.find('>') | |||
|
54 | if f >= 0: | |||
|
55 | author = author[:f] | |||
|
56 | return author | |||
|
57 | ||||
49 | para_re = None |
|
58 | para_re = None | |
50 | space_re = None |
|
59 | space_re = None | |
51 |
|
60 | |||
@@ -81,34 +90,6 b' def firstline(text):' | |||||
81 | except IndexError: |
|
90 | except IndexError: | |
82 | return '' |
|
91 | return '' | |
83 |
|
92 | |||
84 | def addbreaks(text): |
|
|||
85 | '''replace raw newlines with xhtml line breaks.''' |
|
|||
86 | return text.replace('\n', '<br/>\n') |
|
|||
87 |
|
||||
88 | def obfuscate(text): |
|
|||
89 | text = unicode(text, encoding.encoding, 'replace') |
|
|||
90 | return ''.join(['&#%d;' % ord(c) for c in text]) |
|
|||
91 |
|
||||
92 | def domain(author): |
|
|||
93 | '''get domain of author, or empty string if none.''' |
|
|||
94 | f = author.find('@') |
|
|||
95 | if f == -1: |
|
|||
96 | return '' |
|
|||
97 | author = author[f + 1:] |
|
|||
98 | f = author.find('>') |
|
|||
99 | if f >= 0: |
|
|||
100 | author = author[:f] |
|
|||
101 | return author |
|
|||
102 |
|
||||
103 | def person(author): |
|
|||
104 | '''get name of author, or else username.''' |
|
|||
105 | if not '@' in author: |
|
|||
106 | return author |
|
|||
107 | f = author.find('<') |
|
|||
108 | if f == -1: |
|
|||
109 | return util.shortuser(author) |
|
|||
110 | return author[:f].rstrip() |
|
|||
111 |
|
||||
112 | def indent(text, prefix): |
|
93 | def indent(text, prefix): | |
113 | '''indent each non-empty line of text after first with prefix.''' |
|
94 | '''indent each non-empty line of text after first with prefix.''' | |
114 | lines = text.splitlines() |
|
95 | lines = text.splitlines() | |
@@ -124,38 +105,6 b' def indent(text, prefix):' | |||||
124 | yield '\n' |
|
105 | yield '\n' | |
125 | return "".join(indenter()) |
|
106 | return "".join(indenter()) | |
126 |
|
107 | |||
127 | def permissions(flags): |
|
|||
128 | if "l" in flags: |
|
|||
129 | return "lrwxrwxrwx" |
|
|||
130 | if "x" in flags: |
|
|||
131 | return "-rwxr-xr-x" |
|
|||
132 | return "-rw-r--r--" |
|
|||
133 |
|
||||
134 | def xmlescape(text): |
|
|||
135 | text = (text |
|
|||
136 | .replace('&', '&') |
|
|||
137 | .replace('<', '<') |
|
|||
138 | .replace('>', '>') |
|
|||
139 | .replace('"', '"') |
|
|||
140 | .replace("'", ''')) # ' invalid in HTML |
|
|||
141 | return re.sub('[\x00-\x08\x0B\x0C\x0E-\x1F]', ' ', text) |
|
|||
142 |
|
||||
143 | def uescape(c): |
|
|||
144 | if ord(c) < 0x80: |
|
|||
145 | return c |
|
|||
146 | else: |
|
|||
147 | return '\\u%04x' % ord(c) |
|
|||
148 |
|
||||
149 | _escapes = [ |
|
|||
150 | ('\\', '\\\\'), ('"', '\\"'), ('\t', '\\t'), ('\n', '\\n'), |
|
|||
151 | ('\r', '\\r'), ('\f', '\\f'), ('\b', '\\b'), |
|
|||
152 | ] |
|
|||
153 |
|
||||
154 | def jsonescape(s): |
|
|||
155 | for k, v in _escapes: |
|
|||
156 | s = s.replace(k, v) |
|
|||
157 | return ''.join(uescape(c) for c in s) |
|
|||
158 |
|
||||
159 | def json(obj): |
|
108 | def json(obj): | |
160 | if obj is None or obj is False or obj is True: |
|
109 | if obj is None or obj is False or obj is True: | |
161 | return {None: 'null', False: 'false', True: 'true'}[obj] |
|
110 | return {None: 'null', False: 'false', True: 'true'}[obj] | |
@@ -180,6 +129,51 b' def json(obj):' | |||||
180 | else: |
|
129 | else: | |
181 | raise TypeError('cannot encode type %s' % obj.__class__.__name__) |
|
130 | raise TypeError('cannot encode type %s' % obj.__class__.__name__) | |
182 |
|
131 | |||
|
132 | def uescape(c): | |||
|
133 | if ord(c) < 0x80: | |||
|
134 | return c | |||
|
135 | else: | |||
|
136 | return '\\u%04x' % ord(c) | |||
|
137 | ||||
|
138 | _escapes = [ | |||
|
139 | ('\\', '\\\\'), ('"', '\\"'), ('\t', '\\t'), ('\n', '\\n'), | |||
|
140 | ('\r', '\\r'), ('\f', '\\f'), ('\b', '\\b'), | |||
|
141 | ] | |||
|
142 | ||||
|
143 | def jsonescape(s): | |||
|
144 | for k, v in _escapes: | |||
|
145 | s = s.replace(k, v) | |||
|
146 | return ''.join(uescape(c) for c in s) | |||
|
147 | ||||
|
148 | def nonempty(str): | |||
|
149 | return str or "(none)" | |||
|
150 | ||||
|
151 | def obfuscate(text): | |||
|
152 | text = unicode(text, encoding.encoding, 'replace') | |||
|
153 | return ''.join(['&#%d;' % ord(c) for c in text]) | |||
|
154 | ||||
|
155 | def permissions(flags): | |||
|
156 | if "l" in flags: | |||
|
157 | return "lrwxrwxrwx" | |||
|
158 | if "x" in flags: | |||
|
159 | return "-rwxr-xr-x" | |||
|
160 | return "-rw-r--r--" | |||
|
161 | ||||
|
162 | def person(author): | |||
|
163 | '''get name of author, or else username.''' | |||
|
164 | if not '@' in author: | |||
|
165 | return author | |||
|
166 | f = author.find('<') | |||
|
167 | if f == -1: | |||
|
168 | return util.shortuser(author) | |||
|
169 | return author[:f].rstrip() | |||
|
170 | ||||
|
171 | def stringify(thing): | |||
|
172 | '''turn nested template iterator into string.''' | |||
|
173 | if hasattr(thing, '__iter__') and not isinstance(thing, str): | |||
|
174 | return "".join([stringify(t) for t in thing if t is not None]) | |||
|
175 | return str(thing) | |||
|
176 | ||||
183 | def stripdir(text): |
|
177 | def stripdir(text): | |
184 | '''Treat the text as path and strip a directory level, if possible.''' |
|
178 | '''Treat the text as path and strip a directory level, if possible.''' | |
185 | dir = os.path.dirname(text) |
|
179 | dir = os.path.dirname(text) | |
@@ -188,8 +182,14 b' def stripdir(text):' | |||||
188 | else: |
|
182 | else: | |
189 | return dir |
|
183 | return dir | |
190 |
|
184 | |||
191 | def nonempty(str): |
|
185 | def xmlescape(text): | |
192 | return str or "(none)" |
|
186 | text = (text | |
|
187 | .replace('&', '&') | |||
|
188 | .replace('<', '<') | |||
|
189 | .replace('>', '>') | |||
|
190 | .replace('"', '"') | |||
|
191 | .replace("'", ''')) # ' invalid in HTML | |||
|
192 | return re.sub('[\x00-\x08\x0B\x0C\x0E-\x1F]', ' ', text) | |||
193 |
|
193 | |||
194 | filters = { |
|
194 | filters = { | |
195 | "addbreaks": addbreaks, |
|
195 | "addbreaks": addbreaks, |
General Comments 0
You need to be logged in to leave comments.
Login now