11from parser import parse_css , parse_html
22from cleaner import clean
3+ from definer import define
34from sys import exit
45
56def main ():
67 # parse the files
78 c = parse_css ()
89 h = parse_html ()
9- unused , fileNames , css , fileCount = {}, [], [], 0
10+ unused , undefined , results , fileNames , css , fileCount = {}, {}, '' , [], [], 0
1011
1112 # identify unused classes
1213 for cla , num in c [0 ][0 ].items ():
1314 x = cla .split ()
1415 if ':' not in x [0 ]:
1516 css .append (x [0 ])
16- if x [0 ] not in h [0 ][0 ]:
17+ if x [0 ] not in h [0 ][0 ][ 0 ] :
1718 unused [cla ] = num
1819
1920 # identify unused IDs
2021 for ID , num in c [1 ][0 ].items ():
2122 y = ID .split ()
2223 if ':' not in y [0 ]:
2324 css .append (y [0 ])
24- if y [0 ] not in h [1 ][0 ]:
25+ if y [0 ] not in h [0 ][ 1 ][0 ]:
2526 unused [ID ] = num
2627
27- print (f'\n Identified { c [0 ][1 ]} unique classes and { c [1 ][1 ]} unique IDs.\n ' )
28+ i = f'Identified { c [0 ][1 ]} unique classes and { c [1 ][1 ]} unique IDs.\n '
29+ print ('\n ' + i )
30+ results += i
2831
2932 # identify undefined classes and IDs
30- for d in h :
33+ for d in h [ 0 ] :
3134 for dd in d :
32- for rule , num in dd .items ():
35+ for rule , file in dd .items ():
3336 if rule not in css :
37+ undefined [rule ] = file
3438 pre = 'ID: ' if rule [0 ] == '#' else 'class:'
35- print (f'Undefined { pre } { rule } : { num } ' )
39+ o = f'Undefined { pre } { rule } : { file } '
40+ print (o )
41+ results += '\n ' + o
3642 print ()
37-
43+ results += ' \n '
3844 final = dict (unused )
3945
4046 # identify pseudoclasses
@@ -48,21 +54,38 @@ def main():
4854 if zz [0 ] + ' : ' + z [2 ] not in unused :
4955 del final [rule ]
5056 continue
57+ o = ''
5158 if z [0 ][0 ] == '.' :
52- print ( f'Unused class: { rule } { num } ' )
59+ o = f'Unused class: { rule } { num } '
5360 elif z [0 ][0 ] == '#' :
54- print (f'Unused ID: { rule } { num } ' )
61+ o = f'Unused ID: { rule } { num } '
62+ print (o )
63+ results += '\n ' + o
5564
5665 if not final :
57- print ('No unused classes nor IDs!' )
66+ o = 'No unused classes nor IDs!'
67+ print (o )
68+ resuls += i
5869 else :
5970 q = input ('\n May I remove these unused rules and output new .css files? (yes/no): ' )
6071 if q .lower () in ('yes' , 'y' ):
6172 clean (final , fileNames , fileCount )
62- elif q .lower () in ('no' , 'n' ):
63- exit ('Thank you.' )
64- else :
65- exit ('Invalid response.' )
73+ if undefined :
74+ qq = input ('May I add definitions for undefined rules? (yes/no): ' )
75+ if qq .lower () in ('yes' , 'y' ):
76+ define (undefined , h [1 ])
77+ elif qq .lower () in ('no' , 'n' ):
78+ qqq = input ('Would you instead like a .txt file with your results? (yes/no): ' )
79+ if qqq .lower () in ('yes' , 'y' ):
80+ with open ('results.txt' , 'w' ) as f :
81+ f .write (results )
82+ print ('Wrote results.txt' )
83+ elif qqq .lower () in ('no' , 'n' ):
84+ exit ('Thank you.' )
85+ else :
86+ exit ('Invalid response.' )
87+ else :
88+ exit ('Invalid response.' )
6689
6790if __name__ == '__main__' :
6891 main ()
0 commit comments