on master help sign in
Get elm-doc to extract comments!
commitb1d0c74c2decd0f5b064fb49e2c281b2f185e296 authorevancz <info@elm-lang.org> dateSun 17 Mar 2013, 16:36 (14 years ago) parent9c74b86
2 files changed+39−57
compiler/Docs.hs+24−11 compiler/Language/Elm.hs+15−46
compiler/Docs.hs+24−11
@@ -19,31 +19,44 @@ main = do
1919 Left err -> putStrLn err >> exitFailure
2020 Right ms -> putStrLn (toModules ms)
2121
22−toModules ms =
23− "{ \"modules\" : [\n " ++ intercalate ",\n " (map toModule ms) ++ "\n ]\n}"
22+toModules ms = wrap (intercalate ",\n " (map toModule ms))
23+ where wrap s = "{ \"modules\" : [\n " ++ s ++ "\n ]\n}"
2424
2525toModule (name, values) =
26− "{ \"name\" : " ++ show name ++ ",\n \"values\" : [\n " ++ vs ++ "\n ]\n }"
27− where vs = intercalate ",\n " (map toValue values)
26+ "{ \"name\" : " ++ show name ++ ",\n " ++
27+ "\"values\" : [\n " ++ vs ++ "\n ]\n }"
28+ where vs = intercalate ",\n " (map toValue values)
2829
29−toValue (name, tipe) =
30− "{ \"name\" : " ++ show name ++ ",\n \"type\" : \"" ++ show tipe ++ "\"\n }"
30+toValue (name, tipe, desc) =
31+ "{ \"name\" : " ++ show name ++
32+ ",\n \"type\" : \"" ++ show tipe ++
33+ ",\n \"desc\" : " ++ show desc ++ "\n }"
3134
32−
33−docParse :: String -> Either String (String, [(String, Type)])
35+docParse :: String -> Either String (String, [(String, Type, String)])
3436docParse = setupParser $ do
3537 optional freshLine
3638 (,) <$> option "Main" moduleName <*> types
3739 where
3840 skip = manyTill anyChar simpleNewline >> return []
3941 end = many1 anyChar >> return []
40− tipe = get <$> try typeAnnotation
41− get stmt = case stmt of { TypeAnnotation n t -> [(n,t)] ; _ -> [] }
42− types = concat <$> many (tipe <|> try skip <|> end)
42+ types = concat <$> many (docs <|> try skip <|> end)
4343 getName = intercalate "." . fst
4444 moduleName = do optional freshLine
4545 getName <$> moduleDef `followedBy` freshLine
4646
47+docs :: IParser [(String, Type, String)]
48+docs = (tipe <$> try typeAnnotation) <|> commentTipe
49+ where
50+ clip str = case str of { ' ':rest -> rest ; _ -> str }
51+ tipe stmt = case stmt of { TypeAnnotation n t -> [(n,t,"")] ; _ -> [] }
52+ commentTipe = do
53+ cs <- map clip <$> many1 lineComment
54+ typ <- optionMaybe (try typeAnnotation)
55+ return $ case typ of
56+ Just (TypeAnnotation n t) -> [(n, t, intercalate "\n" cs)]
57+ _ -> []
58+
59+
4760setupParser p source =
4861 case iParse p "" source of
4962 Right result -> Right result
compiler/Language/Elm.hs+15−46
@@ -14,7 +14,7 @@
1414 at <https://github.com/tazjin/Elm/tree/master/Examples>
1515-}
1616module Language.Elm (
17− ElmSource (..),
17+ compile, toHtml,
1818 runtimeLocation
1919 ) where
2020
@@ -30,52 +30,21 @@ import Paths_Elm
3030import qualified Data.Text as TS
3131import qualified Data.Text.Lazy as TL
3232
33+-- |This function compiles Elm code to three separate parts: HTML, CSS,
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.
36+compile :: String -> String
37+compile source = either showErr jsModule modul
38+ where modul = buildFromSource source
39+
40+-- |This function compiles Elm code into a full HTML page.
41+toHtml :: String -- ^ Location of elm-min.js as expected by the browser
42+ -> String -- ^ The page title
43+ -> String -- ^ The elm source code
44+ -> Html
45+toHtml = generateHtml
46+
3347-- |The absolute path to Elm's runtime system.
3448runtimeLocation :: IO FilePath
3549runtimeLocation = getDataFileName "elm-runtime.js"
3650
37−class ElmSource a where
38− -- |This function compiles Elm code to three separate parts: HTML, CSS,
39− -- and JavaScript. The HTML is only the contents of the body, so the three
40− -- parts must be combined in a basic HTML skeleton.
41− compile :: a -> String
42−
43− -- |This function compiles Elm code into a full HTML page.
44− toHtml :: String -- ^ Location of elm-min.js as expected by the browser
45− -> String -- ^ The page title
46− -> a -- ^ The elm source code
47− -> Html
48−
49−instance ElmSource String where
50− compile = compileHelper
51− toHtml = generateHtml
52−
53−instance ElmSource Elm where
54− compile = compileHelper . TL.unpack . renderElm
55− toHtml elmL title = generateHtml elmL title . TL.unpack . renderElm
56−
57−-- |Strict text
58−instance ElmSource TS.Text where
59− compile = compileHelper . TS.unpack
60− toHtml elmL title = generateHtml elmL title . TS.unpack
61−
62−-- |Lazy text
63−instance ElmSource TL.Text where
64− compile = compileHelper . TL.unpack
65− toHtml elmL title = generateHtml elmL title . TL.unpack
66−
67−-- | (urlRenderFn, urlRenderFn -> Elm)
68−instance ElmSource (t, t -> Elm) where
69− compile (f, s) = compileHelper $ TL.unpack $ renderElm $ s f
70− toHtml elmL title (f, s) = generateHtml elmL title $ TL.unpack $ renderElm $ s f
71−
72−-- | to be used without URL interpolation
73−instance ElmSource (t -> Elm) where
74− compile s = compileHelper $ TL.unpack $ renderElm $ s undefined
75− toHtml l t s = generateHtml l t $ TL.unpack $ renderElm $ s undefined
76−
77−
78−-- build helper to avoid boilerplate repetition
79−compileHelper :: String -> String
80−compileHelper source = either showErr jsModule modul
81− where modul = buildFromSource source