summaryrefslogtreecommitdiff
path: root/src/Text
diff options
context:
space:
mode:
authorJohn MacFarlane <jgm@berkeley.edu>2017-02-07 22:33:05 +0100
committerJohn MacFarlane <jgm@berkeley.edu>2017-02-07 22:33:05 +0100
commit857d35fce4e364039092a9da37b93b67bd566bb6 (patch)
tree19a8e160af128f7c92fdc5549a4d824b0580265f /src/Text
parent1ecc48e9f9b027e9b1226fa3a43b52ae8c7004bf (diff)
Refactored some files formerly in LaTeX reader.
* Export readFileFromDirs from Class. * Export insertIncludedFile from Parsing. Simplified code in LaTeX/RST readers.
Diffstat (limited to 'src/Text')
-rw-r--r--src/Text/Pandoc/Class.hs8
-rw-r--r--src/Text/Pandoc/Parsing.hs23
-rw-r--r--src/Text/Pandoc/Readers/LaTeX.hs23
-rw-r--r--src/Text/Pandoc/Readers/RST.hs16
4 files changed, 33 insertions, 37 deletions
diff --git a/src/Text/Pandoc/Class.hs b/src/Text/Pandoc/Class.hs
index f75db4b3a..e6913566f 100644
--- a/src/Text/Pandoc/Class.hs
+++ b/src/Text/Pandoc/Class.hs
@@ -40,11 +40,11 @@ module Text.Pandoc.Class ( PandocMonad(..)
, modifyPureState
, getPOSIXTime
, getZonedTime
+ , readFileFromDirs
, warning
, warningWithPos
, report
, getLog
- , readFileFromDirs
, setVerbosity
, getMediaBag
, setMediaBag
@@ -74,7 +74,7 @@ import qualified Text.Pandoc.Shared as IO ( readDataFile
import qualified Text.Pandoc.UTF8 as UTF8
import Text.Pandoc.Compat.Time (UTCTime)
import Text.Pandoc.Options (Verbosity(..))
-import Text.Pandoc.Parsing (ParserT, SourcePos)
+import Text.Parsec (ParsecT, SourcePos)
import qualified Text.Pandoc.Compat.Time as IO (getCurrentTime)
import Text.Pandoc.MIME (MimeType, getMimeType)
import Data.Time.Clock.POSIX ( utcTimeToPOSIXSeconds
@@ -151,7 +151,7 @@ warning msg = report WARNING msg
warningWithPos :: PandocMonad m
=> SourcePos
-> String
- -> ParserT s st m ()
+ -> ParsecT s st m ()
warningWithPos pos msg = lift $ warning $ msg ++ " " ++ show pos
report :: PandocMonad m => Verbosity -> String -> m ()
@@ -513,7 +513,7 @@ instance PandocMonad PandocPure where
logOutput _level _msg = return ()
-instance PandocMonad m => PandocMonad (ParserT s st m) where
+instance PandocMonad m => PandocMonad (ParsecT s st m) where
lookupEnv = lift . lookupEnv
getCurrentTime = lift getCurrentTime
getCurrentTimeZone = lift getCurrentTimeZone
diff --git a/src/Text/Pandoc/Parsing.hs b/src/Text/Pandoc/Parsing.hs
index 8ffd0758e..f45ac2f71 100644
--- a/src/Text/Pandoc/Parsing.hs
+++ b/src/Text/Pandoc/Parsing.hs
@@ -112,6 +112,7 @@ module Text.Pandoc.Parsing ( anyLine,
token,
(<+?>),
extractIdClass,
+ insertIncludedFile,
-- * Re-exports from Text.Pandoc.Parsec
Stream,
runParser,
@@ -187,6 +188,7 @@ import Text.TeXMath.Readers.TeX.Macros (applyMacros, Macro,
import Text.HTML.TagSoup.Entity ( lookupEntity )
import Text.Pandoc.Asciify (toAsciiChar)
import Data.Monoid ((<>))
+import Text.Pandoc.Class (PandocMonad, readFileFromDirs)
import Data.Default
import qualified Data.Set as Set
import Control.Monad.Reader
@@ -194,6 +196,7 @@ import Control.Monad.Identity
import Data.Maybe (catMaybes)
import Text.Pandoc.Error
+import Control.Monad.Except
type Parser t s = Parsec t s
@@ -1274,3 +1277,23 @@ extractIdClass (ident, cls, kvs) = (ident', cls', kvs')
Just cl -> words cl
Nothing -> cls
kvs' = filter (\(k,_) -> k /= "id" || k /= "class") kvs
+
+insertIncludedFile :: PandocMonad m
+ => ParserT String ParserState m Blocks
+ -> [FilePath] -> FilePath
+ -> ParserT String ParserState m Blocks
+insertIncludedFile blocks 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 <- readFileFromDirs dirs f
+ setPosition $ newPos f 1 1
+ setInput contents
+ bs <- blocks
+ setInput oldInput
+ setPosition oldPos
+ updateState $ \s -> s{ stateContainers = tail $ stateContainers s }
+ return bs
diff --git a/src/Text/Pandoc/Readers/LaTeX.hs b/src/Text/Pandoc/Readers/LaTeX.hs
index 0cbd3a814..99c1056c2 100644
--- a/src/Text/Pandoc/Readers/LaTeX.hs
+++ b/src/Text/Pandoc/Readers/LaTeX.hs
@@ -50,10 +50,8 @@ 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)
-import Text.Pandoc.Class (PandocMonad, PandocPure, lookupEnv,
- warningWithPos, readFileFromDirs)
+import Text.Pandoc.Class (PandocMonad, PandocPure, lookupEnv, warningWithPos )
-- | Parse LaTeX from string and return 'Pandoc' document.
readLaTeX :: PandocMonad m
@@ -959,24 +957,7 @@ include = do
then map (maybeAddExtension ".sty") fs
else map (maybeAddExtension ".tex") fs
dirs <- (splitBy (==':') . fromMaybe ".") <$> lookupEnv "TEXINPUTS"
- mconcat <$> mapM (insertIncludedFile dirs) fs'
-
-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 <- readFileFromDirs dirs f
- setPosition $ newPos f 1 1
- setInput contents
- bs <- blocks
- setInput oldInput
- setPosition oldPos
- updateState $ \s -> s{ stateContainers = tail $ stateContainers s }
- return bs
+ mconcat <$> mapM (insertIncludedFile blocks dirs) fs'
----
diff --git a/src/Text/Pandoc/Readers/RST.hs b/src/Text/Pandoc/Readers/RST.hs
index c9868c11f..85db52e02 100644
--- a/src/Text/Pandoc/Readers/RST.hs
+++ b/src/Text/Pandoc/Readers/RST.hs
@@ -37,7 +37,6 @@ import Text.Pandoc.Shared
import Text.Pandoc.Parsing
import Text.Pandoc.Options
import Text.Pandoc.Error
-import qualified Text.Pandoc.UTF8 as UTF8
import Control.Monad ( when, liftM, guard, mzero )
import Data.List ( findIndex, intercalate, isInfixOf,
transpose, sort, deleteFirstsBy, isSuffixOf , nub, union)
@@ -49,8 +48,9 @@ import qualified Text.Pandoc.Builder as B
import Data.Sequence (viewr, ViewR(..))
import Data.Char (toLower, isHexDigit, isSpace)
import Data.Monoid ((<>))
-import Control.Monad.Except (throwError, catchError)
-import Text.Pandoc.Class (PandocMonad, warning, readFileLazy, warningWithPos)
+import Control.Monad.Except (throwError)
+import Text.Pandoc.Class (PandocMonad, warning, readFileFromDirs,
+ warningWithPos)
-- | Parse reStructuredText string and return Pandoc document.
readRST :: PandocMonad m
@@ -421,12 +421,7 @@ include = try $ do
when (f `elem` containers) $
throwError $ PandocParseError $ "Include file loop at " ++ show oldPos
updateState $ \s -> s{ stateContainers = f : stateContainers s }
- res <- readFileLazy' f
- contents <- case res of
- Right x -> return x
- Left _e -> do
- warning $ "Could not read include file " ++ f ++ "."
- return ""
+ contents <- readFileFromDirs ["."] f
let contentLines = lines contents
let numLines = length contentLines
let startLine' = case startLine of
@@ -470,9 +465,6 @@ include = try $ do
tail $ stateContainers s }
return bs
-readFileLazy' :: PandocMonad m => FilePath -> m (Either PandocError String)
-readFileLazy' f = catchError ((Right . UTF8.toStringLazy) <$> readFileLazy f) $
- \(e :: PandocError) -> return (Left e)
--
-- list blocks