module General.Template(runTemplate) where
import Control.Monad
import Data.Char
import qualified Data.ByteString.Lazy.Char8 as LBS
-- | Template Engine. Perform the following replacements on a line basis:
--
-- * ==>
--
-- * ==>
runTemplate :: Monad m => (FilePath -> m LBS.ByteString) -> LBS.ByteString -> m LBS.ByteString
runTemplate ask = liftM LBS.unlines . mapM f . LBS.lines
where
link = LBS.pack "\n" `LBS.append` res `LBS.append` LBS.pack "\n"
| Just file <- lbs_stripPrefix link y = do res <- grab file; return $ LBS.pack ""
| otherwise = return x
where
y = LBS.dropWhile isSpace x
grab = ask . takeWhile (/= '\"') . LBS.unpack
lbs_stripPrefix :: LBS.ByteString -> LBS.ByteString -> Maybe LBS.ByteString
lbs_stripPrefix prefix text = if a == prefix then Just b else Nothing
where (a,b) = LBS.splitAt (LBS.length prefix) text