Refactored some files formerly in LaTeX reader.
* Export readFileFromDirs from Class. * Export insertIncludedFile from Parsing. Simplified code in LaTeX/RST readers.
This commit is contained in:
parent
1ecc48e9f9
commit
857d35fce4
4 changed files with 33 additions and 37 deletions
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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'
|
||||
|
||||
----
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue