Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate other wikis #769

Merged
merged 12 commits into from
Oct 31, 2022
Prev Previous commit
Next Next commit
Restyle
  • Loading branch information
xsebek committed Oct 30, 2022
commit 2247907465a0970f2d53dc7400ec94b002f4abe1
26 changes: 14 additions & 12 deletions app/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,21 @@
module Main where

import Data.Foldable qualified
import Data.Maybe (fromMaybe)
import Data.Text (Text, pack)
import Data.Text qualified as T
import Data.Text.IO qualified as Text
import GitHash (GitInfo, giBranch, giHash, tGitInfoCwdTry)
import Options.Applicative
import Swarm.App (appMain)
import Swarm.DocGen (EditorType (..), GenerateDocs (..), SheetType (..), generateDocs, PageAddress(..))
import Swarm.DocGen (EditorType (..), GenerateDocs (..), PageAddress (..), SheetType (..), generateDocs)
import Swarm.Language.LSP (lspMain)
import Swarm.Language.Pipeline (processTerm)
import Swarm.TUI.Model (AppOpts (..))
import Swarm.Version
import Swarm.Web (defaultPort)
import System.Exit (exitFailure, exitSuccess)
import System.IO (hPrint, stderr)
import qualified Data.Text as T
import Data.Maybe (fromMaybe)

gitInfo :: Maybe GitInfo
gitInfo = either (const Nothing) Just ($$tGitInfoCwdTry)
Expand Down Expand Up @@ -66,15 +66,17 @@ cliParser =
, Just Emacs <$ switch (long "emacs" <> help "Generate for the Emacs editor")
]
address :: Parser PageAddress
address = let replace a b = T.unpack . T.replace a b . T.pack
opt n = fmap (fromMaybe "") . optional $
option
str
( long n
<> metavar "ADDRESS"
<> help ("Set the address of " <> replace "-" " " n <> ". Default no link.")
)
in PageAddress <$> opt "entities-page" <*> opt "commands-page" <*> opt "capabilities-page" <*> opt "recipes-page"
address =
let replace a b = T.unpack . T.replace a b . T.pack
opt n =
fmap (fromMaybe "") . optional $
option
str
( long n
<> metavar "ADDRESS"
<> help ("Set the address of " <> replace "-" " " n <> ". Default no link.")
)
in PageAddress <$> opt "entities-page" <*> opt "commands-page" <*> opt "capabilities-page" <*> opt "recipes-page"
cheatsheet :: Parser (Maybe SheetType)
cheatsheet =
Data.Foldable.asum
Expand Down
39 changes: 22 additions & 17 deletions src/Swarm/DocGen.hs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ module Swarm.DocGen (
editorList,

-- ** Wiki pages
PageAddress(..),
PageAddress (..),
commandsPage,
capabilityPage,
noPageAddresses,
Expand Down Expand Up @@ -80,7 +80,8 @@ data PageAddress = PageAddress
, commandsAddress :: Text
, capabilityAddress :: Text
, recipesAddress :: Text
} deriving (Eq, Show)
}
deriving (Eq, Show)

noPageAddresses :: PageAddress
noPageAddresses = PageAddress "" "" "" ""
Expand Down Expand Up @@ -204,7 +205,6 @@ tshow = T.pack . show
-- COMMANDS
-- ---------


commandHeader :: [Text]
commandHeader = ["Syntax", "Type", "Capability", "Description"]

Expand All @@ -216,7 +216,7 @@ commandToList c =
, codeQuote . prettyText $ inferConst c
, maybe "" Capability.capabilityName $ Capability.constCaps c
, Syntax.briefDoc . Syntax.constDoc $ Syntax.constInfo c
]
]

constTable :: [Const] -> Text
constTable cs = T.unlines $ header <> map (listToRow mw) commandRows
Expand Down Expand Up @@ -261,20 +261,25 @@ capabilityHeader :: [Text]
capabilityHeader = ["Name", "Commands", "Entities"]

capabilityRow :: PageAddress -> EntityMap -> Capability -> [Text]
capabilityRow PageAddress{..} em cap =
map escapeTable
[ Capability.capabilityName cap
, T.intercalate ", " (linkCommand <$> cs)
, T.intercalate ", " (linkEntity . view entityName <$> es)
]
capabilityRow PageAddress {..} em cap =
map
escapeTable
[ Capability.capabilityName cap
, T.intercalate ", " (linkCommand <$> cs)
, T.intercalate ", " (linkEntity . view entityName <$> es)
]
where
linkEntity t = if T.null entityAddress
then t
else addLink (entityAddress <> "#" <> T.replace " " "-" t) t
linkCommand c = (if T.null commandsAddress
then id
else addLink (commandsAddress <> "#" <> tshow c)
) . codeQuote $ constSyntax c
linkEntity t =
if T.null entityAddress
then t
else addLink (entityAddress <> "#" <> T.replace " " "-" t) t
linkCommand c =
( if T.null commandsAddress
then id
else addLink (commandsAddress <> "#" <> tshow c)
)
. codeQuote
$ constSyntax c

cs = [c | c <- Syntax.allConst, let mcap = Capability.constCaps c, isJust $ find (== cap) mcap]
es = fromMaybe [] $ E.entitiesByCap em Map.!? cap
Expand Down