@@ -27,6 +27,8 @@ class Minify_CSS_Names():
2727 start_prefix (str): Prefix added at the start of the selector to be minified.
2828 end_prefix (str): Prefix added at the end of the selector to be minified.
2929 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.
3032
3133 Raises:
3234 ValueError: If `min_letters` is zero or negative.
@@ -40,6 +42,8 @@ class Minify_CSS_Names():
4042 js (list): List of JS file paths to be checked for CSS selectors to be minified.
4143 min_letters (int): Minimum number of letters in the minified selector.
4244 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.
4347
4448 Methods:
4549 Get_All_CSS_Selectors(self) -> set:
@@ -59,7 +63,7 @@ class Minify_CSS_Names():
5963 Perfoms minification using above functions.
6064 """
6165
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 = '' ):
6367 self .prefix = {
6468 'start-prefix' : start_prefix ,
6569 'end-prefix' : end_prefix
@@ -72,8 +76,10 @@ def __init__(self, css=None, html=None, js=None, start_prefix='-s-', end_prefix=
7276 self .js = js if js else []
7377
7478 self .min_letters = min_letters
79+ self .start_selector = start_selector
80+ self .end_selector = end_selector
7581
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" ]} ) '
7783
7884 if self .min_letters <= 0 :
7985 raise ValueError ("min_letters cannot be equal to 0 or less than 0" )
@@ -92,6 +98,7 @@ def Get_All_CSS_Selectors(self) -> set:
9298 with open (path , 'rb' ) as css_file :
9399 css = css_file .read ().decode ()
94100 for selector in re .findall (self .regex_pattern , css , re .MULTILINE ):
101+ selector = '' .join (selector )
95102 selectors = filter (lambda selector : selector != '' , selector .replace (',' , '' ).split (' ' ))
96103 for selector in selectors :
97104 selector = selector .replace ('\t ' , '' )
@@ -126,7 +133,7 @@ def Generate_Map_For_CSS_Selectors(self) -> dict:
126133
127134 generator = self .Generate_Minifed_Selectors ()
128135 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
130137
131138 return self .name_map
132139
@@ -142,9 +149,9 @@ def Replace_CSS_Selectors_With_Minifed(self, backup=True) -> None:
142149 """
143150
144151 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 :
146153 file .seek (0 )
147- new_css = file .read (). decode ()
154+ new_css = file .read ()
148155
149156 for old_selector , new_selector in self .name_map .items ():
150157 if path .split ('.' )[- 1 ] in ['html' , 'js' ]:
@@ -158,7 +165,7 @@ def Replace_CSS_Selectors_With_Minifed(self, backup=True) -> None:
158165 copyfile (path , path + f'-{ time ()} .bak' )
159166
160167 file .truncate (0 )
161- file .write (new_css . encode () )
168+ file .write (new_css )
162169
163170 def Minify (self , backup = True ) -> None :
164171 """
@@ -175,13 +182,16 @@ def Minify(self, backup=True) -> None:
175182 self .Replace_CSS_Selectors_With_Minifed (backup = backup )
176183
177184if __name__ == "__main__" :
185+ import glob
178186 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 = [],
182190 start_prefix = '-s-' ,
183191 end_prefix = '-e-' ,
184- min_letters = 2
192+ min_letters = 2 ,
193+ start_selector = 'l' ,
194+ end_selector = 's'
185195 )
186196
187197 m .Minify ()
0 commit comments