--- factorcss/FactorCSS.hs 2004/12/27 19:45:02 63 +++ factorcss/FactorCSS.hs 2004/12/30 21:22:59 66 @@ -16,31 +16,65 @@ -- the stylesheet depends on the order of rules (see CSS 2.1 6.4.1). import List +import System import Tokeniser import Parser import CSS +-- Program entry function. +main :: IO () main = do - s <- getContents --- print (alexScanTokens s) --- (print . process . parser . alexScanTokens) s - (putStr . unlines . show_stylesheet . process . - parser . alexScanTokens) s + args <- getArgs + interact (run args) + +-- Produce output from arguments and input. +run :: [String] -> String -> String +run args input + | argument "help" args = usage + | null args = go factor + | argument "factor" args = go factor + | argument "explode" args = go (concatMap explode) + | otherwise = usage + where go f = (unlines . show_stylesheet . process f . + parser . alexScanTokens) input + +-- Check an argument list for an argument in short or long form. +argument :: String -> [String] -> Bool +argument long args = elem short shorts || elem ("--" ++ long) args + where short = head long + shorts = (concat . filter is_short) args + is_short ('-':'-':_) = False + is_short ('-':_) = True + is_short _ = False + +-- Usage help string. +usage :: String +usage = unlines ["Usage: factorcss [OPTION]... Stylesheet -process (Stylesheet charset imports stmts) = - Stylesheet charset imports (process_stmts stmts) +process :: ([Statement] -> [Statement]) -> Stylesheet -> Stylesheet +process f (Stylesheet charset imports stmts) = + Stylesheet charset imports (process_stmts f stmts) -- Process a list of Statements. -process_stmts :: [Statement] -> [Statement] -process_stmts stmts = (factor . filter is_ruleset) stmts ++ - (map process_media . filter is_media) stmts ++ +process_stmts :: ([Statement] -> [Statement]) -> [Statement] -> [Statement] +process_stmts f stmts = (f . filter is_ruleset) stmts ++ + (map (process_media f) . filter is_media) stmts ++ filter is_page stmts -- Process a Media Statement. -process_media :: Statement -> Statement -process_media (Media media stmts) = Media media (factor stmts) +process_media :: ([Statement] -> [Statement]) -> Statement -> Statement +process_media f (Media media stmts) = Media media (f stmts) -- Factor a list of Ruleset Statements. factor :: [Statement] -> [Statement] @@ -87,7 +121,8 @@ eq_sel :: Statement -> Statement -> Bool eq_sel z = (== EQ) . cmp_sel z --- Compare two Ruleset Statements for equality by the first declaration in each. +-- Compare two Ruleset Statements for equality by the first declaration in +-- each. eq_decl :: Statement -> Statement -> Bool eq_decl z = (== EQ) . cmp_decl z