Changed types of writeJSON and readJSON.

Previously they were not monadic; we now have them run in an
instance of the Pandoc monad, like the other readers and writers.

[API change]
This commit is contained in:
John MacFarlane 2018-11-29 12:56:06 -08:00
parent 5c9ddaac2c
commit 90f5dd88a4
2 changed files with 10 additions and 11 deletions

View file

@ -106,7 +106,6 @@ import Text.Pandoc.Readers.TWiki
import Text.Pandoc.Readers.Txt2Tags import Text.Pandoc.Readers.Txt2Tags
import Text.Pandoc.Readers.Vimwiki import Text.Pandoc.Readers.Vimwiki
import Text.Pandoc.Readers.Man import Text.Pandoc.Readers.Man
import Text.Pandoc.Shared (mapLeft)
import qualified Text.Pandoc.UTF8 as UTF8 import qualified Text.Pandoc.UTF8 as UTF8
import Text.Parsec.Error import Text.Parsec.Error
@ -116,10 +115,7 @@ data Reader m = TextReader (ReaderOptions -> Text -> m Pandoc)
-- | Association list of formats and readers. -- | Association list of formats and readers.
readers :: PandocMonad m => [(String, Reader m)] readers :: PandocMonad m => [(String, Reader m)]
readers = [ ("native" , TextReader readNative) readers = [ ("native" , TextReader readNative)
,("json" , TextReader $ \o s -> ,("json" , TextReader readJSON)
case readJSON o s of
Right doc -> return doc
Left _ -> throwError $ PandocParseError "JSON parse error")
,("markdown" , TextReader readMarkdown) ,("markdown" , TextReader readMarkdown)
,("markdown_strict" , TextReader readMarkdown) ,("markdown_strict" , TextReader readMarkdown)
,("markdown_phpextra" , TextReader readMarkdown) ,("markdown_phpextra" , TextReader readMarkdown)
@ -162,6 +158,9 @@ getReader s =
getDefaultExtensions readerName) getDefaultExtensions readerName)
-- | Read pandoc document from JSON format. -- | Read pandoc document from JSON format.
readJSON :: ReaderOptions -> Text -> Either PandocError Pandoc readJSON :: PandocMonad m
readJSON _ = => ReaderOptions -> Text -> m Pandoc
mapLeft PandocParseError . eitherDecode' . BL.fromStrict . UTF8.fromText readJSON _ t =
case eitherDecode' . BL.fromStrict . UTF8.fromText $ t of
Right doc -> return doc
Left _ -> throwError $ PandocParseError "JSON parse error"

View file

@ -132,7 +132,7 @@ data Writer m = TextWriter (WriterOptions -> Pandoc -> m Text)
writers :: PandocMonad m => [ ( String, Writer m) ] writers :: PandocMonad m => [ ( String, Writer m) ]
writers = [ writers = [
("native" , TextWriter writeNative) ("native" , TextWriter writeNative)
,("json" , TextWriter $ \o d -> return $ writeJSON o d) ,("json" , TextWriter $ \o d -> writeJSON o d)
,("docx" , ByteStringWriter writeDocx) ,("docx" , ByteStringWriter writeDocx)
,("odt" , ByteStringWriter writeODT) ,("odt" , ByteStringWriter writeODT)
,("pptx" , ByteStringWriter writePowerpoint) ,("pptx" , ByteStringWriter writePowerpoint)
@ -193,5 +193,5 @@ getWriter s
Just r -> Right (r, setExts $ Just r -> Right (r, setExts $
getDefaultExtensions writerName) getDefaultExtensions writerName)
writeJSON :: WriterOptions -> Pandoc -> Text writeJSON :: PandocMonad m => WriterOptions -> Pandoc -> m Text
writeJSON _ = UTF8.toText . BL.toStrict . encode writeJSON _ = return . UTF8.toText . BL.toStrict . encode