blob: cd03237fe6ef1a3bc2dacc60f0be45d78e3dd27d (
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
-- | 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.Char (isSpace)
import Data.Maybe (listToMaybe)
import Data.List (isPrefixOf)
import Text.Regex.Posix ((=~~))
-- | A simple (but inefficient) regex replace funcion
--
replaceAll :: String -- ^ Pattern
-> (String -> String) -- ^ Replacement (called on capture)
-> String -- ^ Source string
-> String -- ^ Result
replaceAll pattern f source =
case listToMaybe (source =~~ pattern) of
Nothing -> source
Just (o, l) ->
let (before, tmp) = splitAt o source
(capture, after) = splitAt l tmp
in before ++ f capture ++ replaceAll pattern f after
-- | Compress CSS to speed up your site.
--
compressCss :: String -> String
compressCss = compressSeparators
. stripComments
. compressWhitespace
-- | Compresses certain forms of separators.
--
compressSeparators :: String -> String
compressSeparators = replaceAll "; *}" (const "}")
. replaceAll " *([{};:]) *" (take 1 . dropWhile isSpace)
. replaceAll ";;*" (const ";")
-- | Compresses all whitespace.
--
compressWhitespace :: String -> String
compressWhitespace = replaceAll "[ \t\n][ \t\n]*" (const " ")
-- | Function that strips CSS comments away.
--
stripComments :: String -> String
stripComments [] = []
stripComments str
| isPrefixOf "/*" str = stripComments $ eatComments $ drop 2 str
| otherwise = head str : stripComments (drop 1 str)
where
eatComments str' | null str' = []
| isPrefixOf "*/" str' = drop 2 str'
| otherwise = eatComments $ drop 1 str'
|