git.iko.soy/typeable/pandoc
on master help sign in
LaTeX reader: Avoid include loops.
commita6e56235d5dbb06532ca40b9a2e68f6dbd68a2d6 authorJohn MacFarlane <jgm@berkeley.edu> dateThu 1 Nov 2012, 13:10 (14 years ago) parent0f24816
1 file changed+17−8
src/Text/Pandoc/Readers/LaTeX.hs+17−8
src/Text/Pandoc/Readers/LaTeX.hs+17−8
@@ -681,16 +681,25 @@ rawEnv name = do
681681
682682-- | Replace "include" commands with file contents.
683683handleIncludes :: String -> IO String
684−handleIncludes [] = return []
685−handleIncludes ('\\':xs) =
684+handleIncludes = handleIncludes' []
685+
686+-- parents parameter prevents infinite include loops
687+handleIncludes' :: [FilePath] -> String -> IO String
688+handleIncludes' _ [] = return []
689+handleIncludes' parents ('\\':xs) =
686690 case runParser include defaultParserState "input" ('\\':xs) of
687− Right (fs, rest) -> do yss <- mapM readTeXFile fs
688− handleIncludes $ intercalate "\n" yss ++ rest
691+ Right (fs, rest) -> do yss <- mapM (\f -> if f `elem` parents
692+ then "" <$ warn ("Include file loop in '"
693+ ++ f ++ "'.")
694+ else readTeXFile f >>=
695+ handleIncludes' (f:parents)) fs
696+ rest' <- handleIncludes' parents rest
697+ return $ intercalate "\n" yss ++ rest'
689698 _ -> case runParser (verbCmd <|> verbatimEnv) defaultParserState
690− "input" ('\\':xs) of
691− Right (r, rest) -> (r ++) `fmap` handleIncludes rest
692− _ -> ('\\':) `fmap` handleIncludes xs
693−handleIncludes (x:xs) = (x:) `fmap` handleIncludes xs
699+ "input" ('\\':xs) of
700+ Right (r, rest) -> (r ++) `fmap` handleIncludes' parents rest
701+ _ -> ('\\':) `fmap` handleIncludes' parents xs
702+handleIncludes' parents (x:xs) = (x:) `fmap` handleIncludes' parents xs
694703
695704readTeXFile :: FilePath -> IO String
696705readTeXFile f = do