<s> def jsmin ( js ) : <EOL> from StringIO import StringIO <EOL> ins = StringIO ( js ) <EOL> outs = StringIO ( ) <EOL> JavascriptMinify ( ) . minify ( ins , outs ) <EOL> str = outs . getvalue ( ) <EOL> if len ( str ) > <NUM_LIT:0> and str [ <NUM_LIT:0> ] == '<STR_LIT:\n>' : <EOL> str = str [ <NUM_LIT:1> : ] <EOL> return str <EOL> def isAlphanum ( c ) : <EOL> """<STR_LIT>""" <EOL> return ( ( c >= '<STR_LIT:a>' and c <= '<STR_LIT:z>' ) or ( c >= '<STR_LIT:0>' and c <= '<STR_LIT>' ) or <EOL> ( c >= '<STR_LIT:A>' and c <= '<STR_LIT>' ) or c == '<STR_LIT:_>' or c == '<STR_LIT:$>' or c == '<STR_LIT:\\>' or <EOL> ( c is not None and ord ( c ) > <NUM_LIT> ) ) <EOL> class UnterminatedComment ( Exception ) : <EOL> pass <EOL> class UnterminatedStringLiteral ( Exception ) : <EOL> pass <EOL> class UnterminatedRegularExpression ( Exception ) : <EOL> pass <EOL> class JavascriptMinify ( object ) : <EOL> def _outA ( self ) : <EOL> self . outstream . write ( self . theA ) <EOL> def _outB ( self ) : <EOL> self . outstream . write ( self . theB ) <EOL> def _get ( self ) : <EOL> """<STR_LIT>""" <EOL> c = self . theLookahead <EOL> self . theLookahead = None <EOL> if c == None : <EOL> c = self . instream . read ( <NUM_LIT:1> ) <EOL> if c >= '<STR_LIT:U+0020>' or c == '<STR_LIT:\n>' : <EOL> return c <EOL> if c == '<STR_LIT>' : <EOL> return '<STR_LIT>' <EOL> if c == '<STR_LIT:\r>' : <EOL> return '<STR_LIT:\n>' <EOL> return '<STR_LIT:U+0020>' <EOL> def _peek ( self ) : <EOL> self . theLookahead = self . _get ( ) <EOL> return self . theLookahead <EOL> def _next ( self ) : <EOL> """<STR_LIT>""" <EOL> c = self . _get ( ) <EOL> if c == '<STR_LIT:/>' and self . theA != '<STR_LIT:\\>' : <EOL> p = self . _peek ( ) <EOL> if p == '<STR_LIT:/>' : <EOL> c = self . _get ( ) <EOL> while c > '<STR_LIT:\n>' : <EOL> c = self . _get ( ) <EOL> return c <EOL> if p == '<STR_LIT:*>' : <EOL> c = self . _get ( ) <EOL> while <NUM_LIT:1> : <EOL> c = self . _get ( ) <EOL> if c == '<STR_LIT:*>' : <EOL> if self . _peek ( ) == '<STR_LIT:/>' : <EOL> self . _get ( ) <EOL> return '<STR_LIT:U+0020>' <EOL> if c == '<STR_LIT>' : <EOL> raise UnterminatedComment ( ) <EOL> return c <EOL> def _action ( self , action ) : <EOL> """<STR_LIT>""" <EOL> if action <= <NUM_LIT:1> : <EOL> self . _outA ( ) <EOL> if action <= <NUM_LIT:2> : <EOL> self . theA = self . theB <EOL> if self . theA == "<STR_LIT:'>" or self . theA == '<STR_LIT:">' : <EOL> while <NUM_LIT:1> : <EOL> self . _outA ( ) <EOL> self . theA = self . _get ( ) <EOL> if self . theA == self . theB : <EOL> break <EOL> if self . theA <= '<STR_LIT:\n>' : <EOL> raise UnterminatedStringLiteral ( ) <EOL> if self . theA == '<STR_LIT:\\>' : <EOL> self . _outA ( ) <EOL> self . theA = self . _get ( ) <EOL> if action <= <NUM_LIT:3> : <EOL> self . theB = self . _next ( ) <EOL> if self . theB == '<STR_LIT:/>' and ( self . theA == '<STR_LIT:(>' or self . theA == '<STR_LIT:U+002C>' or <EOL> self . theA == '<STR_LIT:=>' or self . theA == '<STR_LIT::>' or <EOL> self . theA == '<STR_LIT:[>' or self . theA == '<STR_LIT:?>' or <EOL> self . theA == '<STR_LIT:!>' or self . theA == '<STR_LIT:&>' or <EOL> self . theA == '<STR_LIT:|>' or self . theA == '<STR_LIT:;>' or <EOL> self . theA == '<STR_LIT:{>' or self . theA == '<STR_LIT:}>' or <EOL> self . theA == '<STR_LIT:\n>' ) : <EOL> self . _outA ( ) <EOL> self . _outB ( ) <EOL> while <NUM_LIT:1> : <EOL> self . theA = self . _get ( ) <EOL> if self . theA == '<STR_LIT:/>' : <EOL> break <EOL> elif self . theA == '<STR_LIT:\\>' : <EOL> self . _outA ( ) <EOL> self . theA = self . _get ( ) <EOL> elif self . theA <= '<STR_LIT:\n>' : <EOL> raise UnterminatedRegularExpression ( ) <EOL> self . _outA ( ) <EOL> self . theB = self . _next ( ) <EOL> def _jsmin ( self ) : <EOL> """<STR_LIT>""" <EOL> self . theA = '<STR_LIT:\n>' <EOL> self . _action ( <NUM_LIT:3> ) <EOL> while self . theA != '<STR_LIT>' : <EOL> if self . theA == '<STR_LIT:U+0020>' : <EOL> if isAlphanum ( self . theB ) : <EOL> self . _action ( <NUM_LIT:1> ) <EOL> else : <EOL> self . _action ( <NUM_LIT:2> ) <EOL> elif self . theA == '<STR_LIT:\n>' : <EOL> if self . theB in [ '<STR_LIT:{>' , '<STR_LIT:[>' , '<STR_LIT:(>' , '<STR_LIT:+>' , '<STR_LIT:->' ] : <EOL> self . _action ( <NUM_LIT:1> ) <EOL> elif self . theB == '<STR_LIT:U+0020>' : <EOL> self . _action ( <NUM_LIT:3> ) <EOL> else : <EOL> if isAlphanum ( self . theB ) : <EOL> self . _action ( <NUM_LIT:1> ) <EOL> else : <EOL> self . _action ( <NUM_LIT:2> ) <EOL> else : <EOL> if self . theB == '<STR_LIT:U+0020>' : <EOL> if isAlphanum ( self . theA ) : <EOL> self . _action ( <NUM_LIT:1> ) <EOL> else : <EOL> self . _action ( <NUM_LIT:3> ) <EOL> elif self . theB == '<STR_LIT:\n>' : <EOL> if self . theA in [ '<STR_LIT:}>' , '<STR_LIT:]>' , '<STR_LIT:)>' , '<STR_LIT:+>' , '<STR_LIT:->' , '<STR_LIT:">' , '<STR_LIT>' ] : <EOL> self . _action ( <NUM_LIT:1> ) <EOL> else : <EOL> if isAlphanum ( self . theA ) : <EOL> self . _action ( <NUM_LIT:1> ) <EOL> else : <EOL> self . _action ( <NUM_LIT:3> ) <EOL> else : <EOL> self . _action ( <NUM_LIT:1> ) <EOL> def minify ( self , instream , outstream ) : <EOL> self . instream = instream <EOL> self . outstream = outstream <EOL> self . theA = '<STR_LIT:\n>' <EOL> self . theB = None <EOL> self . theLookahead = None <EOL> self . _jsmin ( ) <EOL> self . instream . close ( ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> import sys <EOL> jsm = JavascriptMinify ( ) <EOL> jsm . minify ( sys . stdin , sys . stdout ) </s>
