<s> """<STR_LIT>""" <EOL> import re <EOL> from definitions import definitions <EOL> from indexer import Indexer <EOL> index = Indexer ( ) <EOL> index . index ( definitions ) <EOL> line_expr = re . compile ( r'<STR_LIT>' ) <EOL> rule_expr = re . compile ( r'<STR_LIT>' ) <EOL> value_expr = re . compile ( r'<STR_LIT>' ) <EOL> semicolon_expr = re . compile ( r'<STR_LIT>' ) <EOL> selectorlike_expr = re . compile ( r'<STR_LIT>' ) <EOL> ends_in_brace_expr = re . compile ( r'<STR_LIT>' ) <EOL> def expand_statement ( line , usecolon = True ) : <EOL> """<STR_LIT>""" <EOL> indent , snippet = split_indent ( line ) <EOL> separator = '<STR_LIT::>' if usecolon else '<STR_LIT>' <EOL> out = expand_statement_simple ( snippet , separator ) or expand_statement_with_property ( snippet , separator ) or expand_statement_value ( snippet , separator ) <EOL> if out : return indent + out <EOL> def expand_statement_simple ( snippet , separator ) : <EOL> """<STR_LIT>""" <EOL> options = index . statements . get ( snippet ) <EOL> if not options : return <EOL> return "<STR_LIT>" % ( options [ "<STR_LIT>" ] , separator , options [ "<STR_LIT:value>" ] ) <EOL> def expand_statement_with_property ( snippet , separator ) : <EOL> """<STR_LIT>""" <EOL> short , value , unit = split_value ( snippet ) <EOL> options = index . properties . get ( short ) <EOL> if not options : return <EOL> xvalue = expand_full_value ( value + unit , options [ "<STR_LIT>" ] ) <EOL> if xvalue : return "<STR_LIT>" % ( options [ "<STR_LIT>" ] , separator , xvalue ) <EOL> def expand_statement_value ( snippet , separator ) : <EOL> """<STR_LIT>""" <EOL> m = rule_expr . match ( snippet ) <EOL> if not m : return <EOL> if semicolon_expr . match ( snippet ) : return <EOL> prop , value = m . group ( <NUM_LIT:1> ) , m . group ( <NUM_LIT:2> ) <EOL> if is_selectorlike ( value ) : return <EOL> if value . count ( '<STR_LIT:(>' ) != value . count ( '<STR_LIT:)>' ) : return <EOL> new_value = expand_full_value ( value , prop ) <EOL> return "<STR_LIT>" % ( prop , separator , new_value or value ) <EOL> def split_value ( snippet ) : <EOL> """<STR_LIT>""" <EOL> m = value_expr . match ( snippet ) <EOL> if m : <EOL> return ( m . group ( <NUM_LIT:1> ) , m . group ( <NUM_LIT:2> ) , m . group ( <NUM_LIT:3> ) ) <EOL> else : <EOL> return ( None , None , None ) <EOL> def expand_property ( line , usecolon = True ) : <EOL> """<STR_LIT>""" <EOL> indent , snippet = split_indent ( line ) <EOL> options = index . properties . get ( snippet ) <EOL> if not options : return <EOL> separator = '<STR_LIT::>' if usecolon else '<STR_LIT>' <EOL> return "<STR_LIT>" % ( indent , options [ "<STR_LIT>" ] , separator ) <EOL> def expand_full_value ( val , prop ) : <EOL> """<STR_LIT>""" <EOL> options = index . full_properties . get ( prop ) <EOL> if not options : return <EOL> default_unit = options . get ( '<STR_LIT>' ) <EOL> if default_unit : <EOL> _ , number , unit = split_value ( val ) <EOL> if number : <EOL> return unitify ( number , unit , default_unit ) <EOL> values = options . get ( '<STR_LIT>' ) <EOL> if values : <EOL> return match_keyword ( val , values ) <EOL> def match_keyword ( value , keywords ) : <EOL> """<STR_LIT>""" <EOL> for word in keywords : <EOL> if re . match ( '<STR_LIT>' + value , word ) : return word <EOL> for word in keywords : <EOL> if re . match ( value , word ) : return word <EOL> def unitify ( number , unit , default_unit ) : <EOL> """<STR_LIT>""" <EOL> if number == "<STR_LIT:0>" : <EOL> return number <EOL> if unit == "<STR_LIT:p>" or unit == "<STR_LIT:x>" : <EOL> unit = "<STR_LIT>" <EOL> if unit == "<STR_LIT:m>" or unit == "<STR_LIT:e>" : <EOL> unit = "<STR_LIT>" <EOL> if unit == "<STR_LIT>" : <EOL> if default_unit == "<STR_LIT:_>" : <EOL> unit = "<STR_LIT>" <EOL> else : <EOL> unit = ( default_unit or px ) <EOL> return number + unit <EOL> def split_indent ( line ) : <EOL> """<STR_LIT>""" <EOL> match = line_expr . match ( line ) <EOL> return ( match . group ( <NUM_LIT:1> ) , match . group ( <NUM_LIT:2> ) ) <EOL> def is_selectorlike ( value ) : <EOL> """<STR_LIT>""" <EOL> if selectorlike_expr . match ( value ) or ends_in_brace_expr . match ( value ) : <EOL> return True </s>
