1{-# LANGUAGE TupleSections #-}
2----------------------------------------------------------------------------
5-- Copyright : (c) Phil Freeman 2013
8-- Maintainer : Phil Freeman <paf31@cantab.net>
9-- Stability : experimental
14-----------------------------------------------------------------------------
18import Control.Applicative
19import Control.Monad.Trans.Except (runExceptT)
20import Control.Arrow (first, second)
21import Control.Category ((>>>))
22import Control.Monad.Writer
23import Data.Function (on)
25import Data.Maybe (fromMaybe)
26import Data.Tuple (swap)
27import Data.Version (showVersion)
29import Options.Applicative
30import qualified Text.PrettyPrint.ANSI.Leijen as PP
32import qualified Language.PureScript as P
33import qualified Paths_purescript as Paths
34import System.Exit (exitFailure)
35import System.IO (hPutStrLn, stderr)
36import System.Directory (createDirectoryIfMissing)
37import System.FilePath (takeDirectory)
38import System.FilePath.Glob (glob)
42import qualified Language.PureScript.Docs as D
43import qualified Language.PureScript.Docs.AsMarkdown as D
45-- Available output formats
46data Format = Markdown -- Output documentation in Markdown format
47 | Ctags -- Output ctags symbol index suitable for use with vi
48 | Etags -- Output etags symbol index suitable for use with emacs
49 deriving (Show, Eq, Ord)
51-- | Available methods of outputting Markdown documentation
54 | ToStdOut [P.ModuleName]
55 | ToFiles [(P.ModuleName, FilePath)]
58data PSCDocsOptions = PSCDocsOptions
59 { pscdFormat :: Format
60 , pscdInputFiles :: [FilePath]
61 , pscdDocgen :: DocgenOutput
65docgen :: PSCDocsOptions -> IO ()
66docgen (PSCDocsOptions fmt inputGlob output) = do
67 input <- concat <$> mapM glob inputGlob
69 Etags -> dumpTags input dumpEtags
70 Ctags -> dumpTags input dumpCtags
72 ms <- runExceptT (D.parseAndDesugar input []
73 >>= ((\(ms, _, env) -> D.convertModulesInPackage env ms)))
78 putStrLn (D.runDocs (D.modulesAsMarkdown ms))
80 let (ms', missing) = takeByName ms (map P.runModuleName names)
82 putStrLn (D.runDocs (D.modulesAsMarkdown ms'))
84 let (ms', missing) = takeByName' ms (map (first P.runModuleName) names)
87 let ms'' = groupBy ((==) `on` fst) . sortBy (compare `on` fst) $ map swap ms'
88 forM_ ms'' $ \grp -> do
89 let fp = fst (head grp)
90 createDirectoryIfMissing True (takeDirectory fp)
91 writeFile fp (D.runDocs (D.modulesAsMarkdown (map snd grp)))
94 guardMissing [] = return ()
95 guardMissing [mn] = do
96 hPutStrLn stderr ("psc-docs: error: unknown module \"" ++ mn ++ "\"")
99 hPutStrLn stderr "psc-docs: error: unknown modules:"
101 hPutStrLn stderr (" * " ++ mn)
104 successOrExit :: Either P.MultipleErrors a -> IO a
110 hPutStrLn stderr $ P.prettyPrintMultipleErrors False err
113 takeByName = takeModulesByName D.modName
114 takeByName' = takeModulesByName' D.modName
117-- Given a list of module names and a list of modules, return a list of modules
118-- whose names appeared in the given name list, together with a list of names
119-- for which no module could be found in the module list.
121takeModulesByName :: (Eq n) => (m -> n) -> [m] -> [n] -> ([m], [n])
122takeModulesByName getModuleName modules names =
123 first (map fst) (takeModulesByName' getModuleName modules (map (,()) names))
126-- Like takeModulesByName, but also keeps some extra information with each
129takeModulesByName' :: (Eq n) => (m -> n) -> [m] -> [(n, a)] -> ([(m, a)], [n])
130takeModulesByName' getModuleName modules = foldl go ([], [])
132 go (ms, missing) (name, x) =
133 case find ((== name) . getModuleName) modules of
134 Just m -> ((m, x) : ms, missing)
135 Nothing -> (ms, name : missing)
137dumpTags :: [FilePath] -> ([(String, P.Module)] -> [String]) -> IO ()
138dumpTags input renderTags = do
139 e <- P.parseModulesFromFiles (fromMaybe "") <$> mapM (fmap (first Just) . parseFile) (nub input)
142 hPutStrLn stderr (show err)
145 ldump (renderTags (pairs ms))
148 pairs :: [(Maybe String, m)] -> [(String, m)]
149 pairs = map (first (fromMaybe ""))
151 ldump :: [String] -> IO ()
152 ldump = mapM_ putStrLn
154parseFile :: FilePath -> IO (FilePath, String)
155parseFile input = (,) input <$> readFile input
157inputFile :: Parser FilePath
158inputFile = strArgument $
160 <> help "The input .purs file(s)"
162instance Read Format where
163 readsPrec _ "etags" = [(Etags, "")]
164 readsPrec _ "ctags" = [(Ctags, "")]
165 readsPrec _ "markdown" = [(Markdown, "")]
168format :: Parser Format
169format = option auto $ value Markdown
172 <> help "Set output FORMAT (markdown | etags | ctags)"
174docgenModule :: Parser String
175docgenModule = strOption $
177 <> 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."
179pscDocsOptions :: Parser (Format, [FilePath], [String])
180pscDocsOptions = (,,) <$> format <*> many inputFile <*> many docgenModule
182parseDocgen :: [String] -> Either String DocgenOutput
183parseDocgen [] = Right EverythingToStdOut
184parseDocgen xs = go xs
193 = IToStdOut P.ModuleName
194 | IToFile (P.ModuleName, FilePath)
196parseItem :: String -> DocgenOutputItem
197parseItem s = case elemIndex ':' s of
200 >>> first P.moduleNameFromString
204 IToStdOut (P.moduleNameFromString s)
210combine :: [DocgenOutputItem] -> Either String DocgenOutput
211combine [] = Right EverythingToStdOut
212combine (x:xs) = foldM go (initial x) xs
214 initial (IToStdOut m) = ToStdOut [m]
215 initial (IToFile m) = ToFiles [m]
217 go (ToStdOut ms) (IToStdOut m) = Right (ToStdOut (m:ms))
218 go (ToFiles ms) (IToFile m) = Right (ToFiles (m:ms))
219 go _ _ = Left "Can't mix module names and module name/file path pairs in the same invocation."
221buildOptions :: (Format, [FilePath], [String]) -> IO PSCDocsOptions
222buildOptions (fmt, input, mapping) =
223 case parseDocgen mapping of
224 Right mapping' -> return (PSCDocsOptions fmt input mapping')
226 hPutStrLn stderr "psc-docs: error in --docgen option:"
227 hPutStrLn stderr (" " ++ err)
231main = execParser opts >>= buildOptions >>= docgen
233 opts = info (version <*> helper <*> pscDocsOptions) infoModList
234 infoModList = fullDesc <> headerInfo <> footerInfo
235 headerInfo = header "psc-docs - Generate Markdown documentation from PureScript source files"
236 footerInfo = footerDoc $ Just $ PP.vcat
237 [ examples, PP.empty, PP.text ("psc-docs " ++ showVersion Paths.version) ]
239 version :: Parser (a -> a)
240 version = abortOption (InfoMsg (showVersion Paths.version)) $ long "version" <> help "Show the version number" <> hidden
244 PP.vcat $ map PP.text
246 , " print documentation for Data.List to stdout:"
247 , " psc-docs \"src/**/*.purs\" \"bower_components/*/src/**/*.purs\" \\"
248 , " --docgen Data.List"
250 , " write documentation for Data.List to docs/Data.List.md:"
251 , " psc-docs \"src/**/*.purs\" \"bower_components/*/src/**/*.purs\" \\"
252 , " --docgen Data.List:docs/Data.List.md"
254 , " write documentation for Data.List to docs/Data.List.md, and"
255 , " documentation for Data.List.Lazy to docs/Data.List.Lazy.md:"
256 , " psc-docs \"src/**/*.purs\" \"bower_components/*/src/**/*.purs\" \\"
257 , " --docgen Data.List:docs/Data.List.md \\"
258 , " --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