git.iko.soy/typeable/pandoc
on master help sign in
LaTeX reader: Improved handling of included files.

* `\input` now works, as well as `\include`. * TEXINPUTS is used. * We now look recursively into included files for more included files.

commit6dff7dccaa08a394231fc99f29723a939c5e1306 authorJohn MacFarlane <fiddlosopher@gmail.com> dateThu 1 Nov 2012, 09:42 (14 years ago) parent06300e5
1 file changed+24−11
src/Text/Pandoc/Readers/LaTeX.hs+24−11
src/Text/Pandoc/Readers/LaTeX.hs+24−11
@@ -1,3 +1,4 @@
1+{-# LANGUAGE ScopedTypeVariables #-}
12{-
23Copyright (C) 2006-2012 John MacFarlane <jgm@berkeley.edu>
34
@@ -45,7 +46,8 @@ import Text.Pandoc.Builder
4546import Data.Char (isLetter, isPunctuation, isSpace)
4647import Control.Applicative
4748import Data.Monoid
48−import System.FilePath (replaceExtension)
49+import System.Environment (getEnv)
50+import System.FilePath (replaceExtension, (</>))
4951import Data.List (intercalate)
5052import qualified Data.Map as M
5153import qualified Control.Exception as E
@@ -682,27 +684,38 @@ handleIncludes :: String -> IO String
682684handleIncludes [] = return []
683685handleIncludes ('\\':xs) =
684686 case runParser include defaultParserState "input" ('\\':xs) of
685− Right (fs, rest) -> do let getfile f = E.catch (UTF8.readFile f)
686− (\e -> let _ = (e :: E.SomeException)
687− in return "")
688− yss <- mapM getfile fs
689− (intercalate "\n" yss ++) `fmap`
690− handleIncludes rest
687+ Right (fs, rest) -> do yss <- mapM readTeXFile fs
688+ handleIncludes $ intercalate "\n" yss ++ rest
691689 _ -> case runParser (verbCmd <|> verbatimEnv) defaultParserState
692690 "input" ('\\':xs) of
693691 Right (r, rest) -> (r ++) `fmap` handleIncludes rest
694692 _ -> ('\\':) `fmap` handleIncludes xs
695693handleIncludes (x:xs) = (x:) `fmap` handleIncludes xs
696694
695+readTeXFile :: FilePath -> IO String
696+readTeXFile f = do
697+ texinputs <- E.catch (getEnv "TEXINPUTS") $ \(_ :: E.SomeException) ->
698+ return "."
699+ let ds = splitBy (==':') texinputs
700+ readFileFromDirs ds f
701+
702+readFileFromDirs :: [FilePath] -> FilePath -> IO String
703+readFileFromDirs [] _ = return ""
704+readFileFromDirs (d:ds) f =
705+ E.catch (UTF8.readFile $ d </> f) $ \(_ :: E.SomeException) ->
706+ readFileFromDirs ds f
707+
697708include :: LP ([FilePath], String)
698709include = do
699− name <- controlSeq "include" <|> controlSeq "usepackage"
710+ name <- controlSeq "include"
711+ <|> controlSeq "input"
712+ <|> controlSeq "usepackage"
700713 skipopts
701714 fs <- (splitBy (==',')) <$> braced
702715 rest <- getInput
703− let fs' = if name == "include"
704− then map (flip replaceExtension ".tex") fs
705− else map (flip replaceExtension ".sty") fs
716+ let fs' = if name == "usepackage"
717+ then map (flip replaceExtension ".sty") fs
718+ else map (flip replaceExtension ".tex") fs
706719 return (fs', rest)
707720
708721verbCmd :: LP (String, String)