module Simulation.Aivika.Experiment.WebPageRenderer where
import Control.Monad
import Control.Monad.Trans
import System.IO
import System.Directory
import System.FilePath
import Simulation.Aivika
import Simulation.Aivika.Experiment.Types
import Simulation.Aivika.Experiment.HtmlWriter
import Simulation.Aivika.Experiment.ExperimentWriter
data WebPageRenderer a = WebPageRenderer a
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
}
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