<s> from __future__ import print_function <EOL> import sys <EOL> import re <EOL> from cssbeautifier . __version__ import __version__ <EOL> class BeautifierOptions : <EOL> def __init__ ( self ) : <EOL> self . indent_size = <NUM_LIT:4> <EOL> self . indent_char = '<STR_LIT:U+0020>' <EOL> self . indent_with_tabs = False <EOL> self . selector_separator_newline = True <EOL> self . end_with_newline = False <EOL> self . newline_between_rules = True <EOL> self . eol = '<STR_LIT:\n>' <EOL> def __repr__ ( self ) : <EOL> return """<STR_LIT>""" % ( self . indent_size , self . indent_char , self . indent_with_tabs , <EOL> self . selector_separator_newline , self . end_with_newline , self . newline_between_rules ) <EOL> def default_options ( ) : <EOL> return BeautifierOptions ( ) <EOL> def beautify ( string , opts = default_options ( ) ) : <EOL> b = Beautifier ( string , opts ) <EOL> return b . beautify ( ) <EOL> def beautify_file ( file_name , opts = default_options ( ) ) : <EOL> if file_name == '<STR_LIT:->' : <EOL> stream = sys . stdin <EOL> else : <EOL> stream = open ( file_name ) <EOL> content = '<STR_LIT>' . join ( stream . readlines ( ) ) <EOL> b = Beautifier ( content , opts ) <EOL> return b . beautify ( ) <EOL> def usage ( stream = sys . stdout ) : <EOL> print ( "<STR_LIT>" + __version__ + """<STR_LIT>""" , file = stream ) <EOL> if stream == sys . stderr : <EOL> return <NUM_LIT:1> <EOL> else : <EOL> return <NUM_LIT:0> <EOL> WHITE_RE = re . compile ( "<STR_LIT>" ) <EOL> WORD_RE = re . compile ( "<STR_LIT>" ) <EOL> class Printer : <EOL> def __init__ ( self , indent_char , indent_size , default_indent = "<STR_LIT>" ) : <EOL> self . indentSize = indent_size <EOL> self . singleIndent = ( indent_size ) * indent_char <EOL> self . indentLevel = <NUM_LIT:0> <EOL> self . nestedLevel = <NUM_LIT:0> <EOL> self . baseIndentString = default_indent <EOL> self . output = [ ] <EOL> def __lastCharWhitespace ( self ) : <EOL> return len ( self . output ) > <NUM_LIT:0> and WHITE_RE . search ( self . output [ - <NUM_LIT:1> ] ) is not None <EOL> def indent ( self ) : <EOL> self . indentLevel += <NUM_LIT:1> <EOL> self . baseIndentString += self . singleIndent <EOL> def outdent ( self ) : <EOL> if self . indentLevel : <EOL> self . indentLevel -= <NUM_LIT:1> <EOL> self . baseIndentString = self . baseIndentString [ : - ( len ( self . singleIndent ) ) ] <EOL> def push ( self , string ) : <EOL> self . output . append ( string ) <EOL> def openBracket ( self ) : <EOL> self . singleSpace ( ) <EOL> self . output . append ( "<STR_LIT:{>" ) <EOL> self . newLine ( ) <EOL> def closeBracket ( self ) : <EOL> self . newLine ( ) <EOL> self . output . append ( "<STR_LIT:}>" ) <EOL> self . newLine ( ) <EOL> def semicolon ( self ) : <EOL> self . output . append ( "<STR_LIT:;>" ) <EOL> self . newLine ( ) <EOL> def comment ( self , comment ) : <EOL> self . output . append ( comment ) <EOL> def newLine ( self , keepWhitespace = False ) : <EOL> if len ( self . output ) > <NUM_LIT:0> : <EOL> if not keepWhitespace and self . output [ - <NUM_LIT:1> ] != '<STR_LIT:\n>' : <EOL> self . trim ( ) <EOL> self . output . append ( "<STR_LIT:\n>" ) <EOL> if len ( self . baseIndentString ) > <NUM_LIT:0> : <EOL> self . output . append ( self . baseIndentString ) <EOL> def trim ( self ) : <EOL> while self . __lastCharWhitespace ( ) : <EOL> self . output . pop ( ) <EOL> def singleSpace ( self ) : <EOL> if len ( self . output ) > <NUM_LIT:0> and not self . __lastCharWhitespace ( ) : <EOL> self . output . append ( "<STR_LIT:U+0020>" ) <EOL> def preserveSingleSpace ( self , isAfterSpace ) : <EOL> if isAfterSpace : <EOL> self . singleSpace ( ) <EOL> def result ( self ) : <EOL> if self . baseIndentString : <EOL> return self . baseIndentString + "<STR_LIT>" . join ( self . output ) ; <EOL> else : <EOL> return "<STR_LIT>" . join ( self . output ) <EOL> class Beautifier : <EOL> def __init__ ( self , source_text , opts = default_options ( ) ) : <EOL> self . six = __import__ ( "<STR_LIT>" , globals ( ) , locals ( ) , [ "<STR_LIT:object>" ] , <NUM_LIT:0> ) <EOL> if not source_text : <EOL> source_text = '<STR_LIT>' <EOL> lineBreak = re . compile ( self . six . u ( "<STR_LIT>" ) ) <EOL> source_text = re . sub ( lineBreak , '<STR_LIT:\n>' , source_text ) <EOL> self . source_text = source_text <EOL> self . opts = opts <EOL> self . indentSize = opts . indent_size <EOL> self . indentChar = opts . indent_char <EOL> self . pos = - <NUM_LIT:1> <EOL> self . ch = None <EOL> if self . opts . indent_with_tabs : <EOL> self . indentChar = "<STR_LIT:\t>" <EOL> self . indentSize = <NUM_LIT:1> <EOL> self . opts . eol = self . opts . eol . replace ( '<STR_LIT>' , '<STR_LIT:\r>' ) . replace ( '<STR_LIT>' , '<STR_LIT:\n>' ) <EOL> self . NESTED_AT_RULE = [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] <EOL> self . CONDITIONAL_GROUP_RULE = [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] <EOL> def next ( self ) : <EOL> self . pos = self . pos + <NUM_LIT:1> <EOL> if self . pos < len ( self . source_text ) : <EOL> self . ch = self . source_text [ self . pos ] <EOL> else : <EOL> self . ch = '<STR_LIT>' <EOL> return self . ch <EOL> def peek ( self , skipWhitespace = False ) : <EOL> start = self . pos <EOL> if skipWhitespace : <EOL> self . eatWhitespace ( ) <EOL> result = "<STR_LIT>" <EOL> if self . pos + <NUM_LIT:1> < len ( self . source_text ) : <EOL> result = self . source_text [ self . pos + <NUM_LIT:1> ] <EOL> if skipWhitespace : <EOL> self . pos = start - <NUM_LIT:1> <EOL> self . next ( ) <EOL> return result <EOL> def eatString ( self , endChars ) : <EOL> start = self . pos <EOL> while self . next ( ) : <EOL> if self . ch == "<STR_LIT:\\>" : <EOL> self . next ( ) <EOL> elif self . ch in endChars : <EOL> break <EOL> elif self . ch == "<STR_LIT:\n>" : <EOL> break <EOL> return self . source_text [ start : self . pos ] + self . ch <EOL> def peekString ( self , endChar ) : <EOL> start = self . pos <EOL> st = self . eatString ( endChar ) <EOL> self . pos = start - <NUM_LIT:1> <EOL> self . next ( ) <EOL> return st <EOL> def eatWhitespace ( self ) : <EOL> result = '<STR_LIT>' <EOL> while WHITE_RE . search ( self . peek ( ) ) is not None : <EOL> self . next ( ) <EOL> result += self . ch <EOL> return result <EOL> def skipWhitespace ( self ) : <EOL> result = '<STR_LIT>' <EOL> if self . ch and WHITE_RE . search ( self . ch ) : <EOL> result = self . ch <EOL> while WHITE_RE . search ( self . next ( ) ) is not None : <EOL> result += self . ch <EOL> return result <EOL> def eatComment ( self ) : <EOL> start = self . pos <EOL> singleLine = self . peek ( ) == "<STR_LIT:/>" <EOL> self . next ( ) <EOL> while self . next ( ) : <EOL> if not singleLine and self . ch == "<STR_LIT:*>" and self . peek ( ) == "<STR_LIT:/>" : <EOL> self . next ( ) <EOL> break <EOL> elif singleLine and self . ch == "<STR_LIT:\n>" : <EOL> return self . source_text [ start : self . pos ] <EOL> return self . source_text [ start : self . pos ] + self . ch <EOL> def lookBack ( self , string ) : <EOL> past = self . source_text [ self . pos - len ( string ) : self . pos ] <EOL> return past . lower ( ) == string <EOL> def foundNestedPseudoClass ( self ) : <EOL> i = self . pos + <NUM_LIT:1> <EOL> openParen = <NUM_LIT:0> <EOL> while i < len ( self . source_text ) : <EOL> ch = self . source_text [ i ] <EOL> if ch == "<STR_LIT:{>" : <EOL> return True <EOL> elif ch == "<STR_LIT:(>" : <EOL> openParen += <NUM_LIT:1> <EOL> elif ch == "<STR_LIT:)>" : <EOL> if openParen == <NUM_LIT:0> : <EOL> return False <EOL> openParen -= <NUM_LIT:1> <EOL> elif ch == "<STR_LIT:;>" or ch == "<STR_LIT:}>" : <EOL> return False <EOL> i += <NUM_LIT:1> ; <EOL> return False <EOL> def beautify ( self ) : <EOL> m = re . search ( "<STR_LIT>" , self . source_text ) <EOL> baseIndentString = m . group ( <NUM_LIT:0> ) <EOL> printer = Printer ( self . indentChar , self . indentSize , baseIndentString ) <EOL> insideRule = False <EOL> insidePropertyValue = False <EOL> enteringConditionalGroup = False <EOL> top_ch = '<STR_LIT>' <EOL> last_top_ch = '<STR_LIT>' <EOL> parenLevel = <NUM_LIT:0> <EOL> while True : <EOL> whitespace = self . skipWhitespace ( ) <EOL> isAfterSpace = whitespace != '<STR_LIT>' <EOL> isAfterNewline = '<STR_LIT:\n>' in whitespace <EOL> last_top_ch = top_ch <EOL> top_ch = self . ch <EOL> if not self . ch : <EOL> break <EOL> elif self . ch == '<STR_LIT:/>' and self . peek ( ) == '<STR_LIT:*>' : <EOL> header = printer . indentLevel == <NUM_LIT:0> <EOL> if not isAfterNewline or header : <EOL> printer . newLine ( ) <EOL> comment = self . eatComment ( ) <EOL> printer . comment ( comment ) <EOL> printer . newLine ( ) <EOL> if header : <EOL> printer . newLine ( True ) <EOL> elif self . ch == '<STR_LIT:/>' and self . peek ( ) == '<STR_LIT:/>' : <EOL> if not isAfterNewline and last_top_ch != '<STR_LIT:{>' : <EOL> printer . trim ( ) <EOL> printer . singleSpace ( ) <EOL> printer . comment ( self . eatComment ( ) ) <EOL> printer . newLine ( ) <EOL> elif self . ch == '<STR_LIT:@>' : <EOL> printer . preserveSingleSpace ( isAfterSpace ) <EOL> if self . peek ( True ) == '<STR_LIT:{>' : <EOL> printer . push ( self . eatString ( '<STR_LIT:}>' ) ) ; <EOL> else : <EOL> printer . push ( self . ch ) <EOL> variableOrRule = self . peekString ( "<STR_LIT>" ) <EOL> if variableOrRule [ - <NUM_LIT:1> ] in "<STR_LIT>" : <EOL> self . next ( ) <EOL> variableOrRule = self . eatString ( "<STR_LIT>" ) <EOL> if variableOrRule [ - <NUM_LIT:1> ] . isspace ( ) : <EOL> variableOrRule = variableOrRule [ : - <NUM_LIT:1> ] <EOL> printer . push ( variableOrRule ) <EOL> printer . singleSpace ( ) ; <EOL> if variableOrRule [ - <NUM_LIT:1> ] . isspace ( ) : <EOL> variableOrRule = variableOrRule [ : - <NUM_LIT:1> ] <EOL> if variableOrRule in self . NESTED_AT_RULE : <EOL> printer . nestedLevel += <NUM_LIT:1> <EOL> if variableOrRule in self . CONDITIONAL_GROUP_RULE : <EOL> enteringConditionalGroup = True <EOL> elif self . ch == '<STR_LIT:#>' and self . peek ( ) == '<STR_LIT:{>' : <EOL> printer . preserveSingleSpace ( isAfterSpace ) <EOL> printer . push ( self . eatString ( '<STR_LIT:}>' ) ) ; <EOL> elif self . ch == '<STR_LIT:{>' : <EOL> if self . peek ( True ) == '<STR_LIT:}>' : <EOL> self . eatWhitespace ( ) <EOL> self . next ( ) <EOL> printer . singleSpace ( ) <EOL> printer . push ( "<STR_LIT:{}>" ) <EOL> printer . newLine ( ) <EOL> if self . opts . newline_between_rules and printer . indentLevel == <NUM_LIT:0> : <EOL> printer . newLine ( True ) <EOL> else : <EOL> printer . indent ( ) <EOL> printer . openBracket ( ) <EOL> if enteringConditionalGroup : <EOL> enteringConditionalGroup = False <EOL> insideRule = printer . indentLevel > printer . nestedLevel <EOL> else : <EOL> insideRule = printer . indentLevel >= printer . nestedLevel <EOL> elif self . ch == '<STR_LIT:}>' : <EOL> printer . outdent ( ) <EOL> printer . closeBracket ( ) <EOL> insideRule = False <EOL> insidePropertyValue = False <EOL> if printer . nestedLevel : <EOL> printer . nestedLevel -= <NUM_LIT:1> <EOL> if self . opts . newline_between_rules and printer . indentLevel == <NUM_LIT:0> : <EOL> printer . newLine ( True ) <EOL> elif self . ch == "<STR_LIT::>" : <EOL> self . eatWhitespace ( ) <EOL> if ( insideRule or enteringConditionalGroup ) and not ( self . lookBack ( '<STR_LIT:&>' ) or self . foundNestedPseudoClass ( ) ) : <EOL> insidePropertyValue = True <EOL> printer . push ( "<STR_LIT::>" ) <EOL> printer . singleSpace ( ) <EOL> else : <EOL> if self . peek ( ) == "<STR_LIT::>" : <EOL> self . next ( ) <EOL> printer . push ( "<STR_LIT>" ) <EOL> else : <EOL> printer . push ( "<STR_LIT::>" ) <EOL> elif self . ch == '<STR_LIT:">' or self . ch == '<STR_LIT>' : <EOL> printer . preserveSingleSpace ( isAfterSpace ) <EOL> printer . push ( self . eatString ( self . ch ) ) <EOL> elif self . ch == '<STR_LIT:;>' : <EOL> insidePropertyValue = False <EOL> printer . semicolon ( ) <EOL> elif self . ch == '<STR_LIT:(>' : <EOL> if self . lookBack ( "<STR_LIT:url>" ) : <EOL> printer . push ( self . ch ) <EOL> self . eatWhitespace ( ) <EOL> if self . next ( ) : <EOL> if self . ch is not '<STR_LIT:)>' and self . ch is not '<STR_LIT:">' and self . ch is not '<STR_LIT>' : <EOL> printer . push ( self . eatString ( '<STR_LIT:)>' ) ) <EOL> else : <EOL> self . pos = self . pos - <NUM_LIT:1> <EOL> else : <EOL> parenLevel += <NUM_LIT:1> <EOL> printer . preserveSingleSpace ( isAfterSpace ) <EOL> printer . push ( self . ch ) <EOL> self . eatWhitespace ( ) <EOL> elif self . ch == '<STR_LIT:)>' : <EOL> printer . push ( self . ch ) <EOL> parenLevel -= <NUM_LIT:1> <EOL> elif self . ch == '<STR_LIT:U+002C>' : <EOL> printer . push ( self . ch ) <EOL> self . eatWhitespace ( ) <EOL> if not insidePropertyValue and self . opts . selector_separator_newline and parenLevel < <NUM_LIT:1> : <EOL> printer . newLine ( ) <EOL> else : <EOL> printer . singleSpace ( ) <EOL> elif self . ch == '<STR_LIT:]>' : <EOL> printer . push ( self . ch ) <EOL> elif self . ch == '<STR_LIT:[>' : <EOL> printer . preserveSingleSpace ( isAfterSpace ) <EOL> printer . push ( self . ch ) <EOL> elif self . ch == '<STR_LIT:=>' : <EOL> self . eatWhitespace ( ) <EOL> self . ch = '<STR_LIT:=>' <EOL> printer . push ( self . ch ) <EOL> else : <EOL> printer . preserveSingleSpace ( isAfterSpace ) <EOL> printer . push ( self . ch ) <EOL> sweet_code = re . sub ( '<STR_LIT>' , '<STR_LIT>' , printer . result ( ) ) <EOL> if self . opts . end_with_newline : <EOL> sweet_code += '<STR_LIT:\n>' <EOL> if not self . opts . eol == '<STR_LIT:\n>' : <EOL> sweet_code = sweet_code . replace ( '<STR_LIT:\n>' , self . opts . eol ) <EOL> return sweet_code </s>
