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