from cm.library import Library library = Library() 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))
from cm.library import Library from utilities import indent library = Library() def closer(tag, attributes, settings): return Element(tag, (), attributes, "", close=True, settings=settings) class Element: arguments = () def __init__(self, tag, args=(), attributes={}, body="", close=False, strip=False, oneline=False, settings=None): self.tag, self.args, self.attributes = tag, args, attributes self.body, self.close, self.strip = body, close, strip self.oneline, self.settings = oneline, settings self.args = self.map_args(*self.arguments) self.setup() self.render() def setup(self): pass def render(self): if self.args: raise TypeError("Element %s can not handle arguments %s" % (self.tag, self.args)) if self.close and self.body.strip(): raise ValueError("This %s element can not have a body." % self.tag) attrs = self.render_attributes() body = self.body.strip() if self.strip else self.body
from django.core.urlresolvers import reverse from django.utils.safestring import mark_safe from cm.library import Library, give_cm from cm import functions library = Library() def gettext(type, _name, _default="", **kwargs): text, created = type.objects.get_or_create(name=_name) text.content = text.content or _default text.save() 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