Skip to content

Commit f857b53

Browse files
committed
Update changes
1 parent 5132277 commit f857b53

13 files changed

+151
-72
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
.vscode
22
.pytest_cache
3+
test.py

README.md

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,24 @@ It was inspired on Google's approach to minimize classes names to faster load we
55

66
## What it does
77

8-
- [x] Make a hash out of css classes names and apply for all the html's
8+
- [x] Make a hash out of css and id classes
9+
- [x] Auto apply for all the html files
910
- [x] Create `-hashed` file with the output
10-
- [x] Auto change the css names of the link tags
11+
- [x] Auto change the css file to `-hashed` in the html link tag
12+
- [x] Multiple files on multiple folders
1113

1214
## Usage
1315

1416
Just put the script/files on the top folder and execute.
1517

1618
It will search all files with the extensions marked on the configuration file.
1719

18-
It will give the output with `foo-hashed.html` and `foo-hashed.css`
20+
It will give the output with `foo-hashed.html` and `foo-hashed.css`.
1921

2022
### Configure
2123

22-
Use the [configuration file](config.json) to change behaviour and variables of the script
24+
Change in the [configuration file](/config.json) the directories to be ignored, hash length and overwrite files.
2325

24-
## Future features
26+
## To Change?
2527

26-
- [ ] Hashfy id's
27-
- [ ] Multiple files on mutiple folders (with track)
28-
- [ ] Hash function use letters
29-
- [ ] Delete old files on new run
30-
- [ ] Add tests?
28+
- [ ] Separation of classes and ids in prefix

config.json

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,15 @@
66
"filesIgnore": [
77
"-hashed"
88
],
9+
"dirsIgnore":[
10+
".",
11+
"packages",
12+
"src",
13+
"dist"
14+
],
915
"extCopy": "-hashed",
10-
"patternCSS": "\\.-?[_a-zA-Z]+[_a-zA-Z0-9-]*\\s*\\{",
16+
"patternCSS": "[\\.\\#]-?[_a-zA-Z]+[_a-zA-Z0-9-]*\\s*\\{",
1117
"patternHTML": "class[\t]*=[\t]*\"[^\"]+",
12-
"hashLength": 6
18+
"hashLength": 6,
19+
"overwriteFiles": false
1320
}

convert.py

Lines changed: 42 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ def read(file):
1212
return lines
1313

1414

15-
def write(_PATH, new_name, lines):
16-
with open(_PATH + new_name, 'w') as exf:
15+
def write(root, new_name, lines):
16+
with open(os.path.join(root, new_name), 'w') as exf:
1717
exf.writelines(lines)
1818
exf.close()
1919

@@ -39,29 +39,47 @@ def main():
3939
config = json.load(json_data_file)
4040
_PATH = os.getcwd() + '/'
4141

42+
search_files = []
4243
# Look for files
43-
search_files = [f for f in listdir(_PATH) if isfile(join(_PATH, f)) and [bool(
44-
s) for s in config['filesSearch'] if f.endswith(s)] and [bool(i) for i in config['filesIgnore'] if i not in f]]
44+
for root, subdirectories, files in os.walk(_PATH):
45+
# Comprehension to break outter loop
46+
if any([dirsIgnore for dirsIgnore in config['dirsIgnore'] if dirsIgnore in root]):
47+
continue
48+
49+
# And all its files
50+
for index, file in enumerate(files):
51+
for filesSearch in config['filesSearch']:
52+
for filesIgnore in config['filesIgnore']:
53+
if(filesSearch in file and filesIgnore not in file):
54+
search_files.append((root, file))
55+
56+
#print(f'files: {search_files}\n')
57+
58+
print('CSS Hashfy found files:', end='\n\n')
59+
for root, file in search_files:
60+
print(os.path.join(root, file))
61+
print()
4562

46-
print(f'files: {search_files}\n')
63+
print('Starting hashing...', end='\n\n')
4764

4865
# Initializing alg vars
4966
count = 0
5067
classes_dict = {}
5168

5269
# Get all the files with the '.css' extension
53-
css_files = [file for file in search_files if '.css' in file]
70+
css_files = [(root, file)
71+
for root, file in search_files if file.endswith('.css')]
5472

5573
# Copy the files of the search
56-
for file in css_files:
57-
lines = read(file)
74+
for root, file in css_files:
75+
lines = read(os.path.join(root, file))
5876

59-
new_name, substring = getVars(file, config)
77+
new_name, substring = getVars(os.path.join(root, file), config)
6078

6179
# If the file doesn't exist, create a new one
62-
if(not isfile(join(_PATH, new_name))):
80+
if(config['overwriteFiles'] or not isfile(join(root, new_name))):
6381
substring = [s.translate({ord('.'): None, ord(
64-
'{'): None}).strip() for s in substring]
82+
'{'): None, ord('#'): None}).strip() for s in substring]
6583

6684
# Create the dictionary with the classe's hashes of the CSS
6785
classes_dict.update(
@@ -76,29 +94,30 @@ def main():
7694
l = l.replace(k, 'c'+v)
7795
lines[index] = l
7896

79-
write(_PATH, new_name, lines)
97+
write(root, new_name, lines)
8098
count += 1
81-
print(f"{10*'*'} \t {new_name} \t {10*'*'}")
99+
print(f"{10*'*'} \t {new_name.split('/')[-1]} \t {10*'*'}")
82100

83101
# If it exists, pass
84102
else:
85-
print(f'!! file {new_name} already existed! !!')
103+
print(f'!! file already existed: {new_name}')
86104

87105
# Overwrite HTML classes
88-
html_files = [file for file in search_files if '.html' in file]
106+
html_files = [(root, file)
107+
for root, file in search_files if file.endswith('.html')]
89108

90109
# Copy the files of the search
91-
for file in html_files:
92-
lines = read(file)
110+
for root, file in html_files:
111+
lines = read(os.path.join(root, file))
93112

94-
new_name, substring = getVars(file, config)
113+
new_name, substring = getVars(os.path.join(root, file), config)
95114

96115
# HTML WRITE
97116

98117
# HTML overwrite link tags
99-
if(not isfile(join(_PATH, new_name))):
118+
if(config['overwriteFiles'] or not isfile(join(root, new_name))):
100119
for index, l in enumerate(lines):
101-
for file in css_files:
120+
for root, file in css_files:
102121
if file in l and 'link' in l:
103122
l = l.replace(
104123
file, f"{file.split('.')[0]}{config['extCopy']}.css")
@@ -111,13 +130,13 @@ def main():
111130
l = l.replace(k, 'c'+v)
112131
lines[index] = l
113132

114-
write(_PATH, new_name, lines)
133+
write(root, new_name, lines)
115134
count += 1
116-
print(f"{10*'*'} \t {new_name} \t {10*'*'}")
135+
print(f"{10*'*'} \t {new_name.split('/')[-1]} \t {10*'*'}")
117136

118137
# If it exists, pass
119138
else:
120-
print(f'!! file {new_name} already existed! !!')
139+
print(f'!! file already existed: {new_name}')
121140

122141
print()
123142
print(f'finished! {str(int(count/len(search_files))*100)}% done.')

css/newstyle-hashed.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.c427219 {
2+
color: black;
3+
background-color: aqua;
4+
}

css/newstyle.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.unique-class {
2+
color: black;
3+
background-color: aqua;
4+
}

htmls/another-hashed.html

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
4+
<head>
5+
<meta charset="UTF-8">
6+
<meta http-equiv="X-UA-Compatible" content="IE=edge">
7+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
8+
<link href="/style-hashed.css" rel="stylesheet">
9+
<link href="/css/newstyle-hashed.css" rel="stylesheet">
10+
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.13.0/css/all.css"
11+
integrity="sha384-Bfad6CLCknfcloXFOyFnlgtENryhrpZCe29RTifKEixXQZ38WheV+i/6YWSzkz3V" crossorigin="anonymous" />
12+
<title>Document</title>
13+
</head>
14+
15+
<body>
16+
17+
<div class="c427219 c538942 c427219">
18+
<p>Another Test of html</p>
19+
</div>
20+
21+
</body>
22+
23+
</html>

htmls/another.html

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
4+
<head>
5+
<meta charset="UTF-8">
6+
<meta http-equiv="X-UA-Compatible" content="IE=edge">
7+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
8+
<link href="/style.css" rel="stylesheet">
9+
<link href="/css/newstyle.css" rel="stylesheet">
10+
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.13.0/css/all.css"
11+
integrity="sha384-Bfad6CLCknfcloXFOyFnlgtENryhrpZCe29RTifKEixXQZ38WheV+i/6YWSzkz3V" crossorigin="anonymous" />
12+
<title>Document</title>
13+
</head>
14+
15+
<body>
16+
17+
<div class="unique-class center-text unique-class">
18+
<p>Another Test of html</p>
19+
</div>
20+
21+
</body>
22+
23+
</html>

index-hashed.html

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,37 +6,32 @@
66
<meta http-equiv="X-UA-Compatible" content="IE=edge">
77
<meta name="viewport" content="width=device-width, initial-scale=1.0">
88
<link href="/style-hashed.css" rel="stylesheet">
9-
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet"
10-
integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
11-
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.bundle.min.js"
12-
integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM"
13-
crossorigin="anonymous"></script>
14-
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.13.0/css/all.css"
15-
integrity="sha384-Bfad6CLCknfcloXFOyFnlgtENryhrpZCe29RTifKEixXQZ38WheV+i/6YWSzkz3V" crossorigin="anonymous" />
169
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.13.0/css/all.css"
1710
integrity="sha384-Bfad6CLCknfcloXFOyFnlgtENryhrpZCe29RTifKEixXQZ38WheV+i/6YWSzkz3V" crossorigin="anonymous" />
1811
<title>Document</title>
1912
</head>
2013

2114
<body>
2215

23-
<div class="c145221 c953905">
24-
<p class="c882365">This is a text of a html</p>
16+
<div class="c538942 c69861">
17+
<p class="c247297">This is a text of a html</p>
2518
</div>
2619

27-
<div class="c145221">
28-
<div class="c882365">
29-
<span class="c323667">test of another kind</span>
20+
<div class="c538942">
21+
<div class="c247297">
22+
<span class="c496546">test of another kind</span>
3023
</div>
3124
</div>
3225

33-
<!-- It doesn't change the cnd files -->
34-
<ul class="fa-ul">
35-
<li><i class="fa-li fa fa-check-square"></i>List icons</li>
36-
<li><i class="fa-li fa fa-check-square"></i>can be used</li>
37-
<li><i class="fa-li fa fa-spinner fa-spin"></i>as bullets</li>
38-
<li><i class="fa-li fa fa-square"></i>in lists</li>
39-
</ul>
26+
<div id="c501666">
27+
<!-- It doesn't change the cnd files -->
28+
<ul class="fa-ul">
29+
<li><i class="fa-li fa fa-check-square"></i>List icons</li>
30+
<li><i class="fa-li fa fa-check-square"></i>can be used</li>
31+
<li><i class="fa-li fa fa-spinner fa-spin"></i>as bullets</li>
32+
<li><i class="fa-li fa fa-square"></i>in lists</li>
33+
</ul>
34+
</div>
4035

4136
</body>
4237

index.html

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,6 @@
66
<meta http-equiv="X-UA-Compatible" content="IE=edge">
77
<meta name="viewport" content="width=device-width, initial-scale=1.0">
88
<link href="/style.css" rel="stylesheet">
9-
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet"
10-
integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
11-
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.bundle.min.js"
12-
integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM"
13-
crossorigin="anonymous"></script>
14-
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.13.0/css/all.css"
15-
integrity="sha384-Bfad6CLCknfcloXFOyFnlgtENryhrpZCe29RTifKEixXQZ38WheV+i/6YWSzkz3V" crossorigin="anonymous" />
169
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.13.0/css/all.css"
1710
integrity="sha384-Bfad6CLCknfcloXFOyFnlgtENryhrpZCe29RTifKEixXQZ38WheV+i/6YWSzkz3V" crossorigin="anonymous" />
1811
<title>Document</title>
@@ -30,13 +23,15 @@
3023
</div>
3124
</div>
3225

33-
<!-- It doesn't change the cnd files -->
34-
<ul class="fa-ul">
35-
<li><i class="fa-li fa fa-check-square"></i>List icons</li>
36-
<li><i class="fa-li fa fa-check-square"></i>can be used</li>
37-
<li><i class="fa-li fa fa-spinner fa-spin"></i>as bullets</li>
38-
<li><i class="fa-li fa fa-square"></i>in lists</li>
39-
</ul>
26+
<div id="list-items">
27+
<!-- It doesn't change the cnd files -->
28+
<ul class="fa-ul">
29+
<li><i class="fa-li fa fa-check-square"></i>List icons</li>
30+
<li><i class="fa-li fa fa-check-square"></i>can be used</li>
31+
<li><i class="fa-li fa fa-spinner fa-spin"></i>as bullets</li>
32+
<li><i class="fa-li fa fa-square"></i>in lists</li>
33+
</ul>
34+
</div>
4035

4136
</body>
4237

0 commit comments

Comments
 (0)