forked from purescript/purescript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDocs.hs
More file actions
169 lines (149 loc) · 5.48 KB
/
Docs.hs
File metadata and controls
169 lines (149 loc) · 5.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
module Command.Docs (command, infoModList) where
import Prelude
import Command.Docs.Html (asHtml, writeHtmlModules)
import Command.Docs.Markdown (asMarkdown, writeMarkdownModules)
import Control.Applicative (Alternative(..), optional)
import Control.Monad (when)
import Control.Monad.Trans.Except (runExceptT)
import Data.Maybe (fromMaybe)
import Data.Text qualified as T
import Language.PureScript qualified as P
import Language.PureScript.Docs qualified as D
import Language.PureScript.Docs.Tags (dumpCtags, dumpEtags)
import Language.PureScript.Glob (PSCGlobs(..), toInputGlobs, warnFileTypeNotFound)
import Options.Applicative qualified as Opts
import Prettyprinter qualified as PP
import Prettyprinter.Render.Terminal (AnsiStyle)
import SharedCLI qualified
import System.Directory (getCurrentDirectory, createDirectoryIfMissing, removeFile)
import System.Exit (exitFailure)
import System.FilePath ((</>))
import System.FilePath.Glob (compile, globDir1)
import System.IO (hPutStrLn, stderr)
import System.IO.UTF8 (writeUTF8FileT)
-- | Available output formats
data Format
= Markdown
| Html
| Ctags -- Output ctags symbol index suitable for use with vi
| Etags -- Output etags symbol index suitable for use with emacs
deriving (Show, Eq, Ord)
data PSCDocsOptions = PSCDocsOptions
{ _pscdFormat :: Format
, _pscdOutput :: Maybe FilePath
, _pscdCompileOutputDir :: FilePath
, _pscdInputFiles :: [FilePath]
, _pscdInputFromFile :: Maybe FilePath
, _pscdExcludeFiles :: [FilePath]
}
deriving (Show)
docgen :: PSCDocsOptions -> IO ()
docgen (PSCDocsOptions fmt moutput compileOutput inputGlob inputGlobFromFile excludeGlob) = do
input <- toInputGlobs $ PSCGlobs
{ pscInputGlobs = inputGlob
, pscInputGlobsFromFile = inputGlobFromFile
, pscExcludeGlobs = excludeGlob
, pscWarnFileTypeNotFound = warnFileTypeNotFound "docs"
}
when (null input) $ do
hPutStrLn stderr "purs docs: no input files."
exitFailure
let output = fromMaybe (defaultOutputForFormat fmt) moutput
fileMs <- parseAndConvert input
let ms = D.primModules ++ map snd fileMs
case fmt of
Etags -> writeTagsToFile output $ dumpEtags fileMs
Ctags -> writeTagsToFile output $ dumpCtags fileMs
Html -> do
let ext = compile "*.html"
let msHtml = map asHtml ms
createDirectoryIfMissing True output
globDir1 ext output >>= mapM_ removeFile
writeHtmlModules output msHtml
Markdown -> do
let ext = compile "*.md"
let msMarkdown = map asMarkdown ms
createDirectoryIfMissing True output
globDir1 ext output >>= mapM_ removeFile
writeMarkdownModules output msMarkdown
putStrLn $ "Documentation written to: " ++ output
where
successOrExit :: Either P.MultipleErrors a -> IO a
successOrExit act =
case act of
Right x ->
return x
Left err -> do
hPutStrLn stderr $ P.prettyPrintMultipleErrors P.defaultPPEOptions err
exitFailure
parseAndConvert input =
runExceptT (fmap fst (D.collectDocs compileOutput input []))
>>= successOrExit
writeTagsToFile :: String -> [String] -> IO ()
writeTagsToFile outputFilename tags = do
currentDir <- getCurrentDirectory
let outputFile = currentDir </> outputFilename
let text = T.pack . unlines $ tags
writeUTF8FileT outputFile text
instance Read Format where
readsPrec _ "etags" = [(Etags, "")]
readsPrec _ "ctags" = [(Ctags, "")]
readsPrec _ "markdown" = [(Markdown, "")]
readsPrec _ "html" = [(Html, "")]
readsPrec _ _ = []
defaultOutputForFormat :: Format -> FilePath
defaultOutputForFormat fmt =
case fmt of
Markdown -> "generated-docs/md"
Html -> "generated-docs/html"
Etags -> "TAGS"
Ctags -> "tags"
pscDocsOptions :: Opts.Parser PSCDocsOptions
pscDocsOptions =
PSCDocsOptions <$> format
<*> output
<*> compileOutputDir
<*> many SharedCLI.inputFile
<*> SharedCLI.globInputFile
<*> many SharedCLI.excludeFiles
where
format :: Opts.Parser Format
format = Opts.option Opts.auto $
Opts.value Html
<> Opts.long "format"
<> Opts.metavar "FORMAT"
<> Opts.help "Set output FORMAT (markdown | html | etags | ctags)"
output :: Opts.Parser (Maybe FilePath)
output = optional $ Opts.strOption $
Opts.long "output"
<> Opts.short 'o'
<> Opts.metavar "DEST"
<> Opts.help "File/directory path for docs to be written to"
compileOutputDir :: Opts.Parser FilePath
compileOutputDir = Opts.strOption $
Opts.value "output"
<> Opts.showDefault
<> Opts.long "compile-output"
<> Opts.metavar "DIR"
<> Opts.help "Compiler output directory"
command :: Opts.Parser (IO ())
command = docgen <$> (Opts.helper <*> pscDocsOptions)
infoModList :: Opts.InfoMod a
infoModList = Opts.fullDesc <> footerInfo where
footerInfo = Opts.footerDoc $ Just examples
examples :: PP.Doc AnsiStyle
examples =
PP.vcat
[ "Examples:"
, " write documentation for all modules to ./generated-docs:"
, " purs docs \"src/**/*.purs\" \".psc-package/*/*/*/src/**/*.purs\""
, ""
, " write documentation in Markdown format for all modules to ./generated-docs:"
, " purs docs --format markdown \"src/**/*.purs\" \".psc-package/*/*/*/src/**/*.purs\""
, ""
, " write CTags to ./tags:"
, " purs docs --format ctags \"src/**/*.purs\" \".psc-package/*/*/*/src/**/*.purs\""
, ""
, " write ETags to ./TAGS:"
, " purs docs --format etags \"src/**/*.purs\" \".psc-package/*/*/*/src/**/*.purs\""
]