|
26 | 26 | from tempfile import gettempdir
|
27 | 27 | from time import sleep
|
28 | 28 |
|
29 |
| -from bs4 import BeautifulSoup |
| 29 | +try: |
| 30 | + from bs4 import BeautifulSoup |
| 31 | +except ImportError: |
| 32 | + print("BeautifulSoup4 Not Found!, use: sudo apt-get install python3-bs4") |
30 | 33 |
|
31 | 34 | try:
|
32 | 35 | from urllib import request
|
@@ -171,8 +174,10 @@ def _props_grouper(props, pgs):
|
171 | 174 | log.debug("Grouping all CSS / SCSS Properties.")
|
172 | 175 | if not props:
|
173 | 176 | return props
|
174 |
| - props = sorted([_ if _.strip().endswith(";") |
175 |
| - else _.rstrip() + ";\n" for _ in props]) |
| 177 | + props = sorted([ |
| 178 | + _ if _.strip().endswith(";") |
| 179 | + and not _.strip().endswith("*/") and not _.strip().endswith("/*") |
| 180 | + else _.rstrip() + ";\n" for _ in props]) |
176 | 181 | props_pg = zip(map(lambda prop: _prioritify(prop, pgs), props), props)
|
177 | 182 | props_pg = sorted(props_pg, key=lambda item: item[0][1])
|
178 | 183 | props_by_groups = map(
|
@@ -227,7 +232,7 @@ def remove_empty_rules(css):
|
227 | 232 | def condense_zero_units(css):
|
228 | 233 | """Replace `0(px, em, %, etc)` with `0`."""
|
229 | 234 | log.debug("Condensing all zeroes on values.")
|
230 |
| - return re.sub(r"([\s:])(0)(px|em|%|in|cm|mm|pc|pt|ex)", r"\1\2", css) |
| 235 | + return re.sub(r"([\s:])(0)(px|em|rem|%|in|cm|mm|pc|pt|ex)", r"\1\2", css) |
231 | 236 |
|
232 | 237 |
|
233 | 238 | def condense_semicolons(css):
|
@@ -266,11 +271,11 @@ def normalize_whitespace(css):
|
266 | 271 | css = re.sub(r"\n{3}", "\n\n\n", css) # if 3 new lines,make them 2
|
267 | 272 | css = re.sub(r"\n{5}", "\n\n\n\n\n", css) # if 5 new lines, make them 4
|
268 | 273 | h_line = "/* {} */".format("-" * 72) # if >6 new lines, horizontal line
|
269 |
| - css = re.sub(r"\n{6,}", "\n\n\n\n\n{}\n\n\n\n\n".format(h_line), css) |
| 274 | + css = re.sub(r"\n{6,}", "\n\n\n{}\n\n\n".format(h_line), css) |
270 | 275 | css = css.replace(" ;\n", ";\n").replace("{\n", " {\n")
|
271 | 276 | css = re.sub("\s{2,}{\n", " {\n", css)
|
272 | 277 | log.debug("Finished Normalize white spaces on CSS.")
|
273 |
| - return css.replace("\t", " ").rstrip("\n") + "\n\n" |
| 278 | + return css.replace("\t", " ").rstrip() + "\n" |
274 | 279 |
|
275 | 280 |
|
276 | 281 | def justify_right(css):
|
@@ -304,16 +309,39 @@ def justify_right(css):
|
304 | 309 | return right_justified_css if max_indent > 1 else css
|
305 | 310 |
|
306 | 311 |
|
| 312 | +def split_long_selectors(css): |
| 313 | + """Split too large CSS Selectors chained with commas if > 80 chars.""" |
| 314 | + log.debug("Splitting too long chained selectors on CSS / SCSS.") |
| 315 | + result = "" |
| 316 | + for line in css.splitlines(): |
| 317 | + cond_1 = len(line) > 80 and "," in line and line.strip().endswith("{") |
| 318 | + cond_2 = line.startswith(("*", ".", "#")) |
| 319 | + if cond_1 and cond_2: |
| 320 | + result += line.replace(", ", ",").replace(",", ",\n").replace( |
| 321 | + "{", "{\n") |
| 322 | + else: |
| 323 | + result += line + "\n" |
| 324 | + return result |
| 325 | + |
| 326 | + |
| 327 | +def simple_replace(css): |
| 328 | + """dumb simple replacements on CSS.""" |
| 329 | + return css.replace("}\n#", "}\n\n#").replace( |
| 330 | + "}\n.", "}\n\n.").replace("}\n*", "}\n\n*") |
| 331 | + |
| 332 | + |
307 | 333 | def css_prettify(css, justify=False):
|
308 | 334 | """Prettify CSS main function."""
|
309 | 335 | log.info("Prettify CSS / SCSS...")
|
310 | 336 | css = sort_properties(css)
|
311 | 337 | css = condense_zero_units(css)
|
312 | 338 | css = wrap_css_lines(css, 80)
|
| 339 | + css = split_long_selectors(css) |
313 | 340 | css = condense_semicolons(css)
|
314 | 341 | css = normalize_whitespace(css)
|
315 | 342 | css = justify_right(css) if justify else css
|
316 | 343 | css = add_encoding(css)
|
| 344 | + css = simple_replace(css) |
317 | 345 | log.info("Finished Prettify CSS / SCSS !.")
|
318 | 346 | return css
|
319 | 347 |
|
|
0 commit comments