2008-01-31 23:44:19 +03:00
|
|
|
# template-filters.py - common template expansion filters
|
|
|
|
#
|
|
|
|
# Copyright 2005-2008 Matt Mackall <mpm@selenic.com>
|
|
|
|
#
|
2009-04-26 03:08:54 +04:00
|
|
|
# This software may be used and distributed according to the terms of the
|
2010-01-20 07:20:08 +03:00
|
|
|
# GNU General Public License version 2 or any later version.
|
2008-01-31 23:44:19 +03:00
|
|
|
|
replace Python standard textwrap by MBCS sensitive one for i18n text
Mercurial has problem around text wrapping/filling in MBCS encoding
environment, because standard 'textwrap' module of Python can not
treat it correctly. It splits byte sequence for one character into two
lines.
According to unicode specification, "east asian width" classifies
characters into:
W(ide), N(arrow), F(ull-width), H(alf-width), A(mbiguous)
W/N/F/H can be always recognized as 2/1/2/1 bytes in byte sequence,
but 'A' can not. Size of 'A' depends on language in which it is used.
Unicode specification says:
If the context(= language) cannot be established reliably they
should be treated as narrow characters by default
but many of class 'A' characters are full-width, at least, in Japanese
environment.
So, this patch treats class 'A' characters as full-width always for
safety wrapping.
This patch focuses only on MBCS safe-ness, not on writing/printing
rule strict wrapping for each languages
MBCS sensitive textwrap class is originally implemented
by ITO Nobuaki <daydream.trippers@gmail.com>.
2010-06-06 12:20:10 +04:00
|
|
|
import cgi, re, os, time, urllib
|
2013-04-11 03:56:38 +04:00
|
|
|
import encoding, node, util
|
2011-09-22 03:36:01 +04:00
|
|
|
import hbisect
|
2014-06-13 04:45:41 +04:00
|
|
|
import templatekw
|
2008-01-31 23:44:19 +03:00
|
|
|
|
2011-03-12 14:46:31 +03:00
|
|
|
def addbreaks(text):
|
2011-03-12 14:46:31 +03:00
|
|
|
""":addbreaks: Any text. Add an XHTML "<br />" tag before the end of
|
|
|
|
every line except the last.
|
|
|
|
"""
|
2011-03-12 14:46:31 +03:00
|
|
|
return text.replace('\n', '<br/>\n')
|
2009-05-12 14:04:05 +04:00
|
|
|
|
2013-09-17 19:42:13 +04:00
|
|
|
agescales = [("year", 3600 * 24 * 365, 'Y'),
|
|
|
|
("month", 3600 * 24 * 30, 'M'),
|
|
|
|
("week", 3600 * 24 * 7, 'W'),
|
|
|
|
("day", 3600 * 24, 'd'),
|
|
|
|
("hour", 3600, 'h'),
|
|
|
|
("minute", 60, 'm'),
|
|
|
|
("second", 1, 's')]
|
2008-01-31 23:44:19 +03:00
|
|
|
|
2013-09-17 19:42:13 +04:00
|
|
|
def age(date, abbrev=False):
|
2011-03-12 14:46:31 +03:00
|
|
|
""":age: Date. Returns a human-readable date/time difference between the
|
|
|
|
given date/time and the current date/time.
|
|
|
|
"""
|
2008-01-31 23:44:19 +03:00
|
|
|
|
|
|
|
def plural(t, c):
|
|
|
|
if c == 1:
|
|
|
|
return t
|
|
|
|
return t + "s"
|
2013-09-17 19:42:13 +04:00
|
|
|
def fmt(t, c, a):
|
|
|
|
if abbrev:
|
|
|
|
return "%d%s" % (c, a)
|
2008-01-31 23:44:19 +03:00
|
|
|
return "%d %s" % (c, plural(t, c))
|
|
|
|
|
|
|
|
now = time.time()
|
|
|
|
then = date[0]
|
2011-03-16 05:28:56 +03:00
|
|
|
future = False
|
2009-01-22 18:07:44 +03:00
|
|
|
if then > now:
|
2011-03-16 05:28:56 +03:00
|
|
|
future = True
|
|
|
|
delta = max(1, int(then - now))
|
|
|
|
if delta > agescales[0][1] * 30:
|
|
|
|
return 'in the distant future'
|
|
|
|
else:
|
|
|
|
delta = max(1, int(now - then))
|
|
|
|
if delta > agescales[0][1] * 2:
|
|
|
|
return util.shortdate(date)
|
2009-11-05 17:19:54 +03:00
|
|
|
|
2013-09-17 19:42:13 +04:00
|
|
|
for t, s, a in agescales:
|
2009-07-05 13:00:44 +04:00
|
|
|
n = delta // s
|
2008-01-31 23:44:19 +03:00
|
|
|
if n >= 2 or s == 1:
|
2011-03-16 05:28:56 +03:00
|
|
|
if future:
|
2013-09-17 19:42:13 +04:00
|
|
|
return '%s from now' % fmt(t, n, a)
|
|
|
|
return '%s ago' % fmt(t, n, a)
|
2008-01-31 23:44:19 +03:00
|
|
|
|
2011-03-12 14:46:31 +03:00
|
|
|
def basename(path):
|
2011-03-12 14:46:31 +03:00
|
|
|
""":basename: Any text. Treats the text as a path, and returns the last
|
|
|
|
component of the path after splitting by the path separator
|
|
|
|
(ignoring trailing separators). For example, "foo/bar/baz" becomes
|
|
|
|
"baz" and "foo/bar//" becomes "bar".
|
|
|
|
"""
|
2011-03-12 14:46:31 +03:00
|
|
|
return os.path.basename(path)
|
|
|
|
|
|
|
|
def datefilter(text):
|
2011-03-12 14:46:31 +03:00
|
|
|
""":date: Date. Returns a date in a Unix date format, including the
|
|
|
|
timezone: "Mon Sep 04 15:13:13 2006 0700".
|
|
|
|
"""
|
2011-03-12 14:46:31 +03:00
|
|
|
return util.datestr(text)
|
|
|
|
|
2011-03-12 14:46:31 +03:00
|
|
|
def domain(author):
|
2011-03-12 14:46:31 +03:00
|
|
|
""":domain: Any text. Finds the first string that looks like an email
|
|
|
|
address, and extracts just the domain component. Example: ``User
|
|
|
|
<user@example.com>`` becomes ``example.com``.
|
|
|
|
"""
|
2011-03-12 14:46:31 +03:00
|
|
|
f = author.find('@')
|
|
|
|
if f == -1:
|
|
|
|
return ''
|
|
|
|
author = author[f + 1:]
|
|
|
|
f = author.find('>')
|
|
|
|
if f >= 0:
|
|
|
|
author = author[:f]
|
|
|
|
return author
|
|
|
|
|
2011-03-12 14:46:31 +03:00
|
|
|
def email(text):
|
2011-03-12 14:46:31 +03:00
|
|
|
""":email: Any text. Extracts the first string that looks like an email
|
|
|
|
address. Example: ``User <user@example.com>`` becomes
|
|
|
|
``user@example.com``.
|
|
|
|
"""
|
2011-03-12 14:46:31 +03:00
|
|
|
return util.email(text)
|
|
|
|
|
|
|
|
def escape(text):
|
2011-03-12 14:46:31 +03:00
|
|
|
""":escape: Any text. Replaces the special XML/XHTML characters "&", "<"
|
2012-10-15 20:43:34 +04:00
|
|
|
and ">" with XML entities, and filters out NUL characters.
|
2011-03-12 14:46:31 +03:00
|
|
|
"""
|
2012-10-15 20:43:34 +04:00
|
|
|
return cgi.escape(text.replace('\0', ''), True)
|
2011-03-12 14:46:31 +03:00
|
|
|
|
2008-01-31 23:44:19 +03:00
|
|
|
para_re = None
|
|
|
|
space_re = None
|
|
|
|
|
2013-10-03 16:50:47 +04:00
|
|
|
def fill(text, width, initindent='', hangindent=''):
|
2013-04-19 00:48:22 +04:00
|
|
|
'''fill many paragraphs with optional indentation.'''
|
2008-01-31 23:44:19 +03:00
|
|
|
global para_re, space_re
|
|
|
|
if para_re is None:
|
|
|
|
para_re = re.compile('(\n\n|\n\\s*[-*]\\s*)', re.M)
|
|
|
|
space_re = re.compile(r' +')
|
|
|
|
|
|
|
|
def findparas():
|
|
|
|
start = 0
|
|
|
|
while True:
|
|
|
|
m = para_re.search(text, start)
|
|
|
|
if not m:
|
replace Python standard textwrap by MBCS sensitive one for i18n text
Mercurial has problem around text wrapping/filling in MBCS encoding
environment, because standard 'textwrap' module of Python can not
treat it correctly. It splits byte sequence for one character into two
lines.
According to unicode specification, "east asian width" classifies
characters into:
W(ide), N(arrow), F(ull-width), H(alf-width), A(mbiguous)
W/N/F/H can be always recognized as 2/1/2/1 bytes in byte sequence,
but 'A' can not. Size of 'A' depends on language in which it is used.
Unicode specification says:
If the context(= language) cannot be established reliably they
should be treated as narrow characters by default
but many of class 'A' characters are full-width, at least, in Japanese
environment.
So, this patch treats class 'A' characters as full-width always for
safety wrapping.
This patch focuses only on MBCS safe-ness, not on writing/printing
rule strict wrapping for each languages
MBCS sensitive textwrap class is originally implemented
by ITO Nobuaki <daydream.trippers@gmail.com>.
2010-06-06 12:20:10 +04:00
|
|
|
uctext = unicode(text[start:], encoding.encoding)
|
|
|
|
w = len(uctext)
|
|
|
|
while 0 < w and uctext[w - 1].isspace():
|
2010-01-25 09:05:27 +03:00
|
|
|
w -= 1
|
replace Python standard textwrap by MBCS sensitive one for i18n text
Mercurial has problem around text wrapping/filling in MBCS encoding
environment, because standard 'textwrap' module of Python can not
treat it correctly. It splits byte sequence for one character into two
lines.
According to unicode specification, "east asian width" classifies
characters into:
W(ide), N(arrow), F(ull-width), H(alf-width), A(mbiguous)
W/N/F/H can be always recognized as 2/1/2/1 bytes in byte sequence,
but 'A' can not. Size of 'A' depends on language in which it is used.
Unicode specification says:
If the context(= language) cannot be established reliably they
should be treated as narrow characters by default
but many of class 'A' characters are full-width, at least, in Japanese
environment.
So, this patch treats class 'A' characters as full-width always for
safety wrapping.
This patch focuses only on MBCS safe-ness, not on writing/printing
rule strict wrapping for each languages
MBCS sensitive textwrap class is originally implemented
by ITO Nobuaki <daydream.trippers@gmail.com>.
2010-06-06 12:20:10 +04:00
|
|
|
yield (uctext[:w].encode(encoding.encoding),
|
|
|
|
uctext[w:].encode(encoding.encoding))
|
2008-01-31 23:44:19 +03:00
|
|
|
break
|
|
|
|
yield text[start:m.start(0)], m.group(1)
|
|
|
|
start = m.end(1)
|
|
|
|
|
2013-04-19 00:48:22 +04:00
|
|
|
return "".join([util.wrap(space_re.sub(' ', util.wrap(para, width)),
|
|
|
|
width, initindent, hangindent) + rest
|
2008-01-31 23:44:19 +03:00
|
|
|
for para, rest in findparas()])
|
|
|
|
|
2011-03-12 14:46:31 +03:00
|
|
|
def fill68(text):
|
2011-03-12 14:46:31 +03:00
|
|
|
""":fill68: Any text. Wraps the text to fit in 68 columns."""
|
2011-03-12 14:46:31 +03:00
|
|
|
return fill(text, 68)
|
|
|
|
|
|
|
|
def fill76(text):
|
2011-03-12 14:46:31 +03:00
|
|
|
""":fill76: Any text. Wraps the text to fit in 76 columns."""
|
2011-03-12 14:46:31 +03:00
|
|
|
return fill(text, 76)
|
|
|
|
|
2008-01-31 23:44:19 +03:00
|
|
|
def firstline(text):
|
2011-03-12 14:46:31 +03:00
|
|
|
""":firstline: Any text. Returns the first line of text."""
|
2008-01-31 23:44:19 +03:00
|
|
|
try:
|
2009-07-13 04:50:26 +04:00
|
|
|
return text.splitlines(True)[0].rstrip('\r\n')
|
2008-01-31 23:44:19 +03:00
|
|
|
except IndexError:
|
|
|
|
return ''
|
|
|
|
|
2011-03-12 14:46:31 +03:00
|
|
|
def hexfilter(text):
|
2011-03-12 14:46:31 +03:00
|
|
|
""":hex: Any text. Convert a binary Mercurial node identifier into
|
|
|
|
its long hexadecimal representation.
|
|
|
|
"""
|
2011-03-12 14:46:31 +03:00
|
|
|
return node.hex(text)
|
|
|
|
|
|
|
|
def hgdate(text):
|
2011-03-12 14:46:31 +03:00
|
|
|
""":hgdate: Date. Returns the date as a pair of numbers: "1157407993
|
|
|
|
25200" (Unix timestamp, timezone offset).
|
|
|
|
"""
|
2011-03-12 14:46:31 +03:00
|
|
|
return "%d %d" % text
|
|
|
|
|
|
|
|
def isodate(text):
|
2011-03-12 14:46:31 +03:00
|
|
|
""":isodate: Date. Returns the date in ISO 8601 format: "2009-08-18 13:00
|
|
|
|
+0200".
|
|
|
|
"""
|
2011-03-12 14:46:31 +03:00
|
|
|
return util.datestr(text, '%Y-%m-%d %H:%M %1%2')
|
|
|
|
|
|
|
|
def isodatesec(text):
|
2011-03-12 14:46:31 +03:00
|
|
|
""":isodatesec: Date. Returns the date in ISO 8601 format, including
|
|
|
|
seconds: "2009-08-18 13:00:13 +0200". See also the rfc3339date
|
|
|
|
filter.
|
|
|
|
"""
|
2011-03-12 14:46:31 +03:00
|
|
|
return util.datestr(text, '%Y-%m-%d %H:%M:%S %1%2')
|
|
|
|
|
2008-01-31 23:44:19 +03:00
|
|
|
def indent(text, prefix):
|
|
|
|
'''indent each non-empty line of text after first with prefix.'''
|
|
|
|
lines = text.splitlines()
|
|
|
|
num_lines = len(lines)
|
2009-08-22 21:40:15 +04:00
|
|
|
endswithnewline = text[-1:] == '\n'
|
2008-01-31 23:44:19 +03:00
|
|
|
def indenter():
|
|
|
|
for i in xrange(num_lines):
|
|
|
|
l = lines[i]
|
|
|
|
if i and l.strip():
|
|
|
|
yield prefix
|
|
|
|
yield l
|
2009-08-22 21:40:15 +04:00
|
|
|
if i < num_lines - 1 or endswithnewline:
|
2008-01-31 23:44:19 +03:00
|
|
|
yield '\n'
|
|
|
|
return "".join(indenter())
|
|
|
|
|
2008-06-18 09:06:41 +04:00
|
|
|
def json(obj):
|
|
|
|
if obj is None or obj is False or obj is True:
|
|
|
|
return {None: 'null', False: 'false', True: 'true'}[obj]
|
|
|
|
elif isinstance(obj, int) or isinstance(obj, float):
|
|
|
|
return str(obj)
|
|
|
|
elif isinstance(obj, str):
|
2010-08-07 11:27:16 +04:00
|
|
|
u = unicode(obj, encoding.encoding, 'replace')
|
2010-08-10 20:06:21 +04:00
|
|
|
return '"%s"' % jsonescape(u)
|
2008-06-18 09:06:41 +04:00
|
|
|
elif isinstance(obj, unicode):
|
2010-08-10 20:06:21 +04:00
|
|
|
return '"%s"' % jsonescape(obj)
|
2011-07-26 01:02:42 +04:00
|
|
|
elif util.safehasattr(obj, 'keys'):
|
2008-06-18 09:06:41 +04:00
|
|
|
out = []
|
|
|
|
for k, v in obj.iteritems():
|
|
|
|
s = '%s: %s' % (json(k), json(v))
|
|
|
|
out.append(s)
|
|
|
|
return '{' + ', '.join(out) + '}'
|
2011-07-26 00:30:19 +04:00
|
|
|
elif util.safehasattr(obj, '__iter__'):
|
2008-06-18 09:06:41 +04:00
|
|
|
out = []
|
|
|
|
for i in obj:
|
|
|
|
out.append(json(i))
|
|
|
|
return '[' + ', '.join(out) + ']'
|
|
|
|
else:
|
|
|
|
raise TypeError('cannot encode type %s' % obj.__class__.__name__)
|
|
|
|
|
2011-03-12 14:46:31 +03:00
|
|
|
def _uescape(c):
|
2011-03-12 14:46:31 +03:00
|
|
|
if ord(c) < 0x80:
|
|
|
|
return c
|
|
|
|
else:
|
|
|
|
return '\\u%04x' % ord(c)
|
|
|
|
|
|
|
|
_escapes = [
|
|
|
|
('\\', '\\\\'), ('"', '\\"'), ('\t', '\\t'), ('\n', '\\n'),
|
|
|
|
('\r', '\\r'), ('\f', '\\f'), ('\b', '\\b'),
|
2014-07-14 21:44:45 +04:00
|
|
|
('<', '\\u003c'), ('>', '\\u003e'), ('\0', '\\u0000')
|
2011-03-12 14:46:31 +03:00
|
|
|
]
|
|
|
|
|
|
|
|
def jsonescape(s):
|
|
|
|
for k, v in _escapes:
|
|
|
|
s = s.replace(k, v)
|
2011-03-12 14:46:31 +03:00
|
|
|
return ''.join(_uescape(c) for c in s)
|
2011-03-12 14:46:31 +03:00
|
|
|
|
2011-03-12 14:46:31 +03:00
|
|
|
def localdate(text):
|
2011-03-12 14:46:31 +03:00
|
|
|
""":localdate: Date. Converts a date to local date."""
|
2012-08-10 23:37:20 +04:00
|
|
|
return (util.parsedate(text)[0], util.makedate()[1])
|
2011-03-12 14:46:31 +03:00
|
|
|
|
2011-03-12 14:46:31 +03:00
|
|
|
def nonempty(str):
|
2011-03-12 14:46:31 +03:00
|
|
|
""":nonempty: Any text. Returns '(none)' if the string is empty."""
|
2011-03-12 14:46:31 +03:00
|
|
|
return str or "(none)"
|
|
|
|
|
|
|
|
def obfuscate(text):
|
2011-03-12 14:46:31 +03:00
|
|
|
""":obfuscate: Any text. Returns the input text rendered as a sequence of
|
|
|
|
XML entities.
|
|
|
|
"""
|
2011-03-12 14:46:31 +03:00
|
|
|
text = unicode(text, encoding.encoding, 'replace')
|
|
|
|
return ''.join(['&#%d;' % ord(c) for c in text])
|
|
|
|
|
|
|
|
def permissions(flags):
|
|
|
|
if "l" in flags:
|
|
|
|
return "lrwxrwxrwx"
|
|
|
|
if "x" in flags:
|
|
|
|
return "-rwxr-xr-x"
|
|
|
|
return "-rw-r--r--"
|
|
|
|
|
|
|
|
def person(author):
|
templates/filters: strip quotes from {author|person}
RFC5322 (Internet Message Format) [0] says that the 'display name' of
an internet address [1] (what Mercurial calls 'person') can be quoted
with DQUOTE (ASCII 34: ") if it contains non-atom characters [2].
For example, dot '.' is a non-atom character. Also, DQUOTEs in a
quoted string will be escaped using "\" [2][3].
The current {author|person} template+filter just extracts the part
before an email address as-is. This can look ugly, especially on the
web interface, or when generating output for post-processing...
Moreover, as an example, the Mercurial repository has a bunch of
incoherent uses of DQUOTES in author names. As per Matt's digging:
$ hg log --template "{author|person}\n" | grep '"' | sort | uniq
"Andrei Vermel
"Aurelien Jacobs
"Daniel Santa Cruz
"Hidetaka Iwai
"Hiroshi Funai"
"Mathieu Clabaut
"Paul Moore
"Peter Arrenbrecht"
"Rafael Villar Burke
"Shun-ichi GOTO"
"Wallace, Eric S"
"Yann E. MORIN"
Josef "Jeff" Sipek
Radoslaw "AstralStorm" Szkodzinski
Fix the 'person' filter to remove leading and trailing DQUOTES,
and unescape remaining DQUOTES.
Given this author: "J. \"random\" DOE" <john@doe.net>
before: {author|person} : "J. \"random\" DOE"
after: {author|person} : J. "random" DOE
For the Mercurial repository, that leaves us with two authors with
DQUOTES, in acceptable positions:
$ hg log --template "{author|person}\n" | grep '"' | sort | uniq
Josef "Jeff" Sipek
Radoslaw "AstralStorm" Szkodzinski
[0] https://tools.ietf.org/html/rfc5322
[1] https://tools.ietf.org/html/rfc5322#section-3.4
[2] https://tools.ietf.org/html/rfc5322#section-3.2.4
[3] https://tools.ietf.org/html/rfc5322#section-3.2.1
Signed-off-by: "Yann E. MORIN" <yann.morin.1998@free.fr>
2012-03-07 02:23:30 +04:00
|
|
|
""":person: Any text. Returns the name before an email address,
|
|
|
|
interpreting it as per RFC 5322.
|
2012-03-07 04:02:12 +04:00
|
|
|
|
|
|
|
>>> person('foo@bar')
|
|
|
|
'foo'
|
|
|
|
>>> person('Foo Bar <foo@bar>')
|
|
|
|
'Foo Bar'
|
|
|
|
>>> person('"Foo Bar" <foo@bar>')
|
|
|
|
'Foo Bar'
|
|
|
|
>>> person('"Foo \"buz\" Bar" <foo@bar>')
|
|
|
|
'Foo "buz" Bar'
|
|
|
|
>>> # The following are invalid, but do exist in real-life
|
|
|
|
...
|
|
|
|
>>> person('Foo "buz" Bar <foo@bar>')
|
|
|
|
'Foo "buz" Bar'
|
|
|
|
>>> person('"Foo Bar <foo@bar>')
|
|
|
|
'Foo Bar'
|
templates/filters: strip quotes from {author|person}
RFC5322 (Internet Message Format) [0] says that the 'display name' of
an internet address [1] (what Mercurial calls 'person') can be quoted
with DQUOTE (ASCII 34: ") if it contains non-atom characters [2].
For example, dot '.' is a non-atom character. Also, DQUOTEs in a
quoted string will be escaped using "\" [2][3].
The current {author|person} template+filter just extracts the part
before an email address as-is. This can look ugly, especially on the
web interface, or when generating output for post-processing...
Moreover, as an example, the Mercurial repository has a bunch of
incoherent uses of DQUOTES in author names. As per Matt's digging:
$ hg log --template "{author|person}\n" | grep '"' | sort | uniq
"Andrei Vermel
"Aurelien Jacobs
"Daniel Santa Cruz
"Hidetaka Iwai
"Hiroshi Funai"
"Mathieu Clabaut
"Paul Moore
"Peter Arrenbrecht"
"Rafael Villar Burke
"Shun-ichi GOTO"
"Wallace, Eric S"
"Yann E. MORIN"
Josef "Jeff" Sipek
Radoslaw "AstralStorm" Szkodzinski
Fix the 'person' filter to remove leading and trailing DQUOTES,
and unescape remaining DQUOTES.
Given this author: "J. \"random\" DOE" <john@doe.net>
before: {author|person} : "J. \"random\" DOE"
after: {author|person} : J. "random" DOE
For the Mercurial repository, that leaves us with two authors with
DQUOTES, in acceptable positions:
$ hg log --template "{author|person}\n" | grep '"' | sort | uniq
Josef "Jeff" Sipek
Radoslaw "AstralStorm" Szkodzinski
[0] https://tools.ietf.org/html/rfc5322
[1] https://tools.ietf.org/html/rfc5322#section-3.4
[2] https://tools.ietf.org/html/rfc5322#section-3.2.4
[3] https://tools.ietf.org/html/rfc5322#section-3.2.1
Signed-off-by: "Yann E. MORIN" <yann.morin.1998@free.fr>
2012-03-07 02:23:30 +04:00
|
|
|
"""
|
2012-05-12 18:00:57 +04:00
|
|
|
if '@' not in author:
|
2011-03-12 14:46:31 +03:00
|
|
|
return author
|
|
|
|
f = author.find('<')
|
2011-04-17 23:34:25 +04:00
|
|
|
if f != -1:
|
templates/filters: strip quotes from {author|person}
RFC5322 (Internet Message Format) [0] says that the 'display name' of
an internet address [1] (what Mercurial calls 'person') can be quoted
with DQUOTE (ASCII 34: ") if it contains non-atom characters [2].
For example, dot '.' is a non-atom character. Also, DQUOTEs in a
quoted string will be escaped using "\" [2][3].
The current {author|person} template+filter just extracts the part
before an email address as-is. This can look ugly, especially on the
web interface, or when generating output for post-processing...
Moreover, as an example, the Mercurial repository has a bunch of
incoherent uses of DQUOTES in author names. As per Matt's digging:
$ hg log --template "{author|person}\n" | grep '"' | sort | uniq
"Andrei Vermel
"Aurelien Jacobs
"Daniel Santa Cruz
"Hidetaka Iwai
"Hiroshi Funai"
"Mathieu Clabaut
"Paul Moore
"Peter Arrenbrecht"
"Rafael Villar Burke
"Shun-ichi GOTO"
"Wallace, Eric S"
"Yann E. MORIN"
Josef "Jeff" Sipek
Radoslaw "AstralStorm" Szkodzinski
Fix the 'person' filter to remove leading and trailing DQUOTES,
and unescape remaining DQUOTES.
Given this author: "J. \"random\" DOE" <john@doe.net>
before: {author|person} : "J. \"random\" DOE"
after: {author|person} : J. "random" DOE
For the Mercurial repository, that leaves us with two authors with
DQUOTES, in acceptable positions:
$ hg log --template "{author|person}\n" | grep '"' | sort | uniq
Josef "Jeff" Sipek
Radoslaw "AstralStorm" Szkodzinski
[0] https://tools.ietf.org/html/rfc5322
[1] https://tools.ietf.org/html/rfc5322#section-3.4
[2] https://tools.ietf.org/html/rfc5322#section-3.2.4
[3] https://tools.ietf.org/html/rfc5322#section-3.2.1
Signed-off-by: "Yann E. MORIN" <yann.morin.1998@free.fr>
2012-03-07 02:23:30 +04:00
|
|
|
return author[:f].strip(' "').replace('\\"', '"')
|
2011-04-17 23:34:25 +04:00
|
|
|
f = author.find('@')
|
|
|
|
return author[:f].replace('.', ' ')
|
2011-03-12 14:46:31 +03:00
|
|
|
|
2011-03-12 14:46:31 +03:00
|
|
|
def rfc3339date(text):
|
2011-03-12 14:46:31 +03:00
|
|
|
""":rfc3339date: Date. Returns a date using the Internet date format
|
|
|
|
specified in RFC 3339: "2009-08-18T13:00:13+02:00".
|
|
|
|
"""
|
2011-03-12 14:46:31 +03:00
|
|
|
return util.datestr(text, "%Y-%m-%dT%H:%M:%S%1:%2")
|
|
|
|
|
|
|
|
def rfc822date(text):
|
2011-03-12 14:46:31 +03:00
|
|
|
""":rfc822date: Date. Returns a date using the same format used in email
|
|
|
|
headers: "Tue, 18 Aug 2009 13:00:13 +0200".
|
|
|
|
"""
|
2011-03-12 14:46:31 +03:00
|
|
|
return util.datestr(text, "%a, %d %b %Y %H:%M:%S %1%2")
|
|
|
|
|
|
|
|
def short(text):
|
2011-03-12 14:46:31 +03:00
|
|
|
""":short: Changeset hash. Returns the short form of a changeset hash,
|
|
|
|
i.e. a 12 hexadecimal digit string.
|
|
|
|
"""
|
2011-03-12 14:46:31 +03:00
|
|
|
return text[:12]
|
|
|
|
|
2011-09-22 03:36:01 +04:00
|
|
|
def shortbisect(text):
|
|
|
|
""":shortbisect: Any text. Treats `text` as a bisection status, and
|
|
|
|
returns a single-character representing the status (G: good, B: bad,
|
|
|
|
S: skipped, U: untested, I: ignored). Returns single space if `text`
|
|
|
|
is not a valid bisection status.
|
|
|
|
"""
|
|
|
|
return hbisect.shortlabel(text) or ' '
|
|
|
|
|
2011-03-12 14:46:31 +03:00
|
|
|
def shortdate(text):
|
2011-03-12 14:46:31 +03:00
|
|
|
""":shortdate: Date. Returns a date like "2006-09-18"."""
|
2011-03-12 14:46:31 +03:00
|
|
|
return util.shortdate(text)
|
|
|
|
|
2014-06-13 04:45:41 +04:00
|
|
|
def splitlines(text):
|
|
|
|
""":splitlines: Any text. Split text into a list of lines."""
|
|
|
|
return templatekw.showlist('line', text.splitlines(), 'lines')
|
|
|
|
|
2011-03-12 14:46:31 +03:00
|
|
|
def stringescape(text):
|
|
|
|
return text.encode('string_escape')
|
|
|
|
|
2011-03-12 14:46:31 +03:00
|
|
|
def stringify(thing):
|
2011-03-12 14:46:31 +03:00
|
|
|
""":stringify: Any type. Turns the value into text by converting values into
|
|
|
|
text and concatenating them.
|
|
|
|
"""
|
2011-07-26 00:30:19 +04:00
|
|
|
if util.safehasattr(thing, '__iter__') and not isinstance(thing, str):
|
2011-03-12 14:46:31 +03:00
|
|
|
return "".join([stringify(t) for t in thing if t is not None])
|
|
|
|
return str(thing)
|
|
|
|
|
2011-03-12 14:46:31 +03:00
|
|
|
def strip(text):
|
2011-03-12 14:46:31 +03:00
|
|
|
""":strip: Any text. Strips all leading and trailing whitespace."""
|
2011-03-12 14:46:31 +03:00
|
|
|
return text.strip()
|
|
|
|
|
2009-04-24 20:37:44 +04:00
|
|
|
def stripdir(text):
|
2011-03-12 14:46:31 +03:00
|
|
|
""":stripdir: Treat the text as path and strip a directory level, if
|
|
|
|
possible. For example, "foo" and "foo/bar" becomes "foo".
|
|
|
|
"""
|
2009-04-24 20:37:44 +04:00
|
|
|
dir = os.path.dirname(text)
|
|
|
|
if dir == "":
|
|
|
|
return os.path.basename(text)
|
|
|
|
else:
|
|
|
|
return dir
|
|
|
|
|
2011-03-12 14:46:31 +03:00
|
|
|
def tabindent(text):
|
2013-07-19 21:58:30 +04:00
|
|
|
""":tabindent: Any text. Returns the text, with every non-empty line
|
|
|
|
except the first starting with a tab character.
|
2011-03-12 14:46:31 +03:00
|
|
|
"""
|
2011-03-12 14:46:31 +03:00
|
|
|
return indent(text, '\t')
|
|
|
|
|
|
|
|
def urlescape(text):
|
2011-03-12 14:46:31 +03:00
|
|
|
""":urlescape: Any text. Escapes all "special" characters. For example,
|
|
|
|
"foo bar" becomes "foo%20bar".
|
|
|
|
"""
|
2011-03-12 14:46:31 +03:00
|
|
|
return urllib.quote(text)
|
|
|
|
|
|
|
|
def userfilter(text):
|
2012-03-28 18:06:20 +04:00
|
|
|
""":user: Any text. Returns a short representation of a user name or email
|
|
|
|
address."""
|
2011-03-12 14:46:31 +03:00
|
|
|
return util.shortuser(text)
|
|
|
|
|
2012-03-28 18:06:20 +04:00
|
|
|
def emailuser(text):
|
|
|
|
""":emailuser: Any text. Returns the user portion of an email address."""
|
|
|
|
return util.emailuser(text)
|
|
|
|
|
2011-03-12 14:46:31 +03:00
|
|
|
def xmlescape(text):
|
|
|
|
text = (text
|
|
|
|
.replace('&', '&')
|
|
|
|
.replace('<', '<')
|
|
|
|
.replace('>', '>')
|
|
|
|
.replace('"', '"')
|
|
|
|
.replace("'", ''')) # ' invalid in HTML
|
|
|
|
return re.sub('[\x00-\x08\x0B\x0C\x0E-\x1F]', ' ', text)
|
2009-04-20 13:34:15 +04:00
|
|
|
|
2008-01-31 23:44:19 +03:00
|
|
|
filters = {
|
2011-03-12 14:46:31 +03:00
|
|
|
"addbreaks": addbreaks,
|
2008-01-31 23:44:19 +03:00
|
|
|
"age": age,
|
2011-03-12 14:46:31 +03:00
|
|
|
"basename": basename,
|
|
|
|
"date": datefilter,
|
2008-01-31 23:44:19 +03:00
|
|
|
"domain": domain,
|
2011-03-12 14:46:31 +03:00
|
|
|
"email": email,
|
|
|
|
"escape": escape,
|
|
|
|
"fill68": fill68,
|
|
|
|
"fill76": fill76,
|
2008-01-31 23:44:19 +03:00
|
|
|
"firstline": firstline,
|
2011-03-12 14:46:31 +03:00
|
|
|
"hex": hexfilter,
|
|
|
|
"hgdate": hgdate,
|
|
|
|
"isodate": isodate,
|
|
|
|
"isodatesec": isodatesec,
|
2009-04-06 12:51:45 +04:00
|
|
|
"json": json,
|
|
|
|
"jsonescape": jsonescape,
|
2011-03-12 14:46:31 +03:00
|
|
|
"localdate": localdate,
|
2009-04-20 13:34:15 +04:00
|
|
|
"nonempty": nonempty,
|
2008-01-31 23:44:19 +03:00
|
|
|
"obfuscate": obfuscate,
|
|
|
|
"permissions": permissions,
|
|
|
|
"person": person,
|
2011-03-12 14:46:31 +03:00
|
|
|
"rfc3339date": rfc3339date,
|
|
|
|
"rfc822date": rfc822date,
|
|
|
|
"short": short,
|
2011-09-22 03:36:01 +04:00
|
|
|
"shortbisect": shortbisect,
|
2011-03-12 14:46:31 +03:00
|
|
|
"shortdate": shortdate,
|
2014-06-13 04:45:41 +04:00
|
|
|
"splitlines": splitlines,
|
2011-03-12 14:46:31 +03:00
|
|
|
"stringescape": stringescape,
|
2009-05-12 14:04:05 +04:00
|
|
|
"stringify": stringify,
|
2011-03-12 14:46:31 +03:00
|
|
|
"strip": strip,
|
2011-03-12 14:46:31 +03:00
|
|
|
"stripdir": stripdir,
|
2011-03-12 14:46:31 +03:00
|
|
|
"tabindent": tabindent,
|
|
|
|
"urlescape": urlescape,
|
|
|
|
"user": userfilter,
|
2012-03-28 18:06:20 +04:00
|
|
|
"emailuser": emailuser,
|
2008-01-29 06:19:12 +03:00
|
|
|
"xmlescape": xmlescape,
|
2008-06-18 09:06:41 +04:00
|
|
|
}
|
2011-03-12 14:46:31 +03:00
|
|
|
|
2013-02-08 21:05:32 +04:00
|
|
|
def websub(text, websubtable):
|
|
|
|
""":websub: Any text. Only applies to hgweb. Applies the regular
|
|
|
|
expression replacements defined in the websub section.
|
|
|
|
"""
|
|
|
|
if websubtable:
|
|
|
|
for regexp, format in websubtable:
|
|
|
|
text = regexp.sub(format, text)
|
|
|
|
return text
|
|
|
|
|
2011-03-12 14:46:31 +03:00
|
|
|
# tell hggettext to extract docstrings from these functions:
|
|
|
|
i18nfunctions = filters.values()
|