{-# LANGUAGE RecordWildCards            #-}

-- | This module generates Markdown-formatted documentation for an
-- API, like this:
--
-- > ### Foo
-- >
-- > a test defn
-- >
-- > JSON Type : **union object** (Haskell prefix is 'foo')
-- >
-- > | Alternative | Type    | Comment
-- > | ----------- | ------- | -----------
-- > | _`Baz`_     | boolean | just a bool
-- > | _`Qux`_     | integer | just an int

module Data.API.Markdown
    ( markdown
    , MarkdownMethods(..)
    , defaultMarkdownMethods
    , thing
    ) where

import           Data.API.Time
import           Data.API.Types

import qualified Data.CaseInsensitive       as CI
import           Data.Char
import qualified Data.Text                  as T
import           Text.Printf
import           Control.Applicative
import           Control.Lens


data MarkdownMethods
    = MDM
        { MarkdownMethods -> TypeName -> String
mdmSummaryPostfix :: TypeName -> MDComment
        , MarkdownMethods -> TypeName -> String
mdmLink           :: TypeName -> MDComment
        , MarkdownMethods -> String -> String -> String
mdmPp             :: MDComment -> MDComment -> MDComment
        , MarkdownMethods -> FieldName -> APIType -> Maybe DefaultValue
mdmFieldDefault   :: FieldName -> APIType -> Maybe DefaultValue
        }

defaultMarkdownMethods :: MarkdownMethods
defaultMarkdownMethods :: MarkdownMethods
defaultMarkdownMethods =
    MDM { mdmSummaryPostfix :: TypeName -> String
mdmSummaryPostfix = forall a b. a -> b -> a
const String
""
        , mdmLink :: TypeName -> String
mdmLink           = Text -> String
T.unpack forall b c a. (b -> c) -> (a -> b) -> a -> c
. TypeName -> Text
_TypeName
        , mdmPp :: String -> String -> String
mdmPp             = forall a. [a] -> [a] -> [a]
(++)
        , mdmFieldDefault :: FieldName -> APIType -> Maybe DefaultValue
mdmFieldDefault   = \ FieldName
_ APIType
_ -> forall a. Maybe a
Nothing
        }

-- | Create human-readable API documentation in Markdown format
markdown :: MarkdownMethods -> API -> MDComment
markdown :: MarkdownMethods -> API -> String
markdown MarkdownMethods
mdm API
ths = forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr (MarkdownMethods -> Thing -> String -> String
thing MarkdownMethods
mdm) String
"" API
ths

-- | Document a single API comment or node in Markdown format
thing :: MarkdownMethods -> Thing -> MDComment  -> MDComment
thing :: MarkdownMethods -> Thing -> String -> String
thing MarkdownMethods
mdm Thing
th String
tl_md =
    case Thing
th of
      ThComment String
md -> MarkdownMethods -> String -> String -> String
mdmPp MarkdownMethods
mdm String
md String
tl_md
      ThNode    APINode
an -> MarkdownMethods -> APINode -> String -> String
node  MarkdownMethods
mdm APINode
an String
tl_md

node :: MarkdownMethods -> APINode -> MDComment -> MDComment
node :: MarkdownMethods -> APINode -> String -> String
node MarkdownMethods
mdm APINode
an String
tl_md =
        MarkdownMethods -> APINode -> String -> String
header MarkdownMethods
mdm APINode
an forall a b. (a -> b) -> a -> b
$ MarkdownMethods -> APINode -> String -> String
body MarkdownMethods
mdm APINode
an forall a b. (a -> b) -> a -> b
$ APINode -> String -> String
version APINode
an forall a b. (a -> b) -> a -> b
$ String
"\n\n" forall a. [a] -> [a] -> [a]
++ String
tl_md

header :: MarkdownMethods -> APINode -> MDComment -> MDComment
header :: MarkdownMethods -> APINode -> String -> String
header MarkdownMethods
mdm APINode
an String
tl_md =
                                forall r. PrintfType r => String -> r
printf String
"### %s\n\n%s\n\n%s" String
nm_md (MarkdownMethods -> String -> String -> String
mdmPp MarkdownMethods
mdm String
cm_md String
"") String
tl_md
  where
    nm_md :: String
nm_md = APINode -> String
type_name_md APINode
an
    cm_md :: String
cm_md = APINode -> String
comment_md   APINode
an

body :: MarkdownMethods -> APINode -> MDComment  -> MDComment
body :: MarkdownMethods -> APINode -> String -> String
body MarkdownMethods
mdm APINode
an String
tl_md =
    case APINode -> Spec
anSpec APINode
an of
      SpNewtype SpecNewtype
sn -> String -> [String] -> String
block String
tl_md forall a b. (a -> b) -> a -> b
$ MarkdownMethods -> APINode -> SpecNewtype -> [String]
ntype   MarkdownMethods
mdm APINode
an SpecNewtype
sn
      SpRecord  SpecRecord
sr -> String -> [String] -> String
block String
tl_md forall a b. (a -> b) -> a -> b
$ MarkdownMethods -> APINode -> SpecRecord -> [String]
record  MarkdownMethods
mdm APINode
an SpecRecord
sr
      SpUnion   SpecUnion
su -> String -> [String] -> String
block String
tl_md forall a b. (a -> b) -> a -> b
$ MarkdownMethods -> APINode -> SpecUnion -> [String]
union_  MarkdownMethods
mdm APINode
an SpecUnion
su
      SpEnum    SpecEnum
se -> String -> [String] -> String
block String
tl_md forall a b. (a -> b) -> a -> b
$ MarkdownMethods -> APINode -> SpecEnum -> [String]
enum_   MarkdownMethods
mdm APINode
an SpecEnum
se
      SpSynonym APIType
ty -> String -> [String] -> String
block String
tl_md forall a b. (a -> b) -> a -> b
$ MarkdownMethods -> APINode -> APIType -> [String]
synonym MarkdownMethods
mdm APINode
an APIType
ty

ntype :: MarkdownMethods -> APINode -> SpecNewtype -> [MDComment]
ntype :: MarkdownMethods -> APINode -> SpecNewtype -> [String]
ntype MarkdownMethods
mdm APINode
an SpecNewtype
sn =
    MarkdownMethods -> APINode -> String -> [String]
summary_lines MarkdownMethods
mdm APINode
an (BasicType -> String
basic_type_md forall a b. (a -> b) -> a -> b
$ SpecNewtype -> BasicType
snType SpecNewtype
sn) forall a. [a] -> [a] -> [a]
++ [Filter -> String
f Filter
ftr | Just Filter
ftr<-[SpecNewtype -> Maybe Filter
snFilter SpecNewtype
sn]]
  where
    f :: Filter -> String
f (FtrStrg RegEx{Text
Regex
re_regex :: RegEx -> Regex
re_text :: RegEx -> Text
re_regex :: Regex
re_text :: Text
..}   ) = String
"**filter** " forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show Text
re_text
    f (FtrIntg IntRange{Maybe Int
ir_hi :: IntRange -> Maybe Int
ir_lo :: IntRange -> Maybe Int
ir_hi :: Maybe Int
ir_lo :: Maybe Int
..}) = String
"**filter** " forall a. [a] -> [a] -> [a]
++ forall {t}. (t -> String) -> Maybe t -> Maybe t -> String
rg forall a. Show a => a -> String
show   Maybe Int
ir_lo Maybe Int
ir_hi
    f (FtrUTC  UTCRange{Maybe UTCTime
ur_hi :: UTCRange -> Maybe UTCTime
ur_lo :: UTCRange -> Maybe UTCTime
ur_hi :: Maybe UTCTime
ur_lo :: Maybe UTCTime
..}) = String
"**filter** " forall a. [a] -> [a] -> [a]
++ forall {t}. (t -> String) -> Maybe t -> Maybe t -> String
rg (Text -> String
T.unpack forall b c a. (b -> c) -> (a -> b) -> a -> c
. UTCTime -> Text
printUTC) Maybe UTCTime
ur_lo Maybe UTCTime
ur_hi

    rg :: (t -> String) -> Maybe t -> Maybe t -> String
rg t -> String
_  Maybe t
Nothing   Maybe t
Nothing   = String
"**no restriction**" -- should not happen (not produced by parser)
    rg t -> String
sh Maybe t
Nothing   (Just t
hi) = String
"x <= " forall a. [a] -> [a] -> [a]
++ t -> String
sh t
hi
    rg t -> String
sh (Just t
lo) Maybe t
Nothing   = t -> String
sh t
lo forall a. [a] -> [a] -> [a]
++ String
" <= x"
    rg t -> String
sh (Just t
lo) (Just t
hi) = t -> String
sh t
lo forall a. [a] -> [a] -> [a]
++ String
" <= x <= " forall a. [a] -> [a] -> [a]
++ t -> String
sh t
hi

record :: MarkdownMethods -> APINode -> SpecRecord -> [MDComment]
record :: MarkdownMethods -> APINode -> SpecRecord -> [String]
record MarkdownMethods
mdm APINode
an SpecRecord
sr =
    MarkdownMethods -> APINode -> String -> [String]
summary_lines MarkdownMethods
mdm APINode
an String
"record object" forall a. [a] -> [a] -> [a]
++ MarkdownMethods -> [(FieldName, FieldType)] -> [String]
mk_md_record_table MarkdownMethods
mdm (SpecRecord -> [(FieldName, FieldType)]
srFields SpecRecord
sr)

union_ :: MarkdownMethods -> APINode -> SpecUnion -> [MDComment]
union_ :: MarkdownMethods -> APINode -> SpecUnion -> [String]
union_ MarkdownMethods
mdm APINode
an SpecUnion
su =
    MarkdownMethods -> APINode -> String -> [String]
summary_lines MarkdownMethods
mdm APINode
an String
"union object" forall a. [a] -> [a] -> [a]
++ MarkdownMethods -> [(FieldName, (APIType, String))] -> [String]
mk_md_union_table MarkdownMethods
mdm (SpecUnion -> [(FieldName, (APIType, String))]
suFields SpecUnion
su)

enum_ :: MarkdownMethods -> APINode -> SpecEnum -> [MDComment]
enum_ :: MarkdownMethods -> APINode -> SpecEnum -> [String]
enum_ MarkdownMethods
mdm APINode
an SpecEnum{[(FieldName, String)]
seAlts :: SpecEnum -> [(FieldName, String)]
seAlts :: [(FieldName, String)]
..} =
    MarkdownMethods -> APINode -> String -> [String]
summary_lines MarkdownMethods
mdm APINode
an String
"string enumeration" forall a. [a] -> [a] -> [a]
++ forall a b. (a -> b) -> [a] -> [b]
map (String, String) -> String
f ((String, String)
hdr forall a. a -> [a] -> [a]
: (String, String)
dhs forall a. a -> [a] -> [a]
: [(String, String)]
rws)
  where
    f :: (String, String) -> String
f (String
fnm,String
cmt)  = Int -> String -> String
ljust Int
lnx String
fnm forall a. [a] -> [a] -> [a]
++ String
" | " forall a. [a] -> [a] -> [a]
++ String
cmt

    dhs :: (String, String)
dhs          = (forall a. Int -> a -> [a]
replicate Int
lnx Char
'-',forall a. Int -> a -> [a]
replicate Int
7 Char
'-')

    lnx :: Int
lnx          = forall (t :: * -> *) a. (Foldable t, Ord a) => t a -> a
maximum forall a b. (a -> b) -> a -> b
$ Int
0 forall a. a -> [a] -> [a]
: forall a b. (a -> b) -> [a] -> [b]
map (Text -> Int
T.length forall b c a. (b -> c) -> (a -> b) -> a -> c
. FieldName -> Text
_FieldName forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a, b) -> a
fst) [(FieldName, String)]
seAlts

    rws :: [(String, String)]
rws          = forall a b. (a -> b) -> [a] -> [b]
map (FieldName, String) -> (String, String)
fmt [(FieldName, String)]
seAlts

    hdr :: (String, String)
hdr          = (String
"Enumeration",String
"Comment")

    fmt :: (FieldName, String) -> (String, String)
fmt (FieldName
fn0,String
ct) = (Text -> String
T.unpack (FieldName -> Text
_FieldName FieldName
fn0), MarkdownMethods -> String -> String -> String
mdmPp MarkdownMethods
mdm String
"" forall a b. (a -> b) -> a -> b
$ String -> String
cln String
ct)

    cln :: String -> String
cln String
ct       = forall a. [a] -> [a]
reverse forall a b. (a -> b) -> a -> b
$ forall a. (a -> Bool) -> [a] -> [a]
dropWhile Char -> Bool
isSpace forall a b. (a -> b) -> a -> b
$ forall a. [a] -> [a]
reverse forall a b. (a -> b) -> a -> b
$ forall a b. (a -> b) -> [a] -> [b]
map Char -> Char
tr String
ct
      where
        tr :: Char -> Char
tr Char
'\n' = Char
' '
        tr Char
c    = Char
c

synonym :: MarkdownMethods -> APINode -> APIType -> [MDComment]
synonym :: MarkdownMethods -> APINode -> APIType -> [String]
synonym MarkdownMethods
mdm APINode
an APIType
ty = MarkdownMethods -> APINode -> String -> [String]
summary_lines MarkdownMethods
mdm APINode
an forall a b. (a -> b) -> a -> b
$ MarkdownMethods -> APIType -> String
type_md MarkdownMethods
mdm APIType
ty

mk_md_record_table :: MarkdownMethods -> [(FieldName, FieldType)] -> [MDComment]
mk_md_record_table :: MarkdownMethods -> [(FieldName, FieldType)] -> [String]
mk_md_record_table MarkdownMethods
mdm [(FieldName, FieldType)]
fds = forall a b. (a -> b) -> [a] -> [b]
map (String, String, String, String) -> String
f forall a b. (a -> b) -> a -> b
$ (String, String, String, String)
hdr forall a. a -> [a] -> [a]
: (String, String, String, String)
dhs forall a. a -> [a] -> [a]
: [(String, String, String, String)]
rws
  where
    f :: (String, String, String, String) -> String
f          = if forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all (forall (t :: * -> *) a. Foldable t => t a -> Bool
null forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall s (m :: * -> *) a. MonadReader s m => Getting a s a -> m a
view forall s t a b. Field4 s t a b => Lens s t a b
_4) [(String, String, String, String)]
rws then forall {d}. (String, String, String, d) -> String
f3 else (String, String, String, String) -> String
f4

    f3 :: (String, String, String, d) -> String
f3 (String
x,String
y,String
z,d
_) = Int -> String -> String
ljust Int
lnx String
x forall a. [a] -> [a] -> [a]
++ String
" | " forall a. [a] -> [a] -> [a]
++ Int -> String -> String
ljust Int
lny String
y forall a. [a] -> [a] -> [a]
++ String
" | " forall a. [a] -> [a] -> [a]
++ String
z
    f4 :: (String, String, String, String) -> String
f4 (String
x,String
y,String
z,String
a) = Int -> String -> String
ljust Int
lnx String
x forall a. [a] -> [a] -> [a]
++ String
" | " forall a. [a] -> [a] -> [a]
++ Int -> String -> String
ljust Int
lny String
y forall a. [a] -> [a] -> [a]
++ String
" | " forall a. [a] -> [a] -> [a]
++ Int -> String -> String
ljust Int
lnz String
z forall a. [a] -> [a] -> [a]
++ String
" | " forall a. [a] -> [a] -> [a]
++ String
a

    dhs :: (String, String, String, String)
dhs  = (forall a. Int -> a -> [a]
replicate Int
lnx Char
'-',forall a. Int -> a -> [a]
replicate Int
lny Char
'-',forall a. Int -> a -> [a]
replicate Int
lnz Char
'-',forall a. Int -> a -> [a]
replicate Int
7 Char
'-')

    lnx :: Int
lnx  = forall (t :: * -> *) a. (Foldable t, Ord a) => t a -> a
maximum forall a b. (a -> b) -> a -> b
$ forall a b. (a -> b) -> [a] -> [b]
map (forall (t :: * -> *) a. Foldable t => t a -> Int
length forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall s (m :: * -> *) a. MonadReader s m => Getting a s a -> m a
view forall s t a b. Field1 s t a b => Lens s t a b
_1) forall a b. (a -> b) -> a -> b
$ (String, String, String, String)
hdr forall a. a -> [a] -> [a]
: [(String, String, String, String)]
rws
    lny :: Int
lny  = forall (t :: * -> *) a. (Foldable t, Ord a) => t a -> a
maximum forall a b. (a -> b) -> a -> b
$ forall a b. (a -> b) -> [a] -> [b]
map (forall (t :: * -> *) a. Foldable t => t a -> Int
length forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall s (m :: * -> *) a. MonadReader s m => Getting a s a -> m a
view forall s t a b. Field2 s t a b => Lens s t a b
_2) forall a b. (a -> b) -> a -> b
$ (String, String, String, String)
hdr forall a. a -> [a] -> [a]
: [(String, String, String, String)]
rws
    lnz :: Int
lnz  = forall (t :: * -> *) a. (Foldable t, Ord a) => t a -> a
maximum forall a b. (a -> b) -> a -> b
$ forall a b. (a -> b) -> [a] -> [b]
map (forall (t :: * -> *) a. Foldable t => t a -> Int
length forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall s (m :: * -> *) a. MonadReader s m => Getting a s a -> m a
view forall s t a b. Field3 s t a b => Lens s t a b
_3) forall a b. (a -> b) -> a -> b
$ (String, String, String, String)
hdr forall a. a -> [a] -> [a]
: [(String, String, String, String)]
rws

    hdr :: (String, String, String, String)
hdr  = (String
"Field",String
"Type",String
"Default",String
"Comment")

    rws :: [(String, String, String, String)]
rws  = forall a b. (a -> b) -> [a] -> [b]
map (FieldName, FieldType) -> (String, String, String, String)
fmt [(FieldName, FieldType)]
fds

    fmt :: (FieldName, FieldType) -> (String, String, String, String)
fmt (FieldName
fn0,FieldType
fty) = ( String
fn, MarkdownMethods -> APIType -> String
type_md MarkdownMethods
mdm APIType
ty, String
flg_md, MarkdownMethods -> String -> String -> String
mdmPp MarkdownMethods
mdm String
"" forall a b. (a -> b) -> a -> b
$ String -> String
cleanComment String
ct )
      where
        fn :: String
fn  = Text -> String
T.unpack (FieldName -> Text
_FieldName FieldName
fn0)
        ty :: APIType
ty  = FieldType -> APIType
ftType FieldType
fty
        ct :: String
ct  = FieldType -> String
ftComment FieldType
fty

        flg_md :: String
flg_md | FieldType -> Bool
ftReadOnly FieldType
fty = String
"*read-only*"
               | Bool
otherwise      = Maybe DefaultValue -> String
default_md forall a b. (a -> b) -> a -> b
$ FieldType -> Maybe DefaultValue
ftDefault FieldType
fty

        default_md :: Maybe DefaultValue -> String
default_md Maybe DefaultValue
mb_dv = forall b a. b -> (a -> b) -> Maybe a -> b
maybe String
"" (String -> String
backticks forall b c a. (b -> c) -> (a -> b) -> a -> c
. DefaultValue -> String
default_value)
                                 (MarkdownMethods -> FieldName -> APIType -> Maybe DefaultValue
mdmFieldDefault MarkdownMethods
mdm FieldName
fn0 APIType
ty forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Maybe DefaultValue
mb_dv)
        backticks :: String -> String
backticks String
s = String
"`" forall a. [a] -> [a] -> [a]
++ String
s forall a. [a] -> [a] -> [a]
++ String
"`"

mk_md_union_table :: MarkdownMethods ->
                            [(FieldName, (APIType, MDComment))] -> [MDComment]
mk_md_union_table :: MarkdownMethods -> [(FieldName, (APIType, String))] -> [String]
mk_md_union_table MarkdownMethods
mdm [(FieldName, (APIType, String))]
fds = forall a b. (a -> b) -> [a] -> [b]
map (String, String, String) -> String
f forall a b. (a -> b) -> a -> b
$ (String, String, String)
hdr forall a. a -> [a] -> [a]
: (String, String, String)
dhs forall a. a -> [a] -> [a]
: [(String, String, String)]
rws
  where
    f :: (String, String, String) -> String
f          = if forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all (forall (t :: * -> *) a. Foldable t => t a -> Bool
null forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall s (m :: * -> *) a. MonadReader s m => Getting a s a -> m a
view forall s t a b. Field3 s t a b => Lens s t a b
_3) [(String, String, String)]
rws then forall {c}. (String, String, c) -> String
f2 else (String, String, String) -> String
f3

    f2 :: (String, String, c) -> String
f2 (String
x,String
y,c
_)  = Int -> String -> String
ljust Int
lnx String
x forall a. [a] -> [a] -> [a]
++ String
" | " forall a. [a] -> [a] -> [a]
++ String
y
    f3 :: (String, String, String) -> String
f3 (String
x,String
y,String
z)  = Int -> String -> String
ljust Int
lnx String
x forall a. [a] -> [a] -> [a]
++ String
" | " forall a. [a] -> [a] -> [a]
++ Int -> String -> String
ljust Int
lny String
y forall a. [a] -> [a] -> [a]
++ String
" | " forall a. [a] -> [a] -> [a]
++ String
z

    dhs :: (String, String, String)
dhs  = (forall a. Int -> a -> [a]
replicate Int
lnx Char
'-',forall a. Int -> a -> [a]
replicate Int
lny Char
'-',forall a. Int -> a -> [a]
replicate Int
7 Char
'-')

    lnx :: Int
lnx  = forall (t :: * -> *) a. (Foldable t, Ord a) => t a -> a
maximum forall a b. (a -> b) -> a -> b
$ forall a b. (a -> b) -> [a] -> [b]
map (forall (t :: * -> *) a. Foldable t => t a -> Int
length forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall s (m :: * -> *) a. MonadReader s m => Getting a s a -> m a
view forall s t a b. Field1 s t a b => Lens s t a b
_1) forall a b. (a -> b) -> a -> b
$ (String, String, String)
hdr forall a. a -> [a] -> [a]
: [(String, String, String)]
rws
    lny :: Int
lny  = forall (t :: * -> *) a. (Foldable t, Ord a) => t a -> a
maximum forall a b. (a -> b) -> a -> b
$ forall a b. (a -> b) -> [a] -> [b]
map (forall (t :: * -> *) a. Foldable t => t a -> Int
length forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall s (m :: * -> *) a. MonadReader s m => Getting a s a -> m a
view forall s t a b. Field2 s t a b => Lens s t a b
_2) forall a b. (a -> b) -> a -> b
$ (String, String, String)
hdr forall a. a -> [a] -> [a]
: [(String, String, String)]
rws

    hdr :: (String, String, String)
hdr  = (String
"Alternative",String
"Type",String
"Comment")

    rws :: [(String, String, String)]
rws  = forall a b. (a -> b) -> [a] -> [b]
map (FieldName, (APIType, String)) -> (String, String, String)
fmt [(FieldName, (APIType, String))]
fds

    fmt :: (FieldName, (APIType, String)) -> (String, String, String)
fmt (FieldName
fn0,(APIType
ty,String
ct)) = (String
"_" forall a. [a] -> [a] -> [a]
++ String
fn forall a. [a] -> [a] -> [a]
++ String
"_",MarkdownMethods -> APIType -> String
type_md MarkdownMethods
mdm APIType
ty,MarkdownMethods -> String -> String -> String
mdmPp MarkdownMethods
mdm String
"" forall a b. (a -> b) -> a -> b
$ String -> String
cleanComment String
ct)
      where
        fn :: String
fn  = Text -> String
T.unpack (FieldName -> Text
_FieldName FieldName
fn0)

cleanComment :: MDComment -> MDComment
cleanComment :: String -> String
cleanComment String
ct = forall a. [a] -> [a]
reverse forall a b. (a -> b) -> a -> b
$ forall a. (a -> Bool) -> [a] -> [a]
dropWhile Char -> Bool
isSpace forall a b. (a -> b) -> a -> b
$ forall a. [a] -> [a]
reverse forall a b. (a -> b) -> a -> b
$ forall a b. (a -> b) -> [a] -> [b]
map Char -> Char
tr String
ct
      where
        tr :: Char -> Char
tr Char
'\n' = Char
' '
        tr Char
c    = Char
c

summary_lines :: MarkdownMethods -> APINode -> String -> [MDComment]
summary_lines :: MarkdownMethods -> APINode -> String -> [String]
summary_lines MarkdownMethods
mdm APINode
an String
smy =
    [ forall r. PrintfType r => String -> r
printf String
"JSON Type : **%s** [Haskell prefix is `%s`] %s" String
smy String
pfx String
pst
    , String
""
    ]
  where
    pfx :: String
pfx = APINode -> String
prefix_md         APINode
an
    pst :: String
pst = MarkdownMethods -> TypeName -> String
mdmSummaryPostfix MarkdownMethods
mdm forall a b. (a -> b) -> a -> b
$ APINode -> TypeName
anName APINode
an

default_value :: DefaultValue -> MDComment
default_value :: DefaultValue -> String
default_value DefaultValue
dv =
    case DefaultValue
dv of
      DefaultValue
DefValList     -> String
"[]"
      DefaultValue
DefValMaybe    -> String
"null"
      DefValString Text
t -> forall a. Show a => a -> String
show Text
t
      DefValBool   Bool
b -> forall a b. (a -> b) -> [a] -> [b]
map Char -> Char
toLower forall a b. (a -> b) -> a -> b
$ forall a. Show a => a -> String
show Bool
b
      DefValInt    Int
i -> forall a. Show a => a -> String
show Int
i
      DefValUtc    UTCTime
u -> forall a. Show a => a -> String
show forall a b. (a -> b) -> a -> b
$ UTCTime -> Text
printUTC UTCTime
u

type_md :: MarkdownMethods -> APIType -> MDComment
type_md :: MarkdownMethods -> APIType -> String
type_md MarkdownMethods
mdm APIType
ty =
    case APIType
ty of
      TyList  APIType
ty'  -> String
"[" forall a. [a] -> [a] -> [a]
++ MarkdownMethods -> APIType -> String
type_md MarkdownMethods
mdm APIType
ty' forall a. [a] -> [a] -> [a]
++ String
"]"
      TyMaybe APIType
ty'  -> String
"? " forall a. [a] -> [a] -> [a]
++ MarkdownMethods -> APIType -> String
type_md MarkdownMethods
mdm APIType
ty'
      TyName  TypeName
nm   -> MarkdownMethods -> TypeName -> String
mdmLink MarkdownMethods
mdm TypeName
nm
      TyBasic BasicType
bt   -> BasicType -> String
basic_type_md BasicType
bt
      APIType
TyJSON       -> String
"json"

basic_type_md :: BasicType -> MDComment
basic_type_md :: BasicType -> String
basic_type_md BasicType
bt =
    case BasicType
bt of
      BasicType
BTstring -> String
"string"
      BasicType
BTbinary -> String
"base64 string"
      BasicType
BTbool   -> String
"boolean"
      BasicType
BTint    -> String
"integer"
      BasicType
BTutc    -> String
"utc"

type_name_md, prefix_md, comment_md :: APINode -> MDComment
type_name_md :: APINode -> String
type_name_md = Text -> String
T.unpack forall b c a. (b -> c) -> (a -> b) -> a -> c
. TypeName -> Text
_TypeName forall b c a. (b -> c) -> (a -> b) -> a -> c
. APINode -> TypeName
anName
prefix_md :: APINode -> String
prefix_md    = forall s. CI s -> s
CI.original forall b c a. (b -> c) -> (a -> b) -> a -> c
. APINode -> Prefix
anPrefix
comment_md :: APINode -> String
comment_md   =               APINode -> String
anComment

block :: MDComment -> [MDComment] -> MDComment
block :: String -> [String] -> String
block String
tl_md [String]
cmts = [String] -> String
unlines [String]
cmts forall a. [a] -> [a] -> [a]
++ String
tl_md

version :: APINode -> MDComment -> MDComment
version :: APINode -> String -> String
version APINode
_ String
tl_md = String
tl_md

ljust :: Int -> String -> String
ljust :: Int -> String -> String
ljust Int
fw String
s = String
s forall a. [a] -> [a] -> [a]
++ forall a. Int -> a -> [a]
replicate Int
p Char
' '
  where
    p :: Int
p = forall a. Ord a => a -> a -> a
max Int
0 forall a b. (a -> b) -> a -> b
$ Int
fw forall a. Num a => a -> a -> a
- forall (t :: * -> *) a. Foldable t => t a -> Int
length String
s

{-
pp :: MarkdownMethods -> MDComment -> MDComment -> MDComment
pp mdm s0 tl_md = pp0 s0
  where
    pp0 []    = tl_md
    pp0 (c:t) =
        case c of
          '{' -> pp1 $ break ('}' ==) t
          _   -> c : pp0 t

    pp1 (nm,[] ) = '{' : nm ++ tl_md
    pp1 (nm,_:t) = mdmLink mdm (TypeName nm) ++ pp0 t
-}