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

Added --sort option

parent 23ce71c2
......@@ -10,6 +10,7 @@ type args = {
shorthands : bool;
duplicates : bool;
echo : bool;
sort : bool;
}
let parse_args () =
......@@ -31,7 +32,11 @@ let parse_args () =
-d, --duplicates Prune duplicate properties (WARNING: may affect \
cross-browser hacks)\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
let default_args = {
......@@ -43,6 +48,7 @@ let parse_args () =
shorthands = false;
duplicates = false;
echo = false;
sort = false;
} in
let rec handle args = function
......@@ -60,6 +66,8 @@ let parse_args () =
handle {args with simple = true; shorthands = true; duplicates = true} tl
| ("-e" | "--echo") :: tl ->
handle {args with echo = true} tl
| "--sort" :: tl ->
handle {args with sort = true} tl
| ("-h" | "--help") :: tl ->
prerr_string usage;
......@@ -83,17 +91,24 @@ let parse_args () =
in
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;
simple = false;
shorthands = false;
duplicates = false;
echo = false;
_ } as args ->
{ args with
whitespace = true;
simple = true;
shorthands = true;
duplicates = true }
| args -> args
let parse_files = function
......@@ -131,6 +146,7 @@ let handle_args args =
|> switch args.simple Simple.compress
|> switch args.duplicates Duplicates.compress
|> switch args.shorthands Shorthand.compress
|> switch args.sort Util.sort_stylesheet
in
let output =
if args.whitespace
......
......@@ -231,3 +231,19 @@ let transform_stylesheet f stylesheet =
(* Expression identification *)
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