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.Arrow (first, second)
20import Control.Category ((>>>))
21import Control.Monad.Writer
22import Data.Function (on)
24import Data.Maybe (fromMaybe)
25import Data.Tuple (swap)
26import Data.Version (showVersion)
28import Options.Applicative
29import qualified Text.PrettyPrint.ANSI.Leijen as PP
31import qualified Language.PureScript as P
32import qualified Paths_purescript as Paths
33import System.Exit (exitFailure)
34import System.IO (hPutStrLn, stderr)
35import System.Directory (createDirectoryIfMissing)
36import System.FilePath (takeDirectory)
37import System.FilePath.Glob (glob)
41import qualified Language.PureScript.Docs as D
42import qualified Language.PureScript.Docs.AsMarkdown as D
44-- Available output formats
45data Format = Markdown -- Output documentation in Markdown format
46 | Ctags -- Output ctags symbol index suitable for use with vi
47 | Etags -- Output etags symbol index suitable for use with emacs
48 deriving (Show, Eq, Ord)
50-- | Available methods of outputting Markdown documentation
53 | ToStdOut [P.ModuleName]
54 | ToFiles [(P.ModuleName, FilePath)]
57data PSCDocsOptions = PSCDocsOptions
58 { pscdFormat :: Format
59 , pscdInputFiles :: [FilePath]
60 , pscdDocgen :: DocgenOutput
64docgen :: PSCDocsOptions -> IO ()
65docgen (PSCDocsOptions fmt inputGlob output) = do
66 input <- concat <$> mapM glob inputGlob
68 Etags -> dumpTags input dumpEtags
69 Ctags -> dumpTags input dumpCtags
71 e <- D.parseAndDesugar input [] (\_ ms -> return ms)
74 hPutStrLn stderr $ P.prettyPrintMultipleErrors False err
79 putStrLn (D.renderModulesAsMarkdown ms')
81 let (ms, missing) = takeModulesByName ms' names
83 putStrLn (D.renderModulesAsMarkdown ms)
85 let (ms, missing) = takeModulesByName' ms' 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.renderModulesAsMarkdown $ snd `map` grp)
93 guardMissing [] = return ()
94 guardMissing [mn] = do
95 hPutStrLn stderr ("psc-docs: error: unknown module \"" ++ show mn ++ "\"")
98 hPutStrLn stderr "psc-docs: error: unknown modules:"
100 hPutStrLn stderr (" * " ++ show mn)
104-- Given a list of module names and a list of modules, return a list of modules
105-- whose names appeared in the given name list, together with a list of names
106-- for which no module could be found in the module list.
108takeModulesByName :: [P.Module] -> [P.ModuleName] -> ([P.Module], [P.ModuleName])
109takeModulesByName modules names =
110 first (map fst) (takeModulesByName' modules (map (,()) names))
113-- Like takeModulesByName but also keeps some extra data with the module.
115takeModulesByName' :: [P.Module] -> [(P.ModuleName, a)] -> ([(P.Module, a)], [P.ModuleName])
116takeModulesByName' modules = foldl go ([], [])
118 go (ms, missing) (name, x) =
119 case find ((== name) . P.getModuleName) modules of
120 Just m -> ((m, x) : ms, missing)
121 Nothing -> (ms, name : missing)
123dumpTags :: [FilePath] -> ([(String, P.Module)] -> [String]) -> IO ()
124dumpTags input renderTags = do
125 e <- P.parseModulesFromFiles (fromMaybe "") <$> mapM (fmap (first Just) . parseFile) (nub input)
128 hPutStrLn stderr (show err)
131 ldump (renderTags (pairs ms))
134 pairs :: [(Maybe String, m)] -> [(String, m)]
135 pairs = map (first (fromMaybe ""))
137 ldump :: [String] -> IO ()
138 ldump = mapM_ putStrLn
140parseFile :: FilePath -> IO (FilePath, String)
141parseFile input = (,) input <$> readFile input
143inputFile :: Parser FilePath
144inputFile = strArgument $
146 <> help "The input .purs file(s)"
148instance Read Format where
149 readsPrec _ "etags" = [(Etags, "")]
150 readsPrec _ "ctags" = [(Ctags, "")]
151 readsPrec _ "markdown" = [(Markdown, "")]
154format :: Parser Format
155format = option auto $ value Markdown
158 <> help "Set output FORMAT (markdown | etags | ctags)"
160docgenModule :: Parser String
161docgenModule = strOption $
163 <> 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."
165pscDocsOptions :: Parser (Format, [FilePath], [String])
166pscDocsOptions = (,,) <$> format <*> many inputFile <*> many docgenModule
168parseDocgen :: [String] -> Either String DocgenOutput
169parseDocgen [] = Right EverythingToStdOut
170parseDocgen xs = go xs
179 = IToStdOut P.ModuleName
180 | IToFile (P.ModuleName, FilePath)
182parseItem :: String -> DocgenOutputItem
183parseItem s = case elemIndex ':' s of
186 >>> first P.moduleNameFromString
190 IToStdOut (P.moduleNameFromString s)
196combine :: [DocgenOutputItem] -> Either String DocgenOutput
197combine [] = Right EverythingToStdOut
198combine (x:xs) = foldM go (initial x) xs
200 initial (IToStdOut m) = ToStdOut [m]
201 initial (IToFile m) = ToFiles [m]
203 go (ToStdOut ms) (IToStdOut m) = Right (ToStdOut (m:ms))
204 go (ToFiles ms) (IToFile m) = Right (ToFiles (m:ms))
205 go _ _ = Left "Can't mix module names and module name/file path pairs in the same invocation."
207buildOptions :: (Format, [FilePath], [String]) -> IO PSCDocsOptions
208buildOptions (fmt, input, mapping) =
209 case parseDocgen mapping of
210 Right mapping' -> return (PSCDocsOptions fmt input mapping')
212 hPutStrLn stderr "psc-docs: error in --docgen option:"
213 hPutStrLn stderr (" " ++ err)
217main = execParser opts >>= buildOptions >>= docgen
219 opts = info (version <*> helper <*> pscDocsOptions) infoModList
220 infoModList = fullDesc <> headerInfo <> footerInfo
221 headerInfo = header "psc-docs - Generate Markdown documentation from PureScript source files"
222 footerInfo = footerDoc $ Just $ PP.vcat
223 [ examples, PP.empty, PP.text ("psc-docs " ++ showVersion Paths.version) ]
225 version :: Parser (a -> a)
226 version = abortOption (InfoMsg (showVersion Paths.version)) $ long "version" <> help "Show the version number" <> hidden
230 PP.vcat $ map PP.text
232 , " print documentation for Data.List to stdout:"
233 , " psc-docs src/**/*.purs bower_components/*/src/**/*.purs \\"
234 , " --docgen Data.List"
236 , " write documentation for Data.List to docs/Data.List.md:"
237 , " psc-docs src/**/*.purs bower_components/*/src/**/*.purs \\"
238 , " --docgen Data.List:docs/Data.List.md"
240 , " write documentation for Data.List to docs/Data.List.md, and"
241 , " documentation for Data.List.Lazy to docs/Data.List.Lazy.md:"
242 , " psc-docs src/**/*.purs bower_components/*/src/**/*.purs \\"
243 , " --docgen Data.List:docs/Data.List.md \\"
244 , " --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