aboutsummaryrefslogtreecommitdiff
path: root/src/Text
diff options
context:
space:
mode:
authorJohn MacFarlane <jgm@berkeley.edu>2017-02-07 21:42:35 +0100
committerJohn MacFarlane <jgm@berkeley.edu>2017-02-07 21:42:35 +0100
commit1ecc48e9f9b027e9b1226fa3a43b52ae8c7004bf (patch)
treeae9e58d00503c319b1048e9ff24b55aa671f5bc1 /src/Text
parent9db98550dfc39d31072e43ac066154a483865fc4 (diff)
downloadpandoc-1ecc48e9f9b027e9b1226fa3a43b52ae8c7004bf.tar.gz
Moved readFileFromDirs to Text.Pandoc.Class.
This can be used in several different modules, not just LaTeX reader.
Diffstat (limited to 'src/Text')
-rw-r--r--src/Text/Pandoc/Class.hs10
-rw-r--r--src/Text/Pandoc/Parsing.hs6
-rw-r--r--src/Text/Pandoc/Readers/LaTeX.hs38
3 files changed, 22 insertions, 32 deletions
diff --git a/src/Text/Pandoc/Class.hs b/src/Text/Pandoc/Class.hs
index 348da71ba..f75db4b3a 100644
--- a/src/Text/Pandoc/Class.hs
+++ b/src/Text/Pandoc/Class.hs
@@ -44,6 +44,7 @@ module Text.Pandoc.Class ( PandocMonad(..)
, warningWithPos
, report
, getLog
+ , readFileFromDirs
, setVerbosity
, getMediaBag
, setMediaBag
@@ -206,6 +207,15 @@ getZonedTime = do
tz <- getCurrentTimeZone
return $ utcToZonedTime tz t
+-- | Read file, checking in any number of directories.
+readFileFromDirs :: PandocMonad m => [FilePath] -> FilePath -> m String
+readFileFromDirs [] f = do
+ warning $ "Could not load include file " ++ f ++ ", skipping."
+ return ""
+readFileFromDirs (d:ds) f = catchError
+ (UTF8.toStringLazy <$> readFileLazy (d </> f))
+ (\_ -> readFileFromDirs ds f)
+
--
newtype DeferredMediaPath = DeferredMediaPath {unDefer :: String}
diff --git a/src/Text/Pandoc/Parsing.hs b/src/Text/Pandoc/Parsing.hs
index 5e9ff7fd1..8ffd0758e 100644
--- a/src/Text/Pandoc/Parsing.hs
+++ b/src/Text/Pandoc/Parsing.hs
@@ -110,6 +110,8 @@ module Text.Pandoc.Parsing ( anyLine,
askF,
asksF,
token,
+ (<+?>),
+ extractIdClass,
-- * Re-exports from Text.Pandoc.Parsec
Stream,
runParser,
@@ -163,9 +165,7 @@ module Text.Pandoc.Parsing ( anyLine,
sourceLine,
setSourceColumn,
setSourceLine,
- newPos,
- (<+?>),
- extractIdClass
+ newPos
)
where
diff --git a/src/Text/Pandoc/Readers/LaTeX.hs b/src/Text/Pandoc/Readers/LaTeX.hs
index 5950ec852..0cbd3a814 100644
--- a/src/Text/Pandoc/Readers/LaTeX.hs
+++ b/src/Text/Pandoc/Readers/LaTeX.hs
@@ -40,22 +40,20 @@ import Text.Pandoc.Shared
import Text.Pandoc.Options
import Text.Pandoc.Parsing hiding ((<|>), many, optional, space,
mathDisplay, mathInline)
-import qualified Text.Pandoc.UTF8 as UTF8
import Data.Char ( chr, ord, isLetter, isAlphaNum )
-import Control.Monad.Trans (lift)
import Control.Monad
import Text.Pandoc.Builder
import Control.Applicative ((<|>), many, optional)
import Data.Maybe (fromMaybe, maybeToList)
-import System.FilePath (replaceExtension, (</>), takeExtension, addExtension)
+import System.FilePath (replaceExtension, takeExtension, addExtension)
import Data.List (intercalate)
import qualified Data.Map as M
import Text.Pandoc.Highlighting (fromListingsLanguage)
import Text.Pandoc.ImageSize (numUnit, showFl)
import Text.Pandoc.Error
-import Control.Monad.Except (throwError, catchError)
-import Text.Pandoc.Class (PandocMonad, PandocPure, lookupEnv, readFileLazy,
- warning, warningWithPos)
+import Control.Monad.Except (throwError)
+import Text.Pandoc.Class (PandocMonad, PandocPure, lookupEnv,
+ warningWithPos, readFileFromDirs)
-- | Parse LaTeX from string and return 'Pandoc' document.
readLaTeX :: PandocMonad m
@@ -960,17 +958,18 @@ include = do
return $ if name == "usepackage"
then map (maybeAddExtension ".sty") fs
else map (maybeAddExtension ".tex") fs
- mconcat <$> mapM insertIncludedFile fs'
+ dirs <- (splitBy (==':') . fromMaybe ".") <$> lookupEnv "TEXINPUTS"
+ mconcat <$> mapM (insertIncludedFile dirs) fs'
-insertIncludedFile :: PandocMonad m => FilePath -> LP m Blocks
-insertIncludedFile f = do
+insertIncludedFile :: PandocMonad m => [FilePath] -> FilePath -> LP m Blocks
+insertIncludedFile dirs f = do
oldPos <- getPosition
oldInput <- getInput
containers <- stateContainers <$> getState
when (f `elem` containers) $
throwError $ PandocParseError $ "Include file loop at " ++ show oldPos
updateState $ \s -> s{ stateContainers = f : stateContainers s }
- contents <- lift $ readTeXFile f
+ contents <- readFileFromDirs dirs f
setPosition $ newPos f 1 1
setInput contents
bs <- blocks
@@ -979,25 +978,6 @@ insertIncludedFile f = do
updateState $ \s -> s{ stateContainers = tail $ stateContainers s }
return bs
-readTeXFile :: PandocMonad m => FilePath -> m String
-readTeXFile f = do
- texinputs <- fromMaybe "." <$> lookupEnv "TEXINPUTS"
- readFileFromDirs (splitBy (==':') texinputs) f
-
-readFileFromDirs :: PandocMonad m => [FilePath] -> FilePath -> m String
-readFileFromDirs [] f = do
- warning $ "Could not load include file " ++ f ++ ", skipping."
- return ""
-readFileFromDirs (d:ds) f = do
- res <- readFileLazy' (d </> f)
- case res of
- Right s -> return s
- Left _ -> readFileFromDirs ds f
-
-readFileLazy' :: PandocMonad m => FilePath -> m (Either PandocError String)
-readFileLazy' f = catchError ((Right . UTF8.toStringLazy) <$> readFileLazy f) $
- \(e :: PandocError) -> return (Left e)
-
----
keyval :: PandocMonad m => LP m (String, String)