git.iko.soy/typeable/pandoc
on master help sign in
Added ToJsonFilter class, deprecated old jsonFilter function.
commit2e62ec096f0336dd00ec3c2da598a3e6fd2c2b03 authorJohn MacFarlane <fiddlosopher@gmail.com> dateTue 24 Jan 2012, 09:06 (15 years ago) parent0fd0dc2
1 file changed+37
src/Text/Pandoc.hs+37
src/Text/Pandoc.hs+37
@@ -1,3 +1,4 @@
1+{-# LANGUAGE ScopedTypeVariables, FlexibleInstances #-}
12{-
23Copyright (C) 2006-2010 John MacFarlane <jgm@berkeley.edu>
34
@@ -111,6 +112,7 @@ module Text.Pandoc
111112 -- * Miscellaneous
112113 , rtfEmbedImage
113114 , jsonFilter
115+ , ToJsonFilter(..)
114116 ) where
115117
116118import Text.Pandoc.Definition
@@ -205,8 +207,43 @@ writers = [("native" , writeNative)
205207 ,("asciidoc" , writeAsciiDoc)
206208 ]
207209
210+{-# DEPRECATED jsonFilter "Use toJsonFilter instead" #-}
208211-- | Converts a transformation on the Pandoc AST into a function
209212-- that reads and writes a JSON-encoded string. This is useful
210213-- for writing small scripts.
211214jsonFilter :: (Pandoc -> Pandoc) -> String -> String
212215jsonFilter f = encodeJSON . f . decodeJSON
216+
217+-- | 'toJsonFilter' convert a function into a filter that reads pandoc's json output
218+-- from stdin, transforms it, and writes it to stdout. Usage example:
219+--
220+-- > -- capitalize.hs
221+-- > -- compile with:
222+-- > -- ghc --make capitalize
223+-- > -- run with:
224+-- > -- pandoc -t json | ./capitalize | pandoc -f json
225+-- >
226+-- > import Text.Pandoc
227+-- > import Data.Char (toUpper)
228+-- >
229+-- > main :: IO ()
230+-- > main = toJsonFilter capitalizeStrings
231+-- >
232+-- > capitalizeStrings :: Inline -> Inline
233+-- > capitalizeStrings (Str s) = Str $ map toUpper s
234+-- > capitalizeStrings x = x
235+--
236+-- The function can be any type @(a -> a)@ or @(a -> IO a)@, where @a@
237+-- is an instance of 'Data'. So, for example, @a@ can be 'Pandoc',
238+-- 'Inline', 'Block', ['Inline'], ['Block'], 'Meta', 'ListNumberStyle',
239+-- 'Alignment', 'ListNumberDelim', 'QuoteType', etc. See 'Text.Pandoc.Definition'.
240+class ToJsonFilter a where
241+ toJsonFilter :: a -> IO ()
242+
243+instance (Data a) => ToJsonFilter (a -> a) where
244+ toJsonFilter f = getContents
245+ >>= putStr . encodeJSON . (bottomUp f :: Pandoc -> Pandoc) . decodeJSON
246+
247+instance (Data a) => ToJsonFilter (a -> IO a) where
248+ toJsonFilter f = getContents >>= (bottomUpM f :: Pandoc -> IO Pandoc) . decodeJSON
249+ >>= putStr . encodeJSON