mercurial/templatefilters.py
author Matt Harbison <matt_harbison@yahoo.com>
Wed, 04 May 2022 16:01:55 -0400
changeset 49173 90f7d8276e26
parent 49021 51aed118f9dc
child 49284 d44e3c45f0e4
permissions -rw-r--r--
contrib: migrate off of a couple of bitbucket URLs I noticed this when diffing the thg installer against Mercurial to see what was missing. There are a handful of other URLs in i18n and extension example help text if anyone cares to update those. Differential Revision: https://phab.mercurial-scm.org/D12606
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
32740
ae0ebe93ac70 templatefilers: correct filename in header comment
Yuya Nishihara <yuya@tcha.org>
parents: 32128
diff changeset
     1
# templatefilters.py - common template expansion filters
5976
9f1e6ab76069 templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
     2
#
46819
d4ba4d51f85f contributor: change mentions of mpm to olivia
Raphaël Gomès <rgomes@octobus.net>
parents: 46113
diff changeset
     3
# Copyright 2005-2008 Olivia Mackall <olivia@selenic.com>
5976
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
1245049da5f3 templatefilters: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25778
diff changeset
     9
import os
1245049da5f3 templatefilters: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25778
diff changeset
    10
import re
1245049da5f3 templatefilters: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25778
diff changeset
    11
import time
1245049da5f3 templatefilters: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25778
diff changeset
    12
37229
05db42732fce templatefilters: handle TypeError by count()
Yuya Nishihara <yuya@tcha.org>
parents: 37227
diff changeset
    13
from .i18n import _
46113
59fa3890d40a node: import symbols explicitly
Joerg Sonnenberger <joerg@bec.de>
parents: 45942
diff changeset
    14
from .node import hex
25983
1245049da5f3 templatefilters: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25778
diff changeset
    15
from . import (
1245049da5f3 templatefilters: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25778
diff changeset
    16
    encoding,
34837
4fdc4adbc838 templatefilters: defend against evil unicode strs in json filter
Augie Fackler <augie@google.com>
parents: 34695
diff changeset
    17
    error,
32126
e37fd5be0fed py3: alias long to int on Python 3
Pulkit Goyal <7895pulkit@gmail.com>
parents: 32039
diff changeset
    18
    pycompat,
28693
11f623b5668f templatefilters: use templatefilter to mark a function as template filter
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 28692
diff changeset
    19
    registrar,
44590
e3e44e6e7245 templater: fix cbor() filter to accept smartset
Yuya Nishihara <yuya@tcha.org>
parents: 43695
diff changeset
    20
    smartset,
36920
6ff6e1d6b5b8 templater: move stringify() to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents: 36831
diff changeset
    21
    templateutil,
34695
e178fcaa3933 python3: use our bytes-only version of cgi.escape everywhere
Augie Fackler <augie@google.com>
parents: 34131
diff changeset
    22
    url,
25983
1245049da5f3 templatefilters: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25778
diff changeset
    23
    util,
1245049da5f3 templatefilters: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25778
diff changeset
    24
)
37084
f0b6fbea00cf stringutil: bulk-replace call sites to point to new module
Yuya Nishihara <yuya@tcha.org>
parents: 36921
diff changeset
    25
from .utils import (
41997
4df7c4b70e03 templatefilters: add {x|cbor} filter for custom CBOR output
Yuya Nishihara <yuya@tcha.org>
parents: 40029
diff changeset
    26
    cborutil,
37084
f0b6fbea00cf stringutil: bulk-replace call sites to point to new module
Yuya Nishihara <yuya@tcha.org>
parents: 36921
diff changeset
    27
    dateutil,
f0b6fbea00cf stringutil: bulk-replace call sites to point to new module
Yuya Nishihara <yuya@tcha.org>
parents: 36921
diff changeset
    28
    stringutil,
f0b6fbea00cf stringutil: bulk-replace call sites to point to new module
Yuya Nishihara <yuya@tcha.org>
parents: 36921
diff changeset
    29
)
5976
9f1e6ab76069 templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    30
28883
032c4c2f802a pycompat: switch to util.urlreq/util.urlerr for py3 compat
timeless <timeless@mozdev.org>
parents: 28693
diff changeset
    31
urlerr = util.urlerr
032c4c2f802a pycompat: switch to util.urlreq/util.urlerr for py3 compat
timeless <timeless@mozdev.org>
parents: 28693
diff changeset
    32
urlreq = util.urlreq
032c4c2f802a pycompat: switch to util.urlreq/util.urlerr for py3 compat
timeless <timeless@mozdev.org>
parents: 28693
diff changeset
    33
28693
11f623b5668f templatefilters: use templatefilter to mark a function as template filter
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 28692
diff changeset
    34
# filters are callables like:
11f623b5668f templatefilters: use templatefilter to mark a function as template filter
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 28692
diff changeset
    35
#   fn(obj)
11f623b5668f templatefilters: use templatefilter to mark a function as template filter
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 28692
diff changeset
    36
# with:
11f623b5668f templatefilters: use templatefilter to mark a function as template filter
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 28692
diff changeset
    37
#   obj - object to be filtered (text, date, list and so on)
11f623b5668f templatefilters: use templatefilter to mark a function as template filter
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 28692
diff changeset
    38
filters = {}
11f623b5668f templatefilters: use templatefilter to mark a function as template filter
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 28692
diff changeset
    39
11f623b5668f templatefilters: use templatefilter to mark a function as template filter
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 28692
diff changeset
    40
templatefilter = registrar.templatefilter(filters)
11f623b5668f templatefilters: use templatefilter to mark a function as template filter
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 28692
diff changeset
    41
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41997
diff changeset
    42
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    43
@templatefilter(b'addbreaks', intype=bytes)
13588
b8b881f3f3a7 templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents: 13587
diff changeset
    44
def addbreaks(text):
28693
11f623b5668f templatefilters: use templatefilter to mark a function as template filter
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 28692
diff changeset
    45
    """Any text. Add an XHTML "<br />" tag before the end of
13591
264f292a0c6f templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents: 13590
diff changeset
    46
    every line except the last.
264f292a0c6f templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents: 13590
diff changeset
    47
    """
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    48
    return text.replace(b'\n', b'<br/>\n')
8360
acc202b71619 templater: provide the standard template filters by default
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 8234
diff changeset
    49
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41997
diff changeset
    50
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41997
diff changeset
    51
agescales = [
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    52
    (b"year", 3600 * 24 * 365, b'Y'),
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    53
    (b"month", 3600 * 24 * 30, b'M'),
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    54
    (b"week", 3600 * 24 * 7, b'W'),
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    55
    (b"day", 3600 * 24, b'd'),
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    56
    (b"hour", 3600, b'h'),
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    57
    (b"minute", 60, b'm'),
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    58
    (b"second", 1, b's'),
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41997
diff changeset
    59
]
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41997
diff changeset
    60
5976
9f1e6ab76069 templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    61
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    62
@templatefilter(b'age', intype=templateutil.date)
19736
f08e542ce918 templatefilters: add short format for age formatting
David Soria Parra <dsp@experimentalworks.net>
parents: 19467
diff changeset
    63
def age(date, abbrev=False):
28693
11f623b5668f templatefilters: use templatefilter to mark a function as template filter
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 28692
diff changeset
    64
    """Date. Returns a human-readable date/time difference between the
13591
264f292a0c6f templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents: 13590
diff changeset
    65
    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
    66
    """
5976
9f1e6ab76069 templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    67
9f1e6ab76069 templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    68
    def plural(t, c):
9f1e6ab76069 templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    69
        if c == 1:
9f1e6ab76069 templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    70
            return t
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    71
        return t + b"s"
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41997
diff changeset
    72
19736
f08e542ce918 templatefilters: add short format for age formatting
David Soria Parra <dsp@experimentalworks.net>
parents: 19467
diff changeset
    73
    def fmt(t, c, a):
f08e542ce918 templatefilters: add short format for age formatting
David Soria Parra <dsp@experimentalworks.net>
parents: 19467
diff changeset
    74
        if abbrev:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    75
            return b"%d%s" % (c, a)
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    76
        return b"%d %s" % (c, plural(t, c))
5976
9f1e6ab76069 templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    77
9f1e6ab76069 templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    78
    now = time.time()
9f1e6ab76069 templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    79
    then = date[0]
13666
c49cddce0a81 templates: provide granularity for future values for age filter
timeless <timeless@gmail.com>
parents: 13593
diff changeset
    80
    future = False
7682
9c8bbae02e9c templater: fix age filter to state the obvious on future timestamps
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 6691
diff changeset
    81
    if then > now:
13666
c49cddce0a81 templates: provide granularity for future values for age filter
timeless <timeless@gmail.com>
parents: 13593
diff changeset
    82
        future = True
c49cddce0a81 templates: provide granularity for future values for age filter
timeless <timeless@gmail.com>
parents: 13593
diff changeset
    83
        delta = max(1, int(then - now))
c49cddce0a81 templates: provide granularity for future values for age filter
timeless <timeless@gmail.com>
parents: 13593
diff changeset
    84
        if delta > agescales[0][1] * 30:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    85
            return b'in the distant future'
13666
c49cddce0a81 templates: provide granularity for future values for age filter
timeless <timeless@gmail.com>
parents: 13593
diff changeset
    86
    else:
c49cddce0a81 templates: provide granularity for future values for age filter
timeless <timeless@gmail.com>
parents: 13593
diff changeset
    87
        delta = max(1, int(now - then))
c49cddce0a81 templates: provide granularity for future values for age filter
timeless <timeless@gmail.com>
parents: 13593
diff changeset
    88
        if delta > agescales[0][1] * 2:
36607
c6061cadb400 util: extract all date-related utils in utils/dateutil module
Boris Feld <boris.feld@octobus.net>
parents: 36573
diff changeset
    89
            return dateutil.shortdate(date)
9722
4d9dea174b84 templater: readable dates older than 24 months revert to ISO8601 (issue1006)
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 9721
diff changeset
    90
19736
f08e542ce918 templatefilters: add short format for age formatting
David Soria Parra <dsp@experimentalworks.net>
parents: 19467
diff changeset
    91
    for t, s, a in agescales:
9029
0001e49f1c11 compat: use // for integer division
Alejandro Santos <alejolp@alejolp.com>
parents: 8697
diff changeset
    92
        n = delta // s
5976
9f1e6ab76069 templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    93
        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
    94
            if future:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    95
                return b'%s from now' % fmt(t, n, a)
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    96
            return b'%s ago' % fmt(t, n, a)
5976
9f1e6ab76069 templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    97
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41997
diff changeset
    98
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    99
@templatefilter(b'basename', intype=bytes)
13590
1a752dcfe062 templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents: 13589
diff changeset
   100
def basename(path):
28693
11f623b5668f templatefilters: use templatefilter to mark a function as template filter
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 28692
diff changeset
   101
    """Any text. Treats the text as a path, and returns the last
35539
d1aae6d4efc5 templatefilters: fix doc of basename()
Yuya Nishihara <yuya@tcha.org>
parents: 34837
diff changeset
   102
    component of the path after splitting by the path separator.
d1aae6d4efc5 templatefilters: fix doc of basename()
Yuya Nishihara <yuya@tcha.org>
parents: 34837
diff changeset
   103
    For example, "foo/bar/baz" becomes "baz" and "foo/bar//" becomes "".
13591
264f292a0c6f templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents: 13590
diff changeset
   104
    """
13590
1a752dcfe062 templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents: 13589
diff changeset
   105
    return os.path.basename(path)
1a752dcfe062 templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents: 13589
diff changeset
   106
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41997
diff changeset
   107
44596
7333e8bb9781 templater: fix cbor() filter to recursively convert smartset to list
Yuya Nishihara <yuya@tcha.org>
parents: 44590
diff changeset
   108
def _tocborencodable(obj):
7333e8bb9781 templater: fix cbor() filter to recursively convert smartset to list
Yuya Nishihara <yuya@tcha.org>
parents: 44590
diff changeset
   109
    if isinstance(obj, smartset.abstractsmartset):
7333e8bb9781 templater: fix cbor() filter to recursively convert smartset to list
Yuya Nishihara <yuya@tcha.org>
parents: 44590
diff changeset
   110
        return list(obj)
7333e8bb9781 templater: fix cbor() filter to recursively convert smartset to list
Yuya Nishihara <yuya@tcha.org>
parents: 44590
diff changeset
   111
    return obj
7333e8bb9781 templater: fix cbor() filter to recursively convert smartset to list
Yuya Nishihara <yuya@tcha.org>
parents: 44590
diff changeset
   112
7333e8bb9781 templater: fix cbor() filter to recursively convert smartset to list
Yuya Nishihara <yuya@tcha.org>
parents: 44590
diff changeset
   113
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   114
@templatefilter(b'cbor')
41997
4df7c4b70e03 templatefilters: add {x|cbor} filter for custom CBOR output
Yuya Nishihara <yuya@tcha.org>
parents: 40029
diff changeset
   115
def cbor(obj):
4df7c4b70e03 templatefilters: add {x|cbor} filter for custom CBOR output
Yuya Nishihara <yuya@tcha.org>
parents: 40029
diff changeset
   116
    """Any object. Serializes the object to CBOR bytes."""
44596
7333e8bb9781 templater: fix cbor() filter to recursively convert smartset to list
Yuya Nishihara <yuya@tcha.org>
parents: 44590
diff changeset
   117
    # cborutil is stricter about type than json() filter
7333e8bb9781 templater: fix cbor() filter to recursively convert smartset to list
Yuya Nishihara <yuya@tcha.org>
parents: 44590
diff changeset
   118
    obj = pycompat.rapply(_tocborencodable, obj)
41997
4df7c4b70e03 templatefilters: add {x|cbor} filter for custom CBOR output
Yuya Nishihara <yuya@tcha.org>
parents: 40029
diff changeset
   119
    return b''.join(cborutil.streamencode(obj))
4df7c4b70e03 templatefilters: add {x|cbor} filter for custom CBOR output
Yuya Nishihara <yuya@tcha.org>
parents: 40029
diff changeset
   120
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41997
diff changeset
   121
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   122
@templatefilter(b'commondir')
38304
fabfbbf4dee2 templatefilters: rename commonprefix to commondir
Martin von Zweigbergk <martinvonz@google.com>
parents: 38300
diff changeset
   123
def commondir(filelist):
38201
56dd15178190 templatefilters: add commonprefix
Joerg Sonnenberger <joerg@bec.de>
parents: 38024
diff changeset
   124
    """List of text. Treats each list item as file name with /
56dd15178190 templatefilters: add commonprefix
Joerg Sonnenberger <joerg@bec.de>
parents: 38024
diff changeset
   125
    as path separator and returns the longest common directory
56dd15178190 templatefilters: add commonprefix
Joerg Sonnenberger <joerg@bec.de>
parents: 38024
diff changeset
   126
    prefix shared by all list items.
56dd15178190 templatefilters: add commonprefix
Joerg Sonnenberger <joerg@bec.de>
parents: 38024
diff changeset
   127
    Returns the empty string if no common prefix exists.
56dd15178190 templatefilters: add commonprefix
Joerg Sonnenberger <joerg@bec.de>
parents: 38024
diff changeset
   128
56dd15178190 templatefilters: add commonprefix
Joerg Sonnenberger <joerg@bec.de>
parents: 38024
diff changeset
   129
    The list items are not normalized, i.e. "foo/../bar" is handled as
56dd15178190 templatefilters: add commonprefix
Joerg Sonnenberger <joerg@bec.de>
parents: 38024
diff changeset
   130
    file "bar" in the directory "foo/..". Leading slashes are ignored.
56dd15178190 templatefilters: add commonprefix
Joerg Sonnenberger <joerg@bec.de>
parents: 38024
diff changeset
   131
56dd15178190 templatefilters: add commonprefix
Joerg Sonnenberger <joerg@bec.de>
parents: 38024
diff changeset
   132
    For example, ["foo/bar/baz", "foo/baz/bar"] becomes "foo" and
56dd15178190 templatefilters: add commonprefix
Joerg Sonnenberger <joerg@bec.de>
parents: 38024
diff changeset
   133
    ["foo/bar", "baz"] becomes "".
56dd15178190 templatefilters: add commonprefix
Joerg Sonnenberger <joerg@bec.de>
parents: 38024
diff changeset
   134
    """
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41997
diff changeset
   135
38201
56dd15178190 templatefilters: add commonprefix
Joerg Sonnenberger <joerg@bec.de>
parents: 38024
diff changeset
   136
    def common(a, b):
56dd15178190 templatefilters: add commonprefix
Joerg Sonnenberger <joerg@bec.de>
parents: 38024
diff changeset
   137
        if len(a) > len(b):
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41997
diff changeset
   138
            a = b[: len(a)]
38201
56dd15178190 templatefilters: add commonprefix
Joerg Sonnenberger <joerg@bec.de>
parents: 38024
diff changeset
   139
        elif len(b) > len(a):
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41997
diff changeset
   140
            b = b[: len(a)]
38201
56dd15178190 templatefilters: add commonprefix
Joerg Sonnenberger <joerg@bec.de>
parents: 38024
diff changeset
   141
        if a == b:
56dd15178190 templatefilters: add commonprefix
Joerg Sonnenberger <joerg@bec.de>
parents: 38024
diff changeset
   142
            return a
38783
e7aa113b14f7 global: use pycompat.xrange()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 38304
diff changeset
   143
        for i in pycompat.xrange(len(a)):
38201
56dd15178190 templatefilters: add commonprefix
Joerg Sonnenberger <joerg@bec.de>
parents: 38024
diff changeset
   144
            if a[i] != b[i]:
56dd15178190 templatefilters: add commonprefix
Joerg Sonnenberger <joerg@bec.de>
parents: 38024
diff changeset
   145
                return a[:i]
56dd15178190 templatefilters: add commonprefix
Joerg Sonnenberger <joerg@bec.de>
parents: 38024
diff changeset
   146
        return a
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41997
diff changeset
   147
38201
56dd15178190 templatefilters: add commonprefix
Joerg Sonnenberger <joerg@bec.de>
parents: 38024
diff changeset
   148
    try:
56dd15178190 templatefilters: add commonprefix
Joerg Sonnenberger <joerg@bec.de>
parents: 38024
diff changeset
   149
        if not filelist:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   150
            return b""
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   151
        dirlist = [f.lstrip(b'/').split(b'/')[:-1] for f in filelist]
38201
56dd15178190 templatefilters: add commonprefix
Joerg Sonnenberger <joerg@bec.de>
parents: 38024
diff changeset
   152
        if len(dirlist) == 1:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   153
            return b'/'.join(dirlist[0])
38201
56dd15178190 templatefilters: add commonprefix
Joerg Sonnenberger <joerg@bec.de>
parents: 38024
diff changeset
   154
        a = min(dirlist)
56dd15178190 templatefilters: add commonprefix
Joerg Sonnenberger <joerg@bec.de>
parents: 38024
diff changeset
   155
        b = max(dirlist)
56dd15178190 templatefilters: add commonprefix
Joerg Sonnenberger <joerg@bec.de>
parents: 38024
diff changeset
   156
        # The common prefix of a and b is shared with all
56dd15178190 templatefilters: add commonprefix
Joerg Sonnenberger <joerg@bec.de>
parents: 38024
diff changeset
   157
        # elements of the list since Python sorts lexicographical
56dd15178190 templatefilters: add commonprefix
Joerg Sonnenberger <joerg@bec.de>
parents: 38024
diff changeset
   158
        # and [1, x] after [1].
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   159
        return b'/'.join(common(a, b))
38201
56dd15178190 templatefilters: add commonprefix
Joerg Sonnenberger <joerg@bec.de>
parents: 38024
diff changeset
   160
    except TypeError:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   161
        raise error.ParseError(_(b'argument is not a list of text'))
38201
56dd15178190 templatefilters: add commonprefix
Joerg Sonnenberger <joerg@bec.de>
parents: 38024
diff changeset
   162
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41997
diff changeset
   163
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   164
@templatefilter(b'count')
22668
13e3f07d74a3 templater: add count template filter, plus tests
Anton Shestakov <engored@ya.ru>
parents: 21873
diff changeset
   165
def count(i):
28693
11f623b5668f templatefilters: use templatefilter to mark a function as template filter
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 28692
diff changeset
   166
    """List or text. Returns the length as an integer."""
37229
05db42732fce templatefilters: handle TypeError by count()
Yuya Nishihara <yuya@tcha.org>
parents: 37227
diff changeset
   167
    try:
05db42732fce templatefilters: handle TypeError by count()
Yuya Nishihara <yuya@tcha.org>
parents: 37227
diff changeset
   168
        return len(i)
05db42732fce templatefilters: handle TypeError by count()
Yuya Nishihara <yuya@tcha.org>
parents: 37227
diff changeset
   169
    except TypeError:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   170
        raise error.ParseError(_(b'not countable'))
22668
13e3f07d74a3 templater: add count template filter, plus tests
Anton Shestakov <engored@ya.ru>
parents: 21873
diff changeset
   171
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41997
diff changeset
   172
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   173
@templatefilter(b'dirname', intype=bytes)
36246
9ee10b3284da templatefilters: add dirname() filter
Yuya Nishihara <yuya@tcha.org>
parents: 35751
diff changeset
   174
def dirname(path):
9ee10b3284da templatefilters: add dirname() filter
Yuya Nishihara <yuya@tcha.org>
parents: 35751
diff changeset
   175
    """Any text. Treats the text as a path, and strips the last
9ee10b3284da templatefilters: add dirname() filter
Yuya Nishihara <yuya@tcha.org>
parents: 35751
diff changeset
   176
    component of the path after splitting by the path separator.
9ee10b3284da templatefilters: add dirname() filter
Yuya Nishihara <yuya@tcha.org>
parents: 35751
diff changeset
   177
    """
9ee10b3284da templatefilters: add dirname() filter
Yuya Nishihara <yuya@tcha.org>
parents: 35751
diff changeset
   178
    return os.path.dirname(path)
9ee10b3284da templatefilters: add dirname() filter
Yuya Nishihara <yuya@tcha.org>
parents: 35751
diff changeset
   179
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41997
diff changeset
   180
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   181
@templatefilter(b'domain', intype=bytes)
13588
b8b881f3f3a7 templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents: 13587
diff changeset
   182
def domain(author):
28693
11f623b5668f templatefilters: use templatefilter to mark a function as template filter
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 28692
diff changeset
   183
    """Any text. Finds the first string that looks like an email
13591
264f292a0c6f templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents: 13590
diff changeset
   184
    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
   185
    <user@example.com>`` becomes ``example.com``.
264f292a0c6f templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents: 13590
diff changeset
   186
    """
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   187
    f = author.find(b'@')
13588
b8b881f3f3a7 templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents: 13587
diff changeset
   188
    if f == -1:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   189
        return b''
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41997
diff changeset
   190
    author = author[f + 1 :]
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   191
    f = author.find(b'>')
13588
b8b881f3f3a7 templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents: 13587
diff changeset
   192
    if f >= 0:
b8b881f3f3a7 templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents: 13587
diff changeset
   193
        author = author[:f]
b8b881f3f3a7 templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents: 13587
diff changeset
   194
    return author
b8b881f3f3a7 templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents: 13587
diff changeset
   195
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41997
diff changeset
   196
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   197
@templatefilter(b'email', intype=bytes)
13590
1a752dcfe062 templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents: 13589
diff changeset
   198
def email(text):
28693
11f623b5668f templatefilters: use templatefilter to mark a function as template filter
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 28692
diff changeset
   199
    """Any text. Extracts the first string that looks like an email
13591
264f292a0c6f templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents: 13590
diff changeset
   200
    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
   201
    ``user@example.com``.
264f292a0c6f templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents: 13590
diff changeset
   202
    """
37084
f0b6fbea00cf stringutil: bulk-replace call sites to point to new module
Yuya Nishihara <yuya@tcha.org>
parents: 36921
diff changeset
   203
    return stringutil.email(text)
13590
1a752dcfe062 templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents: 13589
diff changeset
   204
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41997
diff changeset
   205
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   206
@templatefilter(b'escape', intype=bytes)
13590
1a752dcfe062 templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents: 13589
diff changeset
   207
def escape(text):
28693
11f623b5668f templatefilters: use templatefilter to mark a function as template filter
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 28692
diff changeset
   208
    """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
   209
    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
   210
    """
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   211
    return url.escape(text.replace(b'\0', b''), True)
13590
1a752dcfe062 templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents: 13589
diff changeset
   212
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41997
diff changeset
   213
5976
9f1e6ab76069 templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   214
para_re = None
9f1e6ab76069 templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   215
space_re = None
9f1e6ab76069 templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   216
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41997
diff changeset
   217
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   218
def fill(text, width, initindent=b'', hangindent=b''):
19228
889807c79384 templater: add indentation arguments to the fill function
Sean Farley <sean.michael.farley@gmail.com>
parents: 19227
diff changeset
   219
    '''fill many paragraphs with optional indentation.'''
5976
9f1e6ab76069 templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   220
    global para_re, space_re
9f1e6ab76069 templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   221
    if para_re is None:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   222
        para_re = re.compile(b'(\n\n|\n\\s*[-*]\\s*)', re.M)
36498
b546181ae451 py3: make regexp literal bytes in templatefilters.py
Yuya Nishihara <yuya@tcha.org>
parents: 36497
diff changeset
   223
        space_re = re.compile(br'  +')
5976
9f1e6ab76069 templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   224
9f1e6ab76069 templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   225
    def findparas():
9f1e6ab76069 templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   226
        start = 0
9f1e6ab76069 templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   227
        while True:
9f1e6ab76069 templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   228
            m = para_re.search(text, start)
9f1e6ab76069 templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   229
            if not m:
36497
b2e54b257832 templatefilters: use encoding.unifromlocal/unitolocal() for py3 compatibility
Yuya Nishihara <yuya@tcha.org>
parents: 36246
diff changeset
   230
                uctext = encoding.unifromlocal(text[start:])
11297
d320e70442a5 replace Python standard textwrap by MBCS sensitive one for i18n text
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 10787
diff changeset
   231
                w = len(uctext)
40029
e2697acd9381 cleanup: some Yoda conditions, this patch removes
Martin von Zweigbergk <martinvonz@google.com>
parents: 38783
diff changeset
   232
                while w > 0 and uctext[w - 1].isspace():
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   233
                    w -= 1
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41997
diff changeset
   234
                yield (
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41997
diff changeset
   235
                    encoding.unitolocal(uctext[:w]),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41997
diff changeset
   236
                    encoding.unitolocal(uctext[w:]),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41997
diff changeset
   237
                )
5976
9f1e6ab76069 templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   238
                break
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41997
diff changeset
   239
            yield text[start : m.start(0)], m.group(1)
5976
9f1e6ab76069 templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   240
            start = m.end(1)
9f1e6ab76069 templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   241
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   242
    return b"".join(
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41997
diff changeset
   243
        [
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41997
diff changeset
   244
            stringutil.wrap(
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   245
                space_re.sub(b' ', stringutil.wrap(para, width)),
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41997
diff changeset
   246
                width,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41997
diff changeset
   247
                initindent,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41997
diff changeset
   248
                hangindent,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41997
diff changeset
   249
            )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41997
diff changeset
   250
            + rest
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41997
diff changeset
   251
            for para, rest in findparas()
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41997
diff changeset
   252
        ]
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41997
diff changeset
   253
    )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41997
diff changeset
   254
5976
9f1e6ab76069 templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   255
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   256
@templatefilter(b'fill68', intype=bytes)
13590
1a752dcfe062 templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents: 13589
diff changeset
   257
def fill68(text):
28693
11f623b5668f templatefilters: use templatefilter to mark a function as template filter
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 28692
diff changeset
   258
    """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
   259
    return fill(text, 68)
1a752dcfe062 templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents: 13589
diff changeset
   260
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41997
diff changeset
   261
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   262
@templatefilter(b'fill76', intype=bytes)
13590
1a752dcfe062 templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents: 13589
diff changeset
   263
def fill76(text):
28693
11f623b5668f templatefilters: use templatefilter to mark a function as template filter
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 28692
diff changeset
   264
    """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
   265
    return fill(text, 76)
1a752dcfe062 templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents: 13589
diff changeset
   266
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41997
diff changeset
   267
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   268
@templatefilter(b'firstline', intype=bytes)
5976
9f1e6ab76069 templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   269
def firstline(text):
28693
11f623b5668f templatefilters: use templatefilter to mark a function as template filter
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 28692
diff changeset
   270
    """Any text. Returns the first line of text."""
49021
51aed118f9dc templates: extract function to `stringutil` for getting first line of text
Martin von Zweigbergk <martinvonz@google.com>
parents: 49020
diff changeset
   271
    return stringutil.firstline(text)
5976
9f1e6ab76069 templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   272
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41997
diff changeset
   273
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   274
@templatefilter(b'hex', intype=bytes)
13590
1a752dcfe062 templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents: 13589
diff changeset
   275
def hexfilter(text):
28693
11f623b5668f templatefilters: use templatefilter to mark a function as template filter
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 28692
diff changeset
   276
    """Any text. Convert a binary Mercurial node identifier into
13591
264f292a0c6f templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents: 13590
diff changeset
   277
    its long hexadecimal representation.
264f292a0c6f templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents: 13590
diff changeset
   278
    """
46113
59fa3890d40a node: import symbols explicitly
Joerg Sonnenberger <joerg@bec.de>
parents: 45942
diff changeset
   279
    return hex(text)
13590
1a752dcfe062 templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents: 13589
diff changeset
   280
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41997
diff changeset
   281
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   282
@templatefilter(b'hgdate', intype=templateutil.date)
13590
1a752dcfe062 templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents: 13589
diff changeset
   283
def hgdate(text):
28693
11f623b5668f templatefilters: use templatefilter to mark a function as template filter
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 28692
diff changeset
   284
    """Date. Returns the date as a pair of numbers: "1157407993
38300
74b4a54002ec templatefilters: undeprecate hgdate
Yuya Nishihara <yuya@tcha.org>
parents: 38287
diff changeset
   285
    25200" (Unix timestamp, timezone offset).
13591
264f292a0c6f templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents: 13590
diff changeset
   286
    """
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   287
    return b"%d %d" % text
13590
1a752dcfe062 templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents: 13589
diff changeset
   288
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41997
diff changeset
   289
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   290
@templatefilter(b'isodate', intype=templateutil.date)
13590
1a752dcfe062 templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents: 13589
diff changeset
   291
def isodate(text):
28693
11f623b5668f templatefilters: use templatefilter to mark a function as template filter
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 28692
diff changeset
   292
    """Date. Returns the date in ISO 8601 format: "2009-08-18 13:00
13591
264f292a0c6f templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents: 13590
diff changeset
   293
    +0200".
264f292a0c6f templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents: 13590
diff changeset
   294
    """
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   295
    return dateutil.datestr(text, b'%Y-%m-%d %H:%M %1%2')
13590
1a752dcfe062 templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents: 13589
diff changeset
   296
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41997
diff changeset
   297
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   298
@templatefilter(b'isodatesec', intype=templateutil.date)
13590
1a752dcfe062 templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents: 13589
diff changeset
   299
def isodatesec(text):
28693
11f623b5668f templatefilters: use templatefilter to mark a function as template filter
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 28692
diff changeset
   300
    """Date. Returns the date in ISO 8601 format, including
13591
264f292a0c6f templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents: 13590
diff changeset
   301
    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
   302
    filter.
264f292a0c6f templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents: 13590
diff changeset
   303
    """
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   304
    return dateutil.datestr(text, b'%Y-%m-%d %H:%M:%S %1%2')
13590
1a752dcfe062 templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents: 13589
diff changeset
   305
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41997
diff changeset
   306
43695
fa246ada356b templates: make {indent("", " ")} be empty
Martin von Zweigbergk <martinvonz@google.com>
parents: 43333
diff changeset
   307
def indent(text, prefix, firstline=b''):
5976
9f1e6ab76069 templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   308
    '''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
   309
    lines = text.splitlines()
9f1e6ab76069 templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   310
    num_lines = len(lines)
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   311
    endswithnewline = text[-1:] == b'\n'
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41997
diff changeset
   312
5976
9f1e6ab76069 templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   313
    def indenter():
38783
e7aa113b14f7 global: use pycompat.xrange()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 38304
diff changeset
   314
        for i in pycompat.xrange(num_lines):
5976
9f1e6ab76069 templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   315
            l = lines[i]
43695
fa246ada356b templates: make {indent("", " ")} be empty
Martin von Zweigbergk <martinvonz@google.com>
parents: 43333
diff changeset
   316
            if l.strip():
fa246ada356b templates: make {indent("", " ")} be empty
Martin von Zweigbergk <martinvonz@google.com>
parents: 43333
diff changeset
   317
                yield prefix if i else firstline
5976
9f1e6ab76069 templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   318
            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
   319
            if i < num_lines - 1 or endswithnewline:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   320
                yield b'\n'
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41997
diff changeset
   321
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   322
    return b"".join(indenter())
5976
9f1e6ab76069 templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   323
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41997
diff changeset
   324
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   325
@templatefilter(b'json')
31782
654e9a1c8a6c formatter: use templatefilters.json()
Yuya Nishihara <yuya@tcha.org>
parents: 31781
diff changeset
   326
def json(obj, paranoid=True):
37948
a25513263075 templatefilters: document the json filter
Yuya Nishihara <yuya@tcha.org>
parents: 37230
diff changeset
   327
    """Any object. Serializes the object to a JSON formatted text."""
31780
8d9eafe01111 templatefilters: unroll handling of None/False/True
Yuya Nishihara <yuya@tcha.org>
parents: 31779
diff changeset
   328
    if obj is None:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   329
        return b'null'
31780
8d9eafe01111 templatefilters: unroll handling of None/False/True
Yuya Nishihara <yuya@tcha.org>
parents: 31779
diff changeset
   330
    elif obj is False:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   331
        return b'false'
31780
8d9eafe01111 templatefilters: unroll handling of None/False/True
Yuya Nishihara <yuya@tcha.org>
parents: 31779
diff changeset
   332
    elif obj is True:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   333
        return b'true'
48932
176f1a0d15dc py3: use int instead of pycompat.long
Gregory Szorc <gregory.szorc@gmail.com>
parents: 48913
diff changeset
   334
    elif isinstance(obj, (int, int, float)):
32127
964e7427a691 py3: use pycompat.bytestr() instead of str()
Pulkit Goyal <7895pulkit@gmail.com>
parents: 32126
diff changeset
   335
        return pycompat.bytestr(obj)
32128
c3342c177211 py3: replace str with bytes in isinstance()
Pulkit Goyal <7895pulkit@gmail.com>
parents: 32127
diff changeset
   336
    elif isinstance(obj, bytes):
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   337
        return b'"%s"' % encoding.jsonescape(obj, paranoid=paranoid)
38024
f5a1aa8c6987 json: reject unicode on py2 as well
Martin von Zweigbergk <martinvonz@google.com>
parents: 37948
diff changeset
   338
    elif isinstance(obj, type(u'')):
34837
4fdc4adbc838 templatefilters: defend against evil unicode strs in json filter
Augie Fackler <augie@google.com>
parents: 34695
diff changeset
   339
        raise error.ProgrammingError(
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   340
            b'Mercurial only does output with bytes: %r' % obj
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41997
diff changeset
   341
        )
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   342
    elif util.safehasattr(obj, b'keys'):
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41997
diff changeset
   343
        out = [
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   344
            b'"%s": %s'
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41997
diff changeset
   345
            % (encoding.jsonescape(k, paranoid=paranoid), json(v, paranoid))
48913
f254fc73d956 global: bulk replace simple pycompat.iteritems(x) with x.items()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 48875
diff changeset
   346
            for k, v in sorted(obj.items())
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41997
diff changeset
   347
        ]
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   348
        return b'{' + b', '.join(out) + b'}'
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   349
    elif util.safehasattr(obj, b'__iter__'):
32743
f924dd043974 json: pass formatting options recursively
Yuya Nishihara <yuya@tcha.org>
parents: 32742
diff changeset
   350
        out = [json(i, paranoid) for i in obj]
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   351
        return b'[' + b', '.join(out) + b']'
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   352
    raise error.ProgrammingError(b'cannot encode %r' % obj)
6691
0dba955c2636 add graph page to hgweb
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 6319
diff changeset
   353
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41997
diff changeset
   354
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   355
@templatefilter(b'lower', intype=bytes)
24566
6abce80e6cbf templatefilters: add "upper" and "lower" for case conversion
Yuya Nishihara <yuya@tcha.org>
parents: 23708
diff changeset
   356
def lower(text):
28693
11f623b5668f templatefilters: use templatefilter to mark a function as template filter
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 28692
diff changeset
   357
    """Any text. Converts the text to lowercase."""
24566
6abce80e6cbf templatefilters: add "upper" and "lower" for case conversion
Yuya Nishihara <yuya@tcha.org>
parents: 23708
diff changeset
   358
    return encoding.lower(text)
6abce80e6cbf templatefilters: add "upper" and "lower" for case conversion
Yuya Nishihara <yuya@tcha.org>
parents: 23708
diff changeset
   359
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41997
diff changeset
   360
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   361
@templatefilter(b'nonempty', intype=bytes)
36552
a185b1af207c templatefilters: stop using str as a variable name
Pulkit Goyal <7895pulkit@gmail.com>
parents: 36498
diff changeset
   362
def nonempty(text):
28693
11f623b5668f templatefilters: use templatefilter to mark a function as template filter
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 28692
diff changeset
   363
    """Any text. Returns '(none)' if the string is empty."""
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   364
    return text or b"(none)"
13588
b8b881f3f3a7 templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents: 13587
diff changeset
   365
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41997
diff changeset
   366
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   367
@templatefilter(b'obfuscate', intype=bytes)
13588
b8b881f3f3a7 templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents: 13587
diff changeset
   368
def obfuscate(text):
28693
11f623b5668f templatefilters: use templatefilter to mark a function as template filter
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 28692
diff changeset
   369
    """Any text. Returns the input text rendered as a sequence of
13591
264f292a0c6f templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents: 13590
diff changeset
   370
    XML entities.
264f292a0c6f templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents: 13590
diff changeset
   371
    """
48934
06de08b36c82 py3: use str instead of pycompat.unicode
Gregory Szorc <gregory.szorc@gmail.com>
parents: 48932
diff changeset
   372
    text = str(text, pycompat.sysstr(encoding.encoding), r'replace')
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   373
    return b''.join([b'&#%d;' % ord(c) for c in text])
13588
b8b881f3f3a7 templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents: 13587
diff changeset
   374
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41997
diff changeset
   375
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   376
@templatefilter(b'permissions', intype=bytes)
13588
b8b881f3f3a7 templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents: 13587
diff changeset
   377
def permissions(flags):
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   378
    if b"l" in flags:
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   379
        return b"lrwxrwxrwx"
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   380
    if b"x" in flags:
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   381
        return b"-rwxr-xr-x"
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   382
    return b"-rw-r--r--"
13588
b8b881f3f3a7 templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents: 13587
diff changeset
   383
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41997
diff changeset
   384
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   385
@templatefilter(b'person', intype=bytes)
13588
b8b881f3f3a7 templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents: 13587
diff changeset
   386
def person(author):
28693
11f623b5668f templatefilters: use templatefilter to mark a function as template filter
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 28692
diff changeset
   387
    """Any text. Returns the name before an email address,
16235
eb39bbda167b templates/filters: strip quotes from {author|person}
"Yann E. MORIN" <yann.morin.1998@free.fr>
parents: 15155
diff changeset
   388
    interpreting it as per RFC 5322.
eb39bbda167b templates/filters: strip quotes from {author|person}
"Yann E. MORIN" <yann.morin.1998@free.fr>
parents: 15155
diff changeset
   389
    """
37155
fb7140f1d09d stringutil: move person function from templatefilters
Connor Sheehan <sheehan@mozilla.com>
parents: 37084
diff changeset
   390
    return stringutil.person(author)
13588
b8b881f3f3a7 templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents: 13587
diff changeset
   391
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41997
diff changeset
   392
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   393
@templatefilter(b'revescape', intype=bytes)
25778
3a33412792f1 templates: introduce revescape filter for escaping symbolic revisions
Anton Shestakov <av6@dwimlabs.net>
parents: 25000
diff changeset
   394
def revescape(text):
28693
11f623b5668f templatefilters: use templatefilter to mark a function as template filter
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 28692
diff changeset
   395
    """Any text. Escapes all "special" characters, except @.
25778
3a33412792f1 templates: introduce revescape filter for escaping symbolic revisions
Anton Shestakov <av6@dwimlabs.net>
parents: 25000
diff changeset
   396
    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
   397
    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
   398
    """
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   399
    return urlreq.quote(text, safe=b'/@').replace(b'/', b'%252F')
25778
3a33412792f1 templates: introduce revescape filter for escaping symbolic revisions
Anton Shestakov <av6@dwimlabs.net>
parents: 25000
diff changeset
   400
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41997
diff changeset
   401
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   402
@templatefilter(b'rfc3339date', intype=templateutil.date)
13590
1a752dcfe062 templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents: 13589
diff changeset
   403
def rfc3339date(text):
28693
11f623b5668f templatefilters: use templatefilter to mark a function as template filter
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 28692
diff changeset
   404
    """Date. Returns a date using the Internet date format
13591
264f292a0c6f templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents: 13590
diff changeset
   405
    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
   406
    """
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   407
    return dateutil.datestr(text, b"%Y-%m-%dT%H:%M:%S%1:%2")
13590
1a752dcfe062 templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents: 13589
diff changeset
   408
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41997
diff changeset
   409
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   410
@templatefilter(b'rfc822date', intype=templateutil.date)
13590
1a752dcfe062 templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents: 13589
diff changeset
   411
def rfc822date(text):
28693
11f623b5668f templatefilters: use templatefilter to mark a function as template filter
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 28692
diff changeset
   412
    """Date. Returns a date using the same format used in email
13591
264f292a0c6f templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents: 13590
diff changeset
   413
    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
   414
    """
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   415
    return dateutil.datestr(text, b"%a, %d %b %Y %H:%M:%S %1%2")
13590
1a752dcfe062 templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents: 13589
diff changeset
   416
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41997
diff changeset
   417
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   418
@templatefilter(b'short', intype=bytes)
13590
1a752dcfe062 templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents: 13589
diff changeset
   419
def short(text):
28693
11f623b5668f templatefilters: use templatefilter to mark a function as template filter
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 28692
diff changeset
   420
    """Changeset hash. Returns the short form of a changeset hash,
13591
264f292a0c6f templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents: 13590
diff changeset
   421
    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
   422
    """
13590
1a752dcfe062 templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents: 13589
diff changeset
   423
    return text[:12]
1a752dcfe062 templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents: 13589
diff changeset
   424
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41997
diff changeset
   425
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   426
@templatefilter(b'shortbisect', intype=bytes)
36830
71f189941791 templatefilters: inline hbisect.shortlabel()
Yuya Nishihara <yuya@tcha.org>
parents: 36607
diff changeset
   427
def shortbisect(label):
71f189941791 templatefilters: inline hbisect.shortlabel()
Yuya Nishihara <yuya@tcha.org>
parents: 36607
diff changeset
   428
    """Any text. Treats `label` as a bisection status, and
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
   429
    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
   430
    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
   431
    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
   432
    """
36830
71f189941791 templatefilters: inline hbisect.shortlabel()
Yuya Nishihara <yuya@tcha.org>
parents: 36607
diff changeset
   433
    if label:
36831
82af07e1ae16 py3: fix slicing of bisect label in templatefilters.shortbisect()
Yuya Nishihara <yuya@tcha.org>
parents: 36830
diff changeset
   434
        return label[0:1].upper()
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   435
    return b' '
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
   436
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41997
diff changeset
   437
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   438
@templatefilter(b'shortdate', intype=templateutil.date)
13590
1a752dcfe062 templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents: 13589
diff changeset
   439
def shortdate(text):
28693
11f623b5668f templatefilters: use templatefilter to mark a function as template filter
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 28692
diff changeset
   440
    """Date. Returns a date like "2006-09-18"."""
36607
c6061cadb400 util: extract all date-related utils in utils/dateutil module
Boris Feld <boris.feld@octobus.net>
parents: 36573
diff changeset
   441
    return dateutil.shortdate(text)
13590
1a752dcfe062 templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents: 13589
diff changeset
   442
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41997
diff changeset
   443
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   444
@templatefilter(b'slashpath', intype=bytes)
35444
dad8a5071b0a templatefilters: add slashpath() to convert path separator to slash
Yuya Nishihara <yuya@tcha.org>
parents: 34837
diff changeset
   445
def slashpath(path):
dad8a5071b0a templatefilters: add slashpath() to convert path separator to slash
Yuya Nishihara <yuya@tcha.org>
parents: 34837
diff changeset
   446
    """Any text. Replaces the native path separator with slash."""
dad8a5071b0a templatefilters: add slashpath() to convert path separator to slash
Yuya Nishihara <yuya@tcha.org>
parents: 34837
diff changeset
   447
    return util.pconvert(path)
dad8a5071b0a templatefilters: add slashpath() to convert path separator to slash
Yuya Nishihara <yuya@tcha.org>
parents: 34837
diff changeset
   448
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41997
diff changeset
   449
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   450
@templatefilter(b'splitlines', intype=bytes)
21820
cce404b0c918 templatefilter: add splitlines function
Ryan McElroy <rmcelroy@fb.com>
parents: 19886
diff changeset
   451
def splitlines(text):
28693
11f623b5668f templatefilters: use templatefilter to mark a function as template filter
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 28692
diff changeset
   452
    """Any text. Split text into a list of lines."""
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   453
    return templateutil.hybridlist(text.splitlines(), name=b'line')
21820
cce404b0c918 templatefilter: add splitlines function
Ryan McElroy <rmcelroy@fb.com>
parents: 19886
diff changeset
   454
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41997
diff changeset
   455
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   456
@templatefilter(b'stringescape', intype=bytes)
13590
1a752dcfe062 templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents: 13589
diff changeset
   457
def stringescape(text):
37084
f0b6fbea00cf stringutil: bulk-replace call sites to point to new module
Yuya Nishihara <yuya@tcha.org>
parents: 36921
diff changeset
   458
    return stringutil.escapestr(text)
13590
1a752dcfe062 templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents: 13589
diff changeset
   459
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41997
diff changeset
   460
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   461
@templatefilter(b'stringify', intype=bytes)
13588
b8b881f3f3a7 templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents: 13587
diff changeset
   462
def stringify(thing):
28693
11f623b5668f templatefilters: use templatefilter to mark a function as template filter
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 28692
diff changeset
   463
    """Any type. Turns the value into text by converting values into
13591
264f292a0c6f templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents: 13590
diff changeset
   464
    text and concatenating them.
264f292a0c6f templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents: 13590
diff changeset
   465
    """
37222
54355c243042 templatefilters: allow declaration of input data type
Yuya Nishihara <yuya@tcha.org>
parents: 37155
diff changeset
   466
    return thing  # coerced by the intype
13588
b8b881f3f3a7 templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents: 13587
diff changeset
   467
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41997
diff changeset
   468
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   469
@templatefilter(b'stripdir', intype=bytes)
8158
1bef3656d9fe templatefilters: add new stripdir filter
Aleix Conchillo Flaque <aleix@member.fsf.org>
parents: 8014
diff changeset
   470
def stripdir(text):
28693
11f623b5668f templatefilters: use templatefilter to mark a function as template filter
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 28692
diff changeset
   471
    """Treat the text as path and strip a directory level, if
13591
264f292a0c6f templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents: 13590
diff changeset
   472
    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
   473
    """
8158
1bef3656d9fe templatefilters: add new stripdir filter
Aleix Conchillo Flaque <aleix@member.fsf.org>
parents: 8014
diff changeset
   474
    dir = os.path.dirname(text)
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   475
    if dir == b"":
8158
1bef3656d9fe templatefilters: add new stripdir filter
Aleix Conchillo Flaque <aleix@member.fsf.org>
parents: 8014
diff changeset
   476
        return os.path.basename(text)
1bef3656d9fe templatefilters: add new stripdir filter
Aleix Conchillo Flaque <aleix@member.fsf.org>
parents: 8014
diff changeset
   477
    else:
1bef3656d9fe templatefilters: add new stripdir filter
Aleix Conchillo Flaque <aleix@member.fsf.org>
parents: 8014
diff changeset
   478
        return dir
1bef3656d9fe templatefilters: add new stripdir filter
Aleix Conchillo Flaque <aleix@member.fsf.org>
parents: 8014
diff changeset
   479
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41997
diff changeset
   480
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   481
@templatefilter(b'tabindent', intype=bytes)
13590
1a752dcfe062 templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents: 13589
diff changeset
   482
def tabindent(text):
28693
11f623b5668f templatefilters: use templatefilter to mark a function as template filter
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 28692
diff changeset
   483
    """Any text. Returns the text, with every non-empty line
19467
1afe5d3939db template: fix tabindent docstring (issue2880)
Matt Mackall <mpm@selenic.com>
parents: 19228
diff changeset
   484
    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
   485
    """
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   486
    return indent(text, b'\t')
13590
1a752dcfe062 templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents: 13589
diff changeset
   487
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41997
diff changeset
   488
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   489
@templatefilter(b'upper', intype=bytes)
24566
6abce80e6cbf templatefilters: add "upper" and "lower" for case conversion
Yuya Nishihara <yuya@tcha.org>
parents: 23708
diff changeset
   490
def upper(text):
28693
11f623b5668f templatefilters: use templatefilter to mark a function as template filter
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 28692
diff changeset
   491
    """Any text. Converts the text to uppercase."""
24566
6abce80e6cbf templatefilters: add "upper" and "lower" for case conversion
Yuya Nishihara <yuya@tcha.org>
parents: 23708
diff changeset
   492
    return encoding.upper(text)
6abce80e6cbf templatefilters: add "upper" and "lower" for case conversion
Yuya Nishihara <yuya@tcha.org>
parents: 23708
diff changeset
   493
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41997
diff changeset
   494
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   495
@templatefilter(b'urlescape', intype=bytes)
13590
1a752dcfe062 templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents: 13589
diff changeset
   496
def urlescape(text):
28693
11f623b5668f templatefilters: use templatefilter to mark a function as template filter
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 28692
diff changeset
   497
    """Any text. Escapes all "special" characters. For example,
13591
264f292a0c6f templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents: 13590
diff changeset
   498
    "foo bar" becomes "foo%20bar".
264f292a0c6f templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents: 13590
diff changeset
   499
    """
28883
032c4c2f802a pycompat: switch to util.urlreq/util.urlerr for py3 compat
timeless <timeless@mozdev.org>
parents: 28693
diff changeset
   500
    return urlreq.quote(text)
13590
1a752dcfe062 templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents: 13589
diff changeset
   501
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41997
diff changeset
   502
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   503
@templatefilter(b'user', intype=bytes)
13590
1a752dcfe062 templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents: 13589
diff changeset
   504
def userfilter(text):
28693
11f623b5668f templatefilters: use templatefilter to mark a function as template filter
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 28692
diff changeset
   505
    """Any text. Returns a short representation of a user name or email
16360
e5788269741a templates/filters: extracting the user portion of an email address
Matteo Capobianco <m.capobianco@gmail.com>
parents: 16251
diff changeset
   506
    address."""
37084
f0b6fbea00cf stringutil: bulk-replace call sites to point to new module
Yuya Nishihara <yuya@tcha.org>
parents: 36921
diff changeset
   507
    return stringutil.shortuser(text)
13590
1a752dcfe062 templatefilters: wrap all filters in dedicated functions
Patrick Mezard <pmezard@gmail.com>
parents: 13589
diff changeset
   508
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41997
diff changeset
   509
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   510
@templatefilter(b'emailuser', intype=bytes)
16360
e5788269741a templates/filters: extracting the user portion of an email address
Matteo Capobianco <m.capobianco@gmail.com>
parents: 16251
diff changeset
   511
def emailuser(text):
28693
11f623b5668f templatefilters: use templatefilter to mark a function as template filter
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 28692
diff changeset
   512
    """Any text. Returns the user portion of an email address."""
37084
f0b6fbea00cf stringutil: bulk-replace call sites to point to new module
Yuya Nishihara <yuya@tcha.org>
parents: 36921
diff changeset
   513
    return stringutil.emailuser(text)
16360
e5788269741a templates/filters: extracting the user portion of an email address
Matteo Capobianco <m.capobianco@gmail.com>
parents: 16251
diff changeset
   514
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41997
diff changeset
   515
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   516
@templatefilter(b'utf8', intype=bytes)
28209
8ddf893560fa templatefilters: add "utf8" to get utf-8 bytes from local-encoding text
Yuya Nishihara <yuya@tcha.org>
parents: 28208
diff changeset
   517
def utf8(text):
28693
11f623b5668f templatefilters: use templatefilter to mark a function as template filter
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 28692
diff changeset
   518
    """Any text. Converts from the local character encoding to UTF-8."""
28209
8ddf893560fa templatefilters: add "utf8" to get utf-8 bytes from local-encoding text
Yuya Nishihara <yuya@tcha.org>
parents: 28208
diff changeset
   519
    return encoding.fromlocal(text)
8ddf893560fa templatefilters: add "utf8" to get utf-8 bytes from local-encoding text
Yuya Nishihara <yuya@tcha.org>
parents: 28208
diff changeset
   520
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41997
diff changeset
   521
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   522
@templatefilter(b'xmlescape', intype=bytes)
13588
b8b881f3f3a7 templatefilters: sort function definitions
Patrick Mezard <pmezard@gmail.com>
parents: 13587
diff changeset
   523
def xmlescape(text):
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41997
diff changeset
   524
    text = (
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   525
        text.replace(b'&', b'&amp;')
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   526
        .replace(b'<', b'&lt;')
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   527
        .replace(b'>', b'&gt;')
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   528
        .replace(b'"', b'&quot;')
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   529
        .replace(b"'", b'&#39;')
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41997
diff changeset
   530
    )  # &apos; invalid in HTML
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   531
    return re.sub(b'[\x00-\x08\x0B\x0C\x0E-\x1F]', b' ', text)
8234
27dbe534397b templatefilters: add "nonempty" template filter
Rocco Rutte <pdmef@gmx.net>
parents: 8225
diff changeset
   532
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41997
diff changeset
   533
18627
4e949b8e0930 hgweb: add websub template filter
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 17772
diff changeset
   534
def websub(text, websubtable):
4e949b8e0930 hgweb: add websub template filter
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 17772
diff changeset
   535
    """: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
   536
    expression replacements defined in the websub section.
4e949b8e0930 hgweb: add websub template filter
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 17772
diff changeset
   537
    """
4e949b8e0930 hgweb: add websub template filter
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 17772
diff changeset
   538
    if websubtable:
4e949b8e0930 hgweb: add websub template filter
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 17772
diff changeset
   539
        for regexp, format in websubtable:
4e949b8e0930 hgweb: add websub template filter
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 17772
diff changeset
   540
            text = regexp.sub(format, text)
4e949b8e0930 hgweb: add websub template filter
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 17772
diff changeset
   541
    return text
4e949b8e0930 hgweb: add websub template filter
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 17772
diff changeset
   542
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41997
diff changeset
   543
28692
6b3b958daf03 registrar: add templatefilter to mark a function as template filter (API)
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 28213
diff changeset
   544
def loadfilter(ui, extname, registrarobj):
45942
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 44596
diff changeset
   545
    """Load template filter from specified registrarobj"""
48913
f254fc73d956 global: bulk replace simple pycompat.iteritems(x) with x.items()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 48875
diff changeset
   546
    for name, func in registrarobj._table.items():
28692
6b3b958daf03 registrar: add templatefilter to mark a function as template filter (API)
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 28213
diff changeset
   547
        filters[name] = func
6b3b958daf03 registrar: add templatefilter to mark a function as template filter (API)
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 28213
diff changeset
   548
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41997
diff changeset
   549
13591
264f292a0c6f templatefilters: move doc from templates.txt to docstrings
Patrick Mezard <pmezard@gmail.com>
parents: 13590
diff changeset
   550
# 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
   551
i18nfunctions = filters.values()