From 5592666ca44aa0d027cd95bf11cff09825896584 Mon Sep 17 00:00:00 2001 From: John MacFarlane Date: Tue, 23 Jul 2013 22:31:50 -0700 Subject: Text.Pandoc: Added readJSON, writeJSON to the API. Closes #817. --- src/Text/Pandoc.hs | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) (limited to 'src/Text/Pandoc.hs') diff --git a/src/Text/Pandoc.hs b/src/Text/Pandoc.hs index 86e78ce53..b5b698e09 100644 --- a/src/Text/Pandoc.hs +++ b/src/Text/Pandoc.hs @@ -72,9 +72,11 @@ module Text.Pandoc , readOPML , readHaddock , readNative + , readJSON -- * Writers: converting /from/ Pandoc format , Writer (..) , writeNative + , writeJSON , writeMarkdown , writePlain , writeRST @@ -190,9 +192,8 @@ markdown o s = do -- | Association list of formats and readers. readers :: [(String, ReaderOptions -> String -> IO Pandoc)] -readers = [("native" , \_ s -> return $ readNative s) - ,("json" , \_ s -> return $ checkJSON - $ decode $ UTF8.fromStringLazy s) +readers = [ ("native" , \_ s -> return $ readNative s) + ,("json" , \o s -> return $ readJSON o s) ,("markdown" , markdown) ,("markdown_strict" , markdown) ,("markdown_phpextra" , markdown) @@ -205,8 +206,8 @@ readers = [("native" , \_ s -> return $ readNative s) ,("textile" , \o s -> return $ readTextile o s) -- TODO : textile+lhs ,("html" , \o s -> return $ readHtml o s) ,("latex" , \o s -> return $ readLaTeX o s) - ,("haddock" , \o s -> return $ readHaddock o s) - ] + ,("haddock" , \o s -> return $ readHaddock o s) + ] data Writer = PureStringWriter (WriterOptions -> Pandoc -> String) | IOStringWriter (WriterOptions -> Pandoc -> IO String) @@ -216,12 +217,12 @@ data Writer = PureStringWriter (WriterOptions -> Pandoc -> String) writers :: [ ( String, Writer ) ] writers = [ ("native" , PureStringWriter writeNative) - ,("json" , PureStringWriter $ \_ -> UTF8.toStringLazy . encode) + ,("json" , PureStringWriter writeJSON) ,("docx" , IOByteStringWriter writeDocx) - ,("odt" , IOByteStringWriter writeODT) - ,("epub" , IOByteStringWriter $ \o -> + ,("odt" , IOByteStringWriter writeODT) + ,("epub" , IOByteStringWriter $ \o -> writeEPUB o{ writerEpubVersion = Just EPUB2 }) - ,("epub3" , IOByteStringWriter $ \o -> + ,("epub3" , IOByteStringWriter $ \o -> writeEPUB o{ writerEpubVersion = Just EPUB3 }) ,("fb2" , IOStringWriter writeFB2) ,("html" , PureStringWriter writeHtmlString) @@ -359,3 +360,10 @@ instance (Data a) => ToJsonFilter (a -> IO [a]) where checkJSON :: Maybe a -> a checkJSON Nothing = error "Error parsing JSON" checkJSON (Just r) = r + +readJSON :: ReaderOptions -> String -> Pandoc +readJSON _ = checkJSON . decode . UTF8.fromStringLazy + +writeJSON :: WriterOptions -> Pandoc -> String +writeJSON _ = UTF8.toStringLazy . encode + -- cgit v1.2.3