on a3801ac help sign in
elk/compiler/Gen/GenerateHtml.hs
75 lines 2.7 kB history raw
1{-# LANGUAGE OverloadedStrings #-}
2module GenerateHtml (generateHtml,
3 createHtml,
4 JSStyle (..),
5 JSSource (..)
6 ) where
8import Data.List (intercalate)
9import Text.Blaze (preEscapedToMarkup)
10import Text.Blaze.Html (Html)
11import qualified Text.Blaze.Html5 as H
12import Text.Blaze.Html5 ((!))
13import qualified Text.Blaze.Html5.Attributes as A
15import Text.Jasmine (minify)
16import qualified Data.ByteString.Lazy.Char8 as BS
18import Ast
19import Initialize (buildFromSource)
20import CompileToJS
21import ExtractNoscript
22import Libraries as Libraries
24data JSStyle = Minified | Readable
25data JSSource = Link String | Source JSStyle String
27makeScript :: JSSource -> H.Html
28makeScript source =
29 case source of
30 Link src -> H.script ! A.type_ "text/javascript" ! A.src (H.toValue src) $ ""
31 Source style src ->
32 H.script ! A.type_ "text/javascript" $
33 preEscapedToMarkup $ case style of
34 Minified -> BS.unpack . minify . BS.pack $ src
35 Readable -> src
37-- |This function compiles Elm code into simple HTML.
38--
39-- Usage example:
40--
41-- > generateHtml "/elm-min.js" "Some title" [elmFile|elm-source/somePage.elm|]
42generateHtml :: String -- ^ Location of elm-runtime.js as expected by the browser
43 -> String -- ^ The page title
44 -> String -- ^ The elm source code.
45 -> Html
46generateHtml libLoc title source = H.span "broken for now"
47{--
48 case buildFromSource True source of
49 Right modul -> createHtml libLoc title [] "" title [modul]
50 Left err -> createHtml Readable libLoc title (Right $ showErr err)
51 (H.noscript "") "Main"
52--}
53{--
54modulesToHtml :: FilePath -> String -> []
55modulesToHtml libLoc title scripts nscrpt modules =
56 createHtml jsStyle libLoc title' js noscript altTitle
57 where
58 js = Right $ jss ++ concatMap jsModule modules
59 noscript = if nscrpt then extractNoscript $ last modules else ""
60 title' = if null title then altTitle else title
61 altTitle = intercalate "." names
62 where Module names _ _ _ = last modules
63--}
65createHtml :: FilePath -> String -> [JSSource] -> String -> Html
66createHtml libLoc title scripts noscript =
67 H.docTypeHtml $ do
68 H.head $ do
69 H.meta ! A.charset "UTF-8"
70 H.title . H.toHtml $ title
71 makeScript (Link libLoc)
72 mapM_ makeScript scripts
73 H.body $ do
74 H.script ! A.type_ "text/javascript" $ preEscapedToMarkup ("Elm.fullscreen(Elm.Main)" :: String)
75 H.noscript $ preEscapedToMarkup noscript