{-# LANGUAGE CPP #-}
{-# LANGUAGE ScopedTypeVariables #-}
#ifdef EMBED_DATA_FILES
{-# LANGUAGE TemplateHaskell #-}
#endif
{- |
Module      : Text.Pandoc.Data
Copyright   : Copyright (C) 2013-2023 John MacFarlane
License     : GNU GPL, version 2 or above

Maintainer  : John MacFarlane <jgm@berkeley@edu>
Stability   : alpha
Portability : portable

Access to pandoc's data files.
-}
module Text.Pandoc.Data ( readDefaultDataFile
                        , readDataFile
                        , getDataFileNames
                        , defaultUserDataDir
                        ) where
import Text.Pandoc.Class (PandocMonad(..), checkUserDataDir, getTimestamp,
                          getUserDataDir, getPOSIXTime)
import Data.Time.Clock.POSIX (utcTimeToPOSIXSeconds)
import qualified Data.ByteString.Lazy as BL
import qualified Data.ByteString as B
import Codec.Archive.Zip
import qualified Data.Text as T
import Control.Monad.Except (throwError)
import Text.Pandoc.Error (PandocError(..))
import System.FilePath
import System.Directory
import qualified Control.Exception as E
#ifdef EMBED_DATA_FILES
import Text.Pandoc.Data.BakedIn (dataFiles)
import Text.Pandoc.Shared (makeCanonical)
#else
import Paths_pandoc (getDataDir)
#endif

-- | Read file from from the default data files.
readDefaultDataFile :: PandocMonad m => FilePath -> m B.ByteString
readDefaultDataFile :: forall (m :: * -> *). PandocMonad m => FilePath -> m ByteString
readDefaultDataFile FilePath
"reference.docx" =
  [ByteString] -> ByteString
B.concat forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> [ByteString]
BL.toChunks forall b c a. (b -> c) -> (a -> b) -> a -> c
. Archive -> ByteString
fromArchive forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (m :: * -> *). PandocMonad m => m Archive
getDefaultReferenceDocx
readDefaultDataFile FilePath
"reference.pptx" =
  [ByteString] -> ByteString
B.concat forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> [ByteString]
BL.toChunks forall b c a. (b -> c) -> (a -> b) -> a -> c
. Archive -> ByteString
fromArchive forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (m :: * -> *). PandocMonad m => m Archive
getDefaultReferencePptx
readDefaultDataFile FilePath
"reference.odt" =
  [ByteString] -> ByteString
B.concat forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> [ByteString]
BL.toChunks forall b c a. (b -> c) -> (a -> b) -> a -> c
. Archive -> ByteString
fromArchive forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (m :: * -> *). PandocMonad m => m Archive
getDefaultReferenceODT
readDefaultDataFile FilePath
fname =
#ifdef EMBED_DATA_FILES
  case lookup (makeCanonical fname) dataFiles of
    Nothing       -> throwError $ PandocCouldNotFindDataFileError $ T.pack fname
    Just contents -> return contents
#else
  forall (m :: * -> *). PandocMonad m => FilePath -> m FilePath
getDataFileName FilePath
fname' forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= forall (m :: * -> *). PandocMonad m => FilePath -> m FilePath
checkExistence forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= forall (m :: * -> *). PandocMonad m => FilePath -> m ByteString
readFileStrict
    where fname' :: FilePath
fname' = if FilePath
fname forall a. Eq a => a -> a -> Bool
== FilePath
"MANUAL.txt" then FilePath
fname else FilePath
"data" FilePath -> FilePath -> FilePath
</> FilePath
fname

-- | Returns the input filename unchanged if the file exits, and throws
-- a `PandocCouldNotFindDataFileError` if it doesn't.
checkExistence :: PandocMonad m => FilePath -> m FilePath
checkExistence :: forall (m :: * -> *). PandocMonad m => FilePath -> m FilePath
checkExistence FilePath
fn = do
  Bool
exists <- forall (m :: * -> *). PandocMonad m => FilePath -> m Bool
fileExists FilePath
fn
  if Bool
exists
     then forall (m :: * -> *) a. Monad m => a -> m a
return FilePath
fn
     else forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError forall a b. (a -> b) -> a -> b
$ Text -> PandocError
PandocCouldNotFindDataFileError forall a b. (a -> b) -> a -> b
$ FilePath -> Text
T.pack FilePath
fn
#endif

--- | Read file from user data directory or,
--- if not found there, from the default data files.
readDataFile :: PandocMonad m => FilePath -> m B.ByteString
readDataFile :: forall (m :: * -> *). PandocMonad m => FilePath -> m ByteString
readDataFile FilePath
fname = do
  Maybe FilePath
datadir <- forall (m :: * -> *).
PandocMonad m =>
FilePath -> m (Maybe FilePath)
checkUserDataDir FilePath
fname
  case Maybe FilePath
datadir of
       Maybe FilePath
Nothing -> forall (m :: * -> *). PandocMonad m => FilePath -> m ByteString
readDefaultDataFile FilePath
fname
       Just FilePath
userDir -> do
         Bool
exists <- forall (m :: * -> *). PandocMonad m => FilePath -> m Bool
fileExists (FilePath
userDir FilePath -> FilePath -> FilePath
</> FilePath
fname)
         if Bool
exists
            then forall (m :: * -> *). PandocMonad m => FilePath -> m ByteString
readFileStrict (FilePath
userDir FilePath -> FilePath -> FilePath
</> FilePath
fname)
            else forall (m :: * -> *). PandocMonad m => FilePath -> m ByteString
readDefaultDataFile FilePath
fname

-- | Retrieve default reference.docx.
getDefaultReferenceDocx :: PandocMonad m => m Archive
getDefaultReferenceDocx :: forall (m :: * -> *). PandocMonad m => m Archive
getDefaultReferenceDocx = do
  let paths :: [FilePath]
paths = [FilePath
"[Content_Types].xml",
               FilePath
"_rels/.rels",
               FilePath
"docProps/app.xml",
               FilePath
"docProps/core.xml",
               FilePath
"docProps/custom.xml",
               FilePath
"word/document.xml",
               FilePath
"word/fontTable.xml",
               FilePath
"word/footnotes.xml",
               FilePath
"word/comments.xml",
               FilePath
"word/numbering.xml",
               FilePath
"word/settings.xml",
               FilePath
"word/webSettings.xml",
               FilePath
"word/styles.xml",
               FilePath
"word/_rels/document.xml.rels",
               FilePath
"word/_rels/footnotes.xml.rels",
               FilePath
"word/theme/theme1.xml"]
  let toLazy :: ByteString -> ByteString
toLazy = [ByteString] -> ByteString
BL.fromChunks forall b c a. (b -> c) -> (a -> b) -> a -> c
. (forall a. a -> [a] -> [a]
:[])
  let pathToEntry :: FilePath -> m Entry
pathToEntry FilePath
path = do
        Integer
epochtime <- forall a b. (RealFrac a, Integral b) => a -> b
floor forall b c a. (b -> c) -> (a -> b) -> a -> c
. UTCTime -> POSIXTime
utcTimeToPOSIXSeconds forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (m :: * -> *). PandocMonad m => m UTCTime
getTimestamp
        ByteString
contents <- ByteString -> ByteString
toLazy forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (m :: * -> *). PandocMonad m => FilePath -> m ByteString
readDataFile (FilePath
"docx/" forall a. [a] -> [a] -> [a]
++ FilePath
path)
        forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ FilePath -> Integer -> ByteString -> Entry
toEntry FilePath
path Integer
epochtime ByteString
contents
  Maybe FilePath
datadir <- forall (m :: * -> *). PandocMonad m => m (Maybe FilePath)
getUserDataDir
  Maybe FilePath
mbArchive <- case Maybe FilePath
datadir of
                    Maybe FilePath
Nothing   -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a. Maybe a
Nothing
                    Just FilePath
d    -> do
                       Bool
exists <- forall (m :: * -> *). PandocMonad m => FilePath -> m Bool
fileExists (FilePath
d FilePath -> FilePath -> FilePath
</> FilePath
"reference.docx")
                       if Bool
exists
                          then forall (m :: * -> *) a. Monad m => a -> m a
return (forall a. a -> Maybe a
Just (FilePath
d FilePath -> FilePath -> FilePath
</> FilePath
"reference.docx"))
                          else forall (m :: * -> *) a. Monad m => a -> m a
return forall a. Maybe a
Nothing
  case Maybe FilePath
mbArchive of
     Just FilePath
arch -> ByteString -> Archive
toArchive forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (m :: * -> *). PandocMonad m => FilePath -> m ByteString
readFileLazy FilePath
arch
     Maybe FilePath
Nothing   -> forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr Entry -> Archive -> Archive
addEntryToArchive Archive
emptyArchive forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$>
                     forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM forall {m :: * -> *}. PandocMonad m => FilePath -> m Entry
pathToEntry [FilePath]
paths

-- | Retrieve default reference.odt.
getDefaultReferenceODT :: PandocMonad m => m Archive
getDefaultReferenceODT :: forall (m :: * -> *). PandocMonad m => m Archive
getDefaultReferenceODT = do
  let paths :: [FilePath]
paths = [FilePath
"mimetype",
               FilePath
"manifest.rdf",
               FilePath
"styles.xml",
               FilePath
"content.xml",
               FilePath
"meta.xml",
               FilePath
"META-INF/manifest.xml"]
  let pathToEntry :: FilePath -> m Entry
pathToEntry FilePath
path = do Integer
epochtime <- forall a b. (RealFrac a, Integral b) => a -> b
floor forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
`fmap` forall (m :: * -> *). PandocMonad m => m POSIXTime
getPOSIXTime
                            ByteString
contents <- ([ByteString] -> ByteString
BL.fromChunks forall b c a. (b -> c) -> (a -> b) -> a -> c
. (forall a. a -> [a] -> [a]
:[])) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
`fmap`
                                          forall (m :: * -> *). PandocMonad m => FilePath -> m ByteString
readDataFile (FilePath
"odt/" forall a. [a] -> [a] -> [a]
++ FilePath
path)
                            forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ FilePath -> Integer -> ByteString -> Entry
toEntry FilePath
path Integer
epochtime ByteString
contents
  Maybe FilePath
datadir <- forall (m :: * -> *). PandocMonad m => m (Maybe FilePath)
getUserDataDir
  Maybe FilePath
mbArchive <- case Maybe FilePath
datadir of
                    Maybe FilePath
Nothing   -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a. Maybe a
Nothing
                    Just FilePath
d    -> do
                       Bool
exists <- forall (m :: * -> *). PandocMonad m => FilePath -> m Bool
fileExists (FilePath
d FilePath -> FilePath -> FilePath
</> FilePath
"reference.odt")
                       if Bool
exists
                          then forall (m :: * -> *) a. Monad m => a -> m a
return (forall a. a -> Maybe a
Just (FilePath
d FilePath -> FilePath -> FilePath
</> FilePath
"reference.odt"))
                          else forall (m :: * -> *) a. Monad m => a -> m a
return forall a. Maybe a
Nothing
  case Maybe FilePath
mbArchive of
     Just FilePath
arch -> ByteString -> Archive
toArchive forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (m :: * -> *). PandocMonad m => FilePath -> m ByteString
readFileLazy FilePath
arch
     Maybe FilePath
Nothing   -> forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr Entry -> Archive -> Archive
addEntryToArchive Archive
emptyArchive forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$>
                     forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM forall {m :: * -> *}. PandocMonad m => FilePath -> m Entry
pathToEntry [FilePath]
paths

-- | Retrieve default reference.pptx.
getDefaultReferencePptx :: PandocMonad m => m Archive
getDefaultReferencePptx :: forall (m :: * -> *). PandocMonad m => m Archive
getDefaultReferencePptx = do
  -- We're going to narrow this down substantially once we get it
  -- working.
  let paths :: [FilePath]
paths = [ FilePath
"[Content_Types].xml"
              , FilePath
"_rels/.rels"
              , FilePath
"docProps/app.xml"
              , FilePath
"docProps/core.xml"
              , FilePath
"ppt/_rels/presentation.xml.rels"
              , FilePath
"ppt/presProps.xml"
              , FilePath
"ppt/presentation.xml"
              , FilePath
"ppt/slideLayouts/_rels/slideLayout1.xml.rels"
              , FilePath
"ppt/slideLayouts/_rels/slideLayout2.xml.rels"
              , FilePath
"ppt/slideLayouts/_rels/slideLayout3.xml.rels"
              , FilePath
"ppt/slideLayouts/_rels/slideLayout4.xml.rels"
              , FilePath
"ppt/slideLayouts/_rels/slideLayout5.xml.rels"
              , FilePath
"ppt/slideLayouts/_rels/slideLayout6.xml.rels"
              , FilePath
"ppt/slideLayouts/_rels/slideLayout7.xml.rels"
              , FilePath
"ppt/slideLayouts/_rels/slideLayout8.xml.rels"
              , FilePath
"ppt/slideLayouts/_rels/slideLayout9.xml.rels"
              , FilePath
"ppt/slideLayouts/_rels/slideLayout10.xml.rels"
              , FilePath
"ppt/slideLayouts/_rels/slideLayout11.xml.rels"
              , FilePath
"ppt/slideLayouts/slideLayout1.xml"
              , FilePath
"ppt/slideLayouts/slideLayout10.xml"
              , FilePath
"ppt/slideLayouts/slideLayout11.xml"
              , FilePath
"ppt/slideLayouts/slideLayout2.xml"
              , FilePath
"ppt/slideLayouts/slideLayout3.xml"
              , FilePath
"ppt/slideLayouts/slideLayout4.xml"
              , FilePath
"ppt/slideLayouts/slideLayout5.xml"
              , FilePath
"ppt/slideLayouts/slideLayout6.xml"
              , FilePath
"ppt/slideLayouts/slideLayout7.xml"
              , FilePath
"ppt/slideLayouts/slideLayout8.xml"
              , FilePath
"ppt/slideLayouts/slideLayout9.xml"
              , FilePath
"ppt/slideMasters/_rels/slideMaster1.xml.rels"
              , FilePath
"ppt/slideMasters/slideMaster1.xml"
              , FilePath
"ppt/slides/_rels/slide1.xml.rels"
              , FilePath
"ppt/slides/slide1.xml"
              , FilePath
"ppt/slides/_rels/slide2.xml.rels"
              , FilePath
"ppt/slides/slide2.xml"
              , FilePath
"ppt/slides/_rels/slide3.xml.rels"
              , FilePath
"ppt/slides/slide3.xml"
              , FilePath
"ppt/slides/_rels/slide4.xml.rels"
              , FilePath
"ppt/slides/slide4.xml"
              , FilePath
"ppt/tableStyles.xml"
              , FilePath
"ppt/theme/theme1.xml"
              , FilePath
"ppt/viewProps.xml"
              -- These relate to notes slides.
              , FilePath
"ppt/notesMasters/notesMaster1.xml"
              , FilePath
"ppt/notesMasters/_rels/notesMaster1.xml.rels"
              , FilePath
"ppt/notesSlides/notesSlide1.xml"
              , FilePath
"ppt/notesSlides/_rels/notesSlide1.xml.rels"
              , FilePath
"ppt/notesSlides/notesSlide2.xml"
              , FilePath
"ppt/notesSlides/_rels/notesSlide2.xml.rels"
              , FilePath
"ppt/theme/theme2.xml"
              ]
  let toLazy :: ByteString -> ByteString
toLazy = [ByteString] -> ByteString
BL.fromChunks forall b c a. (b -> c) -> (a -> b) -> a -> c
. (forall a. a -> [a] -> [a]
:[])
  let pathToEntry :: FilePath -> m Entry
pathToEntry FilePath
path = do
        Integer
epochtime <- forall a b. (RealFrac a, Integral b) => a -> b
floor forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (m :: * -> *). PandocMonad m => m POSIXTime
getPOSIXTime
        ByteString
contents <- ByteString -> ByteString
toLazy forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (m :: * -> *). PandocMonad m => FilePath -> m ByteString
readDataFile (FilePath
"pptx/" forall a. [a] -> [a] -> [a]
++ FilePath
path)
        forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ FilePath -> Integer -> ByteString -> Entry
toEntry FilePath
path Integer
epochtime ByteString
contents
  Maybe FilePath
datadir <- forall (m :: * -> *). PandocMonad m => m (Maybe FilePath)
getUserDataDir
  Maybe FilePath
mbArchive <- case Maybe FilePath
datadir of
                    Maybe FilePath
Nothing   -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a. Maybe a
Nothing
                    Just FilePath
d    -> do
                       Bool
exists <- forall (m :: * -> *). PandocMonad m => FilePath -> m Bool
fileExists (FilePath
d FilePath -> FilePath -> FilePath
</> FilePath
"reference.pptx")
                       if Bool
exists
                          then forall (m :: * -> *) a. Monad m => a -> m a
return (forall a. a -> Maybe a
Just (FilePath
d FilePath -> FilePath -> FilePath
</> FilePath
"reference.pptx"))
                          else forall (m :: * -> *) a. Monad m => a -> m a
return forall a. Maybe a
Nothing
  case Maybe FilePath
mbArchive of
     Just FilePath
arch -> ByteString -> Archive
toArchive forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (m :: * -> *). PandocMonad m => FilePath -> m ByteString
readFileLazy FilePath
arch
     Maybe FilePath
Nothing   -> forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr Entry -> Archive -> Archive
addEntryToArchive Archive
emptyArchive forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$>
                     forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM forall {m :: * -> *}. PandocMonad m => FilePath -> m Entry
pathToEntry [FilePath]
paths

getDataFileNames :: IO [FilePath]
getDataFileNames :: IO [FilePath]
getDataFileNames = do
#ifdef EMBED_DATA_FILES
  let allDataFiles = map fst dataFiles
#else
  [FilePath]
allDataFiles <- forall a. (a -> Bool) -> [a] -> [a]
filter (\FilePath
x -> FilePath
x forall a. Eq a => a -> a -> Bool
/= FilePath
"." Bool -> Bool -> Bool
&& FilePath
x forall a. Eq a => a -> a -> Bool
/= FilePath
"..") forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$>
                      (IO FilePath
getDataDir forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= FilePath -> IO [FilePath]
getDirectoryContents)
#endif
  forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ FilePath
"reference.docx" forall a. a -> [a] -> [a]
: FilePath
"reference.odt" forall a. a -> [a] -> [a]
: FilePath
"reference.pptx" forall a. a -> [a] -> [a]
: [FilePath]
allDataFiles

-- | Return appropriate user data directory for platform.  We use
-- XDG_DATA_HOME (or its default value), but for backwards compatibility,
-- we fall back to the legacy user data directory ($HOME/.pandoc on *nix)
-- if the XDG_DATA_HOME is missing and this exists.  If neither directory
-- is present, we return the XDG data directory.  If the XDG data directory
-- is not defined (e.g. because we are in an environment where $HOME is
-- not defined), we return the empty string.
defaultUserDataDir :: IO FilePath
defaultUserDataDir :: IO FilePath
defaultUserDataDir = do
  FilePath
xdgDir <- forall e a. Exception e => IO a -> (e -> IO a) -> IO a
E.catch (XdgDirectory -> FilePath -> IO FilePath
getXdgDirectory XdgDirectory
XdgData FilePath
"pandoc")
               (\(SomeException
_ :: E.SomeException) -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a. Monoid a => a
mempty)
  Bool
xdgExists <- FilePath -> IO Bool
doesDirectoryExist FilePath
xdgDir
  FilePath
legacyDir <- forall e a. Exception e => IO a -> (e -> IO a) -> IO a
E.catch (FilePath -> IO FilePath
getAppUserDataDirectory FilePath
"pandoc")
                (\(SomeException
_ :: E.SomeException) -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a. Monoid a => a
mempty)
  Bool
legacyDirExists <- FilePath -> IO Bool
doesDirectoryExist FilePath
legacyDir
  if Bool -> Bool
not Bool
xdgExists Bool -> Bool -> Bool
&& Bool
legacyDirExists
     then forall (m :: * -> *) a. Monad m => a -> m a
return FilePath
legacyDir
     else forall (m :: * -> *) a. Monad m => a -> m a
return FilePath
xdgDir