summaryrefslogtreecommitdiff
path: root/src/Hakyll/Web/CompressCss.hs
diff options
context:
space:
mode:
authorJasper Van der Jeugt <jaspervdj@gmail.com>2010-12-30 23:07:07 +0100
committerJasper Van der Jeugt <jaspervdj@gmail.com>2010-12-30 23:07:07 +0100
commit15f2cee5e444735c4206e0e98e8f2506d9fb8659 (patch)
tree5d3d5866bce3a584398f7e1b398f0fa639c5d4c7 /src/Hakyll/Web/CompressCss.hs
parent2b5b27e2e7d40933e59374b1ecd8a080de65a96f (diff)
downloadhakyll-15f2cee5e444735c4206e0e98e8f2506d9fb8659.tar.gz
Backport CompressCss module
Diffstat (limited to 'src/Hakyll/Web/CompressCss.hs')
-rw-r--r--src/Hakyll/Web/CompressCss.hs56
1 files changed, 56 insertions, 0 deletions
diff --git a/src/Hakyll/Web/CompressCss.hs b/src/Hakyll/Web/CompressCss.hs
new file mode 100644
index 0000000..cd03237
--- /dev/null
+++ b/src/Hakyll/Web/CompressCss.hs
@@ -0,0 +1,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'