module Text.Hakyll.Internal.CompressCSS
( compressCSS
) where
import Data.List (isPrefixOf)
import Text.Hakyll.Regex (substituteRegex)
compressCSS :: String -> String
compressCSS = compressSeparators
. stripComments
. compressWhitespace
compressSeparators :: String -> String
compressSeparators = substituteRegex "; *}" "}"
. substituteRegex " *([{};:]) *" "\\1"
. substituteRegex ";;*" ";"
compressWhitespace :: String -> String
compressWhitespace = substituteRegex "[ \t\n][ \t\n]*" " "
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'