on master help sign in
* updated documentation
231beddVincent Ambo
Examples/elm-happstack/Main.hs+3
@@ -8,6 +8,9 @@ import Language.Elm.Quasi
8elmLoc :: String
8elmLoc :: String
9elmLoc = "http://f.cl.ly/items/2e3Z3r3v29263U393c3x/elm-min.js"
9elmLoc = "http://f.cl.ly/items/2e3Z3r3v29263U393c3x/elm-min.js"
10
10
11+-- elmResponse is a "nice to have" helper function for compiling
12+-- Elm code when using Elm with Happstack. At some point this might
13+-- be moved to an elm-happstack package.
11elmResponse :: ElmSource a
14elmResponse :: ElmSource a
12 => String -- ^ Page title
15 => String -- ^ Page title
13 -> a -- ^ elm source
16 -> a -- ^ elm source
Examples/elm-yesod/Main.hs+4−4
@@ -8,6 +8,7 @@ import Text.Hamlet
8
8
9data ElmTest = ElmTest
9data ElmTest = ElmTest
10
10
11+-- loading external elm code
11mousePage = $(elmFile "elm_source/mouse.elm")
12mousePage = $(elmFile "elm_source/mouse.elm")
12
13
13clockPage = $(elmFile "elm_source/clock.elm")
14clockPage = $(elmFile "elm_source/clock.elm")
@@ -33,8 +34,9 @@ mkYesod "ElmTest" [parseRoutes|
33/shapes ShapesR GET
34/shapes ShapesR GET
34|]
35|]
35
36
36−-- generateWidget is called with the result of an Elm QuasiQuoter (which is just a
37+-- elmWidget takes some elm source code and returns the finished elm widget
37−-- string containing some Elm code, with proper newline escaping)
38+-- inside the GHandler monad. URL interpolation is done automatically, all
39+-- interpolated variables have to be in scope when the elmWidget call happens.
38getMouseR :: Handler RepHtml
40getMouseR :: Handler RepHtml
39getMouseR = do
41getMouseR = do
40 widget <- elmWidget mousePage
42 widget <- elmWidget mousePage
@@ -56,8 +58,6 @@ getShapesR = do
56 setTitle "Simple shapes"
58 setTitle "Simple shapes"
57 widget
59 widget
58
60
59−-- URLs are rendered manually and then passed on to the function containing
60−-- the elm QuasiQuoter.
61getRootR :: Handler RepHtml
61getRootR :: Handler RepHtml
62getRootR = do
62getRootR = do
63 widget <- elmWidget $(elmFile "elm_source/index.elm")
63 widget <- elmWidget $(elmFile "elm_source/index.elm")
elm-yesod/Language/Elm/Yesod.hs+2
@@ -31,6 +31,8 @@ import Language.Elm.Quasi
31import qualified Data.Text as TS
31import qualified Data.Text as TS
32import qualified Data.Text.Lazy as TL
32import qualified Data.Text.Lazy as TL
33
33
34+-- |elmWidget returns a Yesod widget from some Elm source code
35+-- with URL interpolation.
34elmWidget :: ElmUrl (Route master) -- ^ Elm source code
36elmWidget :: ElmUrl (Route master) -- ^ Elm source code
35 -> GHandler sub master (GWidget sub master())
37 -> GHandler sub master (GWidget sub master())
36elmWidget source = do
38elmWidget source = do
elm/src/Language/Elm.hs+4−2
@@ -2,8 +2,9 @@
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− It also provides a predefined compileToHtml function for use with the Blaze markup library
5+ The type class @'ElmSource'@ requires an instance for all types that the Elm compiler
6− as well as a simple QuasiQuoter for embedding literal elm code in a Haskell file.
6+ understands. The provided instances for String, Text and QuasiQuoted Elm source code
7+ should be sufficient.
7
8
8 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 <http://elm-lang.org/Documentation.elm>
9-}
10-}
@@ -25,6 +26,7 @@ class ElmSource a where
25 -- and JavaScript. The HTML is only the contents of the body, so the three
26 -- and JavaScript. The HTML is only the contents of the body, so the three
26 -- parts must be combined in a basic HTML skeleton.
27 -- parts must be combined in a basic HTML skeleton.
27 toParts :: a -> (Html, Html, String)
28 toParts :: a -> (Html, Html, String)
29+
28 -- |This function compiles Elm code into a full HTML page.
30 -- |This function compiles Elm code into a full HTML page.
29 toHtml :: String -- ^ Location of elm-min.js as expected by the browser
31 toHtml :: String -- ^ Location of elm-min.js as expected by the browser
30 -> String -- ^ The page title
32 -> String -- ^ The page title
elm/src/Language/Elm/Quasi.hs+17−5
@@ -6,7 +6,7 @@
6
6
7-- | This module contains Shakespearean (see "Text.Shakespeare") templates for Elm.
7-- | This module contains Shakespearean (see "Text.Shakespeare") templates for Elm.
8-- It introduces type-safe compile-time variable and URL interpolation. A typeclass
8-- It introduces type-safe compile-time variable and URL interpolation. A typeclass
9−-- 'ToElm' is provided for interpolated variables.
9+-- @'ToElm'@ is provided for interpolated variables.
10--
10--
11-- Further reading on Shakespearean templates: <http://www.yesodweb.com/book/templates>
11-- Further reading on Shakespearean templates: <http://www.yesodweb.com/book/templates>
12--
12--
@@ -14,8 +14,11 @@
14module Language.Elm.Quasi
14module Language.Elm.Quasi
15 ( -- * Functions
15 ( -- * Functions
16 -- ** Template-Reading Functions
16 -- ** Template-Reading Functions
17− -- | These QuasiQuoter and Template Haskell methods return values of
17+ -- |These QuasiQuoters return functions of the type @(t -> 'Elm')@
18− -- type @'ElmUrl' url@. See the Yesod book for details.
18+ -- where @t@ is the URL rendering function if type-safe URLs are used.
19+ --
20+ -- A usage example for both type-safe (Yesod) and standard path segment (Happstack)
21+ -- URLs is provided in the Examples folder in the Git repository.
19 elm
22 elm
20 , elmFile
23 , elmFile
21 , elmFileReload
24 , elmFileReload
@@ -39,7 +42,7 @@ import qualified Data.Text as TS
39import qualified Data.Text.Lazy as TL
42import qualified Data.Text.Lazy as TL
40import Text.Shakespeare
43import Text.Shakespeare
41
44
42−-- | Render Elm to lazy Text without route interpolation.
45+-- | Render Elm to lazy Text.
43renderElm :: Elm -> TL.Text
46renderElm :: Elm -> TL.Text
44renderElm (Elm b) = toLazyText b
47renderElm (Elm b) = toLazyText b
45
48
@@ -47,7 +50,7 @@ renderElm (Elm b) = toLazyText b
47newtype Elm = Elm { unElm :: Builder }
50newtype Elm = Elm { unElm :: Builder }
48 deriving Monoid
51 deriving Monoid
49
52
50−-- | A typeclass for types that can be interpolated in CoffeeScript templates.
53+-- | A typeclass for types that can be interpolated in Elm templates.
51class ToElm a where
54class ToElm a where
52 toElm :: a -> Builder
55 toElm :: a -> Builder
53instance ToElm [Char] where toElm = fromLazyText . TL.pack
56instance ToElm [Char] where toElm = fromLazyText . TL.pack
@@ -64,12 +67,21 @@ elmSettings = do
64 , unwrap = unWrapExp
67 , unwrap = unWrapExp
65 }
68 }
66
69
70+-- |QuasiQuoter for embedding Elm code inside of Haskell code.
71+--
72+-- Usage:
73+-- @[elm|main = plaintext "Some elm code"|]@
67elm :: QuasiQuoter
74elm :: QuasiQuoter
68elm = QuasiQuoter { quoteExp = \s -> do
75elm = QuasiQuoter { quoteExp = \s -> do
69 rs <- elmSettings
76 rs <- elmSettings
70 quoteExp (shakespeare rs) s
77 quoteExp (shakespeare rs) s
71 }
78 }
72
79
80+-- |A Template Haskell function for embedding Elm code from external
81+-- .elm files.
82+--
83+-- Usage:
84+-- @$(elmFile "elm_source/index.elm")@
73elmFile :: FilePath -> Q Exp
85elmFile :: FilePath -> Q Exp
74elmFile fp = do
86elmFile fp = do
75 rs <- elmSettings
87 rs <- elmSettings