@@ -106,12 +106,13 @@ module Text.Pandoc
106106 -- * Miscellaneous
107107 , getReader
108108 , getWriter
109− , jsonFilter
110109 , ToJsonFilter(..)
110+ , ToJSONFilter(..)
111111 ) where
112112
113113import Text.Pandoc.Definition
114114import Text.Pandoc.Generic
115+import Text.Pandoc.JSON
115116import Text.Pandoc.Readers.Markdown
116117import Text.Pandoc.Readers.MediaWiki
117118import Text.Pandoc.Readers.RST
@@ -146,13 +147,11 @@ import Text.Pandoc.Writers.Custom
146147import Text.Pandoc.Templates
147148import Text.Pandoc.Options
148149import Text.Pandoc.Shared (safeRead, warn)
149−import Data.ByteString.Lazy (ByteString)
150+import Data.Aeson
150151import qualified Data.ByteString.Lazy as BL
151152import Data.List (intercalate, isSuffixOf)
152153import Data.Version (showVersion)
153−import Data.Aeson.Generic
154154import Data.Set (Set)
155−import Data.Data
156155import qualified Data.Set as Set
157156import Text.Parsec
158157import Text.Parsec.Error
@@ -211,7 +210,7 @@ readers = [ ("native" , \_ s -> return $ readNative s)
211210
212211data Writer = PureStringWriter (WriterOptions -> Pandoc -> String)
213212 | IOStringWriter (WriterOptions -> Pandoc -> IO String)
214− | IOByteStringWriter (WriterOptions -> Pandoc -> IO ByteString)
213+ | IOByteStringWriter (WriterOptions -> Pandoc -> IO BL.ByteString)
215214
216215-- | Association list of formats and writers.
217216writers :: [ ( String, Writer ) ]
@@ -304,66 +303,17 @@ getWriter s =
304303 \o -> r o{ writerExtensions = setExts $
305304 getDefaultExtensions writerName }
306305
307−{-# DEPRECATED jsonFilter "Use toJsonFilter instead" #-}
308−-- | Converts a transformation on the Pandoc AST into a function
309−-- that reads and writes a JSON-encoded string. This is useful
310−-- for writing small scripts.
311−jsonFilter :: (Pandoc -> Pandoc) -> String -> String
312−jsonFilter f = UTF8.toStringLazy . encode . f . checkJSON . decode . UTF8.fromStringLazy
313−
314−-- | 'toJsonFilter' convert a function into a filter that reads pandoc's json output
315−-- from stdin, transforms it by walking the AST and applying the specified
316−-- function, and writes the result as json to stdout. Usage example:
317−--
318−-- > -- capitalize.hs
319−-- > -- compile with: ghc --make capitalize
320−-- > -- run with: pandoc -t json | ./capitalize | pandoc -f json
321−-- >
322−-- > import Text.Pandoc
323−-- > import Data.Char (toUpper)
324−-- >
325−-- > main :: IO ()
326−-- > main = toJsonFilter capitalizeStrings
327−-- >
328−-- > capitalizeStrings :: Inline -> Inline
329−-- > capitalizeStrings (Str s) = Str $ map toUpper s
330−-- > capitalizeStrings x = x
331−--
332−-- The function can be any type @(a -> a)@, @(a -> IO a)@, @(a -> [a])@,
333−-- or @(a -> IO [a])@, where @a@ is an instance of 'Data'.
334−-- So, for example, @a@ can be 'Pandoc', 'Inline', 'Block', ['Inline'],
335−-- ['Block'], 'Meta', 'ListNumberStyle', 'Alignment', 'ListNumberDelim',
336−-- 'QuoteType', etc. See 'Text.Pandoc.Definition'.
337−class ToJsonFilter a where
338− toJsonFilter :: a -> IO ()
339−
340−instance (Data a) => ToJsonFilter (a -> a) where
341− toJsonFilter f = BL.getContents >>=
342− BL.putStr . encode . (bottomUp f :: Pandoc -> Pandoc) . checkJSON . decode
343−
344−instance (Data a) => ToJsonFilter (a -> IO a) where
345− toJsonFilter f = BL.getContents >>=
346− (bottomUpM f :: Pandoc -> IO Pandoc) . checkJSON . decode >>=
347− BL.putStr . encode
348−
349−instance (Data a) => ToJsonFilter (a -> [a]) where
350− toJsonFilter f = BL.getContents >>=
351− BL.putStr . encode . (bottomUp (concatMap f) :: Pandoc -> Pandoc) .
352− checkJSON . decode
353−
354−instance (Data a) => ToJsonFilter (a -> IO [a]) where
355− toJsonFilter f = BL.getContents >>=
356− (bottomUpM (fmap concat . mapM f) :: Pandoc -> IO Pandoc)
357− . checkJSON . decode >>=
358− BL.putStr . encode
359−
360−checkJSON :: Maybe a -> a
361−checkJSON Nothing = error "Error parsing JSON"
362−checkJSON (Just r) = r
306+{-# DEPRECATED toJsonFilter "Use toJSONFilter instead" #-}
307+class ToJSONFilter a => ToJsonFilter a
308+ where toJsonFilter :: a -> IO ()
309+ toJsonFilter = toJSONFilter
363310
364311readJSON :: ReaderOptions -> String -> Pandoc
365−readJSON _ = checkJSON . decode . UTF8.fromStringLazy
312+readJSON _ = checkJSON . eitherDecode' . UTF8.fromStringLazy
366313
367314writeJSON :: WriterOptions -> Pandoc -> String
368315writeJSON _ = UTF8.toStringLazy . encode
369316
317+checkJSON :: Either String a -> a
318+checkJSON (Right x) = x
319+checkJSON (Left e) = error e