{-# LANGUAGE CPP #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedStrings #-}
module HsLua.Packaging.Rendering
{-# DEPRECATED "Use getdocumentation with a custom renderer." #-}
(
render
, renderModule
, renderFunction
) where
import Data.Text (Text)
import Data.Version (showVersion)
import HsLua.Core
import HsLua.Packaging.Types
import qualified Data.Text as T
import qualified Data.Text.Encoding as T
import qualified HsLua.Core.Utf8 as Utf8
#if !MIN_VERSION_base(4,12,0)
import Data.Semigroup (Semigroup ((<>)))
#endif
render :: Module e -> Text
render :: Module e -> Text
render = Module e -> Text
forall e. Module e -> Text
renderModule
renderModule :: Module e -> Text
renderModule :: Module e -> Text
renderModule mdl :: Module e
mdl =
let fields :: [Field e]
fields = Module e -> [Field e]
forall e. Module e -> [Field e]
moduleFields Module e
mdl
in [Text] -> Text
T.unlines
[ "# " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> ByteString -> Text
T.decodeUtf8 (Name -> ByteString
fromName (Name -> ByteString) -> Name -> ByteString
forall a b. (a -> b) -> a -> b
$ Module e -> Name
forall e. Module e -> Name
moduleName Module e
mdl)
, ""
, Module e -> Text
forall e. Module e -> Text
moduleDescription Module e
mdl
, [Field e] -> Text
forall e. [Field e] -> Text
renderFields [Field e]
fields
, [DocumentedFunction e] -> Text
forall e. [DocumentedFunction e] -> Text
renderFunctions (Module e -> [DocumentedFunction e]
forall e. Module e -> [DocumentedFunction e]
moduleFunctions Module e
mdl)
]
renderFunctions :: [DocumentedFunction e] -> Text
renderFunctions :: [DocumentedFunction e] -> Text
renderFunctions = \case
[] -> Text
forall a. Monoid a => a
mempty
fs :: [DocumentedFunction e]
fs -> "\n## Functions\n\n"
Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text -> [Text] -> Text
T.intercalate "\n\n" ((DocumentedFunction e -> Text) -> [DocumentedFunction e] -> [Text]
forall a b. (a -> b) -> [a] -> [b]
map (("### " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<>) (Text -> Text)
-> (DocumentedFunction e -> Text) -> DocumentedFunction e -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. DocumentedFunction e -> Text
forall e. DocumentedFunction e -> Text
renderFunction) [DocumentedFunction e]
fs)
renderFunction :: DocumentedFunction e
-> Text
renderFunction :: DocumentedFunction e -> Text
renderFunction fn :: DocumentedFunction e
fn =
let fnDoc :: FunctionDoc
fnDoc = DocumentedFunction e -> FunctionDoc
forall e. DocumentedFunction e -> FunctionDoc
functionDoc DocumentedFunction e
fn
fnName :: Text
fnName = ByteString -> Text
Utf8.toText (ByteString -> Text) -> ByteString -> Text
forall a b. (a -> b) -> a -> b
$ Name -> ByteString
fromName (DocumentedFunction e -> Name
forall e. DocumentedFunction e -> Name
functionName DocumentedFunction e
fn)
name :: Text
name = if Text -> Bool
T.null Text
fnName
then "<anonymous function>"
else Text
fnName
in Text -> [Text] -> Text
T.intercalate "\n"
[ Text
name Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> " (" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> FunctionDoc -> Text
renderFunctionParams FunctionDoc
fnDoc Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> ")"
, ""
, FunctionDoc -> Text
renderFunctionDoc FunctionDoc
fnDoc
]
renderFunctionParams :: FunctionDoc -> Text
renderFunctionParams :: FunctionDoc -> Text
renderFunctionParams fd :: FunctionDoc
fd =
Text -> [Text] -> Text
T.intercalate ", "
([Text] -> Text)
-> ([ParameterDoc] -> [Text]) -> [ParameterDoc] -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (ParameterDoc -> Text) -> [ParameterDoc] -> [Text]
forall a b. (a -> b) -> [a] -> [b]
map ParameterDoc -> Text
parameterName
([ParameterDoc] -> Text) -> [ParameterDoc] -> Text
forall a b. (a -> b) -> a -> b
$ FunctionDoc -> [ParameterDoc]
parameterDocs FunctionDoc
fd
renderFields :: [Field e] -> Text
renderFields :: [Field e] -> Text
renderFields fs :: [Field e]
fs =
if [Field e] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Field e]
fs
then Text
forall a. Monoid a => a
mempty
else [Text] -> Text
forall a. Monoid a => [a] -> a
mconcat
[ "\n"
, Text -> [Text] -> Text
T.intercalate "\n\n" ((Field e -> Text) -> [Field e] -> [Text]
forall a b. (a -> b) -> [a] -> [b]
map (("### " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<>) (Text -> Text) -> (Field e -> Text) -> Field e -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Field e -> Text
forall e. Field e -> Text
renderField) [Field e]
fs)
]
renderField :: Field e -> Text
renderField :: Field e -> Text
renderField f :: Field e
f = Field e -> Text
forall e. Field e -> Text
fieldName Field e
f Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> "\n\n" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Field e -> Text
forall e. Field e -> Text
fieldDescription Field e
f
renderFunctionDoc :: FunctionDoc -> Text
renderFunctionDoc :: FunctionDoc -> Text
renderFunctionDoc (FunctionDoc desc :: Text
desc paramDocs :: [ParameterDoc]
paramDocs resultDoc :: ResultsDoc
resultDoc mVersion :: Maybe Version
mVersion) =
let sinceTag :: Text
sinceTag = case Maybe Version
mVersion of
Nothing -> Text
forall a. Monoid a => a
mempty
Just version :: Version
version -> String -> Text
T.pack (String -> Text) -> String -> Text
forall a b. (a -> b) -> a -> b
$ "\n\n*Since: " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> Version -> String
showVersion Version
version String -> String -> String
forall a. Semigroup a => a -> a -> a
<> "*"
in (if Text -> Bool
T.null Text
desc
then ""
else Text
desc Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
sinceTag Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> "\n\n") Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<>
[ParameterDoc] -> Text
renderParamDocs [ParameterDoc]
paramDocs Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<>
ResultsDoc -> Text
renderResultsDoc ResultsDoc
resultDoc
renderParamDocs :: [ParameterDoc] -> Text
renderParamDocs :: [ParameterDoc] -> Text
renderParamDocs pds :: [ParameterDoc]
pds = "Parameters:\n\n" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<>
Text -> [Text] -> Text
T.intercalate "\n" ((ParameterDoc -> Text) -> [ParameterDoc] -> [Text]
forall a b. (a -> b) -> [a] -> [b]
map ParameterDoc -> Text
renderParamDoc [ParameterDoc]
pds)
renderParamDoc :: ParameterDoc -> Text
renderParamDoc :: ParameterDoc -> Text
renderParamDoc pd :: ParameterDoc
pd = [Text] -> Text
forall a. Monoid a => [a] -> a
mconcat
[ ParameterDoc -> Text
parameterName ParameterDoc
pd
, "\n: "
, ParameterDoc -> Text
parameterDescription ParameterDoc
pd
, " (", ParameterDoc -> Text
parameterType ParameterDoc
pd, ")\n"
]
renderResultsDoc :: ResultsDoc -> Text
renderResultsDoc :: ResultsDoc -> Text
renderResultsDoc = \case
ResultsDocList [] -> Text
forall a. Monoid a => a
mempty
ResultsDocList rds :: [ResultValueDoc]
rds ->
"\nReturns:\n\n" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text -> [Text] -> Text
T.intercalate "\n" ((ResultValueDoc -> Text) -> [ResultValueDoc] -> [Text]
forall a b. (a -> b) -> [a] -> [b]
map ResultValueDoc -> Text
renderResultValueDoc [ResultValueDoc]
rds)
ResultsDocMult txt :: Text
txt -> " - " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Int -> Text -> Text
indent 4 Text
txt
renderResultValueDoc :: ResultValueDoc -> Text
renderResultValueDoc :: ResultValueDoc -> Text
renderResultValueDoc rd :: ResultValueDoc
rd = [Text] -> Text
forall a. Monoid a => [a] -> a
mconcat
[ " - "
, ResultValueDoc -> Text
resultValueDescription ResultValueDoc
rd
, " (", ResultValueDoc -> Text
resultValueType ResultValueDoc
rd, ")"
]
indent :: Int -> Text -> Text
indent :: Int -> Text -> Text
indent n :: Int
n = Text -> Text -> Text -> Text
T.replace "\n" (Int -> Text -> Text
T.replicate Int
n " ")