formatcss.sh - randomcrap - random crap programs of varying quality
(HTM) git clone git://git.codemadness.org/randomcrap
(DIR) Log
(DIR) Files
(DIR) Refs
(DIR) README
(DIR) LICENSE
---
formatcss.sh (1155B)
---
1 #!/bin/sh
2 # sort and format CSS.
3 #
4 # NOTES/CAVEATS:
5 # - this is very crude and not a real parser so it will have bugs.
6 # - when there are multiple rules with the same name the sort order will
7 # change the behaviour (obviously).
8 # - CSS3 keyframes, media selectors and complex rules are not supported.
9 #
10 # What it does:
11 # - replace "MS-DOS"-style line-endings: "\r\n" to "\n".
12 # - strip comments.
13 # - sort rules alphabetically.
14 # - indent rules with a TAB.
15
16 tr -d '\t\r\n' | \
17 sed -E \
18 -e 's,/\*([^*]|(\*+[^*/]))*\*+/,,g' \
19 -e 's/}/;}/g' \
20 -e 's/[{};]/&\
21 /g' | \
22 sed \
23 -e '/^[[:blank:]]*$/d' \
24 -e 's/^[[:blank:]]*//g' \
25 -e 's/[[:blank:]]*$//g' \
26 -e '/^;$/d' | \
27 awk '
28 BEGIN {
29 b=0;
30 type=0;
31 }
32 /{/{
33 sn=0;
34 split($0, sel, ",");
35 for (i = 1; i <= length(sel); i++) {
36 gsub("^[ ]*", "", sel[i]);
37 gsub("[ ]*$", "", sel[i]);
38 if (length(sel) == i) {
39 print b " " 1 " " sn " " sel[i];
40 } else {
41 print b " " 1 " " sn " " sel[i] ",";
42 }
43 sn++;
44 }
45 type=2;
46 next;
47 }
48 /}/{
49 type=0;
50 print b " 3 0 " $0;
51 b++;
52 next;
53 }
54 /@/ {
55 print b " " type " 0 " $0;
56 next;
57 }
58 {
59 print b " " type " 0 " $0;
60 }' | \
61 sort -k1,1n -k2,2n -k3,3n -k4 | \
62 cut -f 4-