{-|
Module      : Slick.Mustache
Description : Slick utilities for working with mustache
Copyright   : (c) Chris Penner, 2019
License     : BSD3
-}
module Slick.Mustache
  ( compileTemplate'
  )
where

import           Development.Shake
import           Text.Mustache
import           Text.Mustache.Compile

-- | Like 'compileTemplate' from <http://hackage.haskell.org/package/mustache mustache> but tracks changes to template files and partials within Shake for cache-busting.
compileTemplate' :: FilePath -> Action Template
compileTemplate' :: FilePath -> Action Template
compileTemplate' FilePath
fp = do
  Partial => [FilePath] -> Action ()
[FilePath] -> Action ()
need [FilePath
fp]
  Either ParseError Template
result <- IO (Either ParseError Template)
-> Action (Either ParseError Template)
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (Either ParseError Template)
 -> Action (Either ParseError Template))
-> IO (Either ParseError Template)
-> Action (Either ParseError Template)
forall a b. (a -> b) -> a -> b
$ FilePath -> IO (Either ParseError Template)
localAutomaticCompile FilePath
fp
  case Either ParseError Template
result of
    Right Template
templ -> do
      Partial => [FilePath] -> Action ()
[FilePath] -> Action ()
need (STree -> [FilePath]
getPartials (STree -> [FilePath])
-> (Template -> STree) -> Template -> [FilePath]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Template -> STree
ast (Template -> [FilePath]) -> Template -> [FilePath]
forall a b. (a -> b) -> a -> b
$ Template
templ)
      Template -> Action Template
forall (m :: * -> *) a. Monad m => a -> m a
return Template
templ
    Left ParseError
err -> FilePath -> Action Template
forall (m :: * -> *) a. MonadFail m => FilePath -> m a
fail (FilePath -> Action Template) -> FilePath -> Action Template
forall a b. (a -> b) -> a -> b
$ ParseError -> FilePath
forall a. Show a => a -> FilePath
show ParseError
err