diff options
author | John MacFarlane <jgm@berkeley.edu> | 2012-11-01 13:10:46 -0700 |
---|---|---|
committer | John MacFarlane <jgm@berkeley.edu> | 2012-11-01 13:49:20 -0700 |
commit | a6e56235d5dbb06532ca40b9a2e68f6dbd68a2d6 (patch) | |
tree | 2214af9d590fdccb90d272e45c23f6f05a8da9a3 /src/Text | |
parent | 0f248162912bf86c78643749c75ad3b7d3ba86f4 (diff) | |
download | pandoc-a6e56235d5dbb06532ca40b9a2e68f6dbd68a2d6.tar.gz |
LaTeX reader: Avoid include loops.
Diffstat (limited to 'src/Text')
-rw-r--r-- | src/Text/Pandoc/Readers/LaTeX.hs | 25 |
1 files changed, 17 insertions, 8 deletions
diff --git a/src/Text/Pandoc/Readers/LaTeX.hs b/src/Text/Pandoc/Readers/LaTeX.hs index 22d9b237a..763765edf 100644 --- a/src/Text/Pandoc/Readers/LaTeX.hs +++ b/src/Text/Pandoc/Readers/LaTeX.hs @@ -681,16 +681,25 @@ rawEnv name = do -- | Replace "include" commands with file contents. handleIncludes :: String -> IO String -handleIncludes [] = return [] -handleIncludes ('\\':xs) = +handleIncludes = handleIncludes' [] + +-- parents parameter prevents infinite include loops +handleIncludes' :: [FilePath] -> String -> IO String +handleIncludes' _ [] = return [] +handleIncludes' parents ('\\':xs) = case runParser include defaultParserState "input" ('\\':xs) of - Right (fs, rest) -> do yss <- mapM readTeXFile fs - handleIncludes $ intercalate "\n" yss ++ rest + Right (fs, rest) -> do yss <- mapM (\f -> if f `elem` parents + then "" <$ warn ("Include file loop in '" + ++ f ++ "'.") + else readTeXFile f >>= + handleIncludes' (f:parents)) fs + rest' <- handleIncludes' parents rest + return $ intercalate "\n" yss ++ rest' _ -> case runParser (verbCmd <|> verbatimEnv) defaultParserState - "input" ('\\':xs) of - Right (r, rest) -> (r ++) `fmap` handleIncludes rest - _ -> ('\\':) `fmap` handleIncludes xs -handleIncludes (x:xs) = (x:) `fmap` handleIncludes xs + "input" ('\\':xs) of + Right (r, rest) -> (r ++) `fmap` handleIncludes' parents rest + _ -> ('\\':) `fmap` handleIncludes' parents xs +handleIncludes' parents (x:xs) = (x:) `fmap` handleIncludes' parents xs readTeXFile :: FilePath -> IO String readTeXFile f = do |