Add pretty printing and require a module declaration and structure comment
This commit is contained in:
parent
caf2d0ab6d
commit
ae5ef9db97
2 changed files with 13 additions and 7 deletions
|
@ -191,6 +191,7 @@ Executable elm-doc
|
||||||
Parse.Type
|
Parse.Type
|
||||||
|
|
||||||
Build-depends: aeson,
|
Build-depends: aeson,
|
||||||
|
aeson-pretty,
|
||||||
base >=4.2 && <5,
|
base >=4.2 && <5,
|
||||||
binary,
|
binary,
|
||||||
bytestring,
|
bytestring,
|
||||||
|
|
|
@ -8,6 +8,7 @@ import System.Exit
|
||||||
|
|
||||||
import Control.Applicative ((<$>), (<*>))
|
import Control.Applicative ((<$>), (<*>))
|
||||||
import Data.Aeson
|
import Data.Aeson
|
||||||
|
import Data.Aeson.Encode.Pretty
|
||||||
import qualified Data.List as List
|
import qualified Data.List as List
|
||||||
import qualified Data.Map as Map
|
import qualified Data.Map as Map
|
||||||
import qualified Data.ByteString.Lazy as BS
|
import qualified Data.ByteString.Lazy as BS
|
||||||
|
@ -41,25 +42,29 @@ parseFile path = do
|
||||||
Right json -> do
|
Right json -> do
|
||||||
putStrLn $ "Documenting " ++ path
|
putStrLn $ "Documenting " ++ path
|
||||||
createDirectoryIfMissing True "docs"
|
createDirectoryIfMissing True "docs"
|
||||||
BS.writeFile ("docs" </> replaceExtension path ".json") (encode json)
|
BS.writeFile ("docs" </> replaceExtension path ".json")
|
||||||
|
(encodePretty' (defConfig { confIndent = 2 }) json)
|
||||||
Left err -> do
|
Left err -> do
|
||||||
putStrLn $ "Parse error in " ++ path ++ " at " ++ show err
|
putStrLn $ "Parse error in " ++ path ++ " at " ++ show err
|
||||||
exitFailure
|
exitFailure
|
||||||
|
|
||||||
docs :: IParser Value
|
docs :: IParser Value
|
||||||
docs = do
|
docs = do
|
||||||
optional freshLine
|
(name, exports, structure) <- moduleDocs
|
||||||
(names, exports) <- option (["Main"],[]) moduleDef
|
|
||||||
optional freshLine
|
|
||||||
structure <- option "" docComment
|
|
||||||
things <- document
|
things <- document
|
||||||
return $ toJson (List.intercalate "." names) exports structure things
|
return $ toJson name exports structure things
|
||||||
|
|
||||||
docComment :: IParser String
|
docComment :: IParser String
|
||||||
docComment = do
|
docComment = do
|
||||||
try (string "{-|")
|
try (string "{-|")
|
||||||
contents <- closeComment
|
contents <- closeComment
|
||||||
return (init (init contents))
|
return $ dropWhile (==' ') (init (init contents))
|
||||||
|
|
||||||
|
moduleDocs = do
|
||||||
|
(names,exports) <- anyThen moduleDef
|
||||||
|
anyThen (lookAhead (string "{-|"))
|
||||||
|
structure <- docComment
|
||||||
|
return (List.intercalate "." names, exports, structure)
|
||||||
|
|
||||||
document :: IParser [(String, Declaration t v, String)]
|
document :: IParser [(String, Declaration t v, String)]
|
||||||
document = go []
|
document = go []
|
||||||
|
|
Loading…
Reference in a new issue