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)
73 Left (D.ParseError err) -> do
74 hPutStrLn stderr $ show err
76 Left (D.SortModulesError err) -> do
77 hPutStrLn stderr $ P.prettyPrintMultipleErrors False err
79 Left (D.DesugarError err) -> do
80 hPutStrLn stderr $ P.prettyPrintMultipleErrors False err
85 putStrLn (D.renderModulesAsMarkdown ms')
87 let (ms, missing) = takeModulesByName ms' names
89 putStrLn (D.renderModulesAsMarkdown ms)
91 let (ms, missing) = takeModulesByName' ms' names
93 let ms'' = groupBy ((==) `on` fst) . sortBy (compare `on` fst) $ map swap ms
94 forM_ ms'' $ \grp -> do
95 let fp = fst (head grp)
96 createDirectoryIfMissing True (takeDirectory fp)
97 writeFile fp (D.renderModulesAsMarkdown $ snd `map` grp)
99 guardMissing [] = return ()
100 guardMissing [mn] = do
101 hPutStrLn stderr ("psc-docs: error: unknown module \"" ++ show mn ++ "\"")
103 guardMissing mns = do
104 hPutStrLn stderr "psc-docs: error: unknown modules:"
106 hPutStrLn stderr (" * " ++ show mn)
110-- Given a list of module names and a list of modules, return a list of modules
111-- whose names appeared in the given name list, together with a list of names
112-- for which no module could be found in the module list.
114takeModulesByName :: [P.Module] -> [P.ModuleName] -> ([P.Module], [P.ModuleName])
115takeModulesByName modules names =
116 first (map fst) (takeModulesByName' modules (map (,()) names))
119-- Like takeModulesByName but also keeps some extra data with the module.
121takeModulesByName' :: [P.Module] -> [(P.ModuleName, a)] -> ([(P.Module, a)], [P.ModuleName])
122takeModulesByName' modules = foldl go ([], [])
124 go (ms, missing) (name, x) =
125 case find ((== name) . P.getModuleName) modules of
126 Just m -> ((m, x) : ms, missing)
127 Nothing -> (ms, name : missing)
129dumpTags :: [FilePath] -> ([(String, P.Module)] -> [String]) -> IO ()
130dumpTags input renderTags = do
131 e <- P.parseModulesFromFiles (fromMaybe "") <$> mapM (fmap (first Just) . parseFile) (nub input)
134 hPutStrLn stderr (show err)
137 ldump (renderTags (pairs ms))
140 pairs :: [(Maybe String, m)] -> [(String, m)]
141 pairs = map (first (fromMaybe ""))
143 ldump :: [String] -> IO ()
144 ldump = mapM_ putStrLn
146parseFile :: FilePath -> IO (FilePath, String)
147parseFile input = (,) input <$> readFile input
149inputFile :: Parser FilePath
150inputFile = strArgument $
152 <> help "The input .purs file(s)"
154instance Read Format where
155 readsPrec _ "etags" = [(Etags, "")]
156 readsPrec _ "ctags" = [(Ctags, "")]
157 readsPrec _ "markdown" = [(Markdown, "")]
160format :: Parser Format
161format = option auto $ value Markdown
164 <> help "Set output FORMAT (markdown | etags | ctags)"
166docgenModule :: Parser String
167docgenModule = strOption $
169 <> 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."
171pscDocsOptions :: Parser (Format, [FilePath], [String])
172pscDocsOptions = (,,) <$> format <*> many inputFile <*> many docgenModule
174parseDocgen :: [String] -> Either String DocgenOutput
175parseDocgen [] = Right EverythingToStdOut
176parseDocgen xs = go xs
185 = IToStdOut P.ModuleName
186 | IToFile (P.ModuleName, FilePath)
188parseItem :: String -> DocgenOutputItem
189parseItem s = case elemIndex ':' s of
192 >>> first P.moduleNameFromString
196 IToStdOut (P.moduleNameFromString s)
202combine :: [DocgenOutputItem] -> Either String DocgenOutput
203combine [] = Right EverythingToStdOut
204combine (x:xs) = foldM go (initial x) xs
206 initial (IToStdOut m) = ToStdOut [m]
207 initial (IToFile m) = ToFiles [m]
209 go (ToStdOut ms) (IToStdOut m) = Right (ToStdOut (m:ms))
210 go (ToFiles ms) (IToFile m) = Right (ToFiles (m:ms))
211 go _ _ = Left "Can't mix module names and module name/file path pairs in the same invocation."
213buildOptions :: (Format, [FilePath], [String]) -> IO PSCDocsOptions
214buildOptions (fmt, input, mapping) =
215 case parseDocgen mapping of
216 Right mapping' -> return (PSCDocsOptions fmt input mapping')
218 hPutStrLn stderr "psc-docs: error in --docgen option:"
219 hPutStrLn stderr (" " ++ err)
223main = 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 bower_components/*/src/**/*.purs \\"
240 , " --docgen Data.List"
242 , " write documentation for Data.List to docs/Data.List.md:"
243 , " psc-docs src/**/*.purs bower_components/*/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 bower_components/*/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