module Simulation.Aivika.Experiment.Base.WebPageRenderer where
import Control.Monad
import Control.Monad.Trans
import System.IO
import System.Directory
import System.FilePath
import Simulation.Aivika
import Simulation.Aivika.Trans.Exception
import Simulation.Aivika.Experiment.Types
import Simulation.Aivika.Experiment.Base.HtmlWriter
import Simulation.Aivika.Experiment.Base.ExperimentWriter
data WebPageRenderer a = WebPageRenderer a ExperimentFilePath
data WebPageWriter =
WebPageWriter { reporterWriteTOCHtml :: Int -> HtmlWriter (),
reporterWriteHtml :: Int -> HtmlWriter ()
}
type WebPageGenerator a = ExperimentGenerator (WebPageRenderer a)
instance ExperimentRendering (WebPageRenderer a) where
newtype ExperimentContext (WebPageRenderer a) =
WebPageContext { runWebPageContext :: WebPageWriter
}
type ExperimentEnvironment (WebPageRenderer a) = FilePath
type ExperimentMonad (WebPageRenderer a) = ExperimentWriter
liftExperiment r = runExperimentWriter
prepareExperiment e (WebPageRenderer _ path0) =
do path <- resolveFilePath "" path0
liftIO $ do
when (experimentVerbose e) $
do putStr "Updating directory "
putStrLn path
createDirectoryIfMissing True path
return path
renderExperiment e r reporters path =
do let html :: HtmlWriter ()
html =
writeHtmlDocumentWithTitle (experimentTitle e) $
do writeHtmlList $
forM_ (zip [1..] reporters) $ \(i, reporter) ->
reporterWriteTOCHtml (runWebPageContext $
reporterContext reporter) i
writeHtmlBreak
unless (null $ experimentDescription e) $
writeHtmlParagraph $
writeHtmlText $ experimentDescription e
forM_ (zip [1..] reporters) $ \(i, reporter) ->
reporterWriteHtml (runWebPageContext $
reporterContext reporter) i
file = combine path "index.html"
((), contents) <- runHtmlWriter html id
liftIO $
withFile file WriteMode $ \h ->
do hSetEncoding h utf8
hPutStr h (contents [])
when (experimentVerbose e) $
do putStr "Generated file "
putStrLn file
onExperimentCompleted e r path = return ()
onExperimentFailed e r path e' = throwComp e'