Beispiel #1
0
def compile_template(line):
    """
	Compile a template expression into a python function (like jsps, but way shorter)
	"""
    extr = []

    def repl(match):
        g = match.group
        if g('dollar'):
            return "$"
        elif g('backslash'):
            return "\\"
        elif g('subst'):
            extr.append(g('code'))
            return "<<|@|>>"
        return None

    line2 = reg_act.sub(repl, line)
    params = line2.split('<<|@|>>')
    assert (extr)

    indent = 0
    buf = []
    app = buf.append

    def app(txt):
        buf.append(indent * '\t' + txt)

    for x in range(len(extr)):
        if params[x]:
            app("lst.append(%r)" % params[x])

        f = extr[x]
        if f.startswith(('if', 'for')):
            app(f + ':')
            indent += 1
        elif f.startswith('py:'):
            app(f[3:])
        elif f.startswith(('endif', 'endfor')):
            indent -= 1
        elif f.startswith(('else', 'elif')):
            indent -= 1
            app(f + ':')
            indent += 1
        elif f.startswith('xml:'):
            app('lst.append(xml_escape(%s))' % f[4:])
        else:
            #app('lst.append((%s) or "cannot find %s")' % (f, f))
            app('lst.append(%s)' % f)

    if extr:
        if params[-1]:
            app("lst.append(%r)" % params[-1])

    fun = COMPILE_TEMPLATE % "\n\t".join(buf)
    #print(fun)
    return Task.funex(fun)
Beispiel #2
0
def compile_template(line):
	"""
	Compile a template expression into a python function (like jsps, but way shorter)
	"""
	extr = []
	def repl(match):
		g = match.group
		if g('dollar'): return "$"
		elif g('backslash'):
			return "\\"
		elif g('subst'):
			extr.append(g('code'))
			return "<<|@|>>"
		return None

	line2 = reg_act.sub(repl, line)
	params = line2.split('<<|@|>>')
	assert(extr)


	indent = 0
	buf = []
	dvars = []
	app = buf.append

	def app(txt):
		buf.append(indent * '\t' + txt)

	for x in range(len(extr)):
		if params[x]:
			app("lst.append(%r)" % params[x])

		f = extr[x]
		if f.startswith('if') or f.startswith('for'):
			app(f + ':')
			indent += 1
		elif f.startswith('py:'):
			app(f[3:])
		elif f.startswith('endif') or f.startswith('endfor'):
			indent -= 1
		elif f.startswith('else') or f.startswith('elif'):
			indent -= 1
			app(f + ':')
			indent += 1
		elif f.startswith('xml:'):
			app('lst.append(xml_escape(%s))' % f[4:])
		else:
			#app('lst.append((%s) or "cannot find %s")' % (f, f))
			app('lst.append(%s)' % f)

	if extr:
		if params[-1]:
			app("lst.append(%r)" % params[-1])

	fun = COMPILE_TEMPLATE % "\n\t".join(buf)
	#print(fun)
	return Task.funex(fun)
Beispiel #3
0
def compile_template(line):
    """
    Compile a template expression into a python function (like jsps, but way shorter)
    """
    extr = []

    def repl(match):
        g = match.group
        if g("dollar"):
            return "$"
        elif g("backslash"):
            return "\\"
        elif g("subst"):
            extr.append(g("code"))
            return "<<|@|>>"
        return None

    line2 = reg_act.sub(repl, line)
    params = line2.split("<<|@|>>")
    assert extr

    indent = 0
    buf = []
    app = buf.append

    def app(txt):
        buf.append(indent * "\t" + txt)

    for x in range(len(extr)):
        if params[x]:
            app("lst.append(%r)" % params[x])

        f = extr[x]
        if f.startswith(("if", "for")):
            app(f + ":")
            indent += 1
        elif f.startswith("py:"):
            app(f[3:])
        elif f.startswith(("endif", "endfor")):
            indent -= 1
        elif f.startswith(("else", "elif")):
            indent -= 1
            app(f + ":")
            indent += 1
        elif f.startswith("xml:"):
            app("lst.append(xml_escape(%s))" % f[4:])
        else:
            # app('lst.append((%s) or "cannot find %s")' % (f, f))
            app("lst.append(%s)" % f)

    if extr:
        if params[-1]:
            app("lst.append(%r)" % params[-1])

    fun = COMPILE_TEMPLATE % "\n\t".join(buf)
    # print(fun)
    return Task.funex(fun)
Beispiel #4
0
def compile_template(line):
    """
        Compile a template expression into a python function (like jsps, but way shorter)
        """
    extr = []

    def repl(match):
        g = match.group
        if g("dollar"):
            return "$"
        elif g("backslash"):
            return "\\"
        elif g("subst"):
            extr.append(g("code"))
            return "<<|@|>>"
        return None

    line2 = reg_act.sub(repl, line)
    params = line2.split("<<|@|>>")
    assert extr

    indent = 0
    buf = []
    app = buf.append

    def app(txt):
        buf.append(indent * "\t" + txt)

    for x in range(len(extr)):
        if params[x]:
            app("lst.append(%r)" % params[x])

        f = extr[x]
        if f.startswith("if") or f.startswith("for"):
            app(f + ":")
            indent += 1
        elif f.startswith("py:"):
            app(f[3:])
        elif f.startswith("endif") or f.startswith("endfor"):
            indent -= 1
        elif f.startswith("else") or f.startswith("elif"):
            indent -= 1
            app(f + ":")
            indent += 1
        elif f.startswith("xml:"):
            app("lst.append(xml_escape(%s))" % f[4:])
        else:
            # app('lst.append((%s) or "cannot find %s")' % (f, f))
            app("lst.append(%s)" % f)

    if extr:
        if params[-1]:
            app("lst.append(%r)" % params[-1])

    fun = COMPILE_TEMPLATE % "\n\t".join(buf)
    # print(fun)
    return Task.funex(fun)
Beispiel #5
0
def compile_template(line):
	extr=[]
	def repl(match):
		g=match.group
		if g('dollar'):return"$"
		elif g('backslash'):
			return"\\"
		elif g('subst'):
			extr.append(g('code'))
			return"<<|@|>>"
		return None
	line2=reg_act.sub(repl,line)
	params=line2.split('<<|@|>>')
	assert(extr)
	indent=0
	buf=[]
	app=buf.append
	def app(txt):
		buf.append(indent*'\t'+txt)
	for x in range(len(extr)):
		if params[x]:
			app("lst.append(%r)"%params[x])
		f=extr[x]
		if f.startswith('if')or f.startswith('for'):
			app(f+':')
			indent+=1
		elif f.startswith('py:'):
			app(f[3:])
		elif f.startswith('endif')or f.startswith('endfor'):
			indent-=1
		elif f.startswith('else')or f.startswith('elif'):
			indent-=1
			app(f+':')
			indent+=1
		elif f.startswith('xml:'):
			app('lst.append(xml_escape(%s))'%f[4:])
		else:
			app('lst.append(%s)'%f)
	if extr:
		if params[-1]:
			app("lst.append(%r)"%params[-1])
	fun=COMPILE_TEMPLATE%"\n\t".join(buf)
	return Task.funex(fun)