{-# LANGUAGE OverloadedStrings #-}
module Yesod.EmbeddedStatic.Css.AbsoluteUrl (
absoluteUrls
, absoluteUrlsAt
, absoluteUrlsWith
, absCssUrlsFileProd
, absCssUrlsProd
) where
import Yesod.EmbeddedStatic.Generators
import Yesod.EmbeddedStatic.Types
import qualified Data.ByteString.Lazy as BL
import qualified Data.Text as T
import qualified Data.Text.Lazy as TL
import qualified Data.Text.IO as T
import qualified Data.Text.Lazy.Encoding as TL
import Control.Monad ((>=>))
import Data.Maybe (fromMaybe)
import System.FilePath ((</>))
import Yesod.EmbeddedStatic.Css.Util
absCssUrlsFileProd :: FilePath
-> FilePath
-> IO BL.ByteString
absCssUrlsFileProd dir file = do
contents <- T.readFile file
return $ TL.encodeUtf8 $ absCssUrlsProd dir contents
absCssUrlsProd :: FilePath
-> T.Text
-> TL.Text
absCssUrlsProd dir contents =
let css = either error id $ parseCssUrls contents
in renderCssWith toAbsoluteUrl css
where
toAbsoluteUrl (UrlReference rel) = T.concat
[ "url('/"
, (T.pack $ dir </> T.unpack rel)
, "')"
]
absoluteUrls :: FilePath -> Generator
absoluteUrls f = absoluteUrlsAt f f
absoluteUrlsAt :: Location -> FilePath -> Generator
absoluteUrlsAt loc f = absoluteUrlsWith loc f Nothing
absoluteUrlsWith ::
Location
-> FilePath
-> Maybe (CssGeneration -> IO BL.ByteString)
-> Generator
absoluteUrlsWith loc file mpostFilter =
return [ cssProductionFilter (absCssUrlsFileProd loc >=> postFilter . mkCssGeneration loc file) loc file
]
where
postFilter = fromMaybe (return . cssContent) mpostFilter