<s> import re <EOL> class BeautifierOptions : <EOL> def __init__ ( self ) : <EOL> self . indent_size = <NUM_LIT:2> <EOL> self . indent_char = '<STR_LIT:U+0020>' <EOL> self . indent_base = '<STR_LIT>' <EOL> def default_options ( ) : <EOL> return BeautifierOptions ( ) <EOL> def beautify ( string , opts = default_options ( ) ) : <EOL> b = Beautifier ( ) <EOL> return b . beautify ( string , opts ) <EOL> def indent_base ( prev_line , opts = default_options ( ) ) : <EOL> b = Beautifier ( ) <EOL> return b . get_base_indent ( prev_line , opts ) <EOL> class Beautifier : <EOL> def __init__ ( self , opts = default_options ( ) ) : <EOL> self . opts = opts <EOL> self . indent_exp = [ <EOL> re . compile ( r'<STR_LIT>' ) , <EOL> re . compile ( r'<STR_LIT>' ) , <EOL> re . compile ( r'<STR_LIT>' ) , <EOL> re . compile ( r'<STR_LIT>' ) , <EOL> re . compile ( r'<STR_LIT>' ) , <EOL> re . compile ( r'<STR_LIT>' ) , <EOL> re . compile ( r'<STR_LIT>' ) , <EOL> re . compile ( r'<STR_LIT>' ) , <EOL> re . compile ( r'<STR_LIT>' ) , <EOL> re . compile ( r'<STR_LIT>' ) , <EOL> re . compile ( r'<STR_LIT>' ) , <EOL> re . compile ( r'<STR_LIT>' ) , <EOL> re . compile ( r'<STR_LIT>' ) , <EOL> re . compile ( r'<STR_LIT>' ) , <EOL> re . compile ( r'<STR_LIT>' ) , <EOL> re . compile ( r'<STR_LIT>' ) , <EOL> re . compile ( r'<STR_LIT>' ) , <EOL> re . compile ( r'<STR_LIT>' ) , <EOL> re . compile ( r'<STR_LIT>' ) , <EOL> ] <EOL> self . outdent_exp = [ <EOL> re . compile ( r'<STR_LIT>' ) , <EOL> re . compile ( r'<STR_LIT>' ) , <EOL> re . compile ( r'<STR_LIT>' ) , <EOL> re . compile ( r'<STR_LIT>' ) , <EOL> re . compile ( r'<STR_LIT>' ) , <EOL> re . compile ( r'<STR_LIT>' ) , <EOL> re . compile ( r'<STR_LIT>' ) , <EOL> re . compile ( r'<STR_LIT>' ) , <EOL> ] <EOL> self . debtdent_exp = [ <EOL> ] <EOL> self . confusion_exp = [ <EOL> re . compile ( r'<STR_LIT>' ) , <EOL> re . compile ( r'<STR_LIT>' ) , <EOL> re . compile ( r'<STR_LIT>' ) , <EOL> re . compile ( r'<STR_LIT>' ) , <EOL> re . compile ( r'<STR_LIT>' ) , <EOL> re . compile ( r'<STR_LIT>' ) , <EOL> re . compile ( r'<STR_LIT>' ) , <EOL> re . compile ( r'<STR_LIT>' ) , <EOL> ] <EOL> def make_tab ( self , tab ) : <EOL> if ( tab < <NUM_LIT:0> ) : <EOL> tab_str = '<STR_LIT>' <EOL> else : <EOL> tab_str = self . opts . indent_char * self . opts . indent_size * tab <EOL> return self . opts . indent_base + tab_str <EOL> def add_line ( self , line , tab ) : <EOL> line = line . strip ( ) <EOL> if len ( line ) > <NUM_LIT:0> : <EOL> line = self . make_tab ( tab ) + line <EOL> return line <EOL> def beautify ( self , s , opts = None ) : <EOL> if opts != None : <EOL> self . opts = opts <EOL> comment_block = False <EOL> in_here_doc = False <EOL> here_doc_term = "<STR_LIT>" <EOL> program_end = False <EOL> multiline_array = [ ] <EOL> multiline_str = "<STR_LIT>" <EOL> tab = <NUM_LIT:0> <EOL> debt_tab = <NUM_LIT:0> <EOL> output = [ ] <EOL> line_point = <NUM_LIT:0> <EOL> lines = s . split ( "<STR_LIT:\n>" ) <EOL> for line in lines : <EOL> line = line . rstrip ( ) <EOL> if ( not program_end ) : <EOL> if ( line_point >= len ( lines ) ) : <EOL> program_end = True <EOL> else : <EOL> if ( ( not re . search ( r'<STR_LIT>' , line ) ) and re . search ( r'<STR_LIT>' , line ) ) : <EOL> multiline_array . append ( line ) <EOL> multiline_str += re . sub ( r'<STR_LIT>' , r'<STR_LIT>' , line ) <EOL> continue <EOL> if ( len ( multiline_str ) > <NUM_LIT:0> ) : <EOL> multiline_array . append ( line ) <EOL> multiline_str += re . sub ( r'<STR_LIT>' , r'<STR_LIT>' , line ) <EOL> if len ( multiline_str ) > <NUM_LIT:0> : <EOL> tline = multiline_str . strip ( ) <EOL> else : <EOL> tline = line . strip ( ) <EOL> if re . search ( r'<STR_LIT>' , tline ) : <EOL> comment_block = True <EOL> if in_here_doc : <EOL> if re . search ( r'<STR_LIT>' % ( here_doc_term ) , tline ) : in_here_doc = False <EOL> else : <EOL> if re . search ( r'<STR_LIT>' , tline ) : <EOL> here_doc_term = re . sub ( r'<STR_LIT>' , r'<STR_LIT>' , tline ) <EOL> in_here_doc = len ( here_doc_term ) > <NUM_LIT:0> <EOL> if comment_block or program_end or in_here_doc : <EOL> output . append ( line ) <EOL> else : <EOL> comment_line = re . search ( r'<STR_LIT>' , tline ) <EOL> if not comment_line : <EOL> for ce in self . confusion_exp : <EOL> while ce . search ( tline ) : <EOL> tline = ce . sub ( '<STR_LIT>' , tline ) <EOL> tline = re . sub ( r'<STR_LIT>' , '<STR_LIT>' , tline , <NUM_LIT:1> ) <EOL> tline = re . sub ( r'<STR_LIT>' , "<STR_LIT:'>" , tline ) <EOL> for oe in self . outdent_exp : <EOL> if oe . search ( tline ) : <EOL> tab -= <NUM_LIT:1> <EOL> break <EOL> for de in self . debtdent_exp : <EOL> if de . search ( tline ) : <EOL> debt_tab += <NUM_LIT:1> <EOL> break <EOL> if len ( multiline_array ) > <NUM_LIT:0> : <EOL> for ml in multiline_array : <EOL> output . append ( self . add_line ( ml , tab ) ) <EOL> multiline_array = [ ] <EOL> multiline_str = "<STR_LIT>" <EOL> else : <EOL> output . append ( self . add_line ( line , tab ) ) <EOL> if not comment_line : <EOL> for ie in self . indent_exp : <EOL> if ie . search ( tline ) and ( not re . search ( r'<STR_LIT>' , tline ) ) : <EOL> tab += <NUM_LIT:1> <EOL> break <EOL> if debt_tab > <NUM_LIT:0> : <EOL> tab -= debt_tab <EOL> debt_tab = <NUM_LIT:0> <EOL> if re . search ( r'<STR_LIT>' , tline ) : <EOL> comment_block = False <EOL> line_point += <NUM_LIT:1> <EOL> error = ( tab != <NUM_LIT:0> ) <EOL> if error : <EOL> print "<STR_LIT>" % ( tab ) <EOL> return "<STR_LIT:\n>" . join ( output ) <EOL> def format_line ( self , line ) : <EOL> pass <EOL> def get_base_indent ( self , prev_line , opts = None ) : <EOL> if opts != None : <EOL> self . opts = opts <EOL> base_str = re . search ( r'<STR_LIT>' , prev_line ) . group ( ) <EOL> for ie in self . indent_exp : <EOL> if ie . search ( prev_line . strip ( ) ) : <EOL> return base_str + self . make_tab ( <NUM_LIT:1> ) <EOL> break <EOL> return base_str </s>
