Here is a better version:
https://gist.github.com/mdipierro/9459290
On Sunday, 9 March 2014 22:26:34 UTC-5, Massimo Di Pierro wrote:
>
> Until today I had not realized how similar Jinja2 is to web2py templates.
> In fact here is some code to convert the former to the latter:
>
> -------- BEGIN jinja2web2py.py ----------------
> import sys
> import re
>
> code1 = re.compile('\{\{(.*?)\}\}',re.DOTALL)
> extends = re.compile('\{\%\s*extends\s+(.*?)\%\}',re.DOTALL)
> code3 = re.compile('\{\%(.*?)\%\}',re.DOTALL)
> endblock = re.compile('\{\%\s*endblock\s+(.*?)\%\}',re.DOTALL)
> macro = re.compile('\{\%\s*macro\s+(.*?)\%\}',re.DOTALL)
> endmacro = re.compile('\{\%\s*endmacro\s+(.*?)\%\}',re.DOTALL)
> endif = re.compile('\{\%\s*endif\s*\%\}',re.DOTALL)
> endfor = re.compile('\{\%\s*endfor\s*\%\}',re.DOTALL)
> refor_sort =
> re.compile('\{\%\s*for\s+(.*?)\s+in\s+(.*?)\s*\|\s*sort\s*\%\}',re.DOTALL)
> reif = re.compile('\{\%\s*(for .*?|if .*?|elif .*?|else)\s*\%\}',re.DOTALL)
>
>
> def main():
> data = open(sys.argv[1]).read()
> data = code1.sub('{{=\g<1>}}',data)
> data = extends.sub('{{ extends \g<1>}}',data)
> data = endblock.sub('{{ end }}', data)
> data = macro.sub('{{ def g<1>: }}',data)
> data = endmacro.sub('{{ return }}',data)
> data = endif.sub('{{ pass }}',data)
> data = endfor.sub('{{ pass }}',data)
> data = refor_sort.sub('{{ for \g<1> in sorted(\g<2>): }}', data)
> data = reif.sub('{{ \g<1>: }}', data)
> data = code3.sub('{{\g<1>}}',data)
> return data
>
> print main()
> ------------- END ---------------
>
>
> I am sure I am missing some corner cases. Perhaps you can help me make
> this better?
> We cannot make it go both ways because the web2py templates are much more
> rich than jinja2, in fact we support full python syntax.
>
> Massimo
>
--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to the Google Groups
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.