mercurial/templatefilters.py
author Gregory Szorc <gregory.szorc@gmail.com>
Sat, 08 Aug 2015 00:16:02 -0700
changeset 26056 5f2a4fc3c4fa
parent 25983 1245049da5f3
child 26106 c568c4db036f
permissions -rw-r--r--
ui: move URL and path detection into path API ui.expandpath() has code for recognizing URLs or local filesystem paths. Our goal is to use ``path`` class instances everywhere a path is represented. Changing ui.expandpath() to return path instances is a lot of work. Our goal is to slowly marginalize it by moving logic into the paths API and to convert callers to the paths API. Many callers of ui.expandpath() pass in a value that could be a local filesystem path or URI. We move the detection of these strings from ui.expandpath() to paths.getpath() and path.__init__(). To do this properly in a way that is compatible with future callers, we need to parse the "#branch" syntax out of locations. This is a bit complicated, but it is necessary. The code for URL parsing is essentially a copy of hg.parseurl(). Once all consumers are speaking the paths API, it is likely that this function won't be called any more and it can be deleted.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
5976
9f1e6ab76069 templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
     1
# template-filters.py - common template expansion filters
9f1e6ab76069 templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
     2
#
9f1e6ab76069 templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
     3
# Copyright 2005-2008 Matt Mackall <mpm@selenic.com>
9f1e6ab76069 templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
     4
#
8225
46293a0c7e9f updated license to be explicit about GPL version 2
Martin Geisler <mg@lazybytes.net>
parents: 8158
diff changeset
     5
# This software may be used and distributed according to the terms of the
10263
25e572394f5c Update license to GPLv2+
Matt Mackall <mpm@selenic.com>
parents: 9722
diff changeset
     6
# GNU General Public License version 2 or any later version.
5976
9f1e6ab76069 templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
     7
25983
1245049da5f3 templatefilters: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25778
diff changeset
     8
from __future__ import absolute_import
1245049da5f3 templatefilters: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25778
diff changeset
     9
1245049da5f3 templatefilters: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25778
diff changeset
    10
import cgi
1245049da5f3 templatefilters: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25778
diff changeset
    11
import os
1245049da5f3 templatefilters: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25778
diff changeset
    12
import re
1245049da5f3 templatefilters: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25778
diff changeset
    13
import time
1245049da5f3 templatefilters: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25778
diff changeset
    14
import urllib
1245049da5f3 templatefilters: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25778
diff changeset
    15
1245049da5f3 templatefilters: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25778
diff changeset
    16
from . import (
1245049da5f3 templatefilters: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25778
diff changeset
    17
    encoding,
1245049da5f3 templatefilters: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25778
diff changeset
    18
    hbisect,
1245049da5f3 templatefilters: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25778
diff changeset
    19
    node,
1245049da5f3 templatefilters: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25778
diff changeset
    20
    templatekw,
1245049da5f3 templatefilters: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25778
diff changeset
    21
    util,
1245049da5f3 templatefilters: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25778
diff changeset
    22
)
5976
9f1e6ab76069 templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    23
13588
b8b881f3f3a7 templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents: 13587
diff changeset
    24
def addbreaks(text):
13591
264f292a0c6f templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents: 13590
diff changeset
    25
    """:addbreaks: Any text. Add an XHTML "<br />" tag before the end of
264f292a0c6f templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents: 13590
diff changeset
    26
    every line except the last.
264f292a0c6f templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents: 13590
diff changeset
    27
    """
13588
b8b881f3f3a7 templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents: 13587
diff changeset
    28
    return text.replace('\n', '<br/>\n')
8360
acc202b71619 templater: provide the standard template filters by default
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 8234
diff changeset
    29
19736
f08e542ce918 templatefilters: add short format for age formatting
David Soria Parra <dsp@experimentalworks.net>
parents: 19467
diff changeset
    30
agescales = [("year", 3600 * 24 * 365, 'Y'),
f08e542ce918 templatefilters: add short format for age formatting
David Soria Parra <dsp@experimentalworks.net>
parents: 19467
diff changeset
    31
             ("month", 3600 * 24 * 30, 'M'),
f08e542ce918 templatefilters: add short format for age formatting
David Soria Parra <dsp@experimentalworks.net>
parents: 19467
diff changeset
    32
             ("week", 3600 * 24 * 7, 'W'),
f08e542ce918 templatefilters: add short format for age formatting
David Soria Parra <dsp@experimentalworks.net>
parents: 19467
diff changeset
    33
             ("day", 3600 * 24, 'd'),
f08e542ce918 templatefilters: add short format for age formatting
David Soria Parra <dsp@experimentalworks.net>
parents: 19467
diff changeset
    34
             ("hour", 3600, 'h'),
f08e542ce918 templatefilters: add short format for age formatting
David Soria Parra <dsp@experimentalworks.net>
parents: 19467
diff changeset
    35
             ("minute", 60, 'm'),
f08e542ce918 templatefilters: add short format for age formatting
David Soria Parra <dsp@experimentalworks.net>
parents: 19467
diff changeset
    36
             ("second", 1, 's')]
5976
9f1e6ab76069 templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    37
19736
f08e542ce918 templatefilters: add short format for age formatting
David Soria Parra <dsp@experimentalworks.net>
parents: 19467
diff changeset
    38
def age(date, abbrev=False):
13591
264f292a0c6f templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents: 13590
diff changeset
    39
    """:age: Date. Returns a human-readable date/time difference between the
264f292a0c6f templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents: 13590
diff changeset
    40
    given date/time and the current date/time.
264f292a0c6f templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents: 13590
diff changeset
    41
    """
5976
9f1e6ab76069 templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    42
9f1e6ab76069 templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    43
    def plural(t, c):
9f1e6ab76069 templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    44
        if c == 1:
9f1e6ab76069 templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    45
            return t
9f1e6ab76069 templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    46
        return t + "s"
19736
f08e542ce918 templatefilters: add short format for age formatting
David Soria Parra <dsp@experimentalworks.net>
parents: 19467
diff changeset
    47
    def fmt(t, c, a):
f08e542ce918 templatefilters: add short format for age formatting
David Soria Parra <dsp@experimentalworks.net>
parents: 19467
diff changeset
    48
        if abbrev:
f08e542ce918 templatefilters: add short format for age formatting
David Soria Parra <dsp@experimentalworks.net>
parents: 19467
diff changeset
    49
            return "%d%s" % (c, a)
5976
9f1e6ab76069 templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    50
        return "%d %s" % (c, plural(t, c))
9f1e6ab76069 templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    51
9f1e6ab76069 templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    52
    now = time.time()
9f1e6ab76069 templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    53
    then = date[0]
13666
c49cddce0a81 templates: provide granularity for future values for age filter
timeless <timeless@gmail.com>
parents: 13593
diff changeset
    54
    future = False
7682
9c8bbae02e9c templater: fix age filter to state the obvious on future timestamps
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 6691
diff changeset
    55
    if then > now:
13666
c49cddce0a81 templates: provide granularity for future values for age filter
timeless <timeless@gmail.com>
parents: 13593
diff changeset
    56
        future = True
c49cddce0a81 templates: provide granularity for future values for age filter
timeless <timeless@gmail.com>
parents: 13593
diff changeset
    57
        delta = max(1, int(then - now))
c49cddce0a81 templates: provide granularity for future values for age filter
timeless <timeless@gmail.com>
parents: 13593
diff changeset
    58
        if delta > agescales[0][1] * 30:
c49cddce0a81 templates: provide granularity for future values for age filter
timeless <timeless@gmail.com>
parents: 13593
diff changeset
    59
            return 'in the distant future'
c49cddce0a81 templates: provide granularity for future values for age filter
timeless <timeless@gmail.com>
parents: 13593
diff changeset
    60
    else:
c49cddce0a81 templates: provide granularity for future values for age filter
timeless <timeless@gmail.com>
parents: 13593
diff changeset
    61
        delta = max(1, int(now - then))
c49cddce0a81 templates: provide granularity for future values for age filter
timeless <timeless@gmail.com>
parents: 13593
diff changeset
    62
        if delta > agescales[0][1] * 2:
c49cddce0a81 templates: provide granularity for future values for age filter
timeless <timeless@gmail.com>
parents: 13593
diff changeset
    63
            return util.shortdate(date)
9722
4d9dea174b84 templater: readable dates older than 24 months revert to ISO8601 (issue1006)
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 9721
diff changeset
    64
19736
f08e542ce918 templatefilters: add short format for age formatting
David Soria Parra <dsp@experimentalworks.net>
parents: 19467
diff changeset
    65
    for t, s, a in agescales:
9029
0001e49f1c11 compat: use // for integer division
Alejandro Santos <alejolp@alejolp.com>
parents: 8697
diff changeset
    66
        n = delta // s
5976
9f1e6ab76069 templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    67
        if n >= 2 or s == 1:
13666
c49cddce0a81 templates: provide granularity for future values for age filter
timeless <timeless@gmail.com>
parents: 13593
diff changeset
    68
            if future:
19736
f08e542ce918 templatefilters: add short format for age formatting
David Soria Parra <dsp@experimentalworks.net>
parents: 19467
diff changeset
    69
                return '%s from now' % fmt(t, n, a)
f08e542ce918 templatefilters: add short format for age formatting
David Soria Parra <dsp@experimentalworks.net>
parents: 19467
diff changeset
    70
            return '%s ago' % fmt(t, n, a)
5976
9f1e6ab76069 templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    71
13590
1a752dcfe062 templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents: 13589
diff changeset
    72
def basename(path):
13591
264f292a0c6f templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents: 13590
diff changeset
    73
    """:basename: Any text. Treats the text as a path, and returns the last
264f292a0c6f templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents: 13590
diff changeset
    74
    component of the path after splitting by the path separator
264f292a0c6f templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents: 13590
diff changeset
    75
    (ignoring trailing separators). For example, "foo/bar/baz" becomes
264f292a0c6f templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents: 13590
diff changeset
    76
    "baz" and "foo/bar//" becomes "bar".
264f292a0c6f templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents: 13590
diff changeset
    77
    """
13590
1a752dcfe062 templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents: 13589
diff changeset
    78
    return os.path.basename(path)
1a752dcfe062 templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents: 13589
diff changeset
    79
22668
13e3f07d74a3 templater: add count template filter, plus tests
Anton Shestakov <engored@ya.ru>
parents: 21873
diff changeset
    80
def count(i):
13e3f07d74a3 templater: add count template filter, plus tests
Anton Shestakov <engored@ya.ru>
parents: 21873
diff changeset
    81
    """:count: List or text. Returns the length as an integer."""
13e3f07d74a3 templater: add count template filter, plus tests
Anton Shestakov <engored@ya.ru>
parents: 21873
diff changeset
    82
    return len(i)
13e3f07d74a3 templater: add count template filter, plus tests
Anton Shestakov <engored@ya.ru>
parents: 21873
diff changeset
    83
13590
1a752dcfe062 templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents: 13589
diff changeset
    84
def datefilter(text):
13591
264f292a0c6f templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents: 13590
diff changeset
    85
    """:date: Date. Returns a date in a Unix date format, including the
264f292a0c6f templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents: 13590
diff changeset
    86
    timezone: "Mon Sep 04 15:13:13 2006 0700".
264f292a0c6f templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents: 13590
diff changeset
    87
    """
13590
1a752dcfe062 templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents: 13589
diff changeset
    88
    return util.datestr(text)
1a752dcfe062 templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents: 13589
diff changeset
    89
13588
b8b881f3f3a7 templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents: 13587
diff changeset
    90
def domain(author):
13591
264f292a0c6f templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents: 13590
diff changeset
    91
    """:domain: Any text. Finds the first string that looks like an email
264f292a0c6f templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents: 13590
diff changeset
    92
    address, and extracts just the domain component. Example: ``User
264f292a0c6f templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents: 13590
diff changeset
    93
    <user@example.com>`` becomes ``example.com``.
264f292a0c6f templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents: 13590
diff changeset
    94
    """
13588
b8b881f3f3a7 templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents: 13587
diff changeset
    95
    f = author.find('@')
b8b881f3f3a7 templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents: 13587
diff changeset
    96
    if f == -1:
b8b881f3f3a7 templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents: 13587
diff changeset
    97
        return ''
b8b881f3f3a7 templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents: 13587
diff changeset
    98
    author = author[f + 1:]
b8b881f3f3a7 templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents: 13587
diff changeset
    99
    f = author.find('>')
b8b881f3f3a7 templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents: 13587
diff changeset
   100
    if f >= 0:
b8b881f3f3a7 templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents: 13587
diff changeset
   101
        author = author[:f]
b8b881f3f3a7 templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents: 13587
diff changeset
   102
    return author
b8b881f3f3a7 templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents: 13587
diff changeset
   103
13590
1a752dcfe062 templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents: 13589
diff changeset
   104
def email(text):
13591
264f292a0c6f templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents: 13590
diff changeset
   105
    """:email: Any text. Extracts the first string that looks like an email
264f292a0c6f templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents: 13590
diff changeset
   106
    address. Example: ``User <user@example.com>`` becomes
264f292a0c6f templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents: 13590
diff changeset
   107
    ``user@example.com``.
264f292a0c6f templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents: 13590
diff changeset
   108
    """
13590
1a752dcfe062 templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents: 13589
diff changeset
   109
    return util.email(text)
1a752dcfe062 templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents: 13589
diff changeset
   110
1a752dcfe062 templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents: 13589
diff changeset
   111
def escape(text):
13591
264f292a0c6f templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents: 13590
diff changeset
   112
    """:escape: Any text. Replaces the special XML/XHTML characters "&", "<"
17772
823a7d79ef82 hgweb: make the escape filter remove null characters (issue2567)
Siddharth Agarwal <sid0@fb.com>
parents: 17755
diff changeset
   113
    and ">" with XML entities, and filters out NUL characters.
13591
264f292a0c6f templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents: 13590
diff changeset
   114
    """
17772
823a7d79ef82 hgweb: make the escape filter remove null characters (issue2567)
Siddharth Agarwal <sid0@fb.com>
parents: 17755
diff changeset
   115
    return cgi.escape(text.replace('\0', ''), True)
13590
1a752dcfe062 templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents: 13589
diff changeset
   116
5976
9f1e6ab76069 templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   117
para_re = None
9f1e6ab76069 templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   118
space_re = None
9f1e6ab76069 templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   119
19872
681f7b9213a4 check-code: check for spaces around = for named parameters
Mads Kiilerich <madski@unity3d.com>
parents: 19736
diff changeset
   120
def fill(text, width, initindent='', hangindent=''):
19228
889807c79384 templater: add indentation arguments to the fill function
Sean Farley <sean.michael.farley@gmail.com>
parents: 19227
diff changeset
   121
    '''fill many paragraphs with optional indentation.'''
5976
9f1e6ab76069 templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   122
    global para_re, space_re
9f1e6ab76069 templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   123
    if para_re is None:
9f1e6ab76069 templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   124
        para_re = re.compile('(\n\n|\n\\s*[-*]\\s*)', re.M)
9f1e6ab76069 templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   125
        space_re = re.compile(r'  +')
9f1e6ab76069 templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   126
9f1e6ab76069 templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   127
    def findparas():
9f1e6ab76069 templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   128
        start = 0
9f1e6ab76069 templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   129
        while True:
9f1e6ab76069 templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   130
            m = para_re.search(text, start)
9f1e6ab76069 templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   131
            if not m:
11297
d320e70442a5 replace Python standard textwrap by MBCS sensitive one for i18n text
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 10787
diff changeset
   132
                uctext = unicode(text[start:], encoding.encoding)
d320e70442a5 replace Python standard textwrap by MBCS sensitive one for i18n text
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 10787
diff changeset
   133
                w = len(uctext)
d320e70442a5 replace Python standard textwrap by MBCS sensitive one for i18n text
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 10787
diff changeset
   134
                while 0 < w and uctext[w - 1].isspace():
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   135
                    w -= 1
11297
d320e70442a5 replace Python standard textwrap by MBCS sensitive one for i18n text
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 10787
diff changeset
   136
                yield (uctext[:w].encode(encoding.encoding),
d320e70442a5 replace Python standard textwrap by MBCS sensitive one for i18n text
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 10787
diff changeset
   137
                       uctext[w:].encode(encoding.encoding))
5976
9f1e6ab76069 templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   138
                break
9f1e6ab76069 templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   139
            yield text[start:m.start(0)], m.group(1)
9f1e6ab76069 templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   140
            start = m.end(1)
9f1e6ab76069 templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   141
19228
889807c79384 templater: add indentation arguments to the fill function
Sean Farley <sean.michael.farley@gmail.com>
parents: 19227
diff changeset
   142
    return "".join([util.wrap(space_re.sub(' ', util.wrap(para, width)),
889807c79384 templater: add indentation arguments to the fill function
Sean Farley <sean.michael.farley@gmail.com>
parents: 19227
diff changeset
   143
                              width, initindent, hangindent) + rest
5976
9f1e6ab76069 templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   144
                    for para, rest in findparas()])
9f1e6ab76069 templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   145
13590
1a752dcfe062 templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents: 13589
diff changeset
   146
def fill68(text):
13591
264f292a0c6f templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents: 13590
diff changeset
   147
    """:fill68: Any text. Wraps the text to fit in 68 columns."""
13590
1a752dcfe062 templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents: 13589
diff changeset
   148
    return fill(text, 68)
1a752dcfe062 templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents: 13589
diff changeset
   149
1a752dcfe062 templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents: 13589
diff changeset
   150
def fill76(text):
13591
264f292a0c6f templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents: 13590
diff changeset
   151
    """:fill76: Any text. Wraps the text to fit in 76 columns."""
13590
1a752dcfe062 templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents: 13589
diff changeset
   152
    return fill(text, 76)
1a752dcfe062 templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents: 13589
diff changeset
   153
5976
9f1e6ab76069 templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   154
def firstline(text):
13591
264f292a0c6f templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents: 13590
diff changeset
   155
    """:firstline: Any text. Returns the first line of text."""
5976
9f1e6ab76069 templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   156
    try:
9136
31177742f54a for calls expecting bool args, pass bool instead of int
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 9029
diff changeset
   157
        return text.splitlines(True)[0].rstrip('\r\n')
5976
9f1e6ab76069 templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   158
    except IndexError:
9f1e6ab76069 templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   159
        return ''
9f1e6ab76069 templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   160
13590
1a752dcfe062 templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents: 13589
diff changeset
   161
def hexfilter(text):
13591
264f292a0c6f templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents: 13590
diff changeset
   162
    """:hex: Any text. Convert a binary Mercurial node identifier into
264f292a0c6f templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents: 13590
diff changeset
   163
    its long hexadecimal representation.
264f292a0c6f templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents: 13590
diff changeset
   164
    """
13590
1a752dcfe062 templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents: 13589
diff changeset
   165
    return node.hex(text)
1a752dcfe062 templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents: 13589
diff changeset
   166
1a752dcfe062 templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents: 13589
diff changeset
   167
def hgdate(text):
13591
264f292a0c6f templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents: 13590
diff changeset
   168
    """:hgdate: Date. Returns the date as a pair of numbers: "1157407993
264f292a0c6f templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents: 13590
diff changeset
   169
    25200" (Unix timestamp, timezone offset).
264f292a0c6f templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents: 13590
diff changeset
   170
    """
13590
1a752dcfe062 templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents: 13589
diff changeset
   171
    return "%d %d" % text
1a752dcfe062 templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents: 13589
diff changeset
   172
1a752dcfe062 templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents: 13589
diff changeset
   173
def isodate(text):
13591
264f292a0c6f templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents: 13590
diff changeset
   174
    """:isodate: Date. Returns the date in ISO 8601 format: "2009-08-18 13:00
264f292a0c6f templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents: 13590
diff changeset
   175
    +0200".
264f292a0c6f templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents: 13590
diff changeset
   176
    """
13590
1a752dcfe062 templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents: 13589
diff changeset
   177
    return util.datestr(text, '%Y-%m-%d %H:%M %1%2')
1a752dcfe062 templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents: 13589
diff changeset
   178
1a752dcfe062 templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents: 13589
diff changeset
   179
def isodatesec(text):
13591
264f292a0c6f templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents: 13590
diff changeset
   180
    """:isodatesec: Date. Returns the date in ISO 8601 format, including
264f292a0c6f templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents: 13590
diff changeset
   181
    seconds: "2009-08-18 13:00:13 +0200". See also the rfc3339date
264f292a0c6f templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents: 13590
diff changeset
   182
    filter.
264f292a0c6f templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents: 13590
diff changeset
   183
    """
13590
1a752dcfe062 templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents: 13589
diff changeset
   184
    return util.datestr(text, '%Y-%m-%d %H:%M:%S %1%2')
1a752dcfe062 templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents: 13589
diff changeset
   185
5976
9f1e6ab76069 templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   186
def indent(text, prefix):
9f1e6ab76069 templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   187
    '''indent each non-empty line of text after first with prefix.'''
9f1e6ab76069 templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   188
    lines = text.splitlines()
9f1e6ab76069 templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   189
    num_lines = len(lines)
9387
20ed9909dbd9 templatefilters: indent: do not compute text.endswith('\n') in each iteration
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 9136
diff changeset
   190
    endswithnewline = text[-1:] == '\n'
5976
9f1e6ab76069 templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   191
    def indenter():
9f1e6ab76069 templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   192
        for i in xrange(num_lines):
9f1e6ab76069 templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   193
            l = lines[i]
9f1e6ab76069 templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   194
            if i and l.strip():
9f1e6ab76069 templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   195
                yield prefix
9f1e6ab76069 templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   196
            yield l
9387
20ed9909dbd9 templatefilters: indent: do not compute text.endswith('\n') in each iteration
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 9136
diff changeset
   197
            if i < num_lines - 1 or endswithnewline:
5976
9f1e6ab76069 templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   198
                yield '\n'
9f1e6ab76069 templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   199
    return "".join(indenter())
9f1e6ab76069 templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   200
6691
0dba955c2636 add graph page to hgweb
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 6319
diff changeset
   201
def json(obj):
0dba955c2636 add graph page to hgweb
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 6319
diff changeset
   202
    if obj is None or obj is False or obj is True:
0dba955c2636 add graph page to hgweb
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 6319
diff changeset
   203
        return {None: 'null', False: 'false', True: 'true'}[obj]
0dba955c2636 add graph page to hgweb
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 6319
diff changeset
   204
    elif isinstance(obj, int) or isinstance(obj, float):
0dba955c2636 add graph page to hgweb
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 6319
diff changeset
   205
        return str(obj)
0dba955c2636 add graph page to hgweb
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 6319
diff changeset
   206
    elif isinstance(obj, str):
11765
aff419e260f9 templatefilters: make json filter handle multibyte characters correctly
Yuya Nishihara <yuya@tcha.org>
parents: 11297
diff changeset
   207
        u = unicode(obj, encoding.encoding, 'replace')
11890
9dac951d0185 templatefilters: use \uxxxx style escape for JSON string
Yuya Nishihara <yuya@tcha.org>
parents: 11765
diff changeset
   208
        return '"%s"' % jsonescape(u)
6691
0dba955c2636 add graph page to hgweb
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 6319
diff changeset
   209
    elif isinstance(obj, unicode):
11890
9dac951d0185 templatefilters: use \uxxxx style escape for JSON string
Yuya Nishihara <yuya@tcha.org>
parents: 11765
diff changeset
   210
        return '"%s"' % jsonescape(obj)
14967
376091a4ad23 templatefilters: use safehasattr instead of hasattr
Augie Fackler <durin42@gmail.com>
parents: 14944
diff changeset
   211
    elif util.safehasattr(obj, 'keys'):
6691
0dba955c2636 add graph page to hgweb
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 6319
diff changeset
   212
        out = []
23708
a9f826c3eaf9 templatefilters.json: stabilize output
Gregory Szorc <gregory.szorc@gmail.com>
parents: 23707
diff changeset
   213
        for k, v in sorted(obj.iteritems()):
6691
0dba955c2636 add graph page to hgweb
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 6319
diff changeset
   214
            s = '%s: %s' % (json(k), json(v))
0dba955c2636 add graph page to hgweb
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 6319
diff changeset
   215
            out.append(s)
0dba955c2636 add graph page to hgweb
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 6319
diff changeset
   216
        return '{' + ', '.join(out) + '}'
14944
e2c413bde8a5 globally: use safehasattr(x, '__iter__') instead of hasattr(x, '__iter__')
Augie Fackler <durin42@gmail.com>
parents: 14318
diff changeset
   217
    elif util.safehasattr(obj, '__iter__'):
6691
0dba955c2636 add graph page to hgweb
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 6319
diff changeset
   218
        out = []
0dba955c2636 add graph page to hgweb
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 6319
diff changeset
   219
        for i in obj:
0dba955c2636 add graph page to hgweb
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 6319
diff changeset
   220
            out.append(json(i))
0dba955c2636 add graph page to hgweb
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 6319
diff changeset
   221
        return '[' + ', '.join(out) + ']'
23707
ae5447de4c11 templatefilters.json: call functions
Gregory Szorc <gregory.szorc@gmail.com>
parents: 22668
diff changeset
   222
    elif util.safehasattr(obj, '__call__'):
ae5447de4c11 templatefilters.json: call functions
Gregory Szorc <gregory.szorc@gmail.com>
parents: 22668
diff changeset
   223
        return json(obj())
6691
0dba955c2636 add graph page to hgweb
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 6319
diff changeset
   224
    else:
0dba955c2636 add graph page to hgweb
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 6319
diff changeset
   225
        raise TypeError('cannot encode type %s' % obj.__class__.__name__)
0dba955c2636 add graph page to hgweb
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 6319
diff changeset
   226
13589
b0a4b05c25e2 templatefilters: prefix helper functions
Patrick Mezard <pmezard@gmail.com>
parents: 13588
diff changeset
   227
def _uescape(c):
13588
b8b881f3f3a7 templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents: 13587
diff changeset
   228
    if ord(c) < 0x80:
b8b881f3f3a7 templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents: 13587
diff changeset
   229
        return c
b8b881f3f3a7 templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents: 13587
diff changeset
   230
    else:
b8b881f3f3a7 templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents: 13587
diff changeset
   231
        return '\\u%04x' % ord(c)
b8b881f3f3a7 templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents: 13587
diff changeset
   232
b8b881f3f3a7 templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents: 13587
diff changeset
   233
_escapes = [
b8b881f3f3a7 templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents: 13587
diff changeset
   234
    ('\\', '\\\\'), ('"', '\\"'), ('\t', '\\t'), ('\n', '\\n'),
b8b881f3f3a7 templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents: 13587
diff changeset
   235
    ('\r', '\\r'), ('\f', '\\f'), ('\b', '\\b'),
21867
829f2dd99f5c templates: escape NUL bytes in jsonescape (issue4303)
Matt Mackall <mpm@selenic.com>
parents: 19886
diff changeset
   236
    ('<', '\\u003c'), ('>', '\\u003e'), ('\0', '\\u0000')
13588
b8b881f3f3a7 templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents: 13587
diff changeset
   237
]
b8b881f3f3a7 templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents: 13587
diff changeset
   238
b8b881f3f3a7 templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents: 13587
diff changeset
   239
def jsonescape(s):
b8b881f3f3a7 templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents: 13587
diff changeset
   240
    for k, v in _escapes:
b8b881f3f3a7 templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents: 13587
diff changeset
   241
        s = s.replace(k, v)
13589
b0a4b05c25e2 templatefilters: prefix helper functions
Patrick Mezard <pmezard@gmail.com>
parents: 13588
diff changeset
   242
    return ''.join(_uescape(c) for c in s)
13588
b8b881f3f3a7 templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents: 13587
diff changeset
   243
13590
1a752dcfe062 templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents: 13589
diff changeset
   244
def localdate(text):
13591
264f292a0c6f templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents: 13590
diff changeset
   245
    """:localdate: Date. Converts a date to local date."""
17755
bededd3f0735 templatefilters: avoid traceback caused by bogus date input (issue3344)
Christian Ebert <blacktrash@gmx.net>
parents: 17639
diff changeset
   246
    return (util.parsedate(text)[0], util.makedate()[1])
13590
1a752dcfe062 templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents: 13589
diff changeset
   247
24566
6abce80e6cbf templatefilters: add "upper" and "lower" for case conversion
Yuya Nishihara <yuya@tcha.org>
parents: 23708
diff changeset
   248
def lower(text):
6abce80e6cbf templatefilters: add "upper" and "lower" for case conversion
Yuya Nishihara <yuya@tcha.org>
parents: 23708
diff changeset
   249
    """:lower: Any text. Converts the text to lowercase."""
6abce80e6cbf templatefilters: add "upper" and "lower" for case conversion
Yuya Nishihara <yuya@tcha.org>
parents: 23708
diff changeset
   250
    return encoding.lower(text)
6abce80e6cbf templatefilters: add "upper" and "lower" for case conversion
Yuya Nishihara <yuya@tcha.org>
parents: 23708
diff changeset
   251
13588
b8b881f3f3a7 templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents: 13587
diff changeset
   252
def nonempty(str):
13591
264f292a0c6f templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents: 13590
diff changeset
   253
    """:nonempty: Any text. Returns '(none)' if the string is empty."""
13588
b8b881f3f3a7 templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents: 13587
diff changeset
   254
    return str or "(none)"
b8b881f3f3a7 templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents: 13587
diff changeset
   255
b8b881f3f3a7 templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents: 13587
diff changeset
   256
def obfuscate(text):
13591
264f292a0c6f templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents: 13590
diff changeset
   257
    """:obfuscate: Any text. Returns the input text rendered as a sequence of
264f292a0c6f templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents: 13590
diff changeset
   258
    XML entities.
264f292a0c6f templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents: 13590
diff changeset
   259
    """
13588
b8b881f3f3a7 templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents: 13587
diff changeset
   260
    text = unicode(text, encoding.encoding, 'replace')
b8b881f3f3a7 templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents: 13587
diff changeset
   261
    return ''.join(['&#%d;' % ord(c) for c in text])
b8b881f3f3a7 templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents: 13587
diff changeset
   262
b8b881f3f3a7 templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents: 13587
diff changeset
   263
def permissions(flags):
b8b881f3f3a7 templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents: 13587
diff changeset
   264
    if "l" in flags:
b8b881f3f3a7 templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents: 13587
diff changeset
   265
        return "lrwxrwxrwx"
b8b881f3f3a7 templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents: 13587
diff changeset
   266
    if "x" in flags:
b8b881f3f3a7 templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents: 13587
diff changeset
   267
        return "-rwxr-xr-x"
b8b881f3f3a7 templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents: 13587
diff changeset
   268
    return "-rw-r--r--"
b8b881f3f3a7 templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents: 13587
diff changeset
   269
b8b881f3f3a7 templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents: 13587
diff changeset
   270
def person(author):
16235
eb39bbda167b templates/filters: strip quotes from {author|person}
"Yann E. MORIN" <yann.morin.1998@free.fr>
parents: 15155
diff changeset
   271
    """:person: Any text. Returns the name before an email address,
eb39bbda167b templates/filters: strip quotes from {author|person}
"Yann E. MORIN" <yann.morin.1998@free.fr>
parents: 15155
diff changeset
   272
    interpreting it as per RFC 5322.
16251
db68ee3289b6 templates/filters: add doctest to the 'person' filter
"Yann E. MORIN" <yann.morin.1998@free.fr>
parents: 16235
diff changeset
   273
db68ee3289b6 templates/filters: add doctest to the 'person' filter
"Yann E. MORIN" <yann.morin.1998@free.fr>
parents: 16235
diff changeset
   274
    >>> person('foo@bar')
db68ee3289b6 templates/filters: add doctest to the 'person' filter
"Yann E. MORIN" <yann.morin.1998@free.fr>
parents: 16235
diff changeset
   275
    'foo'
db68ee3289b6 templates/filters: add doctest to the 'person' filter
"Yann E. MORIN" <yann.morin.1998@free.fr>
parents: 16235
diff changeset
   276
    >>> person('Foo Bar <foo@bar>')
db68ee3289b6 templates/filters: add doctest to the 'person' filter
"Yann E. MORIN" <yann.morin.1998@free.fr>
parents: 16235
diff changeset
   277
    'Foo Bar'
db68ee3289b6 templates/filters: add doctest to the 'person' filter
"Yann E. MORIN" <yann.morin.1998@free.fr>
parents: 16235
diff changeset
   278
    >>> person('"Foo Bar" <foo@bar>')
db68ee3289b6 templates/filters: add doctest to the 'person' filter
"Yann E. MORIN" <yann.morin.1998@free.fr>
parents: 16235
diff changeset
   279
    'Foo Bar'
db68ee3289b6 templates/filters: add doctest to the 'person' filter
"Yann E. MORIN" <yann.morin.1998@free.fr>
parents: 16235
diff changeset
   280
    >>> person('"Foo \"buz\" Bar" <foo@bar>')
db68ee3289b6 templates/filters: add doctest to the 'person' filter
"Yann E. MORIN" <yann.morin.1998@free.fr>
parents: 16235
diff changeset
   281
    'Foo "buz" Bar'
db68ee3289b6 templates/filters: add doctest to the 'person' filter
"Yann E. MORIN" <yann.morin.1998@free.fr>
parents: 16235
diff changeset
   282
    >>> # The following are invalid, but do exist in real-life
db68ee3289b6 templates/filters: add doctest to the 'person' filter
"Yann E. MORIN" <yann.morin.1998@free.fr>
parents: 16235
diff changeset
   283
    ...
db68ee3289b6 templates/filters: add doctest to the 'person' filter
"Yann E. MORIN" <yann.morin.1998@free.fr>
parents: 16235
diff changeset
   284
    >>> person('Foo "buz" Bar <foo@bar>')
db68ee3289b6 templates/filters: add doctest to the 'person' filter
"Yann E. MORIN" <yann.morin.1998@free.fr>
parents: 16235
diff changeset
   285
    'Foo "buz" Bar'
db68ee3289b6 templates/filters: add doctest to the 'person' filter
"Yann E. MORIN" <yann.morin.1998@free.fr>
parents: 16235
diff changeset
   286
    >>> person('"Foo Bar <foo@bar>')
db68ee3289b6 templates/filters: add doctest to the 'person' filter
"Yann E. MORIN" <yann.morin.1998@free.fr>
parents: 16235
diff changeset
   287
    'Foo Bar'
16235
eb39bbda167b templates/filters: strip quotes from {author|person}
"Yann E. MORIN" <yann.morin.1998@free.fr>
parents: 15155
diff changeset
   288
    """
16686
67964cda8701 cleanup: "not x in y" -> "x not in y"
Brodie Rao <brodie@sf.io>
parents: 16360
diff changeset
   289
    if '@' not in author:
13588
b8b881f3f3a7 templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents: 13587
diff changeset
   290
        return author
b8b881f3f3a7 templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents: 13587
diff changeset
   291
    f = author.find('<')
13951
7a6a8a069aac templatefilters: improve person() for john.doe@example.com
Adrian Buehlmann <adrian@cadifra.com>
parents: 13666
diff changeset
   292
    if f != -1:
16235
eb39bbda167b templates/filters: strip quotes from {author|person}
"Yann E. MORIN" <yann.morin.1998@free.fr>
parents: 15155
diff changeset
   293
        return author[:f].strip(' "').replace('\\"', '"')
13951
7a6a8a069aac templatefilters: improve person() for john.doe@example.com
Adrian Buehlmann <adrian@cadifra.com>
parents: 13666
diff changeset
   294
    f = author.find('@')
7a6a8a069aac templatefilters: improve person() for john.doe@example.com
Adrian Buehlmann <adrian@cadifra.com>
parents: 13666
diff changeset
   295
    return author[:f].replace('.', ' ')
13588
b8b881f3f3a7 templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents: 13587
diff changeset
   296
25778
3a33412792f1 templates: introduce revescape filter for escaping symbolic revisions
Anton Shestakov <av6@dwimlabs.net>
parents: 25000
diff changeset
   297
def revescape(text):
3a33412792f1 templates: introduce revescape filter for escaping symbolic revisions
Anton Shestakov <av6@dwimlabs.net>
parents: 25000
diff changeset
   298
    """:revescape: Any text. Escapes all "special" characters, except @.
3a33412792f1 templates: introduce revescape filter for escaping symbolic revisions
Anton Shestakov <av6@dwimlabs.net>
parents: 25000
diff changeset
   299
    Forward slashes are escaped twice to prevent web servers from prematurely
3a33412792f1 templates: introduce revescape filter for escaping symbolic revisions
Anton Shestakov <av6@dwimlabs.net>
parents: 25000
diff changeset
   300
    unescaping them. For example, "@foo bar/baz" becomes "@foo%20bar%252Fbaz".
3a33412792f1 templates: introduce revescape filter for escaping symbolic revisions
Anton Shestakov <av6@dwimlabs.net>
parents: 25000
diff changeset
   301
    """
3a33412792f1 templates: introduce revescape filter for escaping symbolic revisions
Anton Shestakov <av6@dwimlabs.net>
parents: 25000
diff changeset
   302
    return urllib.quote(text, safe='/@').replace('/', '%252F')
3a33412792f1 templates: introduce revescape filter for escaping symbolic revisions
Anton Shestakov <av6@dwimlabs.net>
parents: 25000
diff changeset
   303
13590
1a752dcfe062 templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents: 13589
diff changeset
   304
def rfc3339date(text):
13591
264f292a0c6f templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents: 13590
diff changeset
   305
    """:rfc3339date: Date. Returns a date using the Internet date format
264f292a0c6f templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents: 13590
diff changeset
   306
    specified in RFC 3339: "2009-08-18T13:00:13+02:00".
264f292a0c6f templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents: 13590
diff changeset
   307
    """
13590
1a752dcfe062 templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents: 13589
diff changeset
   308
    return util.datestr(text, "%Y-%m-%dT%H:%M:%S%1:%2")
1a752dcfe062 templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents: 13589
diff changeset
   309
1a752dcfe062 templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents: 13589
diff changeset
   310
def rfc822date(text):
13591
264f292a0c6f templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents: 13590
diff changeset
   311
    """:rfc822date: Date. Returns a date using the same format used in email
264f292a0c6f templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents: 13590
diff changeset
   312
    headers: "Tue, 18 Aug 2009 13:00:13 +0200".
264f292a0c6f templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents: 13590
diff changeset
   313
    """
13590
1a752dcfe062 templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents: 13589
diff changeset
   314
    return util.datestr(text, "%a, %d %b %Y %H:%M:%S %1%2")
1a752dcfe062 templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents: 13589
diff changeset
   315
1a752dcfe062 templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents: 13589
diff changeset
   316
def short(text):
13591
264f292a0c6f templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents: 13590
diff changeset
   317
    """:short: Changeset hash. Returns the short form of a changeset hash,
264f292a0c6f templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents: 13590
diff changeset
   318
    i.e. a 12 hexadecimal digit string.
264f292a0c6f templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents: 13590
diff changeset
   319
    """
13590
1a752dcfe062 templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents: 13589
diff changeset
   320
    return text[:12]
1a752dcfe062 templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents: 13589
diff changeset
   321
15155
f4a8d754cd0a templates: add 'bisect' keyword to return a cset's bisect status
"Yann E. MORIN" <yann.morin.1998@anciens.enib.fr>
parents: 14967
diff changeset
   322
def shortbisect(text):
f4a8d754cd0a templates: add 'bisect' keyword to return a cset's bisect status
"Yann E. MORIN" <yann.morin.1998@anciens.enib.fr>
parents: 14967
diff changeset
   323
    """:shortbisect: Any text. Treats `text` as a bisection status, and
f4a8d754cd0a templates: add 'bisect' keyword to return a cset's bisect status
"Yann E. MORIN" <yann.morin.1998@anciens.enib.fr>
parents: 14967
diff changeset
   324
    returns a single-character representing the status (G: good, B: bad,
f4a8d754cd0a templates: add 'bisect' keyword to return a cset's bisect status
"Yann E. MORIN" <yann.morin.1998@anciens.enib.fr>
parents: 14967
diff changeset
   325
    S: skipped, U: untested, I: ignored). Returns single space if `text`
f4a8d754cd0a templates: add 'bisect' keyword to return a cset's bisect status
"Yann E. MORIN" <yann.morin.1998@anciens.enib.fr>
parents: 14967
diff changeset
   326
    is not a valid bisection status.
f4a8d754cd0a templates: add 'bisect' keyword to return a cset's bisect status
"Yann E. MORIN" <yann.morin.1998@anciens.enib.fr>
parents: 14967
diff changeset
   327
    """
f4a8d754cd0a templates: add 'bisect' keyword to return a cset's bisect status
"Yann E. MORIN" <yann.morin.1998@anciens.enib.fr>
parents: 14967
diff changeset
   328
    return hbisect.shortlabel(text) or ' '
f4a8d754cd0a templates: add 'bisect' keyword to return a cset's bisect status
"Yann E. MORIN" <yann.morin.1998@anciens.enib.fr>
parents: 14967
diff changeset
   329
13590
1a752dcfe062 templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents: 13589
diff changeset
   330
def shortdate(text):
13591
264f292a0c6f templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents: 13590
diff changeset
   331
    """:shortdate: Date. Returns a date like "2006-09-18"."""
13590
1a752dcfe062 templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents: 13589
diff changeset
   332
    return util.shortdate(text)
1a752dcfe062 templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents: 13589
diff changeset
   333
21820
cce404b0c918 templatefilter: add splitlines function
Ryan McElroy <rmcelroy@fb.com>
parents: 19886
diff changeset
   334
def splitlines(text):
cce404b0c918 templatefilter: add splitlines function
Ryan McElroy <rmcelroy@fb.com>
parents: 19886
diff changeset
   335
    """:splitlines: Any text. Split text into a list of lines."""
cce404b0c918 templatefilter: add splitlines function
Ryan McElroy <rmcelroy@fb.com>
parents: 19886
diff changeset
   336
    return templatekw.showlist('line', text.splitlines(), 'lines')
cce404b0c918 templatefilter: add splitlines function
Ryan McElroy <rmcelroy@fb.com>
parents: 19886
diff changeset
   337
13590
1a752dcfe062 templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents: 13589
diff changeset
   338
def stringescape(text):
1a752dcfe062 templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents: 13589
diff changeset
   339
    return text.encode('string_escape')
1a752dcfe062 templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents: 13589
diff changeset
   340
13588
b8b881f3f3a7 templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents: 13587
diff changeset
   341
def stringify(thing):
13591
264f292a0c6f templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents: 13590
diff changeset
   342
    """:stringify: Any type. Turns the value into text by converting values into
264f292a0c6f templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents: 13590
diff changeset
   343
    text and concatenating them.
264f292a0c6f templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents: 13590
diff changeset
   344
    """
14944
e2c413bde8a5 globally: use safehasattr(x, '__iter__') instead of hasattr(x, '__iter__')
Augie Fackler <durin42@gmail.com>
parents: 14318
diff changeset
   345
    if util.safehasattr(thing, '__iter__') and not isinstance(thing, str):
13588
b8b881f3f3a7 templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents: 13587
diff changeset
   346
        return "".join([stringify(t) for t in thing if t is not None])
25000
c54248bbe023 templatefilters: don't stringify None into "None"
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 24566
diff changeset
   347
    if thing is None:
c54248bbe023 templatefilters: don't stringify None into "None"
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 24566
diff changeset
   348
        return ""
13588
b8b881f3f3a7 templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents: 13587
diff changeset
   349
    return str(thing)
b8b881f3f3a7 templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents: 13587
diff changeset
   350
13590
1a752dcfe062 templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents: 13589
diff changeset
   351
def strip(text):
13591
264f292a0c6f templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents: 13590
diff changeset
   352
    """:strip: Any text. Strips all leading and trailing whitespace."""
13590
1a752dcfe062 templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents: 13589
diff changeset
   353
    return text.strip()
1a752dcfe062 templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents: 13589
diff changeset
   354
8158
1bef3656d9fe templatefilters: add new stripdir filter
Aleix Conchillo Flaque <aleix@member.fsf.org>
parents: 8014
diff changeset
   355
def stripdir(text):
13591
264f292a0c6f templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents: 13590
diff changeset
   356
    """:stripdir: Treat the text as path and strip a directory level, if
264f292a0c6f templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents: 13590
diff changeset
   357
    possible. For example, "foo" and "foo/bar" becomes "foo".
264f292a0c6f templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents: 13590
diff changeset
   358
    """
8158
1bef3656d9fe templatefilters: add new stripdir filter
Aleix Conchillo Flaque <aleix@member.fsf.org>
parents: 8014
diff changeset
   359
    dir = os.path.dirname(text)
1bef3656d9fe templatefilters: add new stripdir filter
Aleix Conchillo Flaque <aleix@member.fsf.org>
parents: 8014
diff changeset
   360
    if dir == "":
1bef3656d9fe templatefilters: add new stripdir filter
Aleix Conchillo Flaque <aleix@member.fsf.org>
parents: 8014
diff changeset
   361
        return os.path.basename(text)
1bef3656d9fe templatefilters: add new stripdir filter
Aleix Conchillo Flaque <aleix@member.fsf.org>
parents: 8014
diff changeset
   362
    else:
1bef3656d9fe templatefilters: add new stripdir filter
Aleix Conchillo Flaque <aleix@member.fsf.org>
parents: 8014
diff changeset
   363
        return dir
1bef3656d9fe templatefilters: add new stripdir filter
Aleix Conchillo Flaque <aleix@member.fsf.org>
parents: 8014
diff changeset
   364
13590
1a752dcfe062 templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents: 13589
diff changeset
   365
def tabindent(text):
19467
1afe5d3939db template: fix tabindent docstring (issue2880)
Matt Mackall <mpm@selenic.com>
parents: 19228
diff changeset
   366
    """:tabindent: Any text. Returns the text, with every non-empty line
1afe5d3939db template: fix tabindent docstring (issue2880)
Matt Mackall <mpm@selenic.com>
parents: 19228
diff changeset
   367
    except the first starting with a tab character.
13591
264f292a0c6f templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents: 13590
diff changeset
   368
    """
13590
1a752dcfe062 templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents: 13589
diff changeset
   369
    return indent(text, '\t')
1a752dcfe062 templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents: 13589
diff changeset
   370
24566
6abce80e6cbf templatefilters: add "upper" and "lower" for case conversion
Yuya Nishihara <yuya@tcha.org>
parents: 23708
diff changeset
   371
def upper(text):
6abce80e6cbf templatefilters: add "upper" and "lower" for case conversion
Yuya Nishihara <yuya@tcha.org>
parents: 23708
diff changeset
   372
    """:upper: Any text. Converts the text to uppercase."""
6abce80e6cbf templatefilters: add "upper" and "lower" for case conversion
Yuya Nishihara <yuya@tcha.org>
parents: 23708
diff changeset
   373
    return encoding.upper(text)
6abce80e6cbf templatefilters: add "upper" and "lower" for case conversion
Yuya Nishihara <yuya@tcha.org>
parents: 23708
diff changeset
   374
13590
1a752dcfe062 templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents: 13589
diff changeset
   375
def urlescape(text):
13591
264f292a0c6f templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents: 13590
diff changeset
   376
    """:urlescape: Any text. Escapes all "special" characters. For example,
264f292a0c6f templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents: 13590
diff changeset
   377
    "foo bar" becomes "foo%20bar".
264f292a0c6f templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents: 13590
diff changeset
   378
    """
13590
1a752dcfe062 templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents: 13589
diff changeset
   379
    return urllib.quote(text)
1a752dcfe062 templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents: 13589
diff changeset
   380
1a752dcfe062 templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents: 13589
diff changeset
   381
def userfilter(text):
16360
e5788269741a templates/filters: extracting the user portion of an email address
Matteo Capobianco <m.capobianco@gmail.com>
parents: 16251
diff changeset
   382
    """:user: Any text. Returns a short representation of a user name or email
e5788269741a templates/filters: extracting the user portion of an email address
Matteo Capobianco <m.capobianco@gmail.com>
parents: 16251
diff changeset
   383
    address."""
13590
1a752dcfe062 templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents: 13589
diff changeset
   384
    return util.shortuser(text)
1a752dcfe062 templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents: 13589
diff changeset
   385
16360
e5788269741a templates/filters: extracting the user portion of an email address
Matteo Capobianco <m.capobianco@gmail.com>
parents: 16251
diff changeset
   386
def emailuser(text):
e5788269741a templates/filters: extracting the user portion of an email address
Matteo Capobianco <m.capobianco@gmail.com>
parents: 16251
diff changeset
   387
    """:emailuser: Any text. Returns the user portion of an email address."""
e5788269741a templates/filters: extracting the user portion of an email address
Matteo Capobianco <m.capobianco@gmail.com>
parents: 16251
diff changeset
   388
    return util.emailuser(text)
e5788269741a templates/filters: extracting the user portion of an email address
Matteo Capobianco <m.capobianco@gmail.com>
parents: 16251
diff changeset
   389
13588
b8b881f3f3a7 templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents: 13587
diff changeset
   390
def xmlescape(text):
b8b881f3f3a7 templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents: 13587
diff changeset
   391
    text = (text
b8b881f3f3a7 templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents: 13587
diff changeset
   392
            .replace('&', '&amp;')
b8b881f3f3a7 templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents: 13587
diff changeset
   393
            .replace('<', '&lt;')
b8b881f3f3a7 templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents: 13587
diff changeset
   394
            .replace('>', '&gt;')
b8b881f3f3a7 templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents: 13587
diff changeset
   395
            .replace('"', '&quot;')
b8b881f3f3a7 templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents: 13587
diff changeset
   396
            .replace("'", '&#39;')) # &apos; invalid in HTML
b8b881f3f3a7 templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents: 13587
diff changeset
   397
    return re.sub('[\x00-\x08\x0B\x0C\x0E-\x1F]', ' ', text)
8234
27dbe534397b templatefilters: add "nonempty" template filter
Rocco Rutte <pdmef@gmx.net>
parents: 8225
diff changeset
   398
5976
9f1e6ab76069 templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   399
filters = {
13587
9fb6850d5d97 templatefilters: match filter keys and function names
Patrick Mezard <pmezard@gmail.com>
parents: 13586
diff changeset
   400
    "addbreaks": addbreaks,
13586
57150dc5a9c7 templatefilters: sort filters table
Patrick Mezard <pmezard@gmail.com>
parents: 12371
diff changeset
   401
    "age": age,
13590
1a752dcfe062 templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents: 13589
diff changeset
   402
    "basename": basename,
22668
13e3f07d74a3 templater: add count template filter, plus tests
Anton Shestakov <engored@ya.ru>
parents: 21873
diff changeset
   403
    "count": count,
13590
1a752dcfe062 templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents: 13589
diff changeset
   404
    "date": datefilter,
5976
9f1e6ab76069 templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   405
    "domain": domain,
13590
1a752dcfe062 templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents: 13589
diff changeset
   406
    "email": email,
1a752dcfe062 templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents: 13589
diff changeset
   407
    "escape": escape,
1a752dcfe062 templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents: 13589
diff changeset
   408
    "fill68": fill68,
1a752dcfe062 templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents: 13589
diff changeset
   409
    "fill76": fill76,
5976
9f1e6ab76069 templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   410
    "firstline": firstline,
13590
1a752dcfe062 templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents: 13589
diff changeset
   411
    "hex": hexfilter,
1a752dcfe062 templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents: 13589
diff changeset
   412
    "hgdate": hgdate,
1a752dcfe062 templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents: 13589
diff changeset
   413
    "isodate": isodate,
1a752dcfe062 templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents: 13589
diff changeset
   414
    "isodatesec": isodatesec,
8014
6a77ba181bc6 templatefilters: split out jsonescape() function
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7948
diff changeset
   415
    "json": json,
6a77ba181bc6 templatefilters: split out jsonescape() function
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7948
diff changeset
   416
    "jsonescape": jsonescape,
13590
1a752dcfe062 templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents: 13589
diff changeset
   417
    "localdate": localdate,
24566
6abce80e6cbf templatefilters: add "upper" and "lower" for case conversion
Yuya Nishihara <yuya@tcha.org>
parents: 23708
diff changeset
   418
    "lower": lower,
8234
27dbe534397b templatefilters: add "nonempty" template filter
Rocco Rutte <pdmef@gmx.net>
parents: 8225
diff changeset
   419
    "nonempty": nonempty,
5976
9f1e6ab76069 templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   420
    "obfuscate": obfuscate,
9f1e6ab76069 templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   421
    "permissions": permissions,
9f1e6ab76069 templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   422
    "person": person,
25778
3a33412792f1 templates: introduce revescape filter for escaping symbolic revisions
Anton Shestakov <av6@dwimlabs.net>
parents: 25000
diff changeset
   423
    "revescape": revescape,
13590
1a752dcfe062 templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents: 13589
diff changeset
   424
    "rfc3339date": rfc3339date,
1a752dcfe062 templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents: 13589
diff changeset
   425
    "rfc822date": rfc822date,
1a752dcfe062 templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents: 13589
diff changeset
   426
    "short": short,
15155
f4a8d754cd0a templates: add 'bisect' keyword to return a cset's bisect status
"Yann E. MORIN" <yann.morin.1998@anciens.enib.fr>
parents: 14967
diff changeset
   427
    "shortbisect": shortbisect,
13590
1a752dcfe062 templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents: 13589
diff changeset
   428
    "shortdate": shortdate,
21820
cce404b0c918 templatefilter: add splitlines function
Ryan McElroy <rmcelroy@fb.com>
parents: 19886
diff changeset
   429
    "splitlines": splitlines,
13590
1a752dcfe062 templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents: 13589
diff changeset
   430
    "stringescape": stringescape,
8360
acc202b71619 templater: provide the standard template filters by default
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 8234
diff changeset
   431
    "stringify": stringify,
13590
1a752dcfe062 templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents: 13589
diff changeset
   432
    "strip": strip,
13586
57150dc5a9c7 templatefilters: sort filters table
Patrick Mezard <pmezard@gmail.com>
parents: 12371
diff changeset
   433
    "stripdir": stripdir,
13590
1a752dcfe062 templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents: 13589
diff changeset
   434
    "tabindent": tabindent,
24566
6abce80e6cbf templatefilters: add "upper" and "lower" for case conversion
Yuya Nishihara <yuya@tcha.org>
parents: 23708
diff changeset
   435
    "upper": upper,
13590
1a752dcfe062 templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents: 13589
diff changeset
   436
    "urlescape": urlescape,
1a752dcfe062 templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents: 13589
diff changeset
   437
    "user": userfilter,
16360
e5788269741a templates/filters: extracting the user portion of an email address
Matteo Capobianco <m.capobianco@gmail.com>
parents: 16251
diff changeset
   438
    "emailuser": emailuser,
6174
434139080ed4 Permit XML entities to be escaped in template output.
Jesse Glick <jesse.glick@sun.com>
parents: 6134
diff changeset
   439
    "xmlescape": xmlescape,
6691
0dba955c2636 add graph page to hgweb
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 6319
diff changeset
   440
}
13591
264f292a0c6f templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents: 13590
diff changeset
   441
18627
4e949b8e0930 hgweb: add websub template filter
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 17772
diff changeset
   442
def websub(text, websubtable):
4e949b8e0930 hgweb: add websub template filter
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 17772
diff changeset
   443
    """:websub: Any text. Only applies to hgweb. Applies the regular
4e949b8e0930 hgweb: add websub template filter
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 17772
diff changeset
   444
    expression replacements defined in the websub section.
4e949b8e0930 hgweb: add websub template filter
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 17772
diff changeset
   445
    """
4e949b8e0930 hgweb: add websub template filter
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 17772
diff changeset
   446
    if websubtable:
4e949b8e0930 hgweb: add websub template filter
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 17772
diff changeset
   447
        for regexp, format in websubtable:
4e949b8e0930 hgweb: add websub template filter
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 17772
diff changeset
   448
            text = regexp.sub(format, text)
4e949b8e0930 hgweb: add websub template filter
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 17772
diff changeset
   449
    return text
4e949b8e0930 hgweb: add websub template filter
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 17772
diff changeset
   450
13591
264f292a0c6f templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents: 13590
diff changeset
   451
# tell hggettext to extract docstrings from these functions:
264f292a0c6f templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents: 13590
diff changeset
   452
i18nfunctions = filters.values()