@@ -215,6 +215,7 @@ data Opt = Opt
215215 , optExtractMedia :: Maybe FilePath -- ^ Path to extract embedded media
216216 , optTrace :: Bool -- ^ Print debug information
217217 , optTrackChanges :: TrackChanges -- ^ Accept or reject MS Word track-changes.
218+ , optFileScope :: Bool -- ^ Parse input files before combining
218219 , optKaTeXStylesheet :: Maybe String -- ^ Path to stylesheet for KaTeX
219220 , optKaTeXJS :: Maybe String -- ^ Path to js file for KaTeX
220221 }
@@ -278,6 +279,7 @@ defaultOpts = Opt
278279 , optExtractMedia = Nothing
279280 , optTrace = False
280281 , optTrackChanges = AcceptChanges
282+ , optFileScope = False
281283 , optKaTeXStylesheet = Nothing
282284 , optKaTeXJS = Nothing
283285 }
@@ -387,6 +389,11 @@ options =
387389 "accept|reject|all")
388390 "" -- "Accepting or reject MS Word track-changes.""
389391
392+ , Option "" ["file-scope"]
393+ (NoArg
394+ (\opt -> return opt { optFileScope = True }))
395+ "" -- "Parse input files before combining"
396+
390397 , Option "" ["extract-media"]
391398 (ReqArg
392399 (\arg opt ->
@@ -1117,6 +1124,7 @@ convertWithOpts opts args = do
11171124 , optExtractMedia = mbExtractMedia
11181125 , optTrace = trace
11191126 , optTrackChanges = trackChanges
1127+ , optFileScope = fileScope
11201128 , optKaTeXStylesheet = katexStylesheet
11211129 , optKaTeXJS = katexJS
11221130 } = opts
@@ -1269,6 +1277,7 @@ convertWithOpts opts args = do
12691277 , readerDefaultImageExtension = defaultImageExtension
12701278 , readerTrace = trace
12711279 , readerTrackChanges = trackChanges
1280+ , readerFileScope = fileScope
12721281 }
12731282
12741283 when (not (isTextFormat format) && outputFile == "-") $
@@ -1301,13 +1310,23 @@ convertWithOpts opts args = do
13011310 then handleIncludes
13021311 else return . Right
13031312
1304− (doc, media) <- fmap handleError $
1305− case reader of
1313+ let sourceToDoc :: [FilePath] -> IO (Pandoc, MediaBag)
1314+ sourceToDoc sources' = fmap handleError $
1315+ case reader of
13061316 StringReader r-> do
1307− srcs <- convertTabs . intercalate "\n" <$> readSources sources
1317+ srcs <- convertTabs . intercalate "\n" <$> readSources sources'
13081318 doc <- handleIncludes' srcs
13091319 either (return . Left) (\s -> fmap (,mempty) <$> r readerOpts s) doc
1310− ByteStringReader r -> readFiles sources >>= r readerOpts
1320+ ByteStringReader r -> readFiles sources' >>= r readerOpts
1321+
1322+ -- We parse first if fileScope is set OR if the reader is a
1323+ -- BSReader. So, if it's a StringReader AND not fileScope, we
1324+ -- don't.
1325+ (doc, media) <- case reader of
1326+ (StringReader _) | not fileScope -> sourceToDoc sources
1327+ _ -> do
1328+ pairs <- mapM (\s -> sourceToDoc [s]) sources
1329+ return (mconcat $ map fst pairs, mconcat $ map snd pairs)
13111330
13121331 let writerOptions = def { writerStandalone = standalone',
13131332 writerTemplate = templ,