Back to index

plone3  3.1.7
javascript.py
Go to the documentation of this file.
00001 '''\
00002 The javascript compressor uses the 3rdparty packer module
00003 that is taken from Plone's ResourceRegistries.'''
00004 
00005 from thirdparty.packer import JavascriptPacker
00006 # Packer needs to be created for each packing
00007 
00008 def stripout_comments(data):
00009     'Strips out ;;; lines from the data.'
00010     result = []
00011     for line in data.splitlines(True):
00012         if not line.lstrip().startswith(';;;'):
00013             result.append(line)
00014     return ''.join(result)
00015 
00016 def remove_markers(data):
00017     'Replaces the ;;; markers by spaces but leaves the lines.'
00018     result = []
00019     for line in data.splitlines(True):
00020         if line.lstrip().startswith(';;;'):
00021             line = line.lstrip()[3:]
00022             result.append('   ')
00023         result.append(line)
00024     return ''.join(result)
00025 
00026 def compress(data, compress_level):
00027     if compress_level == "devel":
00028         return remove_markers(data)
00029     elif compress_level == "stripped":
00030         return stripout_comments(data)
00031     elif compress_level == "safe":
00032         data = stripout_comments(data)
00033         jspacker_safe = JavascriptPacker('safe')
00034         return jspacker_safe.pack(data)
00035     elif compress_level == "full":
00036         data = stripout_comments(data)
00037         jspacker_full = JavascriptPacker('full')
00038         return jspacker_full.pack(data)
00039     elif compress_level == "safe-devel":
00040         date = remove_markers(data)
00041         jspacker_safe = JavascriptPacker('safe')
00042         return jspacker_safe.pack(data)
00043     elif compress_level == "full-devel":
00044         date = remove_markers(data)
00045         jspacker_full = JavascriptPacker('full')
00046         return jspacker_full.pack(data)
00047     else:
00048         # none
00049         return data