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