module Text.Hakyll.CompressCSS
( compressCSS
) where
import Data.List (isPrefixOf)
import Text.Regex (subRegex, mkRegex)
subRegex' :: String -> String -> String -> String
subRegex' pattern replacement str = subRegex (mkRegex pattern) str replacement
compressCSS :: String -> String
compressCSS = compressSeparators
. compressWhitespace
. stripComments
compressSeparators :: String -> String
compressSeparators = subRegex' ";\\s*}" "}"
. subRegex' "\\s*([{};:])\\s*" "\\1"
. subRegex' ";;*" ";"
compressWhitespace :: String -> String
compressWhitespace = subRegex' "\\s\\s*" " "
stripComments :: String -> String
stripComments [] = []
stripComments str | isPrefixOf "/*" str = stripComments $ eatComments $ drop 2 str
| otherwise = (head str) : (stripComments $ tail str)
where eatComments str' | null str' = []
| isPrefixOf "*/" str' = drop 2 str'
| otherwise = eatComments $ tail str'