on ce2cef4 help sign in
hakyll/src/Text/Hakyll/CompressCSS.hs
34 lines 1.2 kB history raw
1module Text.Hakyll.CompressCSS
2 ( compressCSS
3 ) where
5import Data.List
6import Text.Regex
8-- | subRegex with arguments flipped for easy function composition.
9subRegex' :: String -> String -> String -> String
10subRegex' pattern replacement str = subRegex (mkRegex pattern) str replacement
12-- | Compress CSS to speed up your site.
13compressCSS :: String -> String
14compressCSS = compressSeparators
15 . compressWhitespace
16 . stripComments
18-- | Compresses certain forms of separators.
19compressSeparators :: String -> String
20compressSeparators = subRegex' ";\\s*}" "}"
21 . subRegex' "\\s*([{};:])\\s*" "\\1"
23-- | Compresses all whitespace.
24compressWhitespace :: String -> String
25compressWhitespace = subRegex' "\\s\\s*" " "
27-- | Function that strips CSS comments away.
28stripComments :: String -> String
29stripComments [] = []
30stripComments str | isPrefixOf "/*" str = stripComments $ eatComments $ drop 2 str
31 | otherwise = (head str) : (stripComments $ tail str)
32 where eatComments str' | null str' = []
33 | isPrefixOf "*/" str' = drop 2 str'
34 | otherwise = eatComments $ tail str'