예제 #1
0
def pairs(list):
    for i in range(len(list) - 1):
        yield (list[i], list[i+1])


@library.register
def add(*args):
    try:
        return reduce(lambda x, y: x + y, args)
    except TypeError:
        return reduce(str.__add__, map(str, args), '')

operation = lambda op: lambda *args: reduce(op, args)
chain = lambda op: lambda *args: all(op(x, y) for (x, y) in pairs(args))

library.register('sub', operation(lambda x, y: x - y))
library.register('mul', operation(lambda x, y: x * y))
library.register('div', operation(lambda x, y: x / y))
library.register('mod', operation(lambda x, y: x % y))

library.register('eq', chain(lambda x, y: x == y))
library.register('gt', chain(lambda x, y: x > y))
library.register('lt', chain(lambda x, y: x < y))
library.register('neq', chain(lambda x, y: x != y))
library.register('gte', chain(lambda x, y: x >= y))
library.register('lte', chain(lambda x, y: x <= y))

library.register('not', lambda x: not(x))
library.register('and', lambda *args: all(args))
library.register('or', lambda *args: any(args))
예제 #2
0
    def setup(self):
        self.close = True


def input(type):
    class Input(AutoCloser):
        arguments = ("value",)

        def setup(self):
            self.attributes.setdefault("type", []).append(type)
            self.tag = "input"

    return Input


library.register("realbutton", remap("button"))
library.register("submit", input("submit"))
library.register("button", input("button"))
library.register("password", input("password"))
library.register("hidden", input("hidden"))
library.register("text", input("text"))
library.register("radio", input("radio"))
library.register("checkbox", input("checkbox"))
for tag in "input", "meta", "link", "br", "hr":
    library.register(tag, AutoCloser)


@library.register("a")
class A(Element):
    arguments = ("href",)
예제 #3
0
    return text.render(**kwargs) or mark_safe(
        '"<a href="{0}">{1}</a>"'.format(
            reverse("admin:cmdjango_{0}_change".format(text.__class__.__name__.lower()), args=[text.pk]), _name
        )
    )


@library.register("")
@give_cm
def tag(*contents, **kwargs):
    contents = [c if i % 2 else "{{%{0}%}}".format(c) for (i, c) in enumerate(contents)]
    cm, compress = kwargs.pop("cm"), kwargs.get("c", False)
    return cm.join(contents, compress)


@library.register
@give_cm
def doctype(type="HTML", silent=False, cm=None):
    from cm.functions import html

    doctype, filetype = html.parse_doctype(type)
    load = "{%load filetype%}"
    set = '{{%filetype "{0}"%}}'.format(filetype)
    return cm.join((load, set, html.doctype(type, silent, cm)))


library.register("get", functions.get, evaluate=False)
library.register("gethard", functions.gethard)
library.register("val", lambda contents: "{{%s}}" % contents)
library.register("url", lambda object: "{{" + object + ".get_absolute_url}}")