Skip to content

Commit

Permalink
Update changes
Browse files Browse the repository at this point in the history
  • Loading branch information
DenysPacheco committed Jan 21, 2022
1 parent 5132277 commit f857b53
Show file tree
Hide file tree
Showing 13 changed files with 151 additions and 72 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
.vscode
.pytest_cache
test.py
18 changes: 8 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,24 @@ It was inspired on Google's approach to minimize classes names to faster load we

## What it does

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

## Usage

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

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

It will give the output with `foo-hashed.html` and `foo-hashed.css`
It will give the output with `foo-hashed.html` and `foo-hashed.css`.

### Configure

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

## Future features
## To Change?

- [ ] Hashfy id's
- [ ] Multiple files on mutiple folders (with track)
- [ ] Hash function use letters
- [ ] Delete old files on new run
- [ ] Add tests?
- [ ] Separation of classes and ids in prefix
11 changes: 9 additions & 2 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,15 @@
"filesIgnore": [
"-hashed"
],
"dirsIgnore":[
".",
"packages",
"src",
"dist"
],
"extCopy": "-hashed",
"patternCSS": "\\.-?[_a-zA-Z]+[_a-zA-Z0-9-]*\\s*\\{",
"patternCSS": "[\\.\\#]-?[_a-zA-Z]+[_a-zA-Z0-9-]*\\s*\\{",
"patternHTML": "class[\t]*=[\t]*\"[^\"]+",
"hashLength": 6
"hashLength": 6,
"overwriteFiles": false
}
65 changes: 42 additions & 23 deletions convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ def read(file):
return lines


def write(_PATH, new_name, lines):
with open(_PATH + new_name, 'w') as exf:
def write(root, new_name, lines):
with open(os.path.join(root, new_name), 'w') as exf:
exf.writelines(lines)
exf.close()

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

search_files = []
# Look for files
search_files = [f for f in listdir(_PATH) if isfile(join(_PATH, f)) and [bool(
s) for s in config['filesSearch'] if f.endswith(s)] and [bool(i) for i in config['filesIgnore'] if i not in f]]
for root, subdirectories, files in os.walk(_PATH):
# Comprehension to break outter loop
if any([dirsIgnore for dirsIgnore in config['dirsIgnore'] if dirsIgnore in root]):
continue

# And all its files
for index, file in enumerate(files):
for filesSearch in config['filesSearch']:
for filesIgnore in config['filesIgnore']:
if(filesSearch in file and filesIgnore not in file):
search_files.append((root, file))

#print(f'files: {search_files}\n')

print('CSS Hashfy found files:', end='\n\n')
for root, file in search_files:
print(os.path.join(root, file))
print()

print(f'files: {search_files}\n')
print('Starting hashing...', end='\n\n')

# Initializing alg vars
count = 0
classes_dict = {}

# Get all the files with the '.css' extension
css_files = [file for file in search_files if '.css' in file]
css_files = [(root, file)
for root, file in search_files if file.endswith('.css')]

# Copy the files of the search
for file in css_files:
lines = read(file)
for root, file in css_files:
lines = read(os.path.join(root, file))

new_name, substring = getVars(file, config)
new_name, substring = getVars(os.path.join(root, file), config)

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

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

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

# If it exists, pass
else:
print(f'!! file {new_name} already existed! !!')
print(f'!! file already existed: {new_name}')

# Overwrite HTML classes
html_files = [file for file in search_files if '.html' in file]
html_files = [(root, file)
for root, file in search_files if file.endswith('.html')]

# Copy the files of the search
for file in html_files:
lines = read(file)
for root, file in html_files:
lines = read(os.path.join(root, file))

new_name, substring = getVars(file, config)
new_name, substring = getVars(os.path.join(root, file), config)

# HTML WRITE

# HTML overwrite link tags
if(not isfile(join(_PATH, new_name))):
if(config['overwriteFiles'] or not isfile(join(root, new_name))):
for index, l in enumerate(lines):
for file in css_files:
for root, file in css_files:
if file in l and 'link' in l:
l = l.replace(
file, f"{file.split('.')[0]}{config['extCopy']}.css")
Expand All @@ -111,13 +130,13 @@ def main():
l = l.replace(k, 'c'+v)
lines[index] = l

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

# If it exists, pass
else:
print(f'!! file {new_name} already existed! !!')
print(f'!! file already existed: {new_name}')

print()
print(f'finished! {str(int(count/len(search_files))*100)}% done.')
Expand Down
4 changes: 4 additions & 0 deletions css/newstyle-hashed.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.c427219 {
color: black;
background-color: aqua;
}
4 changes: 4 additions & 0 deletions css/newstyle.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.unique-class {
color: black;
background-color: aqua;
}
23 changes: 23 additions & 0 deletions htmls/another-hashed.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="/style-hashed.css" rel="stylesheet">
<link href="/css/newstyle-hashed.css" rel="stylesheet">
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.13.0/css/all.css"
integrity="sha384-Bfad6CLCknfcloXFOyFnlgtENryhrpZCe29RTifKEixXQZ38WheV+i/6YWSzkz3V" crossorigin="anonymous" />
<title>Document</title>
</head>

<body>

<div class="c427219 c538942 c427219">
<p>Another Test of html</p>
</div>

</body>

</html>
23 changes: 23 additions & 0 deletions htmls/another.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="/style.css" rel="stylesheet">
<link href="/css/newstyle.css" rel="stylesheet">
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.13.0/css/all.css"
integrity="sha384-Bfad6CLCknfcloXFOyFnlgtENryhrpZCe29RTifKEixXQZ38WheV+i/6YWSzkz3V" crossorigin="anonymous" />
<title>Document</title>
</head>

<body>

<div class="unique-class center-text unique-class">
<p>Another Test of html</p>
</div>

</body>

</html>
33 changes: 14 additions & 19 deletions index-hashed.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,37 +6,32 @@
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="/style-hashed.css" rel="stylesheet">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet"
integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.bundle.min.js"
integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM"
crossorigin="anonymous"></script>
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.13.0/css/all.css"
integrity="sha384-Bfad6CLCknfcloXFOyFnlgtENryhrpZCe29RTifKEixXQZ38WheV+i/6YWSzkz3V" crossorigin="anonymous" />
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.13.0/css/all.css"
integrity="sha384-Bfad6CLCknfcloXFOyFnlgtENryhrpZCe29RTifKEixXQZ38WheV+i/6YWSzkz3V" crossorigin="anonymous" />
<title>Document</title>
</head>

<body>

<div class="c145221 c953905">
<p class="c882365">This is a text of a html</p>
<div class="c538942 c69861">
<p class="c247297">This is a text of a html</p>
</div>

<div class="c145221">
<div class="c882365">
<span class="c323667">test of another kind</span>
<div class="c538942">
<div class="c247297">
<span class="c496546">test of another kind</span>
</div>
</div>

<!-- It doesn't change the cnd files -->
<ul class="fa-ul">
<li><i class="fa-li fa fa-check-square"></i>List icons</li>
<li><i class="fa-li fa fa-check-square"></i>can be used</li>
<li><i class="fa-li fa fa-spinner fa-spin"></i>as bullets</li>
<li><i class="fa-li fa fa-square"></i>in lists</li>
</ul>
<div id="c501666">
<!-- It doesn't change the cnd files -->
<ul class="fa-ul">
<li><i class="fa-li fa fa-check-square"></i>List icons</li>
<li><i class="fa-li fa fa-check-square"></i>can be used</li>
<li><i class="fa-li fa fa-spinner fa-spin"></i>as bullets</li>
<li><i class="fa-li fa fa-square"></i>in lists</li>
</ul>
</div>

</body>

Expand Down
23 changes: 9 additions & 14 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,6 @@
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="/style.css" rel="stylesheet">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet"
integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.bundle.min.js"
integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM"
crossorigin="anonymous"></script>
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.13.0/css/all.css"
integrity="sha384-Bfad6CLCknfcloXFOyFnlgtENryhrpZCe29RTifKEixXQZ38WheV+i/6YWSzkz3V" crossorigin="anonymous" />
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.13.0/css/all.css"
integrity="sha384-Bfad6CLCknfcloXFOyFnlgtENryhrpZCe29RTifKEixXQZ38WheV+i/6YWSzkz3V" crossorigin="anonymous" />
<title>Document</title>
Expand All @@ -30,13 +23,15 @@
</div>
</div>

<!-- It doesn't change the cnd files -->
<ul class="fa-ul">
<li><i class="fa-li fa fa-check-square"></i>List icons</li>
<li><i class="fa-li fa fa-check-square"></i>can be used</li>
<li><i class="fa-li fa fa-spinner fa-spin"></i>as bullets</li>
<li><i class="fa-li fa fa-square"></i>in lists</li>
</ul>
<div id="list-items">
<!-- It doesn't change the cnd files -->
<ul class="fa-ul">
<li><i class="fa-li fa fa-check-square"></i>List icons</li>
<li><i class="fa-li fa fa-check-square"></i>can be used</li>
<li><i class="fa-li fa fa-spinner fa-spin"></i>as bullets</li>
<li><i class="fa-li fa fa-square"></i>in lists</li>
</ul>
</div>

</body>

Expand Down
13 changes: 9 additions & 4 deletions style-hashed.css
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
.c145221 {
.c538942 {
text-align: center !important;
}

.c953905 {
.c69861 {
font-size: calc(1.475rem + 2.7vw);
font-weight: 300;
line-height: 1.2;
}

.c882365 {
.c247297 {
background-color: greenyellow;
}

.c323667 {
.c496546 {
background-color: red;
}

#c501666 {
width: max-content;
margin: auto;
}
5 changes: 5 additions & 0 deletions style.css
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,8 @@
.backg-danger {
background-color: red;
}

#list-items {
width: max-content;
margin: auto;
}
Empty file removed test.py
Empty file.

0 comments on commit f857b53

Please sign in to comment.