|
6 | 6 | {-# LANGUAGE ViewPatterns #-}
|
7 | 7 |
|
8 | 8 | import qualified Data.Aeson as JSON
|
| 9 | +import Data.Aeson ((.=)) |
9 | 10 | import qualified Data.Aeson.KeyMap as KM
|
10 | 11 | import Data.Binary (Binary)
|
11 | 12 | import Data.Data (Typeable)
|
12 |
| -import Data.Foldable (for_) |
| 13 | +import Data.Foldable (for_, foldl') |
13 | 14 | import Data.Function (on)
|
14 | 15 | import Data.Functor ((<&>))
|
15 | 16 | import Data.List (find, lookup, nub, sort, sortBy, stripPrefix)
|
@@ -89,7 +90,7 @@ main = hakyll $ do
|
89 | 90 | match "messages/*/index.md" $ do
|
90 | 91 | route $ setExtension "html"
|
91 | 92 | compile $ do
|
92 |
| - examples <- getExamples |
| 93 | + examples <- getExamples =<< getUnderlying |
93 | 94 | let bread = breadcrumbCtx ["index.html"]
|
94 | 95 | pandocCompiler
|
95 | 96 | >>= loadAndApplyTemplate
|
@@ -142,6 +143,45 @@ main = hakyll $ do
|
142 | 143 |
|
143 | 144 | match "templates/*" $ compile templateBodyCompiler
|
144 | 145 |
|
| 146 | + create ["api/errors.json"] $ do |
| 147 | + route idRoute |
| 148 | + compile $ do |
| 149 | + let exampleItemToJSON :: Item String -> Compiler JSON.Value |
| 150 | + exampleItemToJSON exampleItem = do |
| 151 | + meta <- getMetadata (itemIdentifier exampleItem) |
| 152 | + route <- getRoute (itemIdentifier exampleItem) |
| 153 | + let name = |
| 154 | + case splitDirectories $ toFilePath $ itemIdentifier exampleItem of |
| 155 | + ["messages", _, name, "index.md"] -> name |
| 156 | + other -> error "is not an example" |
| 157 | + pure $ JSON.object |
| 158 | + [ "name" .= name |
| 159 | + , "route" .= route |
| 160 | + , "metadata" .= meta |
| 161 | + ] |
| 162 | + |
| 163 | + let errorItemToJSON :: Item String -> Compiler JSON.Value |
| 164 | + errorItemToJSON errorItem = do |
| 165 | + meta <- getMetadata (itemIdentifier errorItem) |
| 166 | + route <- getRoute (itemIdentifier errorItem) |
| 167 | + let code = |
| 168 | + case splitDirectories (toFilePath (itemIdentifier errorItem)) of |
| 169 | + ["messages", code, "index.md"] -> code |
| 170 | + other -> error "is not a message" |
| 171 | + exampleItems <- getExamples (itemIdentifier errorItem) |
| 172 | + examples <- traverse exampleItemToJSON exampleItems |
| 173 | + pure $ JSON.object |
| 174 | + [ "code" .= code |
| 175 | + , "route" .= route |
| 176 | + , "metadata" .= meta |
| 177 | + , "examples" .= examples |
| 178 | + ] |
| 179 | + |
| 180 | + errorItems <- loadAll $ "messages/*/index.md" .&&. hasNoVersion |
| 181 | + encoded <- traverse errorItemToJSON errorItems |
| 182 | + makeItem $ JSON.encode encoded |
| 183 | + |
| 184 | + |
145 | 185 | --------------------------------------------------------------------------------
|
146 | 186 |
|
147 | 187 | -- | The file extensions to be shown in example lists
|
@@ -199,9 +239,8 @@ getIdentId ident =
|
199 | 239 | [_, x, _] -> Just x
|
200 | 240 | _ -> Nothing
|
201 | 241 |
|
202 |
| -getExamples :: Compiler [Item String] |
203 |
| -getExamples = do |
204 |
| - me <- getUnderlying |
| 242 | +getExamples :: Identifier -> Compiler [Item String] |
| 243 | +getExamples me = do |
205 | 244 | code <- case splitDirectories $ toFilePath me of
|
206 | 245 | ["messages", code, "index.md"] -> pure code
|
207 | 246 | other -> fail $ "Not processing a message: " ++ show other
|
|
0 commit comments