summaryrefslogtreecommitdiff
path: root/src/Text/Hakyll/Internal/CompressCSS.hs
diff options
context:
space:
mode:
Diffstat (limited to 'src/Text/Hakyll/Internal/CompressCSS.hs')
-rw-r--r--src/Text/Hakyll/Internal/CompressCSS.hs36
1 files changed, 36 insertions, 0 deletions
diff --git a/src/Text/Hakyll/Internal/CompressCSS.hs b/src/Text/Hakyll/Internal/CompressCSS.hs
new file mode 100644
index 0000000..7d52bef
--- /dev/null
+++ b/src/Text/Hakyll/Internal/CompressCSS.hs
@@ -0,0 +1,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'