{-# LANGUAGE LambdaCase        #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RankNTypes        #-}
{- |
   Module      : Text.Pandoc.Lua.Documentation
   Copyright   : Copyright © 2026 Albert Krewinkel
   License     : GPL-2.0-or-later
   Maintainer  : Albert Krewinkel <albert+pandoc@tarleb.com>

Render Lua documentation
-}
module Text.Pandoc.Lua.Documentation
  ( renderDocumentation
  ) where

import Data.Default (def)
import Data.List (intersperse)
import Data.Sequence (Seq ((:|>)))
import Data.Version (showVersion)
import HsLua as Lua
import Text.Pandoc.Class (runPure)
import Text.Pandoc.Definition (Pandoc (Pandoc))
import Text.Pandoc.Extensions (extensionsFromList, Extension (..))
import Text.Pandoc.Options (ReaderOptions (readerExtensions))
import Text.Pandoc.Readers (readCommonMark)
import Text.Pandoc.Shared (compactify)
import Text.Pandoc.Walk (walk)

import qualified Data.Text as T
import qualified Text.Pandoc.Builder as B
import qualified Text.Pandoc.UTF8 as UTF8

-- | Render the documentation object as pandoc Blocks
renderDocumentation :: DocumentationObject -> B.Blocks
renderDocumentation :: DocumentationObject -> Many Block
renderDocumentation = \case
  DocObjectFunction FunctionDoc
fn -> Maybe Text -> FunctionDoc -> Many Block
renderFunctionDoc Maybe Text
forall a. Maybe a
Nothing FunctionDoc
fn
  DocObjectModule ModuleDoc
mdl  -> ModuleDoc -> Many Block
renderModuleDoc ModuleDoc
mdl
  DocObjectType TypeDoc
tp     -> Maybe Text -> TypeDoc -> Many Block
renderTypeDoc Maybe Text
forall a. Maybe a
Nothing TypeDoc
tp

renderTypeDoc :: Maybe T.Text -> TypeDoc -> B.Blocks
renderTypeDoc :: Maybe Text -> TypeDoc -> Many Block
renderTypeDoc Maybe Text
mbmodname TypeDoc
td = [Many Block] -> Many Block
forall a. Monoid a => [a] -> a
mconcat
  [ Attr -> Int -> Many Inline -> Many Block
B.headerWith (Text
ident, [], []) Int
1 (Text -> Many Inline
B.str (Text -> Many Inline) -> Text -> Many Inline
forall a b. (a -> b) -> a -> b
$ TypeDoc -> Text
typeDocName TypeDoc
td)
  , Text -> Many Block
parseCommonMark (Text -> Many Block) -> Text -> Many Block
forall a b. (a -> b) -> a -> b
$ TypeDoc -> Text
typeDocDescription TypeDoc
td
  , if [FunctionDoc] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null ([FunctionDoc] -> Bool) -> [FunctionDoc] -> Bool
forall a b. (a -> b) -> a -> b
$ TypeDoc -> [FunctionDoc]
typeDocMethods TypeDoc
td
    then Many Block
forall a. Monoid a => a
mempty
    else
      Int -> Many Inline -> Many Block
B.header Int
2 Many Inline
"Methods" Many Block -> Many Block -> Many Block
forall a. Semigroup a => a -> a -> a
<>
      (Int -> Many Block -> Many Block
shiftHeadings Int
2 (Many Block -> Many Block)
-> ([FunctionDoc] -> Many Block) -> [FunctionDoc] -> Many Block
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Many Block] -> Many Block
forall a. Monoid a => [a] -> a
mconcat ([Many Block] -> Many Block)
-> ([FunctionDoc] -> [Many Block]) -> [FunctionDoc] -> Many Block
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (FunctionDoc -> Many Block) -> [FunctionDoc] -> [Many Block]
forall a b. (a -> b) -> [a] -> [b]
map (Maybe Text -> FunctionDoc -> Many Block
renderFunctionDoc Maybe Text
forall a. Maybe a
Nothing) ([FunctionDoc] -> Many Block) -> [FunctionDoc] -> Many Block
forall a b. (a -> b) -> a -> b
$
       TypeDoc -> [FunctionDoc]
typeDocMethods TypeDoc
td)
  ]
 where
  ident :: Text
ident = case Maybe Text
mbmodname of
    Just Text
modname  -> [Text] -> Text
forall a. Monoid a => [a] -> a
mconcat [ Text
"type-", Text
modname, Text
".", TypeDoc -> Text
typeDocName TypeDoc
td ]
    Maybe Text
Nothing       -> [Text] -> Text
forall a. Monoid a => [a] -> a
mconcat [ Text
"type-", TypeDoc -> Text
typeDocName TypeDoc
td ]

-- Shift headings
shiftHeadings :: Int -> B.Blocks -> B.Blocks
shiftHeadings :: Int -> Many Block -> Many Block
shiftHeadings Int
incr Many Block
blks = ((Block -> Block) -> Many Block -> Many Block)
-> Many Block -> (Block -> Block) -> Many Block
forall a b c. (a -> b -> c) -> b -> a -> c
flip (Block -> Block) -> Many Block -> Many Block
forall a b. Walkable a b => (a -> a) -> b -> b
walk Many Block
blks ((Block -> Block) -> Many Block) -> (Block -> Block) -> Many Block
forall a b. (a -> b) -> a -> b
$ \case
  B.Header Int
level Attr
attr [Inline]
inner -> Int -> Attr -> [Inline] -> Block
B.Header (Int
level Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
incr) Attr
attr [Inline]
inner
  Block
x -> Block
x

renderModuleDoc :: ModuleDoc -> B.Blocks
renderModuleDoc :: ModuleDoc -> Many Block
renderModuleDoc ModuleDoc
moddoc =
  let modname :: Text
modname = ModuleDoc -> Text
moduleDocName ModuleDoc
moddoc
  in [Many Block] -> Many Block
forall a. Monoid a => [a] -> a
mconcat
  [ Attr -> Int -> Many Inline -> Many Block
B.headerWith (Text
"module-" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
modname, [], []) Int
1
      (Text -> Many Inline
B.str (Text -> Many Inline) -> Text -> Many Inline
forall a b. (a -> b) -> a -> b
$ Text
"Module " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
modname)
  , Text -> Many Block
parseCommonMark (ModuleDoc -> Text
moduleDocDescription ModuleDoc
moddoc)
  , if [FieldDoc] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null (ModuleDoc -> [FieldDoc]
moduleDocFields ModuleDoc
moddoc)
    then Many Block
forall a. Monoid a => a
mempty
    else
      let ident :: Text
ident = Text
modname Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"-fields"
      in Attr -> Int -> Many Inline -> Many Block
B.headerWith (Text
ident, [], []) Int
2 (Text -> Many Inline
B.str Text
"Fields") Many Block -> Many Block -> Many Block
forall a. Semigroup a => a -> a -> a
<>
         Int -> Many Block -> Many Block
shiftHeadings Int
0 ([Many Block] -> Many Block
forall a. Monoid a => [a] -> a
mconcat ((FieldDoc -> Many Block) -> [FieldDoc] -> [Many Block]
forall a b. (a -> b) -> [a] -> [b]
map (Text -> FieldDoc -> Many Block
renderFieldDoc Text
modname)
                                   (ModuleDoc -> [FieldDoc]
moduleDocFields ModuleDoc
moddoc)))
  , if [FunctionDoc] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null (ModuleDoc -> [FunctionDoc]
moduleDocFunctions ModuleDoc
moddoc)
    then Many Block
forall a. Monoid a => a
mempty
    else
      let ident :: Text
ident = Text
modname Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"-functions"
      in Attr -> Int -> Many Inline -> Many Block
B.headerWith (Text
ident, [], []) Int
2 (Text -> Many Inline
B.str Text
"Functions") Many Block -> Many Block -> Many Block
forall a. Semigroup a => a -> a -> a
<>
         (Int -> Many Block -> Many Block
shiftHeadings Int
2 (Many Block -> Many Block)
-> ([FunctionDoc] -> Many Block) -> [FunctionDoc] -> Many Block
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Many Block] -> Many Block
forall a. Monoid a => [a] -> a
mconcat ([Many Block] -> Many Block)
-> ([FunctionDoc] -> [Many Block]) -> [FunctionDoc] -> Many Block
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (FunctionDoc -> Many Block) -> [FunctionDoc] -> [Many Block]
forall a b. (a -> b) -> [a] -> [b]
map (Maybe Text -> FunctionDoc -> Many Block
renderFunctionDoc (Maybe Text -> FunctionDoc -> Many Block)
-> Maybe Text -> FunctionDoc -> Many Block
forall a b. (a -> b) -> a -> b
$ Text -> Maybe Text
forall a. a -> Maybe a
Just Text
modname) ([FunctionDoc] -> Many Block) -> [FunctionDoc] -> Many Block
forall a b. (a -> b) -> a -> b
$
          ModuleDoc -> [FunctionDoc]
moduleDocFunctions ModuleDoc
moddoc)
  , if [TypeDoc] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null (ModuleDoc -> [TypeDoc]
moduleDocTypes ModuleDoc
moddoc)
    then Many Block
forall a. Monoid a => a
mempty
    else
      let ident :: Text
ident = Text
modname Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"-types"
      in Attr -> Int -> Many Inline -> Many Block
B.headerWith (Text
ident, [], []) Int
2 (Text -> Many Inline
B.str Text
"Types") Many Block -> Many Block -> Many Block
forall a. Semigroup a => a -> a -> a
<>
         (Int -> Many Block -> Many Block
shiftHeadings Int
2 (Many Block -> Many Block)
-> ([TypeDoc] -> Many Block) -> [TypeDoc] -> Many Block
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Many Block] -> Many Block
forall a. Monoid a => [a] -> a
mconcat ([Many Block] -> Many Block)
-> ([TypeDoc] -> [Many Block]) -> [TypeDoc] -> Many Block
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (TypeDoc -> Many Block) -> [TypeDoc] -> [Many Block]
forall a b. (a -> b) -> [a] -> [b]
map (Maybe Text -> TypeDoc -> Many Block
renderTypeDoc (Maybe Text -> TypeDoc -> Many Block)
-> Maybe Text -> TypeDoc -> Many Block
forall a b. (a -> b) -> a -> b
$ Text -> Maybe Text
forall a. a -> Maybe a
Just Text
modname) ([TypeDoc] -> [Many Block])
-> ([TypeDoc] -> [TypeDoc]) -> [TypeDoc] -> [Many Block]
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
          [TypeDoc] -> [TypeDoc]
forall a. [a] -> [a]
reverse ([TypeDoc] -> Many Block) -> [TypeDoc] -> Many Block
forall a b. (a -> b) -> a -> b
$ ModuleDoc -> [TypeDoc]
moduleDocTypes ModuleDoc
moddoc)
  ]

parseCommonMark :: T.Text -> B.Blocks
parseCommonMark :: Text -> Many Block
parseCommonMark Text
txt =
  let exts :: Extensions
exts = [Extension] -> Extensions
extensionsFromList
        [ Extension
Ext_wikilinks_title_after_pipe
        , Extension
Ext_smart
        ]
      result :: Either PandocError (Many Block)
result = PandocPure (Many Block) -> Either PandocError (Many Block)
forall a. PandocPure a -> Either PandocError a
runPure (PandocPure (Many Block) -> Either PandocError (Many Block))
-> PandocPure (Many Block) -> Either PandocError (Many Block)
forall a b. (a -> b) -> a -> b
$ do
        Pandoc _ blks <- ReaderOptions -> Text -> PandocPure Pandoc
forall (m :: * -> *) a.
(PandocMonad m, ToSources a) =>
ReaderOptions -> a -> m Pandoc
readCommonMark (ReaderOptions
forall a. Default a => a
def {readerExtensions = exts}) Text
txt
        return $ B.fromList blks
  in (PandocError -> Many Block)
-> (Many Block -> Many Block)
-> Either PandocError (Many Block)
-> Many Block
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either PandocError -> Many Block
forall a. Monoid a => a
mempty Many Block -> Many Block
forall a. a -> a
id Either PandocError (Many Block)
result

appendInlines :: B.Blocks -> B.Inlines -> B.Blocks
appendInlines :: Many Block -> Many Inline -> Many Block
appendInlines Many Block
blks Many Inline
inlns = case Many Block -> Seq Block
forall a. Many a -> Seq a
B.unMany Many Block
blks of
  Seq Block
front :|> (B.Para [Inline]
xs) -> Seq Block -> Many Block
forall a. Seq a -> Many a
B.Many Seq Block
front Many Block -> Many Block -> Many Block
forall a. Semigroup a => a -> a -> a
<> Many Inline -> Many Block
B.para ([Inline] -> Many Inline
addTo [Inline]
xs)
  Seq Block
front :|> (B.Plain [Inline]
xs) -> Seq Block -> Many Block
forall a. Seq a -> Many a
B.Many Seq Block
front Many Block -> Many Block -> Many Block
forall a. Semigroup a => a -> a -> a
<> Many Inline -> Many Block
B.plain ([Inline] -> Many Inline
addTo [Inline]
xs)
  Seq Block
_ -> Many Block
blks Many Block -> Many Block -> Many Block
forall a. Semigroup a => a -> a -> a
<> Many Inline -> Many Block
B.para Many Inline
inlns
 where addTo :: [Inline] -> Many Inline
addTo [Inline]
xs = [Inline] -> Many Inline
forall a. [a] -> Many a
B.fromList [Inline]
xs Many Inline -> Many Inline -> Many Inline
forall a. Semigroup a => a -> a -> a
<> Many Inline
B.space Many Inline -> Many Inline -> Many Inline
forall a. Semigroup a => a -> a -> a
<> Many Inline
inlns

appendType :: B.Blocks -> TypeSpec -> B.Blocks
appendType :: Many Block -> TypeSpec -> Many Block
appendType Many Block
blks TypeSpec
typespec =
  Many Block -> Many Inline -> Many Block
appendInlines Many Block
blks (Text -> Many Inline
B.str Text
"(" Many Inline -> Many Inline -> Many Inline
forall a. Semigroup a => a -> a -> a
<> TypeSpec -> Many Inline
typeToInlines TypeSpec
typespec Many Inline -> Many Inline -> Many Inline
forall a. Semigroup a => a -> a -> a
<> Text -> Many Inline
B.str Text
")")

typeToInlines :: TypeSpec -> B.Inlines
typeToInlines :: TypeSpec -> Many Inline
typeToInlines = \case
  bt :: TypeSpec
bt@BasicType{}   -> Many Inline -> Many Inline
builtin (Many Inline -> Many Inline) -> Many Inline -> Many Inline
forall a b. (a -> b) -> a -> b
$ TypeSpec -> Many Inline
tystr TypeSpec
bt
  NamedType Name
"integer" -> Many Inline -> Many Inline
builtin Many Inline
"integer"
  NamedType Name
name   -> Attr -> Text -> Text -> Many Inline -> Many Inline
B.linkWith (Text
"", [Text
"documented-type"], [])
                        (Text
"#" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Name -> Text
n2t Name
name) Text
forall a. Monoid a => a
mempty (Many Inline -> Many Inline) -> Many Inline -> Many Inline
forall a b. (a -> b) -> a -> b
$ Text -> Many Inline
B.str (Name -> Text
n2t Name
name)
  SeqType TypeSpec
itemtype -> Many Inline
"{" Many Inline -> Many Inline -> Many Inline
forall a. Semigroup a => a -> a -> a
<> TypeSpec -> Many Inline
typeToInlines TypeSpec
itemtype Many Inline -> Many Inline -> Many Inline
forall a. Semigroup a => a -> a -> a
<> Many Inline
",...}"
  SumType [TypeSpec]
summands -> [Many Inline] -> Many Inline
forall a. Monoid a => [a] -> a
mconcat ([Many Inline] -> Many Inline)
-> ([Many Inline] -> [Many Inline]) -> [Many Inline] -> Many Inline
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Many Inline -> [Many Inline] -> [Many Inline]
forall a. a -> [a] -> [a]
intersperse (Text -> Many Inline
B.str Text
"|") ([Many Inline] -> Many Inline) -> [Many Inline] -> Many Inline
forall a b. (a -> b) -> a -> b
$ (TypeSpec -> Many Inline) -> [TypeSpec] -> [Many Inline]
forall a b. (a -> b) -> [a] -> [b]
map TypeSpec -> Many Inline
typeToInlines [TypeSpec]
summands
  TypeSpec
AnyType          -> Many Inline
"any"
  TypeSpec
x                -> TypeSpec -> Many Inline
tystr TypeSpec
x
 where
  tystr :: TypeSpec -> Many Inline
tystr = Text -> Many Inline
B.str (Text -> Many Inline)
-> (TypeSpec -> Text) -> TypeSpec -> Many Inline
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Text
T.pack (String -> Text) -> (TypeSpec -> String) -> TypeSpec -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TypeSpec -> String
typeSpecToString
  n2t :: Name -> Text
n2t = ByteString -> Text
UTF8.toText (ByteString -> Text) -> (Name -> ByteString) -> Name -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Name -> ByteString
fromName
  builtin :: Many Inline -> Many Inline
builtin = Attr -> Many Inline -> Many Inline
B.spanWith (Text
"", [Text
"builtin-lua-type"], [])

renderFunctionDoc :: Maybe T.Text -> FunctionDoc -> B.Blocks
renderFunctionDoc :: Maybe Text -> FunctionDoc -> Many Block
renderFunctionDoc Maybe Text
mbmodule FunctionDoc
fndoc =
  let name :: Text
name = case Maybe Text
mbmodule of
        Just Text
_   ->  (Char -> Bool) -> Text -> Text
T.takeWhileEnd (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
/= Char
'.') (Text -> Text) -> Text -> Text
forall a b. (a -> b) -> a -> b
$ FunctionDoc -> Text
funDocName FunctionDoc
fndoc
        Maybe Text
Nothing  -> FunctionDoc -> Text
funDocName FunctionDoc
fndoc
      ident :: Text
ident = FunctionDoc -> Text
funDocName FunctionDoc
fndoc
      level :: Int
level = Int
1
      argsString :: Text
argsString = [ParameterDoc] -> Text
argslist (FunctionDoc -> [ParameterDoc]
funDocParameters FunctionDoc
fndoc)
      paramToDefItem :: ParameterDoc -> (Many Inline, [Many Block])
paramToDefItem ParameterDoc
p = ( Text -> Many Inline
B.code (Text -> Many Inline) -> Text -> Many Inline
forall a b. (a -> b) -> a -> b
$ ParameterDoc -> Text
parameterName ParameterDoc
p
                         , [Many Block] -> [Many Block]
compactify
                           [ Many Block -> TypeSpec -> Many Block
appendType
                               (Text -> Many Block
parseCommonMark (Text -> Many Block) -> Text -> Many Block
forall a b. (a -> b) -> a -> b
$ ParameterDoc -> Text
parameterDescription ParameterDoc
p)
                               (ParameterDoc -> TypeSpec
parameterType ParameterDoc
p)
                           ]
                         )
      paramlist :: Many Block
paramlist = [(Many Inline, [Many Block])] -> Many Block
B.definitionList ([(Many Inline, [Many Block])] -> Many Block)
-> ([ParameterDoc] -> [(Many Inline, [Many Block])])
-> [ParameterDoc]
-> Many Block
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (ParameterDoc -> (Many Inline, [Many Block]))
-> [ParameterDoc] -> [(Many Inline, [Many Block])]
forall a b. (a -> b) -> [a] -> [b]
map ParameterDoc -> (Many Inline, [Many Block])
paramToDefItem ([ParameterDoc] -> Many Block) -> [ParameterDoc] -> Many Block
forall a b. (a -> b) -> a -> b
$
                  FunctionDoc -> [ParameterDoc]
funDocParameters FunctionDoc
fndoc
  in [Many Block] -> Many Block
forall a. Monoid a => [a] -> a
mconcat
     [ Attr -> Int -> Many Inline -> Many Block
B.headerWith (Text
ident, [], []) Int
level (Text -> Many Inline
B.str Text
name)
     , Many Inline -> Many Block
B.plain (Text -> Many Inline
B.code (Text -> Many Inline) -> Text -> Many Inline
forall a b. (a -> b) -> a -> b
$ Text
name Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
" (" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
argsString Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
")")
     , Text -> Many Block
parseCommonMark (FunctionDoc -> Text
funDocDescription FunctionDoc
fndoc)
     , if [ParameterDoc] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null (FunctionDoc -> [ParameterDoc]
funDocParameters FunctionDoc
fndoc)
       then Many Block
forall a. Monoid a => a
mempty
       else Many Inline -> Many Block
B.para Many Inline
"Parameters:" Many Block -> Many Block -> Many Block
forall a. Semigroup a => a -> a -> a
<> Many Block
paramlist
     , if FunctionDoc -> ResultsDoc
funDocResults FunctionDoc
fndoc ResultsDoc -> ResultsDoc -> Bool
forall a. Eq a => a -> a -> Bool
== [ResultValueDoc] -> ResultsDoc
ResultsDocList []
       then Many Block
forall a. Monoid a => a
mempty
       else Many Inline -> Many Block
B.para Many Inline
"Returns:" Many Block -> Many Block -> Many Block
forall a. Semigroup a => a -> a -> a
<> ResultsDoc -> Many Block
renderResults (FunctionDoc -> ResultsDoc
funDocResults FunctionDoc
fndoc)
     , case FunctionDoc -> Maybe Version
funDocSince FunctionDoc
fndoc of
         Maybe Version
Nothing -> Many Block
forall a. Monoid a => a
mempty
         Just Version
version ->
           Many Inline -> Many Block
B.para (Many Inline -> Many Block) -> Many Inline -> Many Block
forall a b. (a -> b) -> a -> b
$ Many Inline -> Many Inline
B.emph (Many Inline -> Many Inline) -> Many Inline -> Many Inline
forall a b. (a -> b) -> a -> b
$ Many Inline
"Since: " Many Inline -> Many Inline -> Many Inline
forall a. Semigroup a => a -> a -> a
<> (Text -> Many Inline
B.str (Text -> Many Inline) -> (String -> Text) -> String -> Many Inline
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Text
T.pack (String -> Many Inline) -> String -> Many Inline
forall a b. (a -> b) -> a -> b
$ Version -> String
showVersion Version
version)
     ]

renderResults :: ResultsDoc -> B.Blocks
renderResults :: ResultsDoc -> Many Block
renderResults (ResultsDocMult Text
descr) = Text -> Many Block
parseCommonMark Text
descr
renderResults (ResultsDocList [ResultValueDoc]
rvd) = [Many Block] -> Many Block
B.bulletList ([Many Block] -> Many Block) -> [Many Block] -> Many Block
forall a b. (a -> b) -> a -> b
$ (ResultValueDoc -> Many Block) -> [ResultValueDoc] -> [Many Block]
forall a b. (a -> b) -> [a] -> [b]
map ResultValueDoc -> Many Block
renderResultVal [ResultValueDoc]
rvd
 where
   renderResultVal :: ResultValueDoc -> Many Block
renderResultVal (ResultValueDoc TypeSpec
typespec Text
descr) =
     Text -> Many Block
parseCommonMark Text
descr Many Block -> TypeSpec -> Many Block
`appendType` TypeSpec
typespec

argslist :: [ParameterDoc] -> T.Text
argslist :: [ParameterDoc] -> Text
argslist [ParameterDoc]
params =
  -- Expect optional values to come after required values.
  let ([ParameterDoc]
required, [ParameterDoc]
optional') = (ParameterDoc -> Bool)
-> [ParameterDoc] -> ([ParameterDoc], [ParameterDoc])
forall a. (a -> Bool) -> [a] -> ([a], [a])
break ParameterDoc -> Bool
parameterIsOptional [ParameterDoc]
params
      reqs :: [Text]
reqs = (ParameterDoc -> Text) -> [ParameterDoc] -> [Text]
forall a b. (a -> b) -> [a] -> [b]
map ParameterDoc -> Text
parameterName [ParameterDoc]
required
      opts :: [Text]
opts = (ParameterDoc -> Text) -> [ParameterDoc] -> [Text]
forall a b. (a -> b) -> [a] -> [b]
map ParameterDoc -> Text
parameterName [ParameterDoc]
optional'
  in if [Text] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Text]
opts
     then Text -> [Text] -> Text
T.intercalate Text
", " [Text]
reqs
     else Text -> [Text] -> Text
T.intercalate Text
", " [Text]
reqs Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<>
          (if [ParameterDoc] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [ParameterDoc]
required then Text
"[" else Text
"[, ") Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<>
          Text -> [Text] -> Text
T.intercalate Text
"[, " [Text]
opts Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Int -> Text -> Text
T.replicate ([Text] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [Text]
opts) Text
"]"

renderFieldDoc :: T.Text -> FieldDoc -> B.Blocks
renderFieldDoc :: Text -> FieldDoc -> Many Block
renderFieldDoc Text
_modname FieldDoc
fd =
  Attr -> Int -> Many Inline -> Many Block
B.headerWith (Text
ident, [], []) Int
3 (Text -> Many Inline
B.str Text
name) Many Block -> Many Block -> Many Block
forall a. Semigroup a => a -> a -> a
<>
  Many Block -> TypeSpec -> Many Block
appendType (Text -> Many Block
parseCommonMark (Text -> Many Block) -> Text -> Many Block
forall a b. (a -> b) -> a -> b
$ FieldDoc -> Text
fieldDocDescription FieldDoc
fd) (FieldDoc -> TypeSpec
fieldDocType FieldDoc
fd)
 where
  ident :: Text
ident = FieldDoc -> Text
fieldDocName FieldDoc
fd
  name :: Text
name = (Char -> Bool) -> Text -> Text
T.takeWhileEnd (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
/= Char
'.') (Text -> Text) -> Text -> Text
forall a b. (a -> b) -> a -> b
$ FieldDoc -> Text
fieldDocName FieldDoc
fd