@@ -15,13 +15,11 @@
15module Main where
15module Main where
17import Control.Applicative
17import Control.Applicative
19import Control.Monad.Writer
18import Control.Monad.Writer
20import Control.Arrow (first)
19import Control.Arrow (first)
21import Data.List
20import Data.List
22import Data.Maybe (fromMaybe)
21import Data.Maybe (fromMaybe)
23import Data.Version (showVersion)
22import Data.Version (showVersion)
24−import Data.Foldable (traverse_)
26import Options.Applicative
24import Options.Applicative
@@ -32,6 +30,7 @@ import System.IO (hPutStrLn, stderr)
33import Etags
31import Etags
34import Ctags
32import Ctags
33+import Language.PureScript.Docs.AsMarkdown
36-- Available output formats
35-- Available output formats
37data Format = Markdown -- Output documentation in Markdown format
36data Format = Markdown -- Output documentation in Markdown format
@@ -52,7 +51,7 @@ docgen (PSCDocsOptions fmt input) = do
52 exitFailure
51 exitFailure
53 Right ms -> do
52 Right ms -> do
54 case fmt of
53 case fmt of
55− Markdown -> putStrLn . runDocs $ renderModules (map snd ms)
54+ Markdown -> putStrLn $ renderModulesAsMarkdown $ map snd ms
56 Etags -> ldump $ dumpEtags $ pairs ms
55 Etags -> ldump $ dumpEtags $ pairs ms
57 Ctags -> ldump $ dumpCtags $ pairs ms
56 Ctags -> ldump $ dumpCtags $ pairs ms
58 exitSuccess
57 exitSuccess
@@ -64,175 +63,6 @@ docgen (PSCDocsOptions fmt input) = do
64parseFile :: FilePath -> IO (FilePath, String)
63parseFile :: FilePath -> IO (FilePath, String)
65parseFile input = (,) input <$> readFile input
64parseFile input = (,) input <$> readFile input
67−type Docs = Writer [String] ()
69−runDocs :: Docs -> String
70−runDocs = unlines . execWriter
75−headerLevel :: Int -> String -> Docs
76−headerLevel level hdr = tell [replicate level '#' ++ ' ' : hdr]
78−withIndent :: Int -> Docs -> Docs
79−withIndent indent = censor (map (replicate indent ' ' ++ ))
81−atIndent :: Int -> String -> Docs
82−atIndent indent text =
83− let ls = lines text in
84− withIndent indent (tell ls)
86−fenced :: String -> Docs
87−fenced text = fencedBlock (tell $ lines text)
89−fencedBlock :: Docs -> Docs
90−fencedBlock inner = do
91− tell ["``` purescript"]
95−ticks :: String -> String
96−ticks = ("`" ++) . (++ "`")
98−renderModules :: [P.Module] -> Docs
100− headerLevel 1 "Module Documentation"
102− mapM_ renderModule ms
104−renderModule :: P.Module -> Docs
105−renderModule mdl@(P.Module coms moduleName _ exps) = do
106− headerLevel 2 $ "Module " ++ P.runModuleName moduleName
108− unless (null coms) $ do
111− renderTopLevel exps (P.exportedDeclarations mdl)
114−renderTopLevel :: Maybe [P.DeclarationRef] -> [P.Declaration] -> Docs
115−renderTopLevel exps decls = forM_ decls $ \decl ->
116− when (canRenderDecl decl) $ do
117− traverse_ (headerLevel 4) (ticks `fmap` getDeclarationTitle decl)
119− renderDeclaration exps decl
122−renderTypeclassImage :: P.ModuleName -> Docs
123−renderTypeclassImage name =
124− let name' = P.runModuleName name
125− in tell [""]
127−getDeclarationTitle :: P.Declaration -> Maybe String
128−getDeclarationTitle (P.TypeDeclaration name _) = Just (show name)
129−getDeclarationTitle (P.ExternDeclaration _ name _ _) = Just (show name)
130−getDeclarationTitle (P.DataDeclaration _ name _ _) = Just (show name)
131−getDeclarationTitle (P.ExternDataDeclaration name _) = Just (show name)
132−getDeclarationTitle (P.TypeSynonymDeclaration name _ _) = Just (show name)
133−getDeclarationTitle (P.TypeClassDeclaration name _ _ _) = Just (show name)
134−getDeclarationTitle (P.TypeInstanceDeclaration name _ _ _ _) = Just (show name)
135−getDeclarationTitle (P.PositionedDeclaration _ _ d) = getDeclarationTitle d
136−getDeclarationTitle _ = Nothing
138−renderDeclaration :: Maybe [P.DeclarationRef] -> P.Declaration -> Docs
139−renderDeclaration _ (P.TypeDeclaration ident ty) =
140− fenced $ show ident ++ " :: " ++ prettyPrintType' ty
141−renderDeclaration _ (P.ExternDeclaration _ ident _ ty) =
142− fenced $ show ident ++ " :: " ++ prettyPrintType' ty
143−renderDeclaration exps (P.DataDeclaration dtype name args ctors) = do
145− typeApp = foldl P.TypeApp (P.TypeConstructor (P.Qualified Nothing name)) (map toTypeVar args)
146− typeName = prettyPrintType' typeApp
147− exported = filter (P.isDctorExported name exps . fst) ctors
149− tell [show dtype ++ " " ++ typeName]
150− zipWithM_ (\isFirst (ctor, tys) ->
151− atIndent 2 $ (if isFirst then "= " else "| ") ++ P.runProperName ctor ++ " " ++ unwords (map P.prettyPrintTypeAtom tys))
152− (True : repeat False) exported
153−renderDeclaration _ (P.ExternDataDeclaration name kind) =
154− fenced $ "data " ++ P.runProperName name ++ " :: " ++ P.prettyPrintKind kind
155−renderDeclaration _ (P.TypeSynonymDeclaration name args ty) = do
157− typeApp = foldl P.TypeApp (P.TypeConstructor (P.Qualified Nothing name)) (map toTypeVar args)
158− typeName = prettyPrintType' typeApp
159− fenced $ "type " ++ typeName ++ " = " ++ prettyPrintType' ty
160−renderDeclaration _ (P.TypeClassDeclaration name args implies ds) = do
161− let impliesText = case implies of
163− is -> "(" ++ intercalate ", " (map (\(pn, tys') -> show pn ++ " " ++ unwords (map P.prettyPrintTypeAtom tys')) is) ++ ") <= "
164− classApp = foldl P.TypeApp (P.TypeConstructor (P.Qualified Nothing name)) (map toTypeVar args)
165− className = prettyPrintType' classApp
167− tell ["class " ++ impliesText ++ className ++ " where"]
168− mapM_ renderClassMember ds
170− renderClassMember (P.PositionedDeclaration _ _ d) = renderClassMember d
171− renderClassMember (P.TypeDeclaration ident ty) = atIndent 2 $ show ident ++ " :: " ++ prettyPrintType' ty
172− renderClassMember _ = error "Invalid argument to renderClassMember."
173−renderDeclaration _ (P.TypeInstanceDeclaration name constraints className tys _) = do
174− let constraintsText = case constraints of
176− cs -> "(" ++ intercalate ", " (map (\(pn, tys') -> show pn ++ " " ++ unwords (map P.prettyPrintTypeAtom tys')) cs) ++ ") => "
177− fenced $ "instance " ++ show name ++ " :: " ++ constraintsText ++ show className ++ " " ++ unwords (map P.prettyPrintTypeAtom tys)
178−renderDeclaration exps (P.PositionedDeclaration _ com d) = do
179− renderDeclaration exps d
181−renderDeclaration _ _ = return ()
183−renderComments :: [P.Comment] -> Docs
184−renderComments cs = do
185− let raw = concatMap toLines cs
186− when (all hasPipe raw) $ do
188− atIndent 0 . unlines . map stripPipes $ raw
191− toLines (P.LineComment s) = [s]
192− toLines (P.BlockComment s) = lines s
194− hasPipe s = case dropWhile (== ' ') s of { ('|':_) -> True; _ -> False }
196− stripPipes = dropPipe . dropWhile (== ' ')
198− dropPipe ('|':' ':s) = s
199− dropPipe ('|':s) = s
202−toTypeVar :: (String, Maybe P.Kind) -> P.Type
203−toTypeVar (s, Nothing) = P.TypeVar s
204−toTypeVar (s, Just k) = P.KindedType (P.TypeVar s) k
206−prettyPrintType' :: P.Type -> String
207−prettyPrintType' = P.prettyPrintType . P.everywhereOnTypes dePrim
209− dePrim ty@(P.TypeConstructor (P.Qualified _ name))
210− | ty == P.tyBoolean || ty == P.tyNumber || ty == P.tyString || ty == P.tyChar || ty == P.tyInt =
211− P.TypeConstructor $ P.Qualified Nothing name
212− dePrim other = other
214−getName :: P.Declaration -> String
215−getName (P.TypeDeclaration ident _) = show ident
216−getName (P.ExternDeclaration _ ident _ _) = show ident
217−getName (P.DataDeclaration _ name _ _) = P.runProperName name
218−getName (P.ExternDataDeclaration name _) = P.runProperName name
219−getName (P.TypeSynonymDeclaration name _ _) = P.runProperName name
220−getName (P.TypeClassDeclaration name _ _ _) = P.runProperName name
221−getName (P.TypeInstanceDeclaration name _ _ _ _) = show name
222−getName (P.PositionedDeclaration _ _ d) = getName d
223−getName _ = error "Invalid argument to getName"
225−canRenderDecl :: P.Declaration -> Bool
226−canRenderDecl P.TypeDeclaration{} = True
227−canRenderDecl P.ExternDeclaration{} = True
228−canRenderDecl P.DataDeclaration{} = True
229−canRenderDecl P.ExternDataDeclaration{} = True
230−canRenderDecl P.TypeSynonymDeclaration{} = True
231−canRenderDecl P.TypeClassDeclaration{} = True
232−canRenderDecl P.TypeInstanceDeclaration{} = True
233−canRenderDecl (P.PositionedDeclaration _ _ d) = canRenderDecl d
234−canRenderDecl _ = False
236inputFile :: Parser FilePath
66inputFile :: Parser FilePath
237inputFile = strArgument $
67inputFile = strArgument $
238 metavar "FILE"
68 metavar "FILE"