module Development.Shake.Report(buildReport) where
import Control.Monad
import Data.Char
import Data.List
import System.FilePath
import Paths_shake
buildReport :: String -> FilePath -> IO ()
buildReport json out = do
htmlDir <- getDataFileName "html"
report <- readFile $ htmlDir </> "report.html"
let f name | name == "data.js" = return $ "var shake = \n" ++ json
| otherwise = readFile $ htmlDir </> name
writeFile out =<< runTemplate f report
runTemplate :: Monad m => (String -> m String) -> String -> m String
runTemplate ask = liftM unlines . mapM f . lines
where
f x | Just file <- stripPrefix "<script src=\"" y = do res <- grab file; return $ "<script>\n" ++ res ++ "\n</script>"
| Just file <- stripPrefix "<link href=\"" y = do res <- grab file; return $ "<style type=\"text/css\">\n" ++ res ++ "\n</style>"
| otherwise = return x
where
y = dropWhile isSpace x
grab = ask . takeWhile (/= '\"')