git.iko.soy/typeable/pandoc
on master help sign in
More refactoring of pandoc.hs for clarity.
commit3549e171bb6cfdb4cd65772777f6225544f12bfe authorJohn MacFarlane <jgm@berkeley.edu> dateSat 10 Dec 2016, 12:07 (10 years ago) parentdc4f659
1 file changed+21−28
pandoc.hs+21−28
pandoc.hs+21−28
@@ -1277,12 +1277,12 @@ convertWithOpts opts args = do
12771277 "an output file with .pdf extension " ++
12781278 "(pandoc -t latex -o filename.pdf)."
12791279 else e
1280− Right w -> return w
1280+ Right w -> return (w :: Writer PandocIO)
12811281
12821282 -- TODO: we have to get the input and the output into the state for
1283− -- the sake of the text2tags reader.
1283+ -- the sake of the text2tags reader.
12841284 reader <- case getReader readerName' of
1285− Right r -> return r
1285+ Right r -> return (r :: Reader PandocIO)
12861286 Left e -> err 7 e'
12871287 where e' = case readerName' of
12881288 "pdf" -> e ++
@@ -1433,12 +1433,9 @@ convertWithOpts opts args = do
14331433 readSources srcs = convertTabs . intercalate "\n" <$>
14341434 mapM readSource srcs
14351435
1436− let readFiles :: MonadIO m => [FilePath] -> m B.ByteString
1437− readFiles [] = error "Cannot read archive from stdin"
1438− readFiles [x] = liftIO $ B.readFile x
1439− readFiles (x:xs) = do
1440− mapM_ (warn . ("Ignoring: " ++)) xs
1441− liftIO $ B.readFile x
1436+ readFile' :: MonadIO m => FilePath -> m B.ByteString
1437+ readFile' "-" = liftIO $ B.getContents
1438+ readFile' f = liftIO $ B.readFile f
14421439
14431440 let runIO' :: PandocIO a -> IO a
14441441 runIO' f = do
@@ -1453,26 +1450,22 @@ convertWithOpts opts args = do
14531450 err 3 "Failing because there were warnings."
14541451 return res
14551452
1456− let sourceToDoc :: [FilePath] -> IO (Pandoc, MediaBag)
1457− sourceToDoc sources' = runIO' $
1453+ let sourceToDoc :: [FilePath] -> PandocIO (Pandoc, MediaBag)
1454+ sourceToDoc sources' =
14581455 case reader of
1459− StringReader r -> readSources sources' >>=
1460− withMediaBag . r readerOpts
1461− ByteStringReader r -> readFiles sources' >>=
1462− withMediaBag . r readerOpts
1463−
1464− -- We parse first if (1) fileScope is set, (2), it's a binary
1465− -- reader, or (3) we're reading JSON. This is easier to do of an AND
1466− -- of negatives as opposed to an OR of positives, so we do default
1467− -- parsing if it's a StringReader AND (fileScope is not set AND it's not
1468− -- a JSON reader).
1469− (doc, media) <- case reader of
1470− (StringReader _) | not fileScope && readerName' /= "json" ->
1471− sourceToDoc sources
1472− _ | null sources -> sourceToDoc sources
1473− _ -> do pairs <- mapM (\s -> sourceToDoc [s]) sources
1474− return (mconcat $ map fst pairs, mconcat $ map snd pairs)
1475−
1456+ StringReader r
1457+ | fileScope || readerName' == "json" -> do
1458+ pairs <- mapM
1459+ (readSource >=> withMediaBag . r readerOpts) sources
1460+ return (mconcat (map fst pairs), mconcat (map snd pairs))
1461+ | otherwise ->
1462+ readSources sources' >>= withMediaBag . r readerOpts
1463+ ByteStringReader r -> do
1464+ pairs <- mapM (readFile' >=>
1465+ withMediaBag . r readerOpts) sources
1466+ return (mconcat (map fst pairs), mconcat (map snd pairs))
1467+
1468+ (doc, media) <- runIO' $ sourceToDoc sources
14761469
14771470 doc' <- (maybe return (extractMedia media) mbExtractMedia >=>
14781471 adjustMetadata metadata >=>