Menu

[r5]: / hexExtractor.py  Maximize  Restore  History

Download this file

136 lines (83 with data), 3.8 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import os, pdb, itertools
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:
replaced_value = False
usable_css = []
for j in css_text:
usable_css.append(j)
counter = 0
for i in usable_css:
if i == '{':
in_rule = True
if i == '}':
in_rule = False
if i == '#' and in_rule:
in_hex = True
hex_begin = counter
if in_hex:
hex_val += i
if (i == ';' or i == ' ') and in_hex:
in_hex = False
hex_end = hex_begin + len(hex_val) - 1
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:\n'
print '----->\t' + ''.join(usable_css)
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_after = usable_css[hex_end:]
usable_css = usable_css[0:hex_begin]
usable_css.extend(var_name)
usable_css.extend(css_temp_after)
counter += len(var_name) - len(hex_val)
var_lookup.append((var_name, hex_val))
hex_val = ''
counter += 1
css_lines.append(''.join(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:\Program Files\Apache Group\Apache2\htdocs\mg\css\instant_messenger.css'
sss_save_dir = raw_input('Where shall we save the site-specific SSS output? ')
#sss_save_dir = 'C:\Program Files\Apache Group\Apache2\htdocs\mg\css\sss'
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()
MongoDB Logo MongoDB