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