on master help sign in
Get the API for Language.Elm in better shape. Trying to get elm-lang.org working again.
1af2ff9evancz
compiler/Compiler.hs+1−8
@@ -79,7 +79,7 @@ fileTo flags what rtLoc file = do
79 let path = fromMaybe "" (output_directory flags) </> file
79 let path = fromMaybe "" (output_directory flags) </> file
80 js = replaceExtension path ".js"
80 js = replaceExtension path ".js"
81 html = replaceExtension path ".html"
81 html = replaceExtension path ".html"
82− ms = if no_prelude flags then ms' else map addPrelude ms'
82+ ms = if no_prelude flags then ms' else map Libraries.addPrelude ms'
83 txt = jss ++ concatMap jsModule ms
83 txt = jss ++ concatMap jsModule ms
84 in case what of
84 in case what of
85 JS -> writeFile js (formatJS txt)
85 JS -> writeFile js (formatJS txt)
@@ -88,10 +88,3 @@ fileTo flags what rtLoc file = do
88 Split ->
88 Split ->
89 do writeFile html . renderHtml $ linkedHtml rtLoc js ms
89 do writeFile html . renderHtml $ linkedHtml rtLoc js ms
90 writeFile js (formatJS txt)
90 writeFile js (formatJS txt)
91−
92−addPrelude (Module name exs ims stmts) = Module name exs (prelude ++ ims) stmts
93− where prelude = concatMap addModule Libraries.prelude
94−
95− addModule (n, method) = case lookup n ims of
96− Nothing -> [(n, method)]
97− Just _ -> []
compiler/Language/Elm.hs+19−7
@@ -15,9 +15,13 @@
15-}
15-}
16module Language.Elm (
16module Language.Elm (
17 compile, toHtml,
17 compile, toHtml,
18− runtimeLocation
18+ moduleName,
19+ runtime, docs
19 ) where
20 ) where
20
21
22+import qualified Ast as Ast
23+import qualified Libraries as Libraries
24+import Data.List (intercalate)
21import Data.Version (showVersion)
25import Data.Version (showVersion)
22import CompileToJS
26import CompileToJS
23import ExtractNoscript
27import ExtractNoscript
@@ -30,13 +34,17 @@ import Paths_Elm
30import qualified Data.Text as TS
34import qualified Data.Text as TS
31import qualified Data.Text.Lazy as TL
35import qualified Data.Text.Lazy as TL
32
36
33−-- |This function compiles Elm code to three separate parts: HTML, CSS,
37+-- |This function compiles Elm code to JavaScript.
34−-- and JavaScript. The HTML is only the contents of the body, so the three
35−-- parts must be combined in a basic HTML skeleton.
36compile :: String -> String
38compile :: String -> String
37−compile source = either showErr jsModule modul
39+compile source = either showErr (jsModule . Libraries.addPrelude) modul
38 where modul = buildFromSource source
40 where modul = buildFromSource source
39
41
42+-- |This function extracts the module name of a given source program.
43+moduleName :: String -> String
44+moduleName source = either (const "Main") getName modul
45+ where modul = buildFromSource source
46+ getName (Ast.Module names _ _ _) = intercalate "." names
47+
40-- |This function compiles Elm code into a full HTML page.
48-- |This function compiles Elm code into a full HTML page.
41toHtml :: String -- ^ Location of elm-min.js as expected by the browser
49toHtml :: String -- ^ Location of elm-min.js as expected by the browser
42 -> String -- ^ The page title
50 -> String -- ^ The page title
@@ -45,6 +53,10 @@ toHtml :: String -- ^ Location of elm-min.js as expected by the browser
45toHtml = generateHtml
53toHtml = generateHtml
46
54
47-- |The absolute path to Elm's runtime system.
55-- |The absolute path to Elm's runtime system.
48−runtimeLocation :: IO FilePath
56+runtime :: IO FilePath
49−runtimeLocation = getDataFileName "elm-runtime.js"
57+runtime = getDataFileName "elm-runtime.js"
58+
59+-- |The absolute path to Elm's core library documentation.
60+docs :: IO FilePath
61+docs = getDataFileName "docs.json"
50
62
compiler/Model/Libraries.hs+9−1
@@ -1,4 +1,4 @@
1−module Libraries (libraries, prelude) where
1+module Libraries (libraries, addPrelude, prelude) where
2
2
3import Ast
3import Ast
4import qualified Data.Map as Map
4import qualified Data.Map as Map
@@ -6,6 +6,14 @@ import Data.List (inits)
6import Text.JSON
6import Text.JSON
7import LoadLibraries (docs)
7import LoadLibraries (docs)
8
8
9+
10+addPrelude (Module name exs ims stmts) = Module name exs (customIms ++ ims) stmts
11+ where customIms = concatMap addModule prelude
12+
13+ addModule (n, method) = case lookup n ims of
14+ Nothing -> [(n, method)]
15+ Just _ -> []
16+
9prelude = text : map (\n -> (n, Hiding [])) modules
17prelude = text : map (\n -> (n, Hiding [])) modules
10 where
18 where
11 text = ("Graphics.Text", Hiding ["link", "color"])
19 text = ("Graphics.Text", Hiding ["link", "color"])