0
  C   ˆl–äY>”JeJe@†à¸Û*bÑ¿�þ_q·nP¸VDHÓ@þÈ„eæžsÍ_ŠI|¥�+Wé3ž¯ÇË ?÷     # templater.py - template expansion for output
#
# Copyright 2005, 2006 Olivia Mackall <olivia@selenic.com>
#
# This software may be used and distributed according to the terms of the
# GNU General Public License version 2 or any later version.

"""Slightly complicated template engine for commands and hgweb

This module provides low-level interface to the template engine. See the
formatter and cmdutil modules if you are looking for high-level functions
such as ``cmdutil.rendertemplate(ctx, tmpl)``.

Internal Data Types
-------------------

Template keywords and functions take a dictionary of current symbols and
resources (a "mapping") and return result. Inputs and outputs must be one
of the following data types:

bytes
    a byte string, which is generally a human-readable text in local encoding.

generator
    a lazily-evaluated byte string, which is a possibly nested generator of
    values of any printable types, and  will be folded by ``stringify()``
    or ``flatten()``.

None
    sometimes represents an empty value, which can be stringified to ''.

True, False, int, float
    can be stringified as such.

wrappedbytes, wrappedvalue
    a wrapper for the above printable types.

date
    represents a (unixtime, offset) tuple.

hybrid
    represents a list/dict of printable values, which can also be converted
    to mappings by % operator.

hybriditem
    represents a scalar printable value, also supports % operator.

revslist
    represents a list of revision numbers.

mappinggenerator, mappinglist
    represents mappings (i.e. a list of dicts), which may have default
    output format.

mappingdict
    represents a single mapping (i.e. a dict), which may have default output
    format.

mappingnone
    represents None of Optional[mappable], which will be mapped to an empty
    string by % operation.

mappedgenerator
    a lazily-evaluated list of byte strings, which is e.g. a result of %
    operation.
"""


import abc
import os

from .i18n import _
from .pycompat import (
    FileNotFoundError,
    getattr,
)
from . import (
    config,
    encoding,
    error,
    parser,
    pycompat,
    templatefilters,
    templatefuncs,
    templateutil,
    util,
)
from .utils import (
    resourceutil,
    stringutil,
)

# template parsing

elements = {
    # token-type: binding-strength, primary, prefix, infix, suffix
    b"(": (20, None, (b"group", 1, b")"), (b"func", 1, b")"), None),
    b".": (18, None, None, (b".", 18), None),
    b"%": (15, None, None, (b"%", 15), None),
    b"|": (15, None, None, (b"|", 15), None),
    b"*": (5, None, None, (b"*", 5), None),
    b"/": (5, None, None, (b"/", 5), None),
    b"+": (4, None, None, (b"+", 4), None),
    b"-": (4, None, (b"negate", 19), (b"-", 4), None),
    b"=": (3, None, None, (b"keyvalue", 3), None),
    b",": (2, None, None, (b"list", 2), None),
    b")": (0, None, None, None, None),
    b"integer": (0, b"integer", None, None, None),
    b"symbol": (0, b"symbol", None, None, None),
    b"string": (0, b"string", None, None, None),
    b"template": (0, b"template", None, None, None),
    b"end": (0, None, None, None, None),
}


def tokenize(program, start, end, term=None):
    """Parse a template expression into a stream of tokens, which must end
    with term if specified"""
    pos = start
    program = pycompat.bytestr(program)
    while pos < end:
        c = program[pos]
        if c.isspace():  # skip inter-token whitespace
            pass
        elif c in b"(=,).%|+-*/":  # handle simple operators
            yield (c, None, pos)
        elif c in b'"\'':  # handle quoted templates
            s = pos + 1
            data, pos = _parsetemplate(program, s, end, c)
            yield (b'template', data, s)
            pos -= 1
        elif c == b'r' and program[pos : pos + 2] in (b"r'", b'r"'):
            # handle quoted strings
            c = program[pos + 1]
            s = pos = pos + 2
            while pos < end:  # find closing quote
                d = program[pos]
     