git.iko.soy/typeable/pandoc
on master help sign in
Finish converting readers over.
commit52859b98632e991e94a3d37c0e0ae6c5d3b3fa34 authorJesse Rosenthal <jrosenthal@jhu.edu> dateThu 1 Dec 2016, 10:00 (10 years ago) parent5a02a81
2 files changed+21−8
pandoc.hs+9−8 src/Text/Pandoc/Class.hs+12
pandoc.hs+9−8
@@ -70,7 +70,6 @@ import Data.Yaml (decode)
7070import qualified Data.Yaml as Yaml
7171import qualified Data.Text as T
7272import Control.Applicative ((<|>))
73−import Text.Pandoc.Readers.Txt2Tags (getT2TMeta)
7473import Paths_pandoc (getDataDir)
7574import Text.Printf (printf)
7675#ifndef _WINDOWS
@@ -78,8 +77,7 @@ import System.Posix.Terminal (queryTerminal)
7877import System.Posix.IO (stdOutput)
7978#endif
8079import Control.Monad.Trans
81−import Text.Pandoc.Class (runIOorExplode, PandocMonad, PandocIO)
82−import qualified Text.Pandoc.Class as P
80+import Text.Pandoc.Class (runIOorExplode, withMediaBag, PandocIO)
8381
8482type Transform = Pandoc -> Pandoc
8583
@@ -916,7 +914,7 @@ options =
916914 map ("--" ++) longs
917915 let allopts = unwords (concatMap optnames options)
918916 UTF8.hPutStrLn stdout $ printf tpl allopts
919− (unwords (map fst readers))
917+ (unwords (map fst (readers :: [(String, Reader PandocIO)])))
920918 (unwords (map fst (writers :: [(String, Writer PandocIO)])))
921919 (unwords $ map fst highlightingStyles)
922920 ddir
@@ -926,7 +924,7 @@ options =
926924 , Option "" ["list-input-formats"]
927925 (NoArg
928926 (\_ -> do
929− let readers'names = sort (map fst readers)
927+ let readers'names = sort (map fst (readers :: [(String, Reader PandocIO)]))
930928 mapM_ (UTF8.hPutStrLn stdout) readers'names
931929 exitSuccess ))
932930 ""
@@ -1410,13 +1408,16 @@ convertWithOpts opts args = do
14101408 else return . Right
14111409
14121410 let sourceToDoc :: [FilePath] -> IO (Pandoc, MediaBag)
1413− sourceToDoc sources' = fmap handleError $
1411+ sourceToDoc sources' =
14141412 case reader of
14151413 StringReader r-> do
14161414 srcs <- convertTabs . intercalate "\n" <$> readSources sources'
14171415 doc <- handleIncludes' srcs
1418− either (return . Left) (\s -> fmap (,mempty) <$> r readerOpts s) doc
1419− ByteStringReader r -> readFiles sources' >>= r readerOpts
1416+ case doc of
1417+ Right doc' -> runIOorExplode $ withMediaBag $ r readerOpts doc'
1418+ Left e -> error $ show e
1419+ ByteStringReader r -> readFiles sources' >>=
1420+ (\bs -> runIOorExplode $ withMediaBag $ r readerOpts bs)
14201421
14211422 -- We parse first if (1) fileScope is set, (2), it's a binary
14221423 -- reader, or (3) we're reading JSON. This is easier to do of an AND
src/Text/Pandoc/Class.hs+12
@@ -44,6 +44,7 @@ module Text.Pandoc.Class ( PandocMonad(..)
4444 , runIO
4545 , runIOorExplode
4646 , runPure
47+ , withMediaBag
4748 ) where
4849
4950import Prelude hiding (readFile, fail)
@@ -115,6 +116,7 @@ class (Functor m, Applicative m, Monad m, MonadError PandocExecutionError m) =>
115116 -- to the record names, so I'd like to work out a better way to deal
116117 -- with it.
117118 setMediaBag :: MediaBag -> m ()
119+ getMediaBag :: m MediaBag
118120 insertMedia :: FilePath -> Maybe MimeType -> BL.ByteString -> m ()
119121 getInputFiles :: m (Maybe [FilePath])
120122 getOutputFile :: m (Maybe FilePath)
@@ -169,6 +171,9 @@ instance Default PandocEnvIO where
169171runIO :: PandocIO a -> IO (Either PandocExecutionError a)
170172runIO ma = flip evalStateT def $ flip runReaderT def $ runExceptT $ unPandocIO ma
171173
174+withMediaBag :: PandocMonad m => m a -> m (a, MediaBag)
175+withMediaBag ma = ((,)) <$> ma <*> getMediaBag
176+
172177runIOorExplode :: PandocIO a -> IO a
173178runIOorExplode ma = do
174179 eitherVal <- runIO ma
@@ -179,6 +184,10 @@ runIOorExplode ma = do
179184 Left (PandocParseError s) -> error $ "parse error" ++ s
180185 Left (PandocSomeError s) -> error s
181186
187+
188+
189+
190+
182191newtype PandocIO a = PandocIO {
183192 unPandocIO :: ExceptT PandocExecutionError (ReaderT PandocEnvIO (StateT PandocStateIO IO)) a
184193 } deriving ( MonadIO
@@ -225,6 +234,7 @@ instance PandocMonad PandocIO where
225234 -- Common functions
226235 setMediaBag mb =
227236 modify $ \st -> st{ioStMediaBag = mb}
237+ getMediaBag = gets ioStMediaBag
228238 insertMedia fp mime bs =
229239 modify $ \st -> st{ioStMediaBag = MB.insertMedia fp mime bs (ioStMediaBag st) }
230240 getInputFiles = asks ioEnvInputFiles
@@ -383,6 +393,8 @@ instance PandocMonad PandocPure where
383393 setMediaBag mb =
384394 modify $ \st -> st{stMediaBag = mb}
385395
396+ getMediaBag = gets stMediaBag
397+
386398 insertMedia fp mime bs =
387399 modify $ \st -> st{stMediaBag = MB.insertMedia fp mime bs (stMediaBag st) }
388400