import os, pdb
css_lines = []
var_lines = []
constant_name = ''
var_list = []
var_lookup = []
def extract_hex(css_list):
counter = 1
in_rule = False;
in_hex = False;
hex_val = ''
hex_list = []
for css_text in css_list:
usable_css = css_text
for i in xrange(len(css_text)):
if css_text[i] == '{':
in_rule = True
if css_text[i] == '}':
in_rule = False
if css_text[i] == '#' and in_rule:
in_hex = True
hex_begin = i
if in_hex:
hex_val += css_text[i]
if (css_text[i] == ';' or css_text[i] == ' ') and in_hex:
in_hex = False
hex_end = i
if hex_val[-1] == ';': hex_val = hex_val[0:-1]
if hex_val.lower() not in hex_list:
hex_list.append(hex_val.lower())
print 'Found hex value in the following line:'
print '----->\t' + css_text
var_name = raw_input('What variable shall I replace the hex code with? ')
if var_name[0] is not '$':
var_name = '$' + var_name
css_temp = []
for j in usable_css:
css_temp.append(j)
css_temp_after = css_temp[hex_end:]
css_temp = css_temp[0:hex_begin]
css_temp.extend(var_name)
css_temp.extend(css_temp_after)
usable_css = ''.join(css_temp)
var_lookup.append((var_name, hex_val))
hex_val = ''
css_lines.append(usable_css)
return hex_list
if __name__ == '__main__':
path_to_core = raw_input('What is the path to core? ')
#path_to_core = 'C:\Documents and Settings\dworley\Desktop'
file_to_parse = raw_input('What is the path to the file you\'d like to parse? ')
#file_to_parse = 'C:\Documents and Settings\dworley\Desktop\TEST.css'
sss_save_dir = raw_input('Where shall we save the site-specific SSS output?')
#sss_save_dir = 'C:\Documents and Settings\dworley\Desktop'
constant_name = raw_input('What would you like to name the constants we\'ll be creating? ')
#constant_name = 'TEST'
constant_list = []
constant_dict = {}
if os.path.isdir(sss_save_dir) and os.path.isfile(file_to_parse):
filename = os.path.splitext(os.path.split(file_to_parse)[1])[0]
sss_output = open(os.path.join(sss_save_dir, filename + '.sss'), 'w')
sss = open(file_to_parse, 'r')
sss_text = sss.readlines()
sss.close()
hexcodes = extract_hex(sss_text)
hexcodes.sort()
for i in xrange(len(hexcodes)):
constant_declaration = '@constant ' + constant_name + '_' + str(i + 1) + ':\t' + hexcodes[i] + ';\n'
constant_list.append(constant_declaration)
constant_dict[hexcodes[i]] = constant_name + '_' + str(i + 1)
print constant_declaration[:-1]
for i in var_lookup:
print i[0] + ':\t' + constant_dict[i[1].lower()] + ';'
var_list.append(i[0] + ':\t' + constant_dict[i[1].lower()] + ';\n')
sss_output.writelines(constant_list)
sss_output.write('\n\n')
sss_output.writelines(var_list)
sss_output.close()
core_output = open(os.path.join(path_to_core, '_' + filename + '.sss'), 'w')
core_output.writelines(css_lines)
core_output.close()