on aef33d1 help sign in
hakyll/src/Text/Hakyll/CompressCSS.hs
36 lines 1.2 kB history raw
1-- | Module used for CSS compression. The compression is currently in a simple
2-- state, but would typically reduce the number of bytes by about 25%.
3module Text.Hakyll.CompressCSS
4 ( compressCSS
5 ) where
7import Data.List (isPrefixOf)
9import Text.Hakyll.Regex (substituteRegex)
11-- | Compress CSS to speed up your site.
12compressCSS :: String -> String
13compressCSS = compressSeparators
14 . stripComments
15 . compressWhitespace
17-- | Compresses certain forms of separators.
18compressSeparators :: String -> String
19compressSeparators = substituteRegex "; *}" "}"
20 . substituteRegex " *([{};:]) *" "\\1"
21 . substituteRegex ";;*" ";"
23-- | Compresses all whitespace.
24compressWhitespace :: String -> String
25compressWhitespace = substituteRegex "[ \t\n][ \t\n]*" " "
27-- | Function that strips CSS comments away.
28stripComments :: String -> String
29stripComments [] = []
30stripComments str
31 | isPrefixOf "/*" str = stripComments $ eatComments $ drop 2 str
32 | otherwise = head str : stripComments (tail str)
33 where
34 eatComments str' | null str' = []
35 | isPrefixOf "*/" str' = drop 2 str'
36 | otherwise = eatComments $ tail str'