Expose getDefaultDataFile in both Shared and Class.

This commit is contained in:
John MacFarlane 2017-08-10 23:04:14 -07:00
parent ed4f6b348a
commit 6aaf8f4770
4 changed files with 18 additions and 7 deletions

View file

@ -84,8 +84,8 @@ import Text.Pandoc.PDF (makePDF)
import Text.Pandoc.Process (pipeProcess)
import Text.Pandoc.SelfContained (makeDataURI, makeSelfContained)
import Text.Pandoc.Shared (headerShift, isURI, openURL, readDataFile,
readDataFileUTF8, safeRead, tabFilter,
eastAsianLineBreakFilter)
readDataFileUTF8, readDefaultDataFile,
safeRead, tabFilter, eastAsianLineBreakFilter)
import qualified Text.Pandoc.UTF8 as UTF8
import Text.Pandoc.XML (toEntities)
import Text.Printf
@ -1007,7 +1007,7 @@ options =
, Option "" ["print-default-data-file"]
(ReqArg
(\arg _ -> do
readDataFile Nothing arg >>= BS.hPutStr stdout
readDefaultDataFile arg >>= BS.hPutStr stdout
exitSuccess)
"FILE")
"" -- "Print default data file"

View file

@ -80,6 +80,7 @@ import Codec.Archive.Zip (Archive, fromArchive, emptyArchive)
import Data.Unique (hashUnique)
import qualified Data.Unique as IO (newUnique)
import qualified Text.Pandoc.Shared as IO ( readDataFile
, readDefaultDataFile
, openURL )
import qualified Text.Pandoc.UTF8 as UTF8
import Text.Pandoc.Compat.Time (UTCTime)
@ -152,6 +153,8 @@ class (Functor m, Applicative m, Monad m, MonadError PandocError m)
-- | Read the strict ByteString contents from a file path,
-- raising an error on failure.
readFileStrict :: FilePath -> m B.ByteString
-- | Read file from from Cabal data directory.
readDefaultDataFile :: FilePath -> m B.ByteString
-- | Read file from specified user data directory or,
-- if not found there, from Cabal data directory.
readDataFile :: Maybe FilePath -> FilePath -> m B.ByteString
@ -326,6 +329,7 @@ instance PandocMonad PandocIO where
Left e -> throwError $ PandocHttpError u e
readFileLazy s = liftIOError BL.readFile s
readFileStrict s = liftIOError B.readFile s
readDefaultDataFile fname = liftIOError IO.readDefaultDataFile fname
readDataFile mfp fname = liftIOError (IO.readDataFile mfp) fname
glob = liftIO . IO.glob
getModificationTime fp = liftIOError IO.getModificationTime fp
@ -602,11 +606,11 @@ instance PandocMonad PandocPure where
case infoFileContents <$> getFileInfo fp fps of
Just bs -> return bs
Nothing -> throwError $ PandocResourceNotFound fp
readDataFile Nothing "reference.docx" =
readDefaultDataFile "reference.docx" =
(B.concat . BL.toChunks . fromArchive) <$> getsPureState stReferenceDocx
readDataFile Nothing "reference.odt" =
readDefaultDataFile "reference.odt" =
(B.concat . BL.toChunks . fromArchive) <$> getsPureState stReferenceODT
readDataFile Nothing fname = do
readDefaultDataFile fname = do
let fname' = if fname == "MANUAL.txt" then fname else "data" </> fname
readFileStrict fname'
readDataFile (Just userDir) fname = do
@ -614,6 +618,7 @@ instance PandocMonad PandocPure where
case infoFileContents <$> getFileInfo (userDir </> fname) userDirFiles of
Just bs -> return bs
Nothing -> readDataFile Nothing fname
readDataFile Nothing fname = readDefaultDataFile fname
glob s = do
FileTree ftmap <- getsPureState stFiles
@ -640,6 +645,7 @@ instance PandocMonad m => PandocMonad (ParsecT s st m) where
openURL = lift . openURL
readFileLazy = lift . readFileLazy
readFileStrict = lift . readFileStrict
readDefaultDataFile = lift . readDefaultDataFile
readDataFile mbuserdir = lift . readDataFile mbuserdir
glob = lift . glob
getModificationTime = lift . getModificationTime
@ -668,6 +674,7 @@ instance PandocMonad m => PandocMonad (ReaderT r m) where
openURL = lift . openURL
readFileLazy = lift . readFileLazy
readFileStrict = lift . readFileStrict
readDefaultDataFile = lift . readDefaultDataFile
readDataFile mbuserdir = lift . readDataFile mbuserdir
glob = lift . glob
getModificationTime = lift . getModificationTime
@ -684,6 +691,7 @@ instance (PandocMonad m, Monoid w) => PandocMonad (WriterT w m) where
openURL = lift . openURL
readFileLazy = lift . readFileLazy
readFileStrict = lift . readFileStrict
readDefaultDataFile = lift . readDefaultDataFile
readDataFile mbuserdir = lift . readDataFile mbuserdir
glob = lift . glob
getModificationTime = lift . getModificationTime
@ -700,6 +708,7 @@ instance (PandocMonad m, Monoid w) => PandocMonad (RWST r w st m) where
openURL = lift . openURL
readFileLazy = lift . readFileLazy
readFileStrict = lift . readFileStrict
readDefaultDataFile = lift . readDefaultDataFile
readDataFile mbuserdir = lift . readDataFile mbuserdir
glob = lift . glob
getModificationTime = lift . getModificationTime
@ -716,6 +725,7 @@ instance PandocMonad m => PandocMonad (StateT st m) where
openURL = lift . openURL
readFileLazy = lift . readFileLazy
readFileStrict = lift . readFileStrict
readDefaultDataFile = lift . readDefaultDataFile
readDataFile mbuserdir = lift . readDataFile mbuserdir
glob = lift . glob
getModificationTime = lift . getModificationTime

View file

@ -78,6 +78,7 @@ module Text.Pandoc.Shared (
inDirectory,
getDefaultReferenceDocx,
getDefaultReferenceODT,
readDefaultDataFile,
readDataFile,
readDataFileUTF8,
openURL,

View file

@ -231,7 +231,7 @@ writeDocx opts doc@(Pandoc meta _) = do
username <- P.lookupEnv "USERNAME"
utctime <- P.getCurrentTime
distArchive <- (toArchive . BL.fromStrict) <$>
P.readDataFile Nothing "reference.docx"
P.readDefaultDataFile "reference.docx"
refArchive <- case writerReferenceDoc opts of
Just f -> toArchive <$> P.readFileLazy f
Nothing -> (toArchive . BL.fromStrict) <$>