@@ -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.
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.
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>
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>
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
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
48instance ElmSource String where
50instance ElmSource String where
49− toParts = toPartsHelper
51+ compile = compileHelper
50 toHtml = generateHtml
52 toHtml = generateHtml
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
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
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
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
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
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