Login

packjs templatetag

Author:
kyprizel
Posted:
December 25, 2007
Language:
Python
Version:
.96
Score:
1 (after 1 ratings)
Based on jspacker by Dean Edwards,
Python port by Florian Schulze: http://www.crowproductions.de/repos/main/public/packer/jspacker.py
Packs javascript

Example usage::

    {% packjs %}
    var a = 1;
    var b = 2;
    var c = 3;
    alert(a+b);
    {% endpackjs %}

This example would return this script::

eval(function(p,a,c,k,e,d){e=function(c){return(c<a?"":e(parseInt(c/a)))+((c=c%a)>35?String.fromCharCode(c+29):c.toString(36))};if(!''.replace(/^/,String)){while(c--)d[c]=k[c]||c;k=[function(e){return d[e]}];e=function(){return'\\w+'};c=1};while(c--)if(k[c])p=p.replace(new RegExp("\\b"+e(c)+"\\b","g"),k[c]);return p}('0 5 = 1;\n      0 4 = 2;\n      0 7 = 3;\n      6(5+4);\n',8,8,'var||||b|a|alert|c'.split('|'),0,{}))
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
class PackedJSNode(Node):
    def __init__(self, nodelist):
        self.nodelist = nodelist

    def render(self, context):
	from jspacker import JavaScriptPacker
	p = JavaScriptPacker()
	script = self.nodelist.render(context).strip()
	#ugly test on empty script, no reason to pack such small scripts
	if len(script) < 20:
	    return script
	packed = p.pack(script, compaction=False, encoding=62, fastDecode=True)
        return packed

def packjs(parser, token):
    """
    Based on jspacker by Dean Edwards,
    Python port by Florian Schulze: http://www.crowproductions.de/repos/main/public/packer/jspacker.py
    Packs javascript

    Example usage::

        {% packjs %}
	    var a = 1;
	    var b = 2;
	    var c = 3;
	    alert(a+b);
        {% endpackjs %}

    This example would return this script::

	eval(function(p,a,c,k,e,d){e=function(c){return(c<a?"":e(parseInt(c/a)))+((c=c%a)>35?String.fromCharCode(c+29):c.toString(36))};if(!''.replace(/^/,String)){while(c--)d[c]=k[c]||c;k=[function(e){return d[e]}];e=function(){return'\\w+'};c=1};while(c--)if(k[c])p=p.replace(new RegExp("\\b"+e(c)+"\\b","g"),k[c]);return p}('0 5 = 1;\n	    0 4 = 2;\n	    0 7 = 3;\n	    6(5+4);\n',8,8,'var||||b|a|alert|c'.split('|'),0,{}))
    """
    nodelist = parser.parse(('endpackjs',))
    parser.delete_first_token()
    return PackedJSNode(nodelist)

packjs = register.tag(packjs)

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 2 months, 2 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 2 months, 3 weeks ago
  3. Serializer factory with Django Rest Framework by julio 9 months, 2 weeks ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 10 months, 1 week ago
  5. Help text hyperlinks by sa2812 11 months ago

Comments

Please login first before commenting.