-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
148 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
{-# LANGUAGE OverloadedStrings #-} | ||
|
||
-- | | ||
-- SPDX-License-Identifier: BSD-3-Clause | ||
-- | ||
-- Render a markdown document fragment | ||
-- from the Scenario JSON schema files. | ||
module Swarm.Doc.Scenario where | ||
|
||
import Control.Applicative ((<|>)) | ||
import Control.Arrow (left, (&&&)) | ||
import Data.Aeson | ||
import Data.Map.Strict qualified as M | ||
import Data.Maybe (fromMaybe) | ||
import Data.Text qualified as T | ||
import Swarm.Doc.Schema | ||
import Swarm.Util (quote, showT) | ||
import System.FilePath ((<.>), (</>)) | ||
import Text.Pandoc | ||
import Text.Pandoc.Builder | ||
|
||
scenariosDir :: FilePath | ||
scenariosDir = "data/scenarios" | ||
|
||
schemasDir :: FilePath | ||
schemasDir = "data/schema" | ||
|
||
inputFiles :: [FilePath] | ||
inputFiles = | ||
[ "scenario" | ||
, "combustion" | ||
, "display" | ||
, "world" | ||
] | ||
|
||
columnHeadings :: [T.Text] | ||
columnHeadings = | ||
[ "Key" | ||
, "Default?" | ||
, "Type" | ||
, "Description" | ||
] | ||
|
||
makePandocTable :: (String, Schema) -> Pandoc | ||
makePandocTable (fn, schm) = | ||
setTitle (text "JSON Schema for Scenarios") $ | ||
doc $ | ||
header 3 (text $ T.toTitle $ T.pack fn) <> para (text $ description schm) <> myTable | ||
where | ||
genRow :: (T.Text, Prop) -> [Blocks] | ||
genRow (k, x) = | ||
[ plain $ code k | ||
, maybe mempty (plain . code . renderValue) $ _default x | ||
, plain . code . fromMaybe "" $ _type x <|> _Sref x | ||
, plain . text . fromMaybe "" $ _description x <|> _Sref x | ||
] | ||
|
||
headerRow = map (plain . text) columnHeadings | ||
myTable = simpleTable headerRow . map genRow . M.toList $ properties schm | ||
|
||
genScenarioSchemaDocs :: IO () | ||
genScenarioSchemaDocs = do | ||
xs <- mapM (sequenceA . (id &&& eitherDecodeFileStrict . makeSchemaPath)) inputFiles | ||
let eitherMarkdown = do | ||
schemas <- left T.pack $ traverse sequenceA xs | ||
let pd = mconcat $ map makePandocTable schemas | ||
left renderError $ runPure (writeMarkdown (def {writerExtensions = extensionsFromList [Ext_pipe_tables]}) pd) | ||
|
||
case eitherMarkdown of | ||
Left e -> print $ unwords ["Failed:", T.unpack e] | ||
Right md -> writeFile (scenariosDir </> "README_NEW.md") $ T.unpack md | ||
where | ||
makeSchemaPath bareName = schemasDir </> bareName <.> "json" | ||
|
||
renderValue :: Value -> T.Text | ||
renderValue = \case | ||
Object obj -> showT obj | ||
Array arr -> showT arr | ||
String t -> quote t | ||
Number num -> showT num | ||
Bool b -> showT b | ||
Null -> "null" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
{-# LANGUAGE DuplicateRecordFields #-} | ||
|
||
-- | | ||
-- SPDX-License-Identifier: BSD-3-Clause | ||
-- | ||
-- There are no modern, comprehensive, JSON Schema parsing | ||
-- libraries in Haskell, as explained in this post: | ||
-- https://dev.to/sshine/a-review-of-json-schema-libraries-for-haskell-321 | ||
-- | ||
-- Therefore, a parser for a small, custom subset of JSON Schema is implemented here, | ||
-- simply for rendering Markdown documentation from Swarm's schema. | ||
module Swarm.Doc.Schema where | ||
|
||
import Data.Aeson | ||
import Data.List.Extra (replace) | ||
import Data.Map (Map) | ||
import Data.Text (Text) | ||
import GHC.Generics (Generic) | ||
|
||
data ArrayItem = ArrayItem | ||
{ _Aname :: Maybe Text | ||
, _Atype :: Text | ||
} | ||
deriving (Eq, Ord, Show, Generic) | ||
|
||
instance FromJSON ArrayItem where | ||
parseJSON = | ||
genericParseJSON | ||
defaultOptions | ||
{ fieldLabelModifier = drop 2 | ||
} | ||
|
||
schemaJsonOptions :: Options | ||
schemaJsonOptions = | ||
defaultOptions | ||
{ fieldLabelModifier = replace "S" "$" . tail -- drops leading underscore | ||
} | ||
|
||
data Prop = Prop | ||
{ _default :: Maybe Value | ||
, _items :: Maybe Value | ||
, _description :: Maybe Text | ||
, _type :: Maybe Text | ||
, _examples :: Maybe [Value] | ||
, _Sref :: Maybe Text | ||
} | ||
deriving (Eq, Ord, Show, Generic) | ||
|
||
instance FromJSON Prop where | ||
parseJSON = genericParseJSON schemaJsonOptions | ||
|
||
data Schema = Schema | ||
{ description :: Text | ||
, title :: Text | ||
, properties :: Map Text Prop | ||
} | ||
deriving (Eq, Ord, Show, Generic, FromJSON) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters