on master help sign in
Move css into RTS. Simplifies Haskell API and compiler code.
commit29a2ec36bbff561012002d3028a1745ce67ad858 authorevancz <info@elm-lang.org> dateSun 10 Mar 2013, 18:52 (14 years ago) parent9aa8e3b
3 files changed+30−39
compiler/src/Gen/GenerateHtml.hs+7−22 compiler/src/Language/Elm.hs+17−17 runtime/Init.js+6
compiler/src/Gen/GenerateHtml.hs+7−22
@@ -1,6 +1,5 @@
11{-# LANGUAGE OverloadedStrings #-}
22module GenerateHtml (generateHtml,
3− body, css, widgetBody,
43 modulesToHtml, linkedHtml,
54 JSStyle (..)
65 ) where
@@ -20,10 +19,6 @@ import Initialize
2019import CompileToJS
2120import ExtractNoscript
2221
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−
2722data JSStyle = Minified | Readable
2823
2924makeScript :: JSStyle -> Either String String -> H.Html
@@ -51,38 +46,28 @@ generateHtml libLoc title source =
5146
5247
5348modulesToHtml jsStyle title libLoc jss nscrpt modules =
54− createHtml jsStyle libLoc title' js noscript
49+ createHtml jsStyle libLoc title' js noscript altTitle
5550 where
5651 js = Right $ jss ++ concatMap jsModule modules
5752 noscript = if nscrpt then extractNoscript $ last modules else ""
5853 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
6056
6157
6258linkedHtml rtLoc jsLoc modules =
63− createHtml Readable rtLoc title (Left jsLoc) (H.noscript "")
59+ createHtml Readable rtLoc title (Left jsLoc) (H.noscript "") title
6460 where
6561 title = (\(Module names _ _ _) -> intercalate "." names) (last modules)
6662
6763
68−createHtml jsStyle libLoc title js noscript =
64+createHtml jsStyle libLoc title js noscript moduleToLoad =
6965 H.docTypeHtml $ do
7066 H.head $ do
7167 H.meta ! A.charset "UTF-8"
7268 H.title . H.toHtml $ title
73− css
7469 H.body $ do
7570 makeScript Readable (Left libLoc)
7671 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()"
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
72+ H.script ! A.type_ "text/javascript" $ "Elm.init(" ++ moduleToLoad ++ ")"
73+ H.noscript $ preEscapedToMarkup noscript
compiler/src/Language/Elm.hs+17−17
@@ -2,14 +2,16 @@
22{- | This module exports the functions necessary for compiling Elm code into the
33 respective HTML, JS and CSS code.
44
5− The type class @'ElmSource'@ requires an instance for all types that the Elm compiler
6− understands. The provided instances for String, Text and QuasiQuoted Elm source code
7− should be sufficient.
5+ The type class @'ElmSource'@ requires an instance for all types that the Elm
6+ compiler understands. The provided instances for String, Text and QuasiQuoted
7+ Elm source code should be sufficient.
88
9− The documentation for the Elm language is available at <http://elm-lang.org/Documentation.elm>,
10− and many interactive examples are available at <http://elm-lang.org/Examples.elm>
9+ The documentation for the Elm language is available at
10+ <http://elm-lang.org/Documentation.elm>, and many interactive examples are
11+ available at <http://elm-lang.org/Examples.elm>
1112
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>
1315-}
1416module Language.Elm (
1517 ElmSource (..),
@@ -37,7 +39,7 @@ class ElmSource a where
3739 -- |This function compiles Elm code to three separate parts: HTML, CSS,
3840 -- and JavaScript. The HTML is only the contents of the body, so the three
3941 -- parts must be combined in a basic HTML skeleton.
40− toParts :: a -> (Html, Html, String)
42+ compile :: a -> String
4143
4244 -- |This function compiles Elm code into a full HTML page.
4345 toHtml :: String -- ^ Location of elm-min.js as expected by the browser
@@ -46,37 +48,35 @@ class ElmSource a where
4648 -> Html
4749
4850instance ElmSource String where
49− toParts = toPartsHelper
51+ compile = compileHelper
5052 toHtml = generateHtml
5153
5254instance ElmSource Elm where
53− toParts = toPartsHelper . TL.unpack . renderElm
55+ compile = compileHelper . TL.unpack . renderElm
5456 toHtml elmL title = generateHtml elmL title . TL.unpack . renderElm
5557
5658-- |Strict text
5759instance ElmSource TS.Text where
58− toParts = toPartsHelper . TS.unpack
60+ compile = compileHelper . TS.unpack
5961 toHtml elmL title = generateHtml elmL title . TS.unpack
6062
6163-- |Lazy text
6264instance ElmSource TL.Text where
63− toParts = toPartsHelper . TL.unpack
65+ compile = compileHelper . TL.unpack
6466 toHtml elmL title = generateHtml elmL title . TL.unpack
6567
6668-- | (urlRenderFn, urlRenderFn -> Elm)
6769instance 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
6971 toHtml elmL title (f, s) = generateHtml elmL title $ TL.unpack $ renderElm $ s f
7072
7173-- | to be used without URL interpolation
7274instance ElmSource (t -> Elm) where
73− toParts s = toPartsHelper $ TL.unpack $ renderElm $ s undefined
75+ compile s = compileHelper $ TL.unpack $ renderElm $ s undefined
7476 toHtml l t s = generateHtml l t $ TL.unpack $ renderElm $ s undefined
7577
7678
7779-- build helper to avoid boilerplate repetition
78−toPartsHelper :: String -> (Html, Html, String)
79−toPartsHelper source = (html, css, js)
80+compileHelper :: String -> String
81+compileHelper source = either showErr jsModule modul
8082 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) {
2828 baseNode.style.width = document.documentElement.clientWidth + 'px';
2929 baseNode.style.height = document.documentElement.clientHeight + 'px';
3030 }, 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);
3137 }
3238 baseNode.style.overflow = 'scroll';
3339