@@ -27,6 +27,8 @@ class Minify_CSS_Names():
27
27
start_prefix (str): Prefix added at the start of the selector to be minified.
28
28
end_prefix (str): Prefix added at the end of the selector to be minified.
29
29
min_letters (int): Minimum number of letters in the minified selector.
30
+ start_selector (str): Unique prefix that you want to put at the start of the minifed selector.
31
+ end_selector (str): Unique prefix that you want to put at the end of the minifed selector.
30
32
31
33
Raises:
32
34
ValueError: If `min_letters` is zero or negative.
@@ -40,6 +42,8 @@ class Minify_CSS_Names():
40
42
js (list): List of JS file paths to be checked for CSS selectors to be minified.
41
43
min_letters (int): Minimum number of letters in the minified selector.
42
44
regex_pattern (str): Regular expression pattern to match CSS selectors.
45
+ start_selector (str): Unique prefix that you want to put at the start of the minifed selector.
46
+ end_selector (str): Unique prefix that you want to put at the end of the minifed selector.
43
47
44
48
Methods:
45
49
Get_All_CSS_Selectors(self) -> set:
@@ -59,7 +63,7 @@ class Minify_CSS_Names():
59
63
Perfoms minification using above functions.
60
64
"""
61
65
62
- def __init__ (self , css = None , html = None , js = None , start_prefix = '-s-' , end_prefix = '-e-' , min_letters = 1 ):
66
+ def __init__ (self , css = None , html = None , js = None , start_prefix = '-s-' , end_prefix = '-e-' , min_letters = 1 , start_selector = '' , end_selector = '' ):
63
67
self .prefix = {
64
68
'start-prefix' : start_prefix ,
65
69
'end-prefix' : end_prefix
@@ -72,8 +76,10 @@ def __init__(self, css=None, html=None, js=None, start_prefix='-s-', end_prefix=
72
76
self .js = js if js else []
73
77
74
78
self .min_letters = min_letters
79
+ self .start_selector = start_selector
80
+ self .end_selector = end_selector
75
81
76
- self .regex_pattern = rf'^.* [.#]{ self .prefix ["start-prefix" ]} .* { self .prefix ["end-prefix" ]} '
82
+ self .regex_pattern = rf'( [.#])( { self .prefix ["start-prefix" ]} [a-zA-Z0-9_-]+ { self .prefix ["end-prefix" ]} ) '
77
83
78
84
if self .min_letters <= 0 :
79
85
raise ValueError ("min_letters cannot be equal to 0 or less than 0" )
@@ -92,6 +98,7 @@ def Get_All_CSS_Selectors(self) -> set:
92
98
with open (path , 'rb' ) as css_file :
93
99
css = css_file .read ().decode ()
94
100
for selector in re .findall (self .regex_pattern , css , re .MULTILINE ):
101
+ selector = '' .join (selector )
95
102
selectors = filter (lambda selector : selector != '' , selector .replace (',' , '' ).split (' ' ))
96
103
for selector in selectors :
97
104
selector = selector .replace ('\t ' , '' )
@@ -126,7 +133,7 @@ def Generate_Map_For_CSS_Selectors(self) -> dict:
126
133
127
134
generator = self .Generate_Minifed_Selectors ()
128
135
for selector in self .css_selectors :
129
- self .name_map [selector ] = selector [0 ] + '' .join (next (generator ))
136
+ self .name_map [selector ] = selector [0 ] + self . start_selector + '' .join (next (generator )) + self . end_selector
130
137
131
138
return self .name_map
132
139
@@ -142,9 +149,9 @@ def Replace_CSS_Selectors_With_Minifed(self, backup=True) -> None:
142
149
"""
143
150
144
151
for path in self .css + self .html + self .js :
145
- with open (path , 'ab+ ' ) as file :
152
+ with open (path , 'a+' , encoding = 'utf-8 ' ) as file :
146
153
file .seek (0 )
147
- new_css = file .read (). decode ()
154
+ new_css = file .read ()
148
155
149
156
for old_selector , new_selector in self .name_map .items ():
150
157
if path .split ('.' )[- 1 ] in ['html' , 'js' ]:
@@ -158,7 +165,7 @@ def Replace_CSS_Selectors_With_Minifed(self, backup=True) -> None:
158
165
copyfile (path , path + f'-{ time ()} .bak' )
159
166
160
167
file .truncate (0 )
161
- file .write (new_css . encode () )
168
+ file .write (new_css )
162
169
163
170
def Minify (self , backup = True ) -> None :
164
171
"""
@@ -175,13 +182,16 @@ def Minify(self, backup=True) -> None:
175
182
self .Replace_CSS_Selectors_With_Minifed (backup = backup )
176
183
177
184
if __name__ == "__main__" :
185
+ import glob
178
186
m = Minify_CSS_Names (
179
- css = ['src/style .css' ],
180
- html = ['src/index.html' ],
181
- js = ['src/main.js' ],
187
+ css = ['main .css' ],
188
+ html = [],
189
+ js = [],
182
190
start_prefix = '-s-' ,
183
191
end_prefix = '-e-' ,
184
- min_letters = 2
192
+ min_letters = 2 ,
193
+ start_selector = 'l' ,
194
+ end_selector = 's'
185
195
)
186
196
187
197
m .Minify ()
0 commit comments