Skip to content

Commit 447d7d4

Browse files
Jack AdamJack Adam
authored andcommitted
parser now defines undefined style rules in css files!
1 parent 2493064 commit 447d7d4

File tree

6 files changed

+82
-20
lines changed

6 files changed

+82
-20
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
2. prints undefined classes and IDs, corresponding files
88
3. prints unused classes and IDs, corresponding files and line numbers
99
4. re-writes .css files without unused classes and IDs
10+
5. defines previously undefined style rules in .css files
1011

1112
TODO:
1213

13-
0. thoroughly edgecase re-writing stage
14-
1. add functionality to write empty style rules for undefined rules?
14+
0. thoroughly edge-case re-writing stage

definer.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
from helper import lookup
2+
from os import path
3+
4+
def define(un, cf):
5+
up = {}
6+
for rule, file in un.items():
7+
# get name of css file
8+
css = lookup(cf, file)
9+
# if already cleaned, open new file
10+
newF = css[:-4] + '-clean.css'
11+
css = css if not path.isfile(newF) else newF
12+
# count number of definitions
13+
if css not in up:
14+
up[css] = 1
15+
else:
16+
up[css] += 1
17+
with open(css) as og:
18+
data = og.read()
19+
# prepend new rules
20+
with open(css, 'w') as new:
21+
new.write(rule + ' {\n\t/* Define your style rule here */\n}\n' + data)
22+
# print changes
23+
for file, count in up.items():
24+
r = 'rules' if count > 1 else 'rule'
25+
print(f'Defined {count} new {r} in {file}')

demo.png

28.5 KB
Loading

helper.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,9 @@ def solo(line):
6868
# removes comma if last character of string
6969
def comma(w):
7070
return w[:-1] if w[-1:] == ',' else w
71+
72+
# look up html file in dictionary of html and css files
73+
def lookup(d, f):
74+
for h, c in d.items():
75+
if h == f:
76+
return c

main.py

Lines changed: 38 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,46 @@
11
from parser import parse_css, parse_html
22
from cleaner import clean
3+
from definer import define
34
from sys import exit
45

56
def 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'\nIdentified {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('\nMay 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

6790
if __name__ == '__main__':
6891
main()

parser.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ def parse_css():
6161
def parse_html():
6262
# obtain filename/path
6363
filepath = intro('html')
64-
cl, id, fc = [], [], 0
64+
cl, id, css, fc = [], [], {}, 0
6565

6666
# read each file
6767
for file in glob(filepath):
@@ -82,20 +82,28 @@ def parse_html():
8282
# found a class
8383
elif piece[:5] == 'class':
8484
start, found = 7, '.'
85+
# found a link
86+
elif piece[:4] == 'href':
87+
start = 6
8588
if start:
8689
for char in piece[start:]:
8790
if char != "'" and char != '"':
8891
found += char
89-
# at end of rule name
92+
# at end, marked by quotes
9093
else:
9194
if start == 4:
9295
id[fc][found] = file
9396
elif start == 7:
9497
cl[fc][found] = file
98+
# if link to .css file
99+
elif start == 6 and found[-4:] == '.css':
100+
# css.append(file + ' ' + found)
101+
if file not in css:
102+
css[file] = found
95103
break
96104
fc += 1
97105

98106
if not cl and not id:
99107
exit('No .html files in this directory!')
100108
else:
101-
return (cl, id)
109+
return ((cl, id), css)

0 commit comments

Comments
 (0)