Commit 5003457c authored by Taddeüs Kroes's avatar Taddeüs Kroes

Added --sort option

parent 23ce71c2
...@@ -10,6 +10,7 @@ type args = { ...@@ -10,6 +10,7 @@ type args = {
shorthands : bool; shorthands : bool;
duplicates : bool; duplicates : bool;
echo : bool; echo : bool;
sort : bool;
} }
let parse_args () = let parse_args () =
...@@ -31,7 +32,11 @@ let parse_args () = ...@@ -31,7 +32,11 @@ let parse_args () =
-d, --duplicates Prune duplicate properties (WARNING: may affect \ -d, --duplicates Prune duplicate properties (WARNING: may affect \
cross-browser hacks)\n \ cross-browser hacks)\n \
-p, --pretty Shorthand for -c -s -d\n \ -p, --pretty Shorthand for -c -s -d\n \
-e, --echo Just parse and pretty-print, no optimizations\n" -e, --echo Just parse and pretty-print, no optimizations\n\
\n\
Formatting options:
--sort Sort declarations in each selector group\n\
"
in in
let default_args = { let default_args = {
...@@ -43,6 +48,7 @@ let parse_args () = ...@@ -43,6 +48,7 @@ let parse_args () =
shorthands = false; shorthands = false;
duplicates = false; duplicates = false;
echo = false; echo = false;
sort = false;
} in } in
let rec handle args = function let rec handle args = function
...@@ -60,6 +66,8 @@ let parse_args () = ...@@ -60,6 +66,8 @@ let parse_args () =
handle {args with simple = true; shorthands = true; duplicates = true} tl handle {args with simple = true; shorthands = true; duplicates = true} tl
| ("-e" | "--echo") :: tl -> | ("-e" | "--echo") :: tl ->
handle {args with echo = true} tl handle {args with echo = true} tl
| "--sort" :: tl ->
handle {args with sort = true} tl
| ("-h" | "--help") :: tl -> | ("-h" | "--help") :: tl ->
prerr_string usage; prerr_string usage;
...@@ -83,17 +91,24 @@ let parse_args () = ...@@ -83,17 +91,24 @@ let parse_args () =
in in
match handle default_args (List.tl (Array.to_list Sys.argv)) with match handle default_args (List.tl (Array.to_list Sys.argv)) with
| { echo = true; _ } as args ->
{ args with
whitespace = false;
simple = false;
shorthands = false;
duplicates = false }
| { whitespace = false; | { whitespace = false;
simple = false; simple = false;
shorthands = false; shorthands = false;
duplicates = false; duplicates = false;
echo = false;
_ } as args -> _ } as args ->
{ args with { args with
whitespace = true; whitespace = true;
simple = true; simple = true;
shorthands = true; shorthands = true;
duplicates = true } duplicates = true }
| args -> args | args -> args
let parse_files = function let parse_files = function
...@@ -131,6 +146,7 @@ let handle_args args = ...@@ -131,6 +146,7 @@ let handle_args args =
|> switch args.simple Simple.compress |> switch args.simple Simple.compress
|> switch args.duplicates Duplicates.compress |> switch args.duplicates Duplicates.compress
|> switch args.shorthands Shorthand.compress |> switch args.shorthands Shorthand.compress
|> switch args.sort Util.sort_stylesheet
in in
let output = let output =
if args.whitespace if args.whitespace
......
...@@ -231,3 +231,19 @@ let transform_stylesheet f stylesheet = ...@@ -231,3 +231,19 @@ let transform_stylesheet f stylesheet =
(* Expression identification *) (* Expression identification *)
let is_color = Color_names.is_color let is_color = Color_names.is_color
(* Sorting declarations *)
let sort_stylesheet =
let transform_sort_decls = function
| Statement (Ruleset (selectors, decls)) ->
let pattern = Str.regexp "^\\([^-]+\\)-" in
let stem x =
if Str.string_match pattern x 0 then Str.matched_group 1 x else x
in
let cmp (a, _, _) (b, _, _) = String.compare (stem a) (stem b) in
Statement (Ruleset (selectors, List.stable_sort cmp decls))
| v -> v
in
transform_stylesheet transform_sort_decls
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment