on a3801ac help sign in
57 lines 2.1 kB history raw
1{-# LANGUAGE TypeSynonymInstances, FlexibleInstances #-}
2{- | This module exports the functions necessary for compiling Elm code into the
3 respective HTML, CSS, and JS code.
5 The documentation for the Elm language is available at
6 <http://elm-lang.org/Documentation.elm>, and many interactive examples are
7 available at <http://elm-lang.org/Examples.elm>
9 Example implementations using Yesod and Happstack are available
10 at <https://github.com/tazjin/Elm/tree/master/Examples>
11-}
12module Language.Elm (compile, toHtml, moduleName, runtime, docs) where
14import Data.List (intercalate)
15import Data.Version (showVersion)
16import CompileToJS (showErr, jsModule)
17import GenerateHtml (generateHtml)
18import Initialize (buildFromSource)
19import Parse.Library
20import Parse.Modules (moduleDef)
21import Text.Blaze.Html (Html)
22import Text.Parsec (option,optional)
23import Paths_Elm
25-- |This function compiles Elm code to JavaScript. It will return either
26-- an error message or the compiled JS code.
27compile :: String -> Either String String
28compile source = fmap jsModule modul
29 where
30 modul = buildFromSource False source
32-- |This function extracts the module name of a given source program.
33moduleName :: String -> Maybe String
34moduleName source = case iParse getModuleName "" source of
35 Right name -> Just name
36 Left _ -> Nothing
37 where
38 getModuleName = do
39 optional freshLine
40 (names, _) <- moduleDef
41 return (intercalate "." names)
43-- |This function compiles Elm code into a full HTML page.
44toHtml :: String -- ^ Location of elm-min.js as expected by the browser
45 -> String -- ^ The page title
46 -> String -- ^ The elm source code
47 -> Html
48toHtml = generateHtml
50-- |The absolute path to Elm's runtime system.
51runtime :: IO FilePath
52runtime = getDataFileName "elm-runtime.js"
54-- |The absolute path to Elm's core library documentation.
55docs :: IO FilePath
56docs = getDataFileName "docs.json"