module Hakyll.Web.Sass
( sassCompiler
, sassCompilerWith
, renderSass
, renderSassWith
, selectFileType
, sassDefConfig
, module Text.Sass.Options
) where
import Control.Monad (join)
import Data.Default.Class
import Hakyll.Core.Compiler
import Hakyll.Core.Identifier
import Hakyll.Core.Item
import System.FilePath (takeExtension)
import Text.Sass.Compilation
import Text.Sass.Options
import Prelude
sassCompiler :: Compiler (Item String)
sassCompiler = getResourceBody >>= renderSass
sassCompilerWith :: SassOptions -> Compiler (Item String)
sassCompilerWith options = getResourceBody >>= renderSassWith options
renderSass :: Item String -> Compiler (Item String)
renderSass item =
let extension = (takeExtension . toFilePath . itemIdentifier) item
in case selectFileType sassDefConfig extension of
Just options -> renderSassWith options item
Nothing -> fail "File type must be .scss or .sass."
renderSassWith :: SassOptions -> Item String -> Compiler (Item String)
renderSassWith options item =
let bodyStr = itemBody item
in join $ unsafeCompiler $ do
resultOrErr <- compileString bodyStr options
case resultOrErr of
Left sassError -> errorMessage sassError >>= fail
Right result -> return (makeItem result)
selectFileType :: SassOptions -> String -> Maybe SassOptions
selectFileType options ".scss" = Just $ options { sassIsIndentedSyntax = False }
selectFileType options ".sass" = Just $ options { sassIsIndentedSyntax = True }
selectFileType _ _ = Nothing
sassDefConfig :: SassOptions
sassDefConfig = def