on master help sign in
Get elm-doc to extract comments!
b1d0c74evancz
compiler/Docs.hs+24−11
@@ -19,31 +19,44 @@ main = do
19 Left err -> putStrLn err >> exitFailure
19 Left err -> putStrLn err >> exitFailure
20 Right ms -> putStrLn (toModules ms)
20 Right ms -> putStrLn (toModules ms)
21
21
22−toModules ms =
22+toModules ms = wrap (intercalate ",\n " (map toModule ms))
23− "{ \"modules\" : [\n " ++ intercalate ",\n " (map toModule ms) ++ "\n ]\n}"
23+ where wrap s = "{ \"modules\" : [\n " ++ s ++ "\n ]\n}"
24
24
25toModule (name, values) =
25toModule (name, values) =
26− "{ \"name\" : " ++ show name ++ ",\n \"values\" : [\n " ++ vs ++ "\n ]\n }"
26+ "{ \"name\" : " ++ show name ++ ",\n " ++
27+ "\"values\" : [\n " ++ vs ++ "\n ]\n }"
27− where vs = intercalate ",\n " (map toValue values)
28+ where vs = intercalate ",\n " (map toValue values)
28
29
29−toValue (name, tipe) =
30+toValue (name, tipe, desc) =
31+ "{ \"name\" : " ++ show name ++
30− "{ \"name\" : " ++ show name ++ ",\n \"type\" : \"" ++ show tipe ++ "\"\n }"
32+ ",\n \"type\" : \"" ++ show tipe ++
33+ ",\n \"desc\" : " ++ show desc ++ "\n }"
31
34
32−
33−docParse :: String -> Either String (String, [(String, Type)])
35+docParse :: String -> Either String (String, [(String, Type, String)])
34docParse = setupParser $ do
36docParse = setupParser $ do
35 optional freshLine
37 optional freshLine
36 (,) <$> option "Main" moduleName <*> types
38 (,) <$> option "Main" moduleName <*> types
37 where
39 where
38 skip = manyTill anyChar simpleNewline >> return []
40 skip = manyTill anyChar simpleNewline >> return []
39 end = many1 anyChar >> return []
41 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)
43 getName = intercalate "." . fst
43 getName = intercalate "." . fst
44 moduleName = do optional freshLine
44 moduleName = do optional freshLine
45 getName <$> moduleDef `followedBy` freshLine
45 getName <$> moduleDef `followedBy` freshLine
46
46
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+
47setupParser p source =
60setupParser p source =
48 case iParse p "" source of
61 case iParse p "" source of
49 Right result -> Right result
62 Right result -> Right result
compiler/Language/Elm.hs+15−46
@@ -14,7 +14,7 @@
14 at <https://github.com/tazjin/Elm/tree/master/Examples>
14 at <https://github.com/tazjin/Elm/tree/master/Examples>
15-}
15-}
16module Language.Elm (
16module Language.Elm (
17− ElmSource (..),
17+ compile, toHtml,
18 runtimeLocation
18 runtimeLocation
19 ) where
19 ) where
20
20
@@ -30,52 +30,21 @@ import Paths_Elm
30import qualified Data.Text as TS
30import qualified Data.Text as TS
31import qualified Data.Text.Lazy as TL
31import qualified Data.Text.Lazy as TL
32
32
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+
33-- |The absolute path to Elm's runtime system.
47-- |The absolute path to Elm's runtime system.
34runtimeLocation :: IO FilePath
48runtimeLocation :: IO FilePath
35runtimeLocation = getDataFileName "elm-runtime.js"
49runtimeLocation = getDataFileName "elm-runtime.js"
36
50
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