aboutsummaryrefslogtreecommitdiff
path: root/haddock-api/src/Haddock/Interface/Json.hs
blob: 92fb2e754f499e1d227c0cce3033d1ab045422d5 (plain) (blame)
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
{-# LANGUAGE RecordWildCards #-}
module Haddock.Interface.Json (
    jsonInstalledInterface
  , jsonInterfaceFile
  , renderJson
  ) where

import GHC.Types.Fixity
import GHC.Utils.Json
import GHC.Unit.Module
import GHC.Types.Name
import GHC.Utils.Outputable

import Control.Arrow
import Data.Map (Map)
import qualified Data.Map as Map

import Haddock.Types
import Haddock.InterfaceFile

jsonInterfaceFile :: InterfaceFile -> JsonDoc
jsonInterfaceFile InterfaceFile{..} =
  jsonObject [ ("link_env" , jsonMap nameStableString (jsonString . moduleNameString . moduleName) ifLinkEnv)
             , ("inst_ifaces", jsonArray (map jsonInstalledInterface ifInstalledIfaces))
             ]

jsonInstalledInterface :: InstalledInterface -> JsonDoc
jsonInstalledInterface InstalledInterface{..} = jsonObject properties
  where
    properties =
      [ ("module"          , jsonModule instMod)
      , ("is_sig"          , jsonBool instIsSig)
      , ("info"            , jsonHaddockModInfo instInfo)
      , ("doc_map"         , jsonMap nameStableString jsonMDoc instDocMap)
      , ("arg_map"         , jsonMap nameStableString (jsonMap show jsonMDoc) instArgMap)
      , ("exports"         , jsonArray (map jsonName instExports))
      , ("visible_exports" , jsonArray (map jsonName instVisibleExports))
      , ("options"         , jsonArray (map (jsonString . show) instOptions))
      , ("fix_map"         , jsonMap nameStableString jsonFixity instFixMap)
      ]

jsonHaddockModInfo :: HaddockModInfo Name -> JsonDoc
jsonHaddockModInfo HaddockModInfo{..} =
  jsonObject [ ("description" , jsonMaybe jsonDoc hmi_description)
             , ("copyright"   , jsonMaybe jsonString hmi_copyright)
             , ("maintainer"  , jsonMaybe jsonString hmi_maintainer)
             , ("stability"   , jsonMaybe jsonString hmi_stability)
             , ("protability" , jsonMaybe jsonString hmi_portability)
             , ("safety"      , jsonMaybe jsonString hmi_safety)
             , ("language"    , jsonMaybe (jsonString . show) hmi_language)
             , ("extensions"  , jsonArray (map (jsonString . show) hmi_extensions))
             ]

jsonMap :: (a -> String) -> (b -> JsonDoc) -> Map a b -> JsonDoc
jsonMap f g = jsonObject . map (f *** g) . Map.toList

jsonMDoc :: MDoc Name -> JsonDoc
jsonMDoc MetaDoc{..} =
  jsonObject [ ("meta", jsonObject [("version", jsonMaybe (jsonString . show) (_version _meta))])
             , ("document",  jsonDoc _doc)
             ]

showModName :: Wrap (ModuleName, OccName) -> String
showModName = showWrapped (moduleNameString . fst)

showName :: Wrap Name -> String
showName = showWrapped nameStableString


jsonDoc :: Doc Name -> JsonDoc

jsonDoc DocEmpty = jsonObject
    [ ("tag", jsonString "DocEmpty") ]

jsonDoc (DocAppend x y) = jsonObject
    [ ("tag", jsonString "DocAppend")
    , ("first", jsonDoc x)
    , ("second", jsonDoc y)
    ]

jsonDoc (DocString s) = jsonObject
    [ ("tag", jsonString "DocString")
    , ("string", jsonString s)
    ]

jsonDoc (DocParagraph x) = jsonObject
    [ ("tag", jsonString "DocParagraph")
    , ("document", jsonDoc x)
    ]

jsonDoc (DocIdentifier name) = jsonObject
    [ ("tag", jsonString "DocIdentifier")
    , ("name", jsonString (showName name))
    ]

jsonDoc (DocIdentifierUnchecked modName) = jsonObject
    [ ("tag", jsonString "DocIdentifierUnchecked")
    , ("modName", jsonString (showModName modName))
    ]

jsonDoc (DocModule (ModLink m _l)) = jsonObject
    [ ("tag", jsonString "DocModule")
    , ("string", jsonString m)
    ]

jsonDoc (DocWarning x) = jsonObject
    [ ("tag", jsonString "DocWarning")
    , ("document", jsonDoc x)
    ]

jsonDoc (DocEmphasis x) = jsonObject
    [ ("tag", jsonString "DocEmphasis")
    , ("document", jsonDoc x)
    ]

jsonDoc (DocMonospaced x) = jsonObject
    [ ("tag", jsonString "DocMonospaced")
    , ("document", jsonDoc x)
    ]

jsonDoc (DocBold x) = jsonObject
    [ ("tag", jsonString "DocBold")
    , ("document", jsonDoc x)
    ]

jsonDoc (DocUnorderedList xs) = jsonObject
    [ ("tag", jsonString "DocUnorderedList")
    , ("documents", jsonArray (fmap jsonDoc xs))
    ]

jsonDoc (DocOrderedList xs) = jsonObject
    [ ("tag", jsonString "DocOrderedList")
    , ("documents", jsonArray (fmap jsonDoc xs))
    ]

jsonDoc (DocDefList xys) = jsonObject
    [ ("tag", jsonString "DocDefList")
    , ("definitions", jsonArray (fmap jsonDef xys))
    ]
  where
    jsonDef (x, y) = jsonObject [("document", jsonDoc x), ("y", jsonDoc y)]

jsonDoc (DocCodeBlock x) = jsonObject
    [ ("tag", jsonString "DocCodeBlock")
    , ("document", jsonDoc x)
    ]

jsonDoc (DocHyperlink hyperlink) = jsonObject
    [ ("tag", jsonString "DocHyperlink")
    , ("hyperlink", jsonHyperlink hyperlink)
    ]
  where
    jsonHyperlink Hyperlink{..} = jsonObject
        [ ("hyperlinkUrl", jsonString hyperlinkUrl)
        , ("hyperlinkLabel", jsonMaybe jsonDoc hyperlinkLabel)
        ]

jsonDoc (DocPic picture) = jsonObject
    [ ("tag", jsonString "DocPic")
    , ("picture", jsonPicture picture)
    ]
  where
    jsonPicture Picture{..} = jsonObject
        [ ("pictureUrl", jsonString pictureUri)
        , ("pictureLabel", jsonMaybe jsonString pictureTitle)
        ]

jsonDoc (DocMathInline s) = jsonObject
    [ ("tag", jsonString "DocMathInline")
    , ("string", jsonString s)
    ]

jsonDoc (DocMathDisplay s) = jsonObject
    [ ("tag", jsonString "DocMathDisplay")
    , ("string", jsonString s)
    ]

jsonDoc (DocAName s) = jsonObject
    [ ("tag", jsonString "DocAName")
    , ("string", jsonString s)
    ]

jsonDoc (DocProperty s) = jsonObject
    [ ("tag", jsonString "DocProperty")
    , ("string", jsonString s)
    ]

jsonDoc (DocExamples examples) = jsonObject
    [ ("tag", jsonString "DocExamples")
    , ("examples", jsonArray (fmap jsonExample examples))
    ]
  where
    jsonExample Example{..} = jsonObject
        [ ("exampleExpression", jsonString exampleExpression)
        , ("exampleResult", jsonArray (fmap jsonString exampleResult))
        ]

jsonDoc (DocHeader header) = jsonObject
    [ ("tag", jsonString "DocHeader")
    , ("header", jsonHeader header)
    ]
  where
    jsonHeader Header{..} = jsonObject
        [ ("headerLevel", jsonInt headerLevel)
        , ("headerTitle", jsonDoc headerTitle)
        ]

jsonDoc (DocTable table) = jsonObject
    [ ("tag", jsonString "DocTable")
    , ("table", jsonTable table)
    ]
  where
    jsonTable Table{..} = jsonObject
        [ ("tableHeaderRows", jsonArray (fmap jsonTableRow tableHeaderRows))
        , ("tableBodyRows", jsonArray (fmap jsonTableRow tableBodyRows))
        ]

    jsonTableRow TableRow{..} = jsonArray (fmap jsonTableCell tableRowCells)

    jsonTableCell TableCell{..} = jsonObject
        [ ("tableCellColspan", jsonInt tableCellColspan)
        , ("tableCellRowspan", jsonInt tableCellRowspan)
        , ("tableCellContents", jsonDoc tableCellContents)
        ]


jsonModule :: Module -> JsonDoc
jsonModule = JSString . moduleStableString

jsonName :: Name -> JsonDoc
jsonName = JSString . nameStableString

jsonFixity :: Fixity -> JsonDoc
jsonFixity (Fixity _ prec dir) =
  jsonObject [ ("prec"      , jsonInt prec)
             , ("direction" , jsonFixityDirection dir)
             ]

jsonFixityDirection :: FixityDirection -> JsonDoc
jsonFixityDirection InfixL = jsonString "infixl"
jsonFixityDirection InfixR = jsonString "infixr"
jsonFixityDirection InfixN = jsonString "infix"

renderJson :: JsonDoc -> SDoc
renderJson = renderJSON

jsonMaybe :: (a -> JsonDoc) -> Maybe a -> JsonDoc
jsonMaybe = maybe jsonNull

jsonString :: String -> JsonDoc
jsonString = JSString

jsonObject :: [(String, JsonDoc)] -> JsonDoc
jsonObject = JSObject

jsonArray :: [JsonDoc] -> JsonDoc
jsonArray = JSArray

jsonNull :: JsonDoc
jsonNull = JSNull

jsonInt :: Int -> JsonDoc
jsonInt = JSInt

jsonBool :: Bool -> JsonDoc
jsonBool = JSBool