on fb71a21 help sign in
hakyll/src/Text/Hakyll/CompressCSS.hs
31 lines 1.1 kB history raw
1module Text.Hakyll.CompressCSS
2 ( compressCSS
3 ) where
5import Data.List (isPrefixOf)
6import Text.Hakyll.Regex (substitute)
8-- | Compress CSS to speed up your site.
9compressCSS :: String -> String
10compressCSS = compressSeparators
11 . stripComments
12 . compressWhitespace
14-- | Compresses certain forms of separators.
15compressSeparators :: String -> String
16compressSeparators = substitute "; *}" "}"
17 . substitute " *([{};:]) *" "\\1"
18 . substitute ";;*" ";"
20-- | Compresses all whitespace.
21compressWhitespace :: String -> String
22compressWhitespace = substitute "[ \t\n][ \t\n]*" " "
24-- | Function that strips CSS comments away.
25stripComments :: String -> String
26stripComments [] = []
27stripComments str | isPrefixOf "/*" str = stripComments $ eatComments $ drop 2 str
28 | otherwise = (head str) : (stripComments $ tail str)
29 where eatComments str' | null str' = []
30 | isPrefixOf "*/" str' = drop 2 str'
31 | otherwise = eatComments $ tail str'