2012-05-29 18:25:43 +00:00
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
2012-06-14 10:02:33 +00:00
|
|
|
module GenerateHtml (generateHtml,
|
2013-06-05 07:44:04 +00:00
|
|
|
createHtml,
|
|
|
|
JSStyle (..),
|
|
|
|
JSSource (..)
|
2012-06-14 10:02:33 +00:00
|
|
|
) where
|
2012-05-29 18:25:43 +00:00
|
|
|
|
2012-06-14 08:43:04 +00:00
|
|
|
import Data.List (intercalate)
|
2012-05-29 18:25:43 +00:00
|
|
|
import Text.Blaze (preEscapedToMarkup)
|
|
|
|
import Text.Blaze.Html (Html)
|
|
|
|
import qualified Text.Blaze.Html5 as H
|
|
|
|
import Text.Blaze.Html5 ((!))
|
|
|
|
import qualified Text.Blaze.Html5.Attributes as A
|
|
|
|
|
2012-09-11 09:53:45 +00:00
|
|
|
import Text.Jasmine (minify)
|
|
|
|
import qualified Data.ByteString.Lazy.Char8 as BS
|
|
|
|
|
2012-06-12 06:28:45 +00:00
|
|
|
import Ast
|
2013-04-08 08:48:30 +00:00
|
|
|
import Initialize (buildFromSource)
|
2012-05-29 18:25:43 +00:00
|
|
|
import CompileToJS
|
|
|
|
import ExtractNoscript
|
2013-04-08 08:48:30 +00:00
|
|
|
import Libraries as Libraries
|
2012-05-29 18:25:43 +00:00
|
|
|
|
2012-09-11 09:53:45 +00:00
|
|
|
data JSStyle = Minified | Readable
|
2013-06-05 07:44:04 +00:00
|
|
|
data JSSource = Link String | Source JSStyle String
|
2012-09-11 09:53:45 +00:00
|
|
|
|
2013-06-05 07:44:04 +00:00
|
|
|
makeScript :: JSSource -> H.Html
|
|
|
|
makeScript source =
|
|
|
|
case source of
|
|
|
|
Link src -> H.script ! A.type_ "text/javascript" ! A.src (H.toValue src) $ ""
|
|
|
|
Source style src ->
|
|
|
|
H.script ! A.type_ "text/javascript" $
|
|
|
|
preEscapedToMarkup $ case style of
|
|
|
|
Minified -> BS.unpack . minify . BS.pack $ src
|
|
|
|
Readable -> src
|
2012-06-12 10:42:00 +00:00
|
|
|
|
2012-05-29 18:25:43 +00:00
|
|
|
-- |This function compiles Elm code into simple HTML.
|
|
|
|
--
|
|
|
|
-- Usage example:
|
|
|
|
--
|
|
|
|
-- > generateHtml "/elm-min.js" "Some title" [elmFile|elm-source/somePage.elm|]
|
2013-03-12 07:48:11 +00:00
|
|
|
generateHtml :: String -- ^ Location of elm-runtime.js as expected by the browser
|
2012-05-29 18:25:43 +00:00
|
|
|
-> String -- ^ The page title
|
|
|
|
-> String -- ^ The elm source code.
|
|
|
|
-> Html
|
2013-06-05 07:44:04 +00:00
|
|
|
generateHtml libLoc title source = H.span "broken for now"
|
|
|
|
{--
|
2013-04-08 08:48:30 +00:00
|
|
|
case buildFromSource True source of
|
2013-06-05 07:44:04 +00:00
|
|
|
Right modul -> createHtml libLoc title [] "" title [modul]
|
2013-04-08 08:48:30 +00:00
|
|
|
Left err -> createHtml Readable libLoc title (Right $ showErr err)
|
2013-05-11 20:29:55 +00:00
|
|
|
(H.noscript "") "Main"
|
2013-06-05 07:44:04 +00:00
|
|
|
--}
|
|
|
|
{--
|
|
|
|
modulesToHtml :: FilePath -> String -> []
|
|
|
|
modulesToHtml libLoc title scripts nscrpt modules =
|
2013-03-11 01:52:16 +00:00
|
|
|
createHtml jsStyle libLoc title' js noscript altTitle
|
2013-02-27 07:33:47 +00:00
|
|
|
where
|
|
|
|
js = Right $ jss ++ concatMap jsModule modules
|
|
|
|
noscript = if nscrpt then extractNoscript $ last modules else ""
|
|
|
|
title' = if null title then altTitle else title
|
2013-03-11 01:52:16 +00:00
|
|
|
altTitle = intercalate "." names
|
|
|
|
where Module names _ _ _ = last modules
|
2013-06-05 07:44:04 +00:00
|
|
|
--}
|
2012-06-14 10:02:33 +00:00
|
|
|
|
2013-06-05 07:44:04 +00:00
|
|
|
createHtml :: FilePath -> String -> [JSSource] -> String -> Html
|
|
|
|
createHtml libLoc title scripts noscript =
|
2012-05-29 18:25:43 +00:00
|
|
|
H.docTypeHtml $ do
|
|
|
|
H.head $ do
|
|
|
|
H.meta ! A.charset "UTF-8"
|
|
|
|
H.title . H.toHtml $ title
|
2013-06-05 07:44:04 +00:00
|
|
|
makeScript (Link libLoc)
|
|
|
|
mapM_ makeScript scripts
|
2013-04-05 16:56:21 +00:00
|
|
|
H.body $ do
|
2013-06-05 07:44:04 +00:00
|
|
|
H.script ! A.type_ "text/javascript" $ preEscapedToMarkup ("Elm.fullscreen(Elm.Main)" :: String)
|
2013-03-11 01:52:16 +00:00
|
|
|
H.noscript $ preEscapedToMarkup noscript
|