##// END OF EJS Templates
templatefilters: move doc from templates.txt to docstrings
Patrick Mezard -
r13591:264f292a default
parent child Browse files
Show More
@@ -13,7 +13,7 b' import hg, util, revlog, extensions, cop'
13 import patch, help, mdiff, url, encoding, templatekw, discovery
13 import patch, help, mdiff, url, encoding, templatekw, discovery
14 import archival, changegroup, cmdutil, sshserver, hbisect, hgweb, hgweb.server
14 import archival, changegroup, cmdutil, sshserver, hbisect, hgweb, hgweb.server
15 import merge as mergemod
15 import merge as mergemod
16 import minirst, revset
16 import minirst, revset, templatefilters
17 import dagparser
17 import dagparser
18
18
19 # Commands start here, listed alphabetically
19 # Commands start here, listed alphabetically
@@ -2143,6 +2143,7 b' def help_(ui, name=None, with_version=Fa'
2143
2143
2144 help.addtopichook('revsets', revset.makedoc)
2144 help.addtopichook('revsets', revset.makedoc)
2145 help.addtopichook('templates', templatekw.makedoc)
2145 help.addtopichook('templates', templatekw.makedoc)
2146 help.addtopichook('templates', templatefilters.makedoc)
2146
2147
2147 if name and name != 'shortlist':
2148 if name and name != 'shortlist':
2148 i = None
2149 i = None
@@ -37,82 +37,4 b' You can also use a chain of filters to g'
37
37
38 List of filters:
38 List of filters:
39
39
40 :addbreaks: Any text. Add an XHTML "<br />" tag before the end of
40 .. filtersmarker
41 every line except the last.
42
43 :age: Date. Returns a human-readable date/time difference between the
44 given date/time and the current date/time.
45
46 :basename: Any text. Treats the text as a path, and returns the last
47 component of the path after splitting by the path separator
48 (ignoring trailing separators). For example, "foo/bar/baz" becomes
49 "baz" and "foo/bar//" becomes "bar".
50
51 :stripdir: Treat the text as path and strip a directory level, if
52 possible. For example, "foo" and "foo/bar" becomes "foo".
53
54 :date: Date. Returns a date in a Unix date format, including the
55 timezone: "Mon Sep 04 15:13:13 2006 0700".
56
57 :domain: Any text. Finds the first string that looks like an email
58 address, and extracts just the domain component. Example: ``User
59 <user@example.com>`` becomes ``example.com``.
60
61 :email: Any text. Extracts the first string that looks like an email
62 address. Example: ``User <user@example.com>`` becomes
63 ``user@example.com``.
64
65 :escape: Any text. Replaces the special XML/XHTML characters "&", "<"
66 and ">" with XML entities.
67
68 :hex: Any text. Convert a binary Mercurial node identifier into
69 its long hexadecimal representation.
70
71 :fill68: Any text. Wraps the text to fit in 68 columns.
72
73 :fill76: Any text. Wraps the text to fit in 76 columns.
74
75 :firstline: Any text. Returns the first line of text.
76
77 :nonempty: Any text. Returns '(none)' if the string is empty.
78
79 :hgdate: Date. Returns the date as a pair of numbers: "1157407993
80 25200" (Unix timestamp, timezone offset).
81
82 :isodate: Date. Returns the date in ISO 8601 format: "2009-08-18 13:00
83 +0200".
84
85 :isodatesec: Date. Returns the date in ISO 8601 format, including
86 seconds: "2009-08-18 13:00:13 +0200". See also the rfc3339date
87 filter.
88
89 :localdate: Date. Converts a date to local date.
90
91 :obfuscate: Any text. Returns the input text rendered as a sequence of
92 XML entities.
93
94 :person: Any text. Returns the text before an email address.
95
96 :rfc822date: Date. Returns a date using the same format used in email
97 headers: "Tue, 18 Aug 2009 13:00:13 +0200".
98
99 :rfc3339date: Date. Returns a date using the Internet date format
100 specified in RFC 3339: "2009-08-18T13:00:13+02:00".
101
102 :short: Changeset hash. Returns the short form of a changeset hash,
103 i.e. a 12 hexadecimal digit string.
104
105 :shortdate: Date. Returns a date like "2006-09-18".
106
107 :stringify: Any type. Turns the value into text by converting values into
108 text and concatenating them.
109
110 :strip: Any text. Strips all leading and trailing whitespace.
111
112 :tabindent: Any text. Returns the text, with every line except the
113 first starting with a tab character.
114
115 :urlescape: Any text. Escapes all "special" characters. For example,
116 "foo bar" becomes "foo%20bar".
117
118 :user: Any text. Returns the user portion of an email address.
@@ -7,9 +7,12 b''
7
7
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 from i18n import gettext
10
11
11 def addbreaks(text):
12 def addbreaks(text):
12 '''replace raw newlines with xhtml line breaks.'''
13 """:addbreaks: Any text. Add an XHTML "<br />" tag before the end of
14 every line except the last.
15 """
13 return text.replace('\n', '<br/>\n')
16 return text.replace('\n', '<br/>\n')
14
17
15 agescales = [("year", 3600 * 24 * 365),
18 agescales = [("year", 3600 * 24 * 365),
@@ -21,7 +24,9 b' agescales = [("year", 3600 * 24 * 365),'
21 ("second", 1)]
24 ("second", 1)]
22
25
23 def age(date):
26 def age(date):
24 '''turn a (timestamp, tzoff) tuple into an age string.'''
27 """:age: Date. Returns a human-readable date/time difference between the
28 given date/time and the current date/time.
29 """
25
30
26 def plural(t, c):
31 def plural(t, c):
27 if c == 1:
32 if c == 1:
@@ -45,13 +50,24 b' def age(date):'
45 return '%s ago' % fmt(t, n)
50 return '%s ago' % fmt(t, n)
46
51
47 def basename(path):
52 def basename(path):
53 """:basename: Any text. Treats the text as a path, and returns the last
54 component of the path after splitting by the path separator
55 (ignoring trailing separators). For example, "foo/bar/baz" becomes
56 "baz" and "foo/bar//" becomes "bar".
57 """
48 return os.path.basename(path)
58 return os.path.basename(path)
49
59
50 def datefilter(text):
60 def datefilter(text):
61 """:date: Date. Returns a date in a Unix date format, including the
62 timezone: "Mon Sep 04 15:13:13 2006 0700".
63 """
51 return util.datestr(text)
64 return util.datestr(text)
52
65
53 def domain(author):
66 def domain(author):
54 '''get domain of author, or empty string if none.'''
67 """:domain: Any text. Finds the first string that looks like an email
68 address, and extracts just the domain component. Example: ``User
69 <user@example.com>`` becomes ``example.com``.
70 """
55 f = author.find('@')
71 f = author.find('@')
56 if f == -1:
72 if f == -1:
57 return ''
73 return ''
@@ -62,9 +78,16 b' def domain(author):'
62 return author
78 return author
63
79
64 def email(text):
80 def email(text):
81 """:email: Any text. Extracts the first string that looks like an email
82 address. Example: ``User <user@example.com>`` becomes
83 ``user@example.com``.
84 """
65 return util.email(text)
85 return util.email(text)
66
86
67 def escape(text):
87 def escape(text):
88 """:escape: Any text. Replaces the special XML/XHTML characters "&", "<"
89 and ">" with XML entities.
90 """
68 return cgi.escape(text, True)
91 return cgi.escape(text, True)
69
92
70 para_re = None
93 para_re = None
@@ -96,28 +119,43 b' def fill(text, width):'
96 for para, rest in findparas()])
119 for para, rest in findparas()])
97
120
98 def fill68(text):
121 def fill68(text):
122 """:fill68: Any text. Wraps the text to fit in 68 columns."""
99 return fill(text, 68)
123 return fill(text, 68)
100
124
101 def fill76(text):
125 def fill76(text):
126 """:fill76: Any text. Wraps the text to fit in 76 columns."""
102 return fill(text, 76)
127 return fill(text, 76)
103
128
104 def firstline(text):
129 def firstline(text):
105 '''return the first line of text'''
130 """:firstline: Any text. Returns the first line of text."""
106 try:
131 try:
107 return text.splitlines(True)[0].rstrip('\r\n')
132 return text.splitlines(True)[0].rstrip('\r\n')
108 except IndexError:
133 except IndexError:
109 return ''
134 return ''
110
135
111 def hexfilter(text):
136 def hexfilter(text):
137 """:hex: Any text. Convert a binary Mercurial node identifier into
138 its long hexadecimal representation.
139 """
112 return node.hex(text)
140 return node.hex(text)
113
141
114 def hgdate(text):
142 def hgdate(text):
143 """:hgdate: Date. Returns the date as a pair of numbers: "1157407993
144 25200" (Unix timestamp, timezone offset).
145 """
115 return "%d %d" % text
146 return "%d %d" % text
116
147
117 def isodate(text):
148 def isodate(text):
149 """:isodate: Date. Returns the date in ISO 8601 format: "2009-08-18 13:00
150 +0200".
151 """
118 return util.datestr(text, '%Y-%m-%d %H:%M %1%2')
152 return util.datestr(text, '%Y-%m-%d %H:%M %1%2')
119
153
120 def isodatesec(text):
154 def isodatesec(text):
155 """:isodatesec: Date. Returns the date in ISO 8601 format, including
156 seconds: "2009-08-18 13:00:13 +0200". See also the rfc3339date
157 filter.
158 """
121 return util.datestr(text, '%Y-%m-%d %H:%M:%S %1%2')
159 return util.datestr(text, '%Y-%m-%d %H:%M:%S %1%2')
122
160
123 def indent(text, prefix):
161 def indent(text, prefix):
@@ -176,12 +214,17 b' def jsonescape(s):'
176 return ''.join(_uescape(c) for c in s)
214 return ''.join(_uescape(c) for c in s)
177
215
178 def localdate(text):
216 def localdate(text):
217 """:localdate: Date. Converts a date to local date."""
179 return (text[0], util.makedate()[1])
218 return (text[0], util.makedate()[1])
180
219
181 def nonempty(str):
220 def nonempty(str):
221 """:nonempty: Any text. Returns '(none)' if the string is empty."""
182 return str or "(none)"
222 return str or "(none)"
183
223
184 def obfuscate(text):
224 def obfuscate(text):
225 """:obfuscate: Any text. Returns the input text rendered as a sequence of
226 XML entities.
227 """
185 text = unicode(text, encoding.encoding, 'replace')
228 text = unicode(text, encoding.encoding, 'replace')
186 return ''.join(['&#%d;' % ord(c) for c in text])
229 return ''.join(['&#%d;' % ord(c) for c in text])
187
230
@@ -193,7 +236,7 b' def permissions(flags):'
193 return "-rw-r--r--"
236 return "-rw-r--r--"
194
237
195 def person(author):
238 def person(author):
196 '''get name of author, or else username.'''
239 """:person: Any text. Returns the text before an email address."""
197 if not '@' in author:
240 if not '@' in author:
198 return author
241 return author
199 f = author.find('<')
242 f = author.find('<')
@@ -202,31 +245,46 b' def person(author):'
202 return author[:f].rstrip()
245 return author[:f].rstrip()
203
246
204 def rfc3339date(text):
247 def rfc3339date(text):
248 """:rfc3339date: Date. Returns a date using the Internet date format
249 specified in RFC 3339: "2009-08-18T13:00:13+02:00".
250 """
205 return util.datestr(text, "%Y-%m-%dT%H:%M:%S%1:%2")
251 return util.datestr(text, "%Y-%m-%dT%H:%M:%S%1:%2")
206
252
207 def rfc822date(text):
253 def rfc822date(text):
254 """:rfc822date: Date. Returns a date using the same format used in email
255 headers: "Tue, 18 Aug 2009 13:00:13 +0200".
256 """
208 return util.datestr(text, "%a, %d %b %Y %H:%M:%S %1%2")
257 return util.datestr(text, "%a, %d %b %Y %H:%M:%S %1%2")
209
258
210 def short(text):
259 def short(text):
260 """:short: Changeset hash. Returns the short form of a changeset hash,
261 i.e. a 12 hexadecimal digit string.
262 """
211 return text[:12]
263 return text[:12]
212
264
213 def shortdate(text):
265 def shortdate(text):
266 """:shortdate: Date. Returns a date like "2006-09-18"."""
214 return util.shortdate(text)
267 return util.shortdate(text)
215
268
216 def stringescape(text):
269 def stringescape(text):
217 return text.encode('string_escape')
270 return text.encode('string_escape')
218
271
219 def stringify(thing):
272 def stringify(thing):
220 '''turn nested template iterator into string.'''
273 """:stringify: Any type. Turns the value into text by converting values into
274 text and concatenating them.
275 """
221 if hasattr(thing, '__iter__') and not isinstance(thing, str):
276 if hasattr(thing, '__iter__') and not isinstance(thing, str):
222 return "".join([stringify(t) for t in thing if t is not None])
277 return "".join([stringify(t) for t in thing if t is not None])
223 return str(thing)
278 return str(thing)
224
279
225 def strip(text):
280 def strip(text):
281 """:strip: Any text. Strips all leading and trailing whitespace."""
226 return text.strip()
282 return text.strip()
227
283
228 def stripdir(text):
284 def stripdir(text):
229 '''Treat the text as path and strip a directory level, if possible.'''
285 """:stripdir: Treat the text as path and strip a directory level, if
286 possible. For example, "foo" and "foo/bar" becomes "foo".
287 """
230 dir = os.path.dirname(text)
288 dir = os.path.dirname(text)
231 if dir == "":
289 if dir == "":
232 return os.path.basename(text)
290 return os.path.basename(text)
@@ -234,12 +292,19 b' def stripdir(text):'
234 return dir
292 return dir
235
293
236 def tabindent(text):
294 def tabindent(text):
295 """:tabindent: Any text. Returns the text, with every line except the
296 first starting with a tab character.
297 """
237 return indent(text, '\t')
298 return indent(text, '\t')
238
299
239 def urlescape(text):
300 def urlescape(text):
301 """:urlescape: Any text. Escapes all "special" characters. For example,
302 "foo bar" becomes "foo%20bar".
303 """
240 return urllib.quote(text)
304 return urllib.quote(text)
241
305
242 def userfilter(text):
306 def userfilter(text):
307 """:user: Any text. Returns the user portion of an email address."""
243 return util.shortuser(text)
308 return util.shortuser(text)
244
309
245 def xmlescape(text):
310 def xmlescape(text):
@@ -286,3 +351,21 b' filters = {'
286 "user": userfilter,
351 "user": userfilter,
287 "xmlescape": xmlescape,
352 "xmlescape": xmlescape,
288 }
353 }
354
355 def makedoc(topic, doc):
356 """Generate and include templatefilters help in templating topic."""
357 entries = []
358 for name in sorted(filters):
359 text = (filters[name].__doc__ or '').rstrip()
360 if not text:
361 continue
362 text = gettext(text)
363 lines = text.splitlines()
364 lines[1:] = [(' ' + l.strip()) for l in lines[1:]]
365 entries.append('\n'.join(lines))
366 entries = '\n\n'.join(entries)
367 doc = doc.replace('.. filtersmarker', entries)
368 return doc
369
370 # tell hggettext to extract docstrings from these functions:
371 i18nfunctions = filters.values()
General Comments 0
You need to be logged in to leave comments. Login now