on ca2e161 help sign in
hakyll/src/Text/Hakyll/CompressCSS.hs
35 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"
22 . subRegex' ";;*" ";"
24-- | Compresses all whitespace.
25compressWhitespace :: String -> String
26compressWhitespace = subRegex' "\\s\\s*" " "
28-- | Function that strips CSS comments away.
29stripComments :: String -> String
30stripComments [] = []
31stripComments str | isPrefixOf "/*" str = stripComments $ eatComments $ drop 2 str
32 | otherwise = (head str) : (stripComments $ tail str)
33 where eatComments str' | null str' = []
34 | isPrefixOf "*/" str' = drop 2 str'
35 | otherwise = eatComments $ tail str'