1{-# LANGUAGE TupleSections #-}
5import Control.Applicative
6import Control.Monad.Trans.Except (runExceptT)
7import Control.Arrow (first, second)
8import Control.Category ((>>>))
9import Control.Monad.Writer
10import Data.Text (Text)
11import Data.Function (on)
13import Data.Maybe (fromMaybe)
14import Data.Tuple (swap)
15import Data.Version (showVersion)
17import Options.Applicative
18import qualified Text.PrettyPrint.ANSI.Leijen as PP
20import qualified Language.PureScript as P
21import qualified Paths_purescript as Paths
22import System.Exit (exitFailure)
23import System.IO (hPutStrLn, hPrint, hSetEncoding, stderr, stdout, utf8)
24import System.IO.UTF8 (readUTF8FileT)
25import System.Directory (createDirectoryIfMissing)
26import System.FilePath (takeDirectory)
27import System.FilePath.Glob (glob)
31import qualified Language.PureScript.Docs as D
32import qualified Language.PureScript.Docs.AsMarkdown as D
34-- Available output formats
35data Format = Markdown -- Output documentation in Markdown format
36 | Ctags -- Output ctags symbol index suitable for use with vi
37 | Etags -- Output etags symbol index suitable for use with emacs
38 deriving (Show, Eq, Ord)
40-- | Available methods of outputting Markdown documentation
43 | ToStdOut [P.ModuleName]
44 | ToFiles [(P.ModuleName, FilePath)]
47data PSCDocsOptions = PSCDocsOptions
48 { pscdFormat :: Format
49 , pscdInputFiles :: [FilePath]
50 , pscdDocgen :: DocgenOutput
54docgen :: PSCDocsOptions -> IO ()
55docgen (PSCDocsOptions fmt inputGlob output) = do
56 input <- concat <$> mapM glob inputGlob
58 Etags -> dumpTags input dumpEtags
59 Ctags -> dumpTags input dumpCtags
61 ms <- runExceptT (D.parseAndBookmark input []
62 >>= (fst >>> D.convertModulesInPackage))
67 putStrLn (D.runDocs (D.modulesAsMarkdown ms))
69 let (ms', missing) = takeByName ms names
71 putStrLn (D.runDocs (D.modulesAsMarkdown ms'))
73 let (ms', missing) = takeByName' ms names
76 let ms'' = groupBy ((==) `on` fst) . sortBy (compare `on` fst) $ map swap ms'
77 forM_ ms'' $ \grp -> do
78 let fp = fst (head grp)
79 createDirectoryIfMissing True (takeDirectory fp)
80 writeFile fp (D.runDocs (D.modulesAsMarkdown (map snd grp)))
83 guardMissing [] = return ()
84 guardMissing [mn] = do
85 hPutStrLn stderr ("psc-docs: error: unknown module \"" ++ P.runModuleName mn ++ "\"")
88 hPutStrLn stderr "psc-docs: error: unknown modules:"
90 hPutStrLn stderr (" * " ++ P.runModuleName mn)
93 successOrExit :: Either P.MultipleErrors a -> IO a
99 hPutStrLn stderr $ P.prettyPrintMultipleErrors P.defaultPPEOptions err
102 takeByName = takeModulesByName D.modName
103 takeByName' = takeModulesByName' D.modName
106-- Given a list of module names and a list of modules, return a list of modules
107-- whose names appeared in the given name list, together with a list of names
108-- for which no module could be found in the module list.
110takeModulesByName :: (Eq n) => (m -> n) -> [m] -> [n] -> ([m], [n])
111takeModulesByName getModuleName modules names =
112 first (map fst) (takeModulesByName' getModuleName modules (map (,()) names))
115-- Like takeModulesByName, but also keeps some extra information with each
118takeModulesByName' :: (Eq n) => (m -> n) -> [m] -> [(n, a)] -> ([(m, a)], [n])
119takeModulesByName' getModuleName modules = foldl go ([], [])
121 go (ms, missing) (name, x) =
122 case find ((== name) . getModuleName) modules of
123 Just m -> ((m, x) : ms, missing)
124 Nothing -> (ms, name : missing)
126dumpTags :: [FilePath] -> ([(String, P.Module)] -> [String]) -> IO ()
127dumpTags input renderTags = do
128 e <- P.parseModulesFromFiles (fromMaybe "") <$> mapM (fmap (first Just) . parseFile) (nub input)
134 ldump (renderTags (pairs ms))
137 pairs :: [(Maybe String, m)] -> [(String, m)]
138 pairs = map (first (fromMaybe ""))
140 ldump :: [String] -> IO ()
141 ldump = mapM_ putStrLn
143parseFile :: FilePath -> IO (FilePath, Text)
144parseFile input = (,) input <$> readUTF8FileT input
146inputFile :: Parser FilePath
147inputFile = strArgument $
149 <> help "The input .purs file(s)"
151instance Read Format where
152 readsPrec _ "etags" = [(Etags, "")]
153 readsPrec _ "ctags" = [(Ctags, "")]
154 readsPrec _ "markdown" = [(Markdown, "")]
157format :: Parser Format
158format = option auto $ value Markdown
161 <> help "Set output FORMAT (markdown | etags | ctags)"
163docgenModule :: Parser String
164docgenModule = strOption $
166 <> help "A list of module names which should appear in the output. This can optionally include file paths to write individual modules to, by separating with a colon ':'. For example, Prelude:docs/Prelude.md. This option may be specified multiple times."
168pscDocsOptions :: Parser (Format, [FilePath], [String])
169pscDocsOptions = (,,) <$> format <*> many inputFile <*> many docgenModule
171parseDocgen :: [String] -> Either String DocgenOutput
172parseDocgen [] = Right EverythingToStdOut
173parseDocgen xs = go xs
182 = IToStdOut P.ModuleName
183 | IToFile (P.ModuleName, FilePath)
185parseItem :: String -> DocgenOutputItem
186parseItem s = case elemIndex ':' s of
189 >>> first P.moduleNameFromString
193 IToStdOut (P.moduleNameFromString s)
199combine :: [DocgenOutputItem] -> Either String DocgenOutput
200combine [] = Right EverythingToStdOut
201combine (x:xs) = foldM go (initial x) xs
203 initial (IToStdOut m) = ToStdOut [m]
204 initial (IToFile m) = ToFiles [m]
206 go (ToStdOut ms) (IToStdOut m) = Right (ToStdOut (m:ms))
207 go (ToFiles ms) (IToFile m) = Right (ToFiles (m:ms))
208 go _ _ = Left "Can't mix module names and module name/file path pairs in the same invocation."
210buildOptions :: (Format, [FilePath], [String]) -> IO PSCDocsOptions
211buildOptions (fmt, input, mapping) =
212 case parseDocgen mapping of
213 Right mapping' -> return (PSCDocsOptions fmt input mapping')
215 hPutStrLn stderr "psc-docs: error in --docgen option:"
216 hPutStrLn stderr (" " ++ err)
221 hSetEncoding stdout utf8
222 hSetEncoding stderr utf8
223 execParser opts >>= buildOptions >>= docgen
225 opts = info (version <*> helper <*> pscDocsOptions) infoModList
226 infoModList = fullDesc <> headerInfo <> footerInfo
227 headerInfo = header "psc-docs - Generate Markdown documentation from PureScript source files"
228 footerInfo = footerDoc $ Just $ PP.vcat
229 [ examples, PP.empty, PP.text ("psc-docs " ++ showVersion Paths.version) ]
231 version :: Parser (a -> a)
232 version = abortOption (InfoMsg (showVersion Paths.version)) $ long "version" <> help "Show the version number" <> hidden
236 PP.vcat $ map PP.text
238 , " print documentation for Data.List to stdout:"
239 , " psc-docs \"src/**/*.purs\" \".psc-package/*/*/*/src/**/*.purs\" \\"
240 , " --docgen Data.List"
242 , " write documentation for Data.List to docs/Data.List.md:"
243 , " psc-docs \"src/**/*.purs\" \".psc-package/*/*/*/src/**/*.purs\" \\"
244 , " --docgen Data.List:docs/Data.List.md"
246 , " write documentation for Data.List to docs/Data.List.md, and"
247 , " documentation for Data.List.Lazy to docs/Data.List.Lazy.md:"
248 , " psc-docs \"src/**/*.purs\" \".psc-package/*/*/*/src/**/*.purs\" \\"
249 , " --docgen Data.List:docs/Data.List.md \\"
250 , " --docgen Data.List.Lazy:docs/Data.List.Lazy.md"
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list