Merge pull request #834 from ivan-m/group-notes-together

servant-docs: Offer the ability to group notes together
This commit is contained in:
Alp Mestanogullari 2017-10-11 11:54:52 +02:00 committed by GitHub
commit 602806296e
3 changed files with 24 additions and 13 deletions

View file

@ -7,8 +7,10 @@
* Document the HTTP Method the parameters of an endpoint belong to * Document the HTTP Method the parameters of an endpoint belong to
(rather than assuming `GET` for all of them). (rather than assuming `GET` for all of them).
* Content type of sample response body is also displayed. * Content type of sample response body is also displayed.
* Can now control how many content-types for each example are shown * Can now customise various aspects of how the document is produced
with `markdownWith` and `RenderingOptions`. using `markdownWith` and `RenderingOptions`:
- How many content-types for each example are shown
- Whether notes should be grouped together under their own header.
0.11 0.11
---- ----

View file

@ -26,7 +26,7 @@ module Servant.Docs
HasDocs(..), docs, pretty, markdown HasDocs(..), docs, pretty, markdown
-- ** Customising generated documentation -- ** Customising generated documentation
, markdownWith, RenderingOptions(..), defRenderingOptions , markdownWith, RenderingOptions(..), defRenderingOptions
, requestExamples, responseExamples, ShowContentTypes(..) , requestExamples, responseExamples, ShowContentTypes(..), notesHeading
-- * Generating docs with extra information -- * Generating docs with extra information
, docsWith, docsWithIntros, docsWithOptions , docsWith, docsWithIntros, docsWithOptions
, ExtraInfo(..), extraInfo , ExtraInfo(..), extraInfo

View file

@ -313,18 +313,22 @@ data RenderingOptions = RenderingOptions
-- ^ How many content types to display for request body examples? -- ^ How many content types to display for request body examples?
, _responseExamples :: !ShowContentTypes , _responseExamples :: !ShowContentTypes
-- ^ How many content types to display for response body examples? -- ^ How many content types to display for response body examples?
, _notesHeading :: !(Maybe String)
-- ^ Optionally group all 'notes' together under a common heading.
} deriving (Show) } deriving (Show)
-- | Default API generation options. -- | Default API generation options.
-- --
-- All content types are shown for both 'requestExamples' and -- All content types are shown for both 'requestExamples' and
-- 'responseExamples'. -- 'responseExamples'; 'notesHeading' is set to 'Nothing'
-- (i.e. un-grouped).
-- --
-- @since 0.11.1 -- @since 0.11.1
defRenderingOptions :: RenderingOptions defRenderingOptions :: RenderingOptions
defRenderingOptions = RenderingOptions defRenderingOptions = RenderingOptions
{ _requestExamples = AllContentTypes { _requestExamples = AllContentTypes
, _responseExamples = AllContentTypes , _responseExamples = AllContentTypes
, _notesHeading = Nothing
} }
-- gimme some lenses -- gimme some lenses
@ -615,23 +619,28 @@ markdownWith RenderingOptions{..} api = unlines $
[] []
notesStr :: [DocNote] -> [String] notesStr :: [DocNote] -> [String]
notesStr = concatMap noteStr notesStr = addHeading
. concatMap noteStr
where
addHeading nts = maybe nts (\hd -> ("### " ++ hd) : "" : nts) _notesHeading
noteStr :: DocNote -> [String] noteStr :: DocNote -> [String]
noteStr nt = noteStr nt =
("#### " ++ nt ^. noteTitle) : (hdr ++ nt ^. noteTitle) :
"" : "" :
intersperse "" (nt ^. noteBody) ++ intersperse "" (nt ^. noteBody) ++
"" : "" :
[] []
where
hdr | isJust _notesHeading = "#### "
| otherwise = "### "
authStr :: [DocAuthentication] -> [String] authStr :: [DocAuthentication] -> [String]
authStr [] = [] authStr [] = []
authStr auths = authStr auths =
let authIntros = mapped %~ view authIntro $ auths let authIntros = mapped %~ view authIntro $ auths
clientInfos = mapped %~ view authDataRequired $ auths clientInfos = mapped %~ view authDataRequired $ auths
in "#### Authentication": in "### Authentication":
"": "":
unlines authIntros : unlines authIntros :
"": "":
@ -643,7 +652,7 @@ markdownWith RenderingOptions{..} api = unlines $
capturesStr :: [DocCapture] -> [String] capturesStr :: [DocCapture] -> [String]
capturesStr [] = [] capturesStr [] = []
capturesStr l = capturesStr l =
"#### Captures:" : "### Captures:" :
"" : "" :
map captureStr l ++ map captureStr l ++
"" : "" :
@ -655,7 +664,7 @@ markdownWith RenderingOptions{..} api = unlines $
headersStr :: [Text] -> [String] headersStr :: [Text] -> [String]
headersStr [] = [] headersStr [] = []
headersStr l = headersStr l =
"#### Headers:" : "### Headers:" :
"" : "" :
map headerStr l ++ map headerStr l ++
"" : "" :
@ -667,7 +676,7 @@ markdownWith RenderingOptions{..} api = unlines $
paramsStr :: HTTP.Method -> [DocQueryParam] -> [String] paramsStr :: HTTP.Method -> [DocQueryParam] -> [String]
paramsStr _ [] = [] paramsStr _ [] = []
paramsStr m l = paramsStr m l =
("#### " ++ cs m ++ " Parameters:") : ("### " ++ cs m ++ " Parameters:") :
"" : "" :
map (paramStr m) l ++ map (paramStr m) l ++
"" : "" :
@ -693,7 +702,7 @@ markdownWith RenderingOptions{..} api = unlines $
rqbodyStr :: [M.MediaType] -> [(Text, M.MediaType, ByteString)]-> [String] rqbodyStr :: [M.MediaType] -> [(Text, M.MediaType, ByteString)]-> [String]
rqbodyStr [] [] = [] rqbodyStr [] [] = []
rqbodyStr types s = rqbodyStr types s =
["#### Request:", ""] ["### Request:", ""]
<> formatTypes types <> formatTypes types
<> formatBodies _requestExamples s <> formatBodies _requestExamples s
@ -749,7 +758,7 @@ markdownWith RenderingOptions{..} api = unlines $
responseStr :: Response -> [String] responseStr :: Response -> [String]
responseStr resp = responseStr resp =
"#### Response:" : "### Response:" :
"" : "" :
("- Status code " ++ show (resp ^. respStatus)) : ("- Status code " ++ show (resp ^. respStatus)) :
("- Headers: " ++ show (resp ^. respHeaders)) : ("- Headers: " ++ show (resp ^. respHeaders)) :