on master help sign in
Move css into RTS. Simplifies Haskell API and compiler code.
29a2ec3evancz
compiler/src/Gen/GenerateHtml.hs+7−22
@@ -1,6 +1,5 @@
1{-# LANGUAGE OverloadedStrings #-}
1{-# LANGUAGE OverloadedStrings #-}
2module GenerateHtml (generateHtml,
2module GenerateHtml (generateHtml,
3− body, css, widgetBody,
4 modulesToHtml, linkedHtml,
3 modulesToHtml, linkedHtml,
5 JSStyle (..)
4 JSStyle (..)
6 ) where
5 ) where
@@ -20,10 +19,6 @@ import Initialize
20import CompileToJS
19import CompileToJS
21import ExtractNoscript
20import ExtractNoscript
22
21
23−css = H.style ! A.type_ "text/css" $ preEscapedToMarkup
24− ("html,head,body { padding:0; margin:0; }\
25− \body { font-family: helvetica, arial, sans-serif; }" :: String)
26−
27data JSStyle = Minified | Readable
22data JSStyle = Minified | Readable
28
23
29makeScript :: JSStyle -> Either String String -> H.Html
24makeScript :: JSStyle -> Either String String -> H.Html
@@ -51,38 +46,28 @@ generateHtml libLoc title source =
51
46
52
47
53modulesToHtml jsStyle title libLoc jss nscrpt modules =
48modulesToHtml jsStyle title libLoc jss nscrpt modules =
54− createHtml jsStyle libLoc title' js noscript
49+ createHtml jsStyle libLoc title' js noscript altTitle
55 where
50 where
56 js = Right $ jss ++ concatMap jsModule modules
51 js = Right $ jss ++ concatMap jsModule modules
57 noscript = if nscrpt then extractNoscript $ last modules else ""
52 noscript = if nscrpt then extractNoscript $ last modules else ""
58 title' = if null title then altTitle else title
53 title' = if null title then altTitle else title
59− altTitle = (\(Module names _ _ _) -> intercalate "." names) $ last modules
54+ altTitle = intercalate "." names
55+ where Module names _ _ _ = last modules
60
56
61
57
62linkedHtml rtLoc jsLoc modules =
58linkedHtml rtLoc jsLoc modules =
63− createHtml Readable rtLoc title (Left jsLoc) (H.noscript "")
59+ createHtml Readable rtLoc title (Left jsLoc) (H.noscript "") title
64 where
60 where
65 title = (\(Module names _ _ _) -> intercalate "." names) (last modules)
61 title = (\(Module names _ _ _) -> intercalate "." names) (last modules)
66
62
67
63
68−createHtml jsStyle libLoc title js noscript =
64+createHtml jsStyle libLoc title js noscript moduleToLoad =
69 H.docTypeHtml $ do
65 H.docTypeHtml $ do
70 H.head $ do
66 H.head $ do
71 H.meta ! A.charset "UTF-8"
67 H.meta ! A.charset "UTF-8"
72 H.title . H.toHtml $ title
68 H.title . H.toHtml $ title
73− css
74 H.body $ do
69 H.body $ do
75 makeScript Readable (Left libLoc)
70 makeScript Readable (Left libLoc)
76 makeScript jsStyle js
71 makeScript jsStyle js
77− body noscript
78−
79−body noscript = do
80− H.div ! A.id "widthChecker" ! A.style "width:100%; height:1px; position:absolute; top:-1px;" $ ""
81− H.div ! A.id "content" $ ""
82− H.script ! A.type_ "text/javascript" $ "Dispatcher.initialize()"
72+ H.script ! A.type_ "text/javascript" $ "Elm.init(" ++ moduleToLoad ++ ")"
83− H.noscript $ preEscapedToMarkup noscript
84−
85−widgetBody noscript = do
86− H.div ! A.id "widthChecker" ! A.style "width:100%; height:1px; position:absolute; top:-1px;" $ ""
87− H.div ! A.id "content" $ ""
88− H.noscript $ preEscapedToMarkup noscript
73+ H.noscript $ preEscapedToMarkup noscript
compiler/src/Language/Elm.hs+17−17
@@ -2,14 +2,16 @@
2{- | This module exports the functions necessary for compiling Elm code into the
2{- | This module exports the functions necessary for compiling Elm code into the
3 respective HTML, JS and CSS code.
3 respective HTML, JS and CSS code.
4
4
5− The type class @'ElmSource'@ requires an instance for all types that the Elm compiler
5+ The type class @'ElmSource'@ requires an instance for all types that the Elm
6− understands. The provided instances for String, Text and QuasiQuoted Elm source code
6+ compiler understands. The provided instances for String, Text and QuasiQuoted
7− should be sufficient.
7+ Elm source code should be sufficient.
8
8
9− The documentation for the Elm language is available at <http://elm-lang.org/Documentation.elm>,
9+ The documentation for the Elm language is available at
10+ <http://elm-lang.org/Documentation.elm>, and many interactive examples are
10− and many interactive examples are available at <http://elm-lang.org/Examples.elm>
11+ available at <http://elm-lang.org/Examples.elm>
11
12
12− Example implementations using Yesod and Happstack are available at <https://github.com/tazjin/Elm/tree/master/Examples>
13+ Example implementations using Yesod and Happstack are available
14+ at <https://github.com/tazjin/Elm/tree/master/Examples>
13-}
15-}
14module Language.Elm (
16module Language.Elm (
15 ElmSource (..),
17 ElmSource (..),
@@ -37,7 +39,7 @@ class ElmSource a where
37 -- |This function compiles Elm code to three separate parts: HTML, CSS,
39 -- |This function compiles Elm code to three separate parts: HTML, CSS,
38 -- and JavaScript. The HTML is only the contents of the body, so the three
40 -- and JavaScript. The HTML is only the contents of the body, so the three
39 -- parts must be combined in a basic HTML skeleton.
41 -- parts must be combined in a basic HTML skeleton.
40− toParts :: a -> (Html, Html, String)
42+ compile :: a -> String
41
43
42 -- |This function compiles Elm code into a full HTML page.
44 -- |This function compiles Elm code into a full HTML page.
43 toHtml :: String -- ^ Location of elm-min.js as expected by the browser
45 toHtml :: String -- ^ Location of elm-min.js as expected by the browser
@@ -46,37 +48,35 @@ class ElmSource a where
46 -> Html
48 -> Html
47
49
48instance ElmSource String where
50instance ElmSource String where
49− toParts = toPartsHelper
51+ compile = compileHelper
50 toHtml = generateHtml
52 toHtml = generateHtml
51
53
52instance ElmSource Elm where
54instance ElmSource Elm where
53− toParts = toPartsHelper . TL.unpack . renderElm
55+ compile = compileHelper . TL.unpack . renderElm
54 toHtml elmL title = generateHtml elmL title . TL.unpack . renderElm
56 toHtml elmL title = generateHtml elmL title . TL.unpack . renderElm
55
57
56-- |Strict text
58-- |Strict text
57instance ElmSource TS.Text where
59instance ElmSource TS.Text where
58− toParts = toPartsHelper . TS.unpack
60+ compile = compileHelper . TS.unpack
59 toHtml elmL title = generateHtml elmL title . TS.unpack
61 toHtml elmL title = generateHtml elmL title . TS.unpack
60
62
61-- |Lazy text
63-- |Lazy text
62instance ElmSource TL.Text where
64instance ElmSource TL.Text where
63− toParts = toPartsHelper . TL.unpack
65+ compile = compileHelper . TL.unpack
64 toHtml elmL title = generateHtml elmL title . TL.unpack
66 toHtml elmL title = generateHtml elmL title . TL.unpack
65
67
66-- | (urlRenderFn, urlRenderFn -> Elm)
68-- | (urlRenderFn, urlRenderFn -> Elm)
67instance ElmSource (t, t -> Elm) where
69instance ElmSource (t, t -> Elm) where
68− toParts (f, s) = toPartsHelper $ TL.unpack $ renderElm $ s f
70+ compile (f, s) = compileHelper $ TL.unpack $ renderElm $ s f
69 toHtml elmL title (f, s) = generateHtml elmL title $ TL.unpack $ renderElm $ s f
71 toHtml elmL title (f, s) = generateHtml elmL title $ TL.unpack $ renderElm $ s f
70
72
71-- | to be used without URL interpolation
73-- | to be used without URL interpolation
72instance ElmSource (t -> Elm) where
74instance ElmSource (t -> Elm) where
73− toParts s = toPartsHelper $ TL.unpack $ renderElm $ s undefined
75+ compile s = compileHelper $ TL.unpack $ renderElm $ s undefined
74 toHtml l t s = generateHtml l t $ TL.unpack $ renderElm $ s undefined
76 toHtml l t s = generateHtml l t $ TL.unpack $ renderElm $ s undefined
75
77
76
78
77-- build helper to avoid boilerplate repetition
79-- build helper to avoid boilerplate repetition
78−toPartsHelper :: String -> (Html, Html, String)
80+compileHelper :: String -> String
79−toPartsHelper source = (html, css, js)
81+compileHelper source = either showErr jsModule modul
80 where modul = buildFromSource source
82 where modul = buildFromSource source
81− js = either showErr jsModule modul
82− html = widgetBody $ either id extractNoscript modul
runtime/Init.js+6
@@ -28,6 +28,12 @@ Elm.init = function(module, baseNode) {
28 baseNode.style.width = document.documentElement.clientWidth + 'px';
28 baseNode.style.width = document.documentElement.clientWidth + 'px';
29 baseNode.style.height = document.documentElement.clientHeight + 'px';
29 baseNode.style.height = document.documentElement.clientHeight + 'px';
30 }, true);
30 }, true);
31+
32+ var style = Render.newElement('style');
33+ style.type = 'text/css';
34+ style.innerHTML = "html,head,body { padding:0; margin:0; }" +
35+ "body { font-family: calibri, helvetica, arial, sans-serif; }";
36+ document.head.appendChild(style);
31 }
37 }
32 baseNode.style.overflow = 'scroll';
38 baseNode.style.overflow = 'scroll';
33
39