Back to index

plone3  3.1.7
Public Member Functions | Public Attributes
kss.core.pluginregistry._concatresource.compression.thirdparty.packer.JavascriptPacker Class Reference
Inheritance diagram for kss.core.pluginregistry._concatresource.compression.thirdparty.packer.JavascriptPacker:
Inheritance graph
[legend]
Collaboration diagram for kss.core.pluginregistry._concatresource.compression.thirdparty.packer.JavascriptPacker:
Collaboration graph
[legend]

List of all members.

Public Member Functions

def __init__
def copy
def pack
def protect
def sub
def keywordSub

Public Attributes

 patterns
 replacelist

Detailed Description

Definition at line 299 of file packer.py.


Constructor & Destructor Documentation

Definition at line 300 of file packer.py.

00300 
00301     def __init__(self, level='safe'):
00302         Packer.__init__(self)
00303         if level == 'full':
00304             # encode local variables. those are preceeded by dollar signs
00305             # the amount of dollar signs says how many characters are preserved
00306             # any trailing digits are preserved as well
00307             # $name -> n, $$name -> na, $top1 -> t1, $top2 -> t2
00308             def _dollar_replacement(match):
00309                 length = len(match.group(2))
00310                 start = length - max(length - len(match.group(3)), 0)
00311                 result = match.group(1)[start:start+length] + match.group(4)
00312                 return result
00313             self.sub(r"""((\$+)([a-zA-Z\$_]+))(\d*)\b""", _dollar_replacement)
00314             
00315             self.keywordSub(r"""\b_[A-Za-z\d]\w*""", lambda i: "_%i" % i)
00316     
00317             # protect strings
00318             # this is more correct, but needs more testing
00319             # it has to be more accurate because of the more aggresive packing later
00320             self.protect(r"""(?<=return|..case|.....[=\[|(,?:+])\s*((?P<quote>['"])(?:\\(?P=quote)|\\\n|.)*?(?P=quote))""", re.DOTALL)
00321         else:
00322             # protect strings
00323             # these sometimes catch to much, but in safe mode this doesn't hurt
00324             self.protect(r"""('(?:\\'|\\\n|.)*?')""")
00325             self.protect(r'''("(?:\\"|\\\n|.)*?")''')
00326         # protect regular expressions
00327         self.protect(r"""\s+(\/[^\/\n\r\*][^\/\n\r]*\/g?i?)""")
00328         self.protect(r"""([^\w\$\/'"*)\?:]\/[^\/\n\r\*][^\/\n\r]*\/g?i?)""")
00329         # multiline comments
00330         self.sub(r'/\*(?!@).*?\*/', '', re.DOTALL)
00331         # one line comments
00332         self.sub(r'\s*//.*$', '', re.MULTILINE)
00333         # strip whitespace at the beginning and end of each line
00334         self.sub(r'^[ \t\r\f\v]*(.*?)[ \t\r\f\v]*$', r'\1', re.MULTILINE)
00335         # whitespace after some special chars but not
00336         # before function declaration
00337         self.sub(r'([{;\[(,=&|\?:<>%!/])\s+(?!function)', r'\1')
00338         # after an equal sign a function definition is ok
00339         self.sub(r'=\s+(?=function)', r'=')
00340         if level == 'full':
00341             # whitespace after some more special chars
00342             self.sub(r'([};\):,])\s+', r'\1')
00343         # whitespace before some special chars
00344         self.sub(r'\s+([={},&|\?:\.()<>%!/\]])', r'\1')
00345         # whitespace before plus chars if no other plus char before it
00346         self.sub(r'(?<!\+)\s+\+', '+')
00347         # whitespace after plus chars if no other plus char after it
00348         self.sub(r'\+\s+(?!\+)', '+')
00349         # whitespace before minus chars if no other minus char before it
00350         self.sub(r'(?<!-)\s+-', '-')
00351         # whitespace after minus chars if no other minus char after it
00352         self.sub(r'-\s+(?!-)', '-')
00353         # remove redundant semi-colons
00354         self.sub(r';+\s*([};])', r'\1')
00355         # remove any excessive whitespace left except newlines
00356         self.sub(r'[ \t\r\f\v]+', ' ')
00357         # excessive newlines
00358         self.sub(r'\n+', '\n')
00359         # first newline
00360         self.sub(r'^\n', '')
00361 

Here is the call graph for this function:

Here is the caller graph for this function:


Member Function Documentation

Definition at line 238 of file packer.py.

00238 
00239     def copy(self):
00240         result = Packer()
00241         result.patterns = self.patterns[:]
00242         return result

Here is the caller graph for this function:

def kss.core.pluginregistry._concatresource.compression.thirdparty.packer.Packer.keywordSub (   self,
  pattern,
  keyword_encoder,
  flags = None 
) [inherited]

Definition at line 292 of file packer.py.

00292 
00293     def keywordSub(self, pattern, keyword_encoder, flags=None):
00294         if flags is None:
00295             self.patterns.append((re.compile(pattern), None, keyword_encoder))
00296         else:
00297             self.patterns.append((re.compile(pattern, flags), None, keyword_encoder))
00298 

Here is the caller graph for this function:

Definition at line 249 of file packer.py.

00249 
00250     def pack(self, input):
00251         # list of protected parts
00252         self.replacelist = []
00253         # escape the escapechar
00254         output = input.replace('\x00','\x00\x00')
00255         for regexp, replacement, keyword_encoder in self.patterns:
00256             if replacement is None:
00257                 if keyword_encoder is None:
00258                     # protect the matched parts
00259                     output = regexp.sub(self._repl, output)
00260                 else:
00261                     mapper = KeywordMapper(regexp=regexp,
00262                                            encoder=keyword_encoder)
00263                     # get keywords
00264                     mapper.analyse(output)
00265                     # replace keywords
00266                     output = mapper.sub(output)
00267             else:
00268                 # substitute
00269                 output = regexp.sub(replacement, output)
00270         # restore protected parts
00271         replacelist = list(enumerate(self.replacelist))
00272         replacelist.reverse() # from back to front, so 1 doesn't break 10 etc.
00273         for index, replacement in replacelist:
00274             # we use lambda in here, so the real string is used and no escaping
00275             # is done on it
00276             before = len(output)
00277             regexp = re.compile('(?<!\x00)\x00%i' % (index+1))
00278             output = regexp.sub(lambda m:replacement, output)
00279         # unescape
00280         output = output.replace('\x00\x00','\x00')
00281         # done
00282         return output

def kss.core.pluginregistry._concatresource.compression.thirdparty.packer.Packer.protect (   self,
  pattern,
  flags = None 
) [inherited]

Definition at line 283 of file packer.py.

00283 
00284     def protect(self, pattern, flags=None):
00285         self.keywordSub(pattern, None, flags)

Here is the call graph for this function:

Here is the caller graph for this function:

def kss.core.pluginregistry._concatresource.compression.thirdparty.packer.Packer.sub (   self,
  pattern,
  replacement,
  flags = None 
) [inherited]

Definition at line 286 of file packer.py.

00286 
00287     def sub(self, pattern, replacement, flags=None):
00288         if flags is None:
00289             self.patterns.append((re.compile(pattern), replacement, None))
00290         else:
00291             self.patterns.append((re.compile(pattern, flags), replacement, None))

Here is the caller graph for this function:


Member Data Documentation

Definition at line 236 of file packer.py.

Definition at line 251 of file packer.py.


The documentation for this class was generated from the following file: