Skip to content

Commit 1840c76

Browse files
Jack AdamJack Adam
authored andcommitted
fixed media queries (they were never the problem). new fn() determines if entire rule is on one line. v3.0, baby.
1 parent f5a5762 commit 1840c76

File tree

5 files changed

+28
-12
lines changed

5 files changed

+28
-12
lines changed

README.md

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,3 @@
88
3. prints unused classes and IDs, corresponding files and line numbers
99
4. re-writes .css files without unused classes and IDs
1010
5. defines previously undefined style rules in .css files
11-
12-
TODO:
13-
14-
0. fix handling of media queries :-/

cleaner.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from helper import solo, comma
1+
from helper import solo, comma, full
22

33
def clean(u, fn, fc):
44
# pre-populate arrays
@@ -25,20 +25,24 @@ def clean(u, fn, fc):
2525
# open new file to write
2626
with open(new, 'w') as newF:
2727
soloFlag, multiFlag, newline, totalFlag, end = False, False, False, False, False
28+
# for each line
2829
for num, line in enumerate(f, 1):
2930
# if haven't seen all problem lines
3031
if nums[i]:
3132
# if line has an unused rule
3233
if num == nums[i][0]:
3334
# remove line num about to be cleaned
3435
del nums[i][0]
35-
x = solo(line)
36-
soloFlag = x
37-
multiFlag = not(x)
36+
# how many rules are on this line?
37+
soloFlag = solo(line)
38+
multiFlag = not(soloFlag)
39+
# is the entire rule defined on this line?
40+
y = full(line)
3841
# if multiple rules
3942
if multiFlag:
4043
l = line.split()
4144
ll = len(l)
45+
# loop thru words in rule header
4246
for x in range(ll):
4347
# replace unused rules with ?
4448
if comma(l[x]) in rules[i]:
@@ -55,6 +59,7 @@ def clean(u, fn, fc):
5559
else:
5660
soloFlag = True
5761
totalFlag = True
62+
# re-assemble line from list
5863
line = ' '.join(l) + '\n'
5964
multiFlag = False
6065
# if one rule
@@ -71,12 +76,17 @@ def clean(u, fn, fc):
7176
if not newline or line != '\n':
7277
newF.write(line)
7378
newline = True if line == '\n' else False
74-
# hack: remove definition of last rule
75-
else:
79+
# remove definition of last rule, unless it was all on one line
80+
elif not nums[i] and not end and not y:
7681
for c in line:
7782
if end:
7883
newF.write(c)
84+
# keep reading until definition ends
7985
if c == '}':
8086
end = True
87+
# no need to examine char at a time
88+
else:
89+
newF.write(line)
90+
8191
print(f'Wrote {new}')
8292
newF.close()

helper.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,8 @@ def lookup(d, f):
7676
for h, c in d.items():
7777
if h == f:
7878
return c
79+
80+
def full(l):
81+
if '{' in l and '}' in l:
82+
return True
83+
return False

main.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ def main():
77
# parse the files
88
c = parse_css()
99
h = parse_html()
10-
unused, undefined, results, fileNames, css, fileCount = {}, {}, '', [], [], 0
10+
unused, undefined, fileNames, css = {}, {}, [], []
11+
results, fileCount = '', 0
1112

1213
# identify UNUSED classes
1314
for cla, num in c[0][0].items():
@@ -77,6 +78,7 @@ def main():
7778
print(o)
7879
results += i
7980
if final:
81+
# may i clean?
8082
q = input('\nMay I remove these unused rules and output new .css files? (yes/no): ')
8183
if q.lower() in ('yes', 'y'):
8284
clean(final, fileNames, fileCount)
@@ -85,10 +87,12 @@ def main():
8587
print(o)
8688
results += i
8789
if undefined:
90+
# may i define?
8891
qq = input('May I add definitions for undefined rules? (yes/no): ')
8992
if qq.lower() in ('yes', 'y'):
9093
define(undefined, h[1])
9194

95+
# no cleaning, but maybe a humble .txt file?
9296
if q.lower() in ('no', 'n') and qq.lower() in ('no', 'n'):
9397
qqq = input('Would you instead like a .txt file with your results? (yes/no): ')
9498
if qqq.lower() in ('yes', 'y'):

parser.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
def parse_css():
77
# obtain filename/path
88
filepath = intro('css')
9-
classes, ids, found, flag = {}, {}, '', False
9+
classes, ids = {}, {}
10+
found, flag, op, close = '', False, 0, 0
1011

1112
# read each file
1213
for file in glob(filepath):

0 commit comments

Comments
 (0)