git.iko.soy/typeable/pandoc
on master help sign in
Pass writename as argument to filters.

This way filters can figure out what the target format is and react appropriately. Example: #!/usr/bin/env runghc import Text.Pandoc.JSON import Data.Char main = toJSONFilter cap where cap (Just "html") (Str xs) = Str $ map toUpper xs cap _ x = x This capitalizes text only for html output.

commit99bb066bb925134b506d39c8d6694fe81337d9c1 authorJohn MacFarlane <jgm@berkeley.edu> dateThu 8 Aug 2013, 15:15 (13 years ago) parent83f2631
3 files changed+13−11
pandoc.cabal+1−1 pandoc.hs+11−6 src/Text/Pandoc.hs+1−4
pandoc.cabal+1−1
@@ -352,8 +352,8 @@ Executable pandoc
352352 bytestring >= 0.9 && < 0.11,
353353 extensible-exceptions >= 0.1 && < 0.2,
354354 highlighting-kate >= 0.5.5 && < 0.6,
355+ aeson >= 0.6 && < 0.7,
355356 HTTP >= 4000.0.5 && < 4000.3,
356− process >= 1 && < 1.2,
357357 citeproc-hs >= 0.3.7 && < 0.4
358358 Ghc-Options: -rtsopts -with-rtsopts=-K16m -Wall -fno-warn-unused-do-bind
359359 Ghc-Prof-Options: -auto-all -caf-all -rtsopts -with-rtsopts=-K16m
pandoc.hs+11−6
@@ -37,13 +37,13 @@ import Text.Pandoc.Shared ( tabFilter, readDataFileUTF8, safeRead,
3737 headerShift, normalize, err, warn )
3838import Text.Pandoc.XML ( toEntities, fromEntities )
3939import Text.Pandoc.SelfContained ( makeSelfContained )
40+import Text.Pandoc.Process (pipeProcess)
4041import Text.Highlighting.Kate ( languages, Style, tango, pygments,
4142 espresso, zenburn, kate, haddock, monochrome )
4243import System.Environment ( getArgs, getProgName )
4344import System.Exit ( exitWith, ExitCode (..) )
4445import System.FilePath
4546import System.Console.GetOpt
46−import System.Process (readProcess)
4747import Data.Char ( toLower )
4848import Data.List ( intercalate, isPrefixOf, sort )
4949import System.Directory ( getAppUserDataDirectory, doesFileExist, findExecutable )
@@ -59,6 +59,7 @@ import Network.HTTP (simpleHTTP, mkRequest, getResponseBody, RequestMethod(..))
5959import Network.URI (parseURI, isURI, URI(..))
6060import qualified Data.ByteString.Lazy as B
6161import Text.CSL.Reference (Reference(..))
62+import Data.Aeson (eitherDecode', encode)
6263
6364copyrightMessage :: String
6465copyrightMessage = "\nCopyright (C) 2006-2013 John MacFarlane\n" ++
@@ -88,9 +89,13 @@ wrapWords indent c = wrap' (c - indent) (c - indent)
8889isTextFormat :: String -> Bool
8990isTextFormat s = takeWhile (`notElem` "+-") s `notElem` ["odt","docx","epub","epub3"]
9091
91−externalFilter :: FilePath -> Pandoc -> IO Pandoc
92−externalFilter f d = E.catch
93− (readJSON def `fmap` readProcess f [] (writeJSON def d))
92+externalFilter :: FilePath -> [String] -> Pandoc -> IO Pandoc
93+externalFilter f args' d = E.catch
94+ (do (exitcode, outbs, errbs) <- pipeProcess Nothing f args' $ encode d
95+ case exitcode of
96+ ExitSuccess -> return $ either error id $ eitherDecode' outbs
97+ ExitFailure _ -> err 83 $ "Error running filter `" ++ UTF8.toStringLazy outbs ++
98+ UTF8.toStringLazy errbs ++ "'")
9499 (\e -> let _ = (e :: E.SomeException)
95100 in err 83 $ "Error running filter `" ++ f ++ "'")
96101
@@ -132,7 +137,7 @@ data Opt = Opt
132137 , optReferenceLinks :: Bool -- ^ Use reference links in writing markdown, rst
133138 , optWrapText :: Bool -- ^ Wrap text
134139 , optColumns :: Int -- ^ Line length in characters
135− , optPlugins :: [Pandoc -> IO Pandoc] -- ^ Plugins to apply
140+ , optPlugins :: [[String] -> Pandoc -> IO Pandoc] -- ^ Plugins to apply
136141 , optEmailObfuscation :: ObfuscationMethod
137142 , optIdentifierPrefix :: String
138143 , optIndentedCodeClasses :: [String] -- ^ Default classes for indented code blocks
@@ -1115,7 +1120,7 @@ main = do
11151120 reader readerOpts
11161121
11171122 let doc0 = foldr ($) doc transforms
1118− doc1 <- foldrM ($) doc0 plugins
1123+ doc1 <- foldrM ($) doc0 $ map ($ [writerName']) plugins
11191124
11201125 let writeBinary :: B.ByteString -> IO ()
11211126 writeBinary = B.writeFile (UTF8.encodePath outputFile)
src/Text/Pandoc.hs+1−4
@@ -309,11 +309,8 @@ class ToJSONFilter a => ToJsonFilter a
309309 toJsonFilter = toJSONFilter
310310
311311readJSON :: ReaderOptions -> String -> Pandoc
312−readJSON _ = checkJSON . eitherDecode' . UTF8.fromStringLazy
312+readJSON _ = either error id . eitherDecode' . UTF8.fromStringLazy
313313
314314writeJSON :: WriterOptions -> Pandoc -> String
315315writeJSON _ = UTF8.toStringLazy . encode
316316
317−checkJSON :: Either String a -> a
318−checkJSON (Right x) = x
319−checkJSON (Left e) = error e