module StaticFileGenerators (compressJs, compressPjax) where
import Prelude
import Yesod.EmbeddedStatic
import qualified Data.ByteString.Lazy as BL
findComment :: BL.ByteString -> BL.ByteString
findComment b = result
where
(untilStar, afterStar) = BL.break (==42) b
result = if "*/" `BL.isPrefixOf` afterStar
then untilStar `BL.append` "*/"
else BL.concat [untilStar, "*", findComment (BL.drop 1 afterStar)]
keepFirstComment :: (BL.ByteString -> IO BL.ByteString) -> BL.ByteString -> IO BL.ByteString
keepFirstComment compressor b = do
comp <- compressor b
let withoutSpace = BL.dropWhile (==32) b
return $ if "/*" `BL.isPrefixOf` withoutSpace
then findComment withoutSpace `BL.append` comp
else comp
compressJs :: Bool
-> BL.ByteString -> IO BL.ByteString
compressJs b = tryCompressTools
[ keepFirstComment uglifyJs
, (if b then keepFirstComment else id) yuiJavascript
, keepFirstComment closureJs
, keepFirstComment jasmine
]
compressPjax :: BL.ByteString -> IO BL.ByteString
compressPjax b = do
lic <- BL.readFile "js/pjax-LICENSE"
js <- compressJs True b
return $ BL.concat ["/*", lic, "*/", js]