Commit 52cdbb3c authored by Taddeüs Kroes's avatar Taddeüs Kroes

Grouped font-weight and color optimizations in simple.ml

parent a9d17d07
RESULT := mincss RESULT := mincss
PRE_TGTS := types PRE_TGTS := types
MODULES := color_names util stringify parser lexer parse selector color \ MODULES := color_names util stringify parser lexer parse selector simple \
shorthand duplicates font main shorthand duplicates main
ALL_NAMES := $(PRE_TGTS) $(MODULES) ALL_NAMES := $(PRE_TGTS) $(MODULES)
OCAMLCFLAGS := -g OCAMLCFLAGS := -g
...@@ -36,10 +36,10 @@ lexer.cmi: lexer.ml ...@@ -36,10 +36,10 @@ lexer.cmi: lexer.ml
parser.cmx: parser.cmi lexer.cmx parser.cmx: parser.cmi lexer.cmx
parser.mli: parser.ml parser.mli: parser.ml
parse.cmx: lexer.cmi parser.cmx parse.cmx: lexer.cmi parser.cmx
main.cmx: parse.cmx util.cmx color.cmx shorthand.cmx duplicates.cmx font.cmx main.cmx: parse.cmx util.cmx simple.cmx shorthand.cmx duplicates.cmx
util.cmx: OCAMLCFLAGS += -pp cpp util.cmx: OCAMLCFLAGS += -pp cpp
util.cmx color.cmx: color_names.cmx util.cmx simple.cmx: color_names.cmx
stringify.cmx parser.cmx color.cmx shorthand.cmx: util.cmi stringify.cmx parser.cmx simple.cmx shorthand.cmx: util.cmi
$(addsuffix .cmx,$(MODULES)): $(addsuffix .cmi,$(PRE_TGTS)) $(addsuffix .cmx,$(MODULES)): $(addsuffix .cmi,$(PRE_TGTS))
clean: clean:
......
open Types
let rec shorten = function
| Ident "normal" -> Number (400.0, None)
| Ident "bold" -> Number (700.0, None)
| v -> v
let transform = function
| Declaration ("font-weight", value, imp) ->
Declaration ("font-weight", shorten value, imp)
| v -> v
let compress = Util.transform_stylesheet transform
...@@ -6,8 +6,7 @@ type args = { ...@@ -6,8 +6,7 @@ type args = {
outfile : string option; outfile : string option;
verbose : bool; verbose : bool;
whitespace : bool; whitespace : bool;
color : bool; simple : bool;
font : bool;
shorthands : bool; shorthands : bool;
duplicates : bool; duplicates : bool;
echo : bool; echo : bool;
...@@ -27,12 +26,11 @@ let parse_args () = ...@@ -27,12 +26,11 @@ let parse_args () =
Optimization flags (if none are specified, all are enabled):\n \ Optimization flags (if none are specified, all are enabled):\n \
-w, --whitespace Eliminate unnecessary whitespaces (has the greatest \ -w, --whitespace Eliminate unnecessary whitespaces (has the greatest \
effect, omit for pretty-printing)\n \ effect, omit for pretty-printing)\n \
-c, --color Shorten colors\n \ -c, --simple Shorten colors and font weights\n \
-f, --font Shorten font weights\n \
-s, --shorthands Generate shorthand properties\n \ -s, --shorthands Generate shorthand properties\n \
-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 -f -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"
in in
...@@ -41,8 +39,7 @@ let parse_args () = ...@@ -41,8 +39,7 @@ let parse_args () =
outfile = None; outfile = None;
verbose = false; verbose = false;
whitespace = false; whitespace = false;
color = false; simple = false;
font = false;
shorthands = false; shorthands = false;
duplicates = false; duplicates = false;
echo = false; echo = false;
...@@ -53,16 +50,14 @@ let parse_args () = ...@@ -53,16 +50,14 @@ let parse_args () =
handle {args with verbose = true} tl handle {args with verbose = true} tl
| ("-w" | "--whitespace") :: tl -> | ("-w" | "--whitespace") :: tl ->
handle {args with whitespace = true} tl handle {args with whitespace = true} tl
| ("-c" | "--color") :: tl -> | ("-c" | "--simple") :: tl ->
handle {args with color = true} tl handle {args with simple = true} tl
| ("-f" | "--font") :: tl ->
handle {args with font = true} tl
| ("-s" | "--shorthands") :: tl -> | ("-s" | "--shorthands") :: tl ->
handle {args with shorthands = true} tl handle {args with shorthands = true} tl
| ("-d" | "-duplicates") :: tl -> | ("-d" | "-duplicates") :: tl ->
handle {args with duplicates = true} tl handle {args with duplicates = true} tl
| ("-p" | "--pretty") :: tl -> | ("-p" | "--pretty") :: tl ->
handle args ("-c" :: "-f" :: "-s" :: "-d" :: 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
...@@ -89,16 +84,14 @@ let parse_args () = ...@@ -89,16 +84,14 @@ let parse_args () =
match handle default_args (List.tl (Array.to_list Sys.argv)) with match handle default_args (List.tl (Array.to_list Sys.argv)) with
| { whitespace = false; | { whitespace = false;
color = false; simple = false;
font = false;
shorthands = false; shorthands = false;
duplicates = false; duplicates = false;
echo = false; echo = false;
_ } as args -> _ } as args ->
{ args with { args with
whitespace = true; whitespace = true;
color = true; simple = true;
font = true;
shorthands = true; shorthands = true;
duplicates = true } duplicates = true }
| args -> args | args -> args
...@@ -130,12 +123,10 @@ let handle_args args = ...@@ -130,12 +123,10 @@ let handle_args args =
let input, css = parse_files args.infiles in let input, css = parse_files args.infiles in
let css = css let css = css
|> switch args.color Color.compress
(* unfold before pruning duplicates so that shorthand components are (* unfold before pruning duplicates so that shorthand components are
* correctly pruned *) * correctly pruned *)
|> switch args.shorthands Shorthand.unfold_stylesheet |> switch args.shorthands Shorthand.unfold_stylesheet
|> switch args.font Font.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
in in
......
...@@ -12,7 +12,7 @@ let clip = function ...@@ -12,7 +12,7 @@ let clip = function
| Number (n, Some "%") when n > 100. -> Number (100., Some "%") | Number (n, Some "%") when n > 100. -> Number (100., Some "%")
| value -> value | value -> value
let rec short = function let rec shorten_expr = function
(* #aabbcc -> #abc *) (* #aabbcc -> #abc *)
| Hexcolor h when Str.string_match hex6 h 0 -> | Hexcolor h when Str.string_match hex6 h 0 ->
let gr n = Str.matched_group n h in let gr n = Str.matched_group n h in
...@@ -27,7 +27,7 @@ let rec short = function ...@@ -27,7 +27,7 @@ let rec short = function
| Number (n, Some "%") -> int_of_float (n *. 2.55 +. 0.5) | Number (n, Some "%") -> int_of_float (n *. 2.55 +. 0.5)
| _ -> assert false | _ -> assert false
in in
short (Hexcolor (Printf.sprintf "%02x%02x%02x" (i r) (i g) (i b))) shorten_expr (Hexcolor (Printf.sprintf "%02x%02x%02x" (i r) (i g) (i b)))
(* clip rgb values, e.g. rgb(-1,256,0) -> rgb(0,255,0) *) (* clip rgb values, e.g. rgb(-1,256,0) -> rgb(0,255,0) *)
| Function ("rgb", Nary (",", [r; g; b])) -> | Function ("rgb", Nary (",", [r; g; b])) ->
...@@ -35,15 +35,22 @@ let rec short = function ...@@ -35,15 +35,22 @@ let rec short = function
(* rgba(r,g,b,1.0) -> rgb(r,g,b) *) (* rgba(r,g,b,1.0) -> rgb(r,g,b) *)
| Function ("rgba", Nary (",", [r; g; b; Number (1., None)])) -> | Function ("rgba", Nary (",", [r; g; b; Number (1., None)])) ->
short (Function ("rgb", Nary (",", [r; g; b]))) shorten_expr (Function ("rgb", Nary (",", [r; g; b])))
(* TODO: hsl[a](...) *) (* TODO: hsl[a](...) *)
(* transform color names to shorter hex codes and vice-versa *) (* transform color names to shorter hex codes and vice-versa *)
| v -> Color_names.compress v | v -> Color_names.compress v
let shorten_font_weight = function
| Ident "normal" -> Number (400.0, None)
| Ident "bold" -> Number (700.0, None)
| v -> v
let transform = function let transform = function
| Expr value -> Expr (short value) | Expr value -> Expr (shorten_expr value)
| Declaration ("font-weight", value, imp) ->
Declaration ("font-weight", shorten_font_weight value, imp)
| v -> v | v -> v
let compress = Util.transform_stylesheet transform let compress = Util.transform_stylesheet transform
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