@@ -18,8 +18,8 @@ data ELM =
1818 ELM { make :: Bool
1919 , files :: [FilePath]
2020 , runtime :: Maybe FilePath
21−-- , separate_js :: Bool
22−-- , only_js :: Bool
21+ , separate_js :: Bool
22+ , only_js :: Bool
2323-- , verbose_js :: Bool
2424 }
2525 deriving (Data,Typeable,Show,Eq)
@@ -28,8 +28,8 @@ elm = ELM { make = False &= help "automatically compile dependencies."
2828 , files = def &= args &= typ "FILES"
2929 , runtime = Nothing &= typ "FILE" &=
3030 help "Specify a custom location for Elm's runtime system."
31−-- , separate_js = False &= help "Compile to separate HTML and JS files."
32−-- , only_js = False &= help "Compile only to JavaScript."
31+ , separate_js = False &= help "Compile to separate HTML and JS files."
32+ , only_js = False &= help "Compile only to JavaScript."
3333-- , verbose_js = False &= help "Produce JavaScript that may be easier to debug."
3434 } &=
3535 help "Compile Elm programs to HTML, CSS, and JavaScript." &=
@@ -40,20 +40,30 @@ main = do
4040 mini <- getDataFileName "elm-runtime-0.3.0.js"
4141 compileArgs mini args
4242
43−compileArgs mini (ELM _ [] _) =
43+compileArgs mini (ELM _ [] _ _ _) =
4444 putStrLn "Usage: elm [OPTIONS] [FILES]\nFor more help: elm --help"
45−compileArgs mini (ELM make files rtLoc) =
46− mapM_ (fileToHtml $ fromMaybe mini rtLoc) files
45+compileArgs mini (ELM make files rtLoc split only) =
46+ mapM_ (fileTo get what $ fromMaybe mini rtLoc) files
47+ where get = if make then getModules [] else getModule
48+ what = if only then JS else
49+ if split then Split else HTML
4750
48−fileToHtml rtLoc file = do
49− ems <- getModules [] file
51+data What = JS | HTML | Split
52+
53+fileTo get what rtLoc file = do
54+ ems <- get file
5055 case ems of
5156 Left err -> putStrLn $ "Error while compiling " ++ file ++ ":\n" ++ err
5257 Right ms ->
53− let name = reverse . tail . dropWhile (/='.') $ reverse file in
54− writeFile (name ++ ".html") . renderHtml $
55− modulesToHtml rtLoc ms
56−
58+ let name = reverse . tail . dropWhile (/='.') $ reverse file
59+ js = name ++ ".js"
60+ html = name ++ ".html"
61+ in case what of
62+ JS -> writeFile js $ concatMap jsModule ms
63+ HTML -> writeFile html . renderHtml $ modulesToHtml rtLoc ms
64+ Split -> do
65+ writeFile html . renderHtml $ linkedHtml rtLoc js ms
66+ writeFile js $ concatMap jsModule ms
5767
5868getModules :: [String] -> FilePath -> IO (Either String [Module])
5969getModules uses file = do
@@ -71,6 +81,11 @@ getModules uses file = do
7181 [] -> Right $ concat (rights ems) ++ [modul]
7282 errs -> Left $ intercalate "\n" errs
7383
84+getModule :: FilePath -> IO (Either String [Module])
85+getModule file = do
86+ code <- readFile file
87+ return . fmap (:[]) $ initialize code
88+
7489toFilePath :: String -> FilePath
7590toFilePath modul = map (\c -> if c == '.' then '/' else c) modul ++ ".elm"
7691