From 4dfc30ca1020baa15a40d6b890be1a511747861c Mon Sep 17 00:00:00 2001 From: John MacFarlane Date: Sat, 13 Aug 2022 16:47:16 -0700 Subject: [PATCH] Server: add Default instance for Params. --- pandoc.cabal | 1 + server/PandocServer.hs | 22 +++++++++++++++++----- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/pandoc.cabal b/pandoc.cabal index 1e050ef42..996fc4de5 100644 --- a/pandoc.cabal +++ b/pandoc.cabal @@ -799,6 +799,7 @@ executable pandoc-server pandoc, aeson, text, + data-default, bytestring, base64 >= 0.4, servant-server, diff --git a/server/PandocServer.hs b/server/PandocServer.hs index 644cb0b92..5f6b47607 100644 --- a/server/PandocServer.hs +++ b/server/PandocServer.hs @@ -22,6 +22,7 @@ import Data.Maybe (fromMaybe) import Data.Char (isAlphaNum) import Data.ByteString.Lazy (fromStrict, toStrict) import Data.ByteString.Base64 +import Data.Default -- This is the data to be supplied by the JSON payload -- of requests. Maybe values may be omitted and will be @@ -36,6 +37,17 @@ data Params = Params , template :: Maybe Text } deriving (Show) +instance Default Params where + def = Params + { text = "" + , from = Nothing + , to = Nothing + , wrapText = Nothing + , columns = Nothing + , standalone = Nothing + , template = Nothing + } + -- Automatically derive code to convert to/from JSON. $(deriveJSON defaultOptions ''Params) @@ -65,10 +77,9 @@ server = convert :<|> pure pandocVersion where babelmark text' from' to' standalone' = do - res <- convert Params{ text = text', - from = from', to = to', - standalone = Just standalone', wrapText = Nothing, - columns = Nothing, template = Nothing } + res <- convert def{ text = text', + from = from', to = to', + standalone = Just standalone' } return $ toJSON $ object [ "html" .= res, "version" .= pandocVersion ] -- We use runPure for the pandoc conversions, which ensures that @@ -98,7 +109,8 @@ server = convert compileCustomTemplate toformat t else return Nothing let readeropts = def{ readerExtensions = readerExts - , readerStandalone = isStandalone } + , readerStandalone = isStandalone + } let writeropts = def{ writerExtensions = writerExts , writerWrapText = fromMaybe WrapAuto (wrapText params) , writerColumns = fromMaybe 72 (columns params)