--- factorcss/FactorCSS.hs 2004/12/30 21:22:59 66 +++ factorcss/FactorCSS.hs 2004/12/30 23:22:02 67 @@ -34,9 +34,12 @@ | null args = go factor | argument "factor" args = go factor | argument "explode" args = go (concatMap explode) + | argument "identity" args = go id + | argument "tree" args = show stylesheet ++ "\n" + | argument "statistics" args = stats stylesheet ++ "\n" | otherwise = usage - where go f = (unlines . show_stylesheet . process f . - parser . alexScanTokens) input + where go f = (unlines . show_stylesheet . process f) stylesheet + stylesheet = (parser . alexScanTokens) input -- Check an argument list for an argument in short or long form. argument :: String -> [String] -> Bool @@ -56,10 +59,13 @@ "standard output.", "", "Output mode:", - " -f, --factor factor out common declarations (default)", - " -e, --explode produce rulesets with one selector and declaration each", + " -f, --factor factor out common declarations (default)", + " -e, --explode produce rulesets with one selector and declaration each", + " -i, --identity just parse and output unmodified", + " -t, --tree parse and display parse tree", + " -s, --statistics count rulesets, selectors, and declarations", "", - " -h, --help display this help and exit"] + " -h, --help display this help and exit"] -- Process a Stylesheet. process :: ([Statement] -> [Statement]) -> Stylesheet -> Stylesheet @@ -136,8 +142,6 @@ implode_sel :: [Statement] -> Statement implode_sel ((Ruleset sels decls):stmts) = Ruleset sels (concat (decls:(map declarations stmts))) - where declarations :: Statement -> [Declaration] - declarations (Ruleset s d) = d -- Implode a list of Ruleset Statements with equal lists of Declarations to a -- single equivalent Ruleset Statement. @@ -149,5 +153,40 @@ implode_decl :: [Statement] -> Statement implode_decl ((Ruleset sels decls):stmts) = Ruleset (concat (sels:(map selectors stmts))) decls - where selectors :: Statement -> [Selector] - selectors (Ruleset s d) = s + +-- Count rulesets, selectors, and declarations in a Stylesheet. +stats :: Stylesheet -> String +stats (Stylesheet charset imports stmts) + | n == 0 = "0 rulesets" + | otherwise = "rulesets " ++ show n ++ + ", selectors min " ++ show s0 ++ + " max " ++ show s1 ++ + " mean " ++ take 5 ( + show (fromIntegral sn / fromIntegral n)) ++ + ", declarations min " ++ show d0 ++ + " max " ++ show d1 ++ + " mean " ++ take 5 ( + show (fromIntegral dn / fromIntegral n)) + where [n, s0, s1, sn, d0, d1, dn] = stats_stmts stmts + +-- Count rulesets, selectors, and declarations in a list of Statements. +stats_stmts :: [Statement] -> [Int] +stats_stmts stmts = stats_rulesets (filter is_ruleset stmts ++ + (concatMap get_stmts (filter is_media stmts))) + where get_stmts (Media media stmts) = stmts + +-- Count rulesets, selectors, and declarations in a list of Ruleset Statements. +stats_rulesets :: [Statement] -> [Int] +stats_rulesets stmts = [length stmts, + minimum sel_lengths, maximum sel_lengths, sum sel_lengths, + minimum decl_lengths, maximum decl_lengths, sum decl_lengths] + where sel_lengths = map (length . selectors) stmts + decl_lengths = map (length . declarations) stmts + +-- Extract the list of Selectors from a Ruleset Statement. +selectors :: Statement -> [Selector] +selectors (Ruleset s d) = s + +-- Extract the list of Declarations from a Ruleset Statement. +declarations :: Statement -> [Declaration] +declarations (Ruleset s d) = d