Back to index

plone3  3.1.7
Public Member Functions | Public Attributes
ResourceRegistries.tools.packer.JavascriptPacker Class Reference
Inheritance diagram for ResourceRegistries.tools.packer.JavascriptPacker:
Inheritance graph
[legend]
Collaboration diagram for ResourceRegistries.tools.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 276 of file packer.py.


Constructor & Destructor Documentation

def ResourceRegistries.tools.packer.JavascriptPacker.__init__ (   self,
  level = 'safe' 
)

Definition at line 277 of file packer.py.

00277 
00278     def __init__(self, level='safe'):
00279         Packer.__init__(self)
00280         # protect strings
00281         # these sometimes catch to much, but in safe mode this doesn't hurt
00282         
00283         # the parts:
00284         # match a single quote
00285         # match anything but the single quote, a backslash and a newline "[^'\\\n]"
00286         # or match a null escape (\0 not followed by another digit) "\\0(?![0-9])"
00287         # or match a character escape (no newline) "\\[^\n]"
00288         # do this until there is another single quote "(?:<snip>)*?'"
00289         # all this return one group "(<snip>)"
00290         self.protect(r"""('(?:[^'\\\n]|\\0(?![0-9])|\\x[0-9a-fA-F]{2}|\\u[0-9a-fA-F]{4}|\\[^\n])*?'|"""
00291                      r""""(?:[^"\\\n]|\\0(?![0-9])|\\x[0-9a-fA-F]{2}|\\u[0-9a-fA-F]{4}|\\[^\n])*?")""")
00292 
00293         # protect regular expressions
00294         self.protect(r"""\s+(\/[^\/\n\r\*](?:\\/|[^\n\r])*\/g?i?)""")
00295         self.protect(r"""([^\w\$\/'"*)\?:]\/[^\/\n\r\*](?:\\/|[^\n\r])*\/g?i?)""")
00296 
00297         # protect IE conditional compilation
00298         self.protect(r'(/\*@.*?(?:\*/|\n|\*/(?!\n)))', re.DOTALL)
00299 
00300         # remove one line comments
00301         self.sub(r'\s*//.*$', '', re.MULTILINE)
00302         # remove multiline comments
00303         self.sub(r'/\*.*?\*/', '', re.DOTALL)
00304 
00305         if level == 'full':
00306             # encode local variables. those are preceeded by dollar signs
00307             # the amount of dollar signs says how many characters are preserved
00308             # any trailing digits are preserved as well
00309             # $name -> n, $$name -> na, $top1 -> t1, $top2 -> t2
00310             def _dollar_replacement(match):
00311                 length = len(match.group(2))
00312                 start = length - max(length - len(match.group(3)), 0)
00313                 result = match.group(1)[start:start+length] + match.group(4)
00314                 return result
00315             self.sub(r"""((\$+)([a-zA-Z\$_]+))(\d*)\b""", _dollar_replacement)
00316             
00317             self.keywordSub(r"""(\b_[A-Za-z\d]\w+)""", lambda i: "_%i" % i)
00318 
00319         # strip whitespace at the beginning and end of each line
00320         self.sub(r'^[ \t\r\f\v]*(.*?)[ \t\r\f\v]*$', r'\1', re.MULTILINE)
00321         # whitespace after some special chars but not
00322         # before function declaration
00323         self.sub(r'([{;\[(,=&|\?:<>%!/])\s+(?!function)', r'\1')
00324         # after an equal sign a function definition is ok
00325         self.sub(r'=\s+(?=function)', r'=')
00326         if level == 'full':
00327             # whitespace after some more special chars
00328             self.sub(r'([};\):,])\s+', r'\1')
00329         # whitespace before some special chars
00330         self.sub(r'\s+([={},&|\?:\.()<>%!/\]])', r'\1')
00331         # whitespace before plus chars if no other plus char before it
00332         self.sub(r'(?<!\+)\s+\+', '+')
00333         # whitespace after plus chars if no other plus char after it
00334         self.sub(r'\+\s+(?!\+)', '+')
00335         # whitespace before minus chars if no other minus char before it
00336         self.sub(r'(?<!-)\s+-', '-')
00337         # whitespace after minus chars if no other minus char after it
00338         self.sub(r'-\s+(?!-)', '-')
00339         # remove redundant semi-colons
00340         self.sub(r';+\s*([};])', r'\1')
00341         # remove any excessive whitespace left except newlines
00342         self.sub(r'[ \t\r\f\v]+', ' ')
00343         # excessive newlines
00344         self.sub(r'\n+', '\n')
00345         # first newline
00346         self.sub(r'^\n', '')
00347 

Here is the call graph for this function:


Member Function Documentation

def ResourceRegistries.tools.packer.Packer.copy (   self) [inherited]

Definition at line 218 of file packer.py.

00218 
00219     def copy(self):
00220         result = Packer()
00221         result.patterns = self.patterns[:]
00222         return result

def ResourceRegistries.tools.packer.Packer.keywordSub (   self,
  pattern,
  keyword_encoder,
  flags = None 
) [inherited]

Definition at line 269 of file packer.py.

00269 
00270     def keywordSub(self, pattern, keyword_encoder, flags=None):
00271         if flags is None:
00272             self.patterns.append((re.compile(pattern), None, keyword_encoder))
00273         else:
00274             self.patterns.append((re.compile(pattern, flags), None, keyword_encoder))
00275 

Here is the caller graph for this function:

def ResourceRegistries.tools.packer.Packer.pack (   self,
  input 
) [inherited]

Definition at line 229 of file packer.py.

00229 
00230     def pack(self, input):
00231         # list of protected parts
00232         self.replacelist = []
00233         output = input
00234         for regexp, replacement, keyword_encoder in self.patterns:
00235             if replacement is None:
00236                 if keyword_encoder is None:
00237                     # protect the matched parts
00238                     output = regexp.sub(self._repl, output)
00239                 else:
00240                     mapper = KeywordMapper(regexp=regexp,
00241                                            encoder=keyword_encoder)
00242                     # get keywords
00243                     mapper.analyse(output)
00244                     # replace keywords
00245                     output = mapper.sub(output)
00246             else:
00247                 # substitute
00248                 output = regexp.sub(replacement, output)
00249         # restore protected parts
00250         replacelist = list(enumerate(self.replacelist))
00251         replacelist.reverse() # from back to front, so 1 doesn't break 10 etc.
00252         for index, replacement in replacelist:
00253             # we use lambda in here, so the real string is used and no escaping
00254             # is done on it
00255             before = len(output)
00256             regexp = re.compile('\x00%i\x00' % (index+1))
00257             output = regexp.sub(lambda m:replacement, output)
00258         # done
00259         return output

def ResourceRegistries.tools.packer.Packer.protect (   self,
  pattern,
  flags = None 
) [inherited]

Definition at line 260 of file packer.py.

00260 
00261     def protect(self, pattern, flags=None):
00262         self.keywordSub(pattern, None, flags)

Here is the call graph for this function:

Here is the caller graph for this function:

def ResourceRegistries.tools.packer.Packer.sub (   self,
  pattern,
  replacement,
  flags = None 
) [inherited]

Definition at line 263 of file packer.py.

00263 
00264     def sub(self, pattern, replacement, flags=None):
00265         if flags is None:
00266             self.patterns.append((re.compile(pattern), replacement, None))
00267         else:
00268             self.patterns.append((re.compile(pattern, flags), replacement, None))

Here is the caller graph for this function:


Member Data Documentation

Definition at line 216 of file packer.py.

Definition at line 231 of file packer.py.


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