Skip to content

Commit

Permalink
LaTeX reader: Avoid include loops.
Browse files Browse the repository at this point in the history
  • Loading branch information
jgm committed Nov 1, 2012
1 parent 0f24816 commit a6e5623
Showing 1 changed file with 17 additions and 8 deletions.
25 changes: 17 additions & 8 deletions src/Text/Pandoc/Readers/LaTeX.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit a6e5623

Please sign in to comment.