{-# LANGUAGE CPP #-}
{-# LANGUAGE ScopedTypeVariables #-}
#ifdef EMBED_DATA_FILES
{-# LANGUAGE TemplateHaskell #-}
#endif
{- |
Module      : Text.Pandoc.Data
Copyright   : Copyright (C) 2013-2024 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(..), getDataDirs, getTimestamp,
                          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 Data.List (sort)
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 the default data files, ignoring the user data
-- directory and @PANDOC_DATA_DIRS@.
readDefaultDataFile :: PandocMonad m => FilePath -> m B.ByteString
readDefaultDataFile :: forall (m :: * -> *). PandocMonad m => FilePath -> m ByteString
readDefaultDataFile FilePath
fname =
  case (FilePath -> m ByteString) -> FilePath -> Maybe (m Archive)
forall (m :: * -> *).
PandocMonad m =>
(FilePath -> m ByteString) -> FilePath -> Maybe (m Archive)
buildReferenceDoc FilePath -> m ByteString
forall (m :: * -> *). PandocMonad m => FilePath -> m ByteString
readDefaultDataFile FilePath
fname of
    Just m Archive
getArchive -> Archive -> ByteString
archiveToByteString (Archive -> ByteString) -> m Archive -> m ByteString
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> m Archive
getArchive
    Maybe (m Archive)
Nothing -> FilePath -> m ByteString
forall (m :: * -> *). PandocMonad m => FilePath -> m ByteString
readDefaultDataFile' FilePath
fname

-- | If the file is a reference document, returns an action that builds
-- it from its component files, which are read using the supplied function.
buildReferenceDoc :: PandocMonad m
                  => (FilePath -> m B.ByteString) -> FilePath
                  -> Maybe (m Archive)
buildReferenceDoc :: forall (m :: * -> *).
PandocMonad m =>
(FilePath -> m ByteString) -> FilePath -> Maybe (m Archive)
buildReferenceDoc FilePath -> m ByteString
readComponent FilePath
fname =
  case FilePath
fname of
    FilePath
"reference.docx" -> m Archive -> Maybe (m Archive)
forall a. a -> Maybe a
Just (m Archive -> Maybe (m Archive)) -> m Archive -> Maybe (m Archive)
forall a b. (a -> b) -> a -> b
$ (FilePath -> m ByteString) -> m Archive
forall (m :: * -> *).
PandocMonad m =>
(FilePath -> m ByteString) -> m Archive
getDefaultReferenceDocx FilePath -> m ByteString
readComponent
    FilePath
"reference.pptx" -> m Archive -> Maybe (m Archive)
forall a. a -> Maybe a
Just (m Archive -> Maybe (m Archive)) -> m Archive -> Maybe (m Archive)
forall a b. (a -> b) -> a -> b
$ (FilePath -> m ByteString) -> m Archive
forall (m :: * -> *).
PandocMonad m =>
(FilePath -> m ByteString) -> m Archive
getDefaultReferencePptx FilePath -> m ByteString
readComponent
    FilePath
"reference.odt"  -> m Archive -> Maybe (m Archive)
forall a. a -> Maybe a
Just (m Archive -> Maybe (m Archive)) -> m Archive -> Maybe (m Archive)
forall a b. (a -> b) -> a -> b
$ (FilePath -> m ByteString) -> m Archive
forall (m :: * -> *).
PandocMonad m =>
(FilePath -> m ByteString) -> m Archive
getDefaultReferenceODT FilePath -> m ByteString
readComponent
    FilePath
_                -> Maybe (m Archive)
forall a. Maybe a
Nothing

archiveToByteString :: Archive -> B.ByteString
archiveToByteString :: Archive -> ByteString
archiveToByteString = [ByteString] -> ByteString
B.concat ([ByteString] -> ByteString)
-> (Archive -> [ByteString]) -> Archive -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. LazyByteString -> [ByteString]
BL.toChunks (LazyByteString -> [ByteString])
-> (Archive -> LazyByteString) -> Archive -> [ByteString]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Archive -> LazyByteString
fromArchive

readDefaultDataFile' :: PandocMonad m => FilePath -> m B.ByteString
readDefaultDataFile' :: forall (m :: * -> *). PandocMonad m => FilePath -> m ByteString
readDefaultDataFile' FilePath
fname =
#ifdef EMBED_DATA_FILES
  case FilePath -> [(FilePath, ByteString)] -> Maybe ByteString
forall a b. Eq a => a -> [(a, b)] -> Maybe b
lookup (FilePath -> FilePath
makeCanonical FilePath
fname) [(FilePath, ByteString)]
dataFiles of
    Maybe ByteString
Nothing       -> PandocError -> m ByteString
forall a. PandocError -> m a
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (PandocError -> m ByteString) -> PandocError -> m ByteString
forall a b. (a -> b) -> a -> b
$ Text -> PandocError
PandocCouldNotFindDataFileError (Text -> PandocError) -> Text -> PandocError
forall a b. (a -> b) -> a -> b
$ FilePath -> Text
T.pack FilePath
fname
    Just ByteString
contents -> ByteString -> m ByteString
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return ByteString
contents
#else
  getDataFileName fname' >>= checkExistence >>= readFileStrict
    where fname' = if fname == "MANUAL.txt" then fname else "data" </> 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 fn = do
  exists <- fileExists fn
  if exists
     then return fn
     else throwError $ PandocCouldNotFindDataFileError $ T.pack fn
#endif

--- | Read file from user data directory or, if not found there,
--- from one of the directories listed in @PANDOC_DATA_DIRS@,
--- or, if not found there, from the default
--- data files.  If a reference document (e.g. @reference.docx@)
--- is not found, it is built from component files (e.g.
--- @docx/word/styles.xml@), which are looked up in the same way.
readDataFile :: PandocMonad m => FilePath -> m B.ByteString
readDataFile :: forall (m :: * -> *). PandocMonad m => FilePath -> m ByteString
readDataFile FilePath
fname = FilePath -> m [FilePath]
forall (m :: * -> *). PandocMonad m => FilePath -> m [FilePath]
getDataDirs FilePath
fname m [FilePath] -> ([FilePath] -> m ByteString) -> m ByteString
forall a b. m a -> (a -> m b) -> m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= [FilePath] -> m ByteString
forall {f :: * -> *}. PandocMonad f => [FilePath] -> f ByteString
go
 where
  go :: [FilePath] -> f ByteString
go [] = case (FilePath -> f ByteString) -> FilePath -> Maybe (f Archive)
forall (m :: * -> *).
PandocMonad m =>
(FilePath -> m ByteString) -> FilePath -> Maybe (m Archive)
buildReferenceDoc FilePath -> f ByteString
forall (m :: * -> *). PandocMonad m => FilePath -> m ByteString
readDataFile FilePath
fname of
            Just f Archive
getArchive -> Archive -> ByteString
archiveToByteString (Archive -> ByteString) -> f Archive -> f ByteString
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> f Archive
getArchive
            Maybe (f Archive)
Nothing -> FilePath -> f ByteString
forall (m :: * -> *). PandocMonad m => FilePath -> m ByteString
readDefaultDataFile' FilePath
fname
  go (FilePath
d:[FilePath]
ds) = do
    exists <- FilePath -> f Bool
forall (m :: * -> *). PandocMonad m => FilePath -> m Bool
fileExists (FilePath
d FilePath -> FilePath -> FilePath
</> FilePath
fname)
    if exists
       then readFileStrict (d </> fname)
       else go ds

-- | Retrieve default reference.docx.
getDefaultReferenceDocx :: PandocMonad m
                        => (FilePath -> m B.ByteString) -> m Archive
getDefaultReferenceDocx :: forall (m :: * -> *).
PandocMonad m =>
(FilePath -> m ByteString) -> m Archive
getDefaultReferenceDocx FilePath -> m ByteString
readComponent = 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 -> LazyByteString
toLazy = [ByteString] -> LazyByteString
BL.fromChunks ([ByteString] -> LazyByteString)
-> (ByteString -> [ByteString]) -> ByteString -> LazyByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (ByteString -> [ByteString] -> [ByteString]
forall a. a -> [a] -> [a]
:[])
  let pathToEntry :: FilePath -> m Entry
pathToEntry FilePath
path = do
        epochtime <- POSIXTime -> Integer
forall b. Integral b => POSIXTime -> b
forall a b. (RealFrac a, Integral b) => a -> b
floor (POSIXTime -> Integer)
-> (UTCTime -> POSIXTime) -> UTCTime -> Integer
forall b c a. (b -> c) -> (a -> b) -> a -> c
. UTCTime -> POSIXTime
utcTimeToPOSIXSeconds (UTCTime -> Integer) -> m UTCTime -> m Integer
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> m UTCTime
forall (m :: * -> *). PandocMonad m => m UTCTime
getTimestamp
        contents <- toLazy <$> readComponent ("docx/" ++ path)
        return $ toEntry path epochtime contents
  (Entry -> Archive -> Archive) -> Archive -> [Entry] -> Archive
forall a b. (a -> b -> b) -> b -> [a] -> b
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr Entry -> Archive -> Archive
addEntryToArchive Archive
emptyArchive ([Entry] -> Archive) -> m [Entry] -> m Archive
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (FilePath -> m Entry) -> [FilePath] -> m [Entry]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]
mapM FilePath -> m Entry
pathToEntry [FilePath]
paths

-- | Retrieve default reference.odt.
getDefaultReferenceODT :: PandocMonad m
                        => (FilePath -> m B.ByteString) -> m Archive
getDefaultReferenceODT :: forall (m :: * -> *).
PandocMonad m =>
(FilePath -> m ByteString) -> m Archive
getDefaultReferenceODT FilePath -> m ByteString
readComponent = 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 epochtime <- POSIXTime -> Integer
forall b. Integral b => POSIXTime -> b
forall a b. (RealFrac a, Integral b) => a -> b
floor (POSIXTime -> Integer) -> m POSIXTime -> m Integer
forall a b. (a -> b) -> m a -> m b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
`fmap` m POSIXTime
forall (m :: * -> *). PandocMonad m => m POSIXTime
getPOSIXTime
                            contents <- (BL.fromChunks . (:[])) `fmap`
                                          readComponent ("odt/" ++ path)
                            return $ toEntry path epochtime contents
  (Entry -> Archive -> Archive) -> Archive -> [Entry] -> Archive
forall a b. (a -> b -> b) -> b -> [a] -> b
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr Entry -> Archive -> Archive
addEntryToArchive Archive
emptyArchive ([Entry] -> Archive) -> m [Entry] -> m Archive
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (FilePath -> m Entry) -> [FilePath] -> m [Entry]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]
mapM FilePath -> m Entry
pathToEntry [FilePath]
paths

-- | Retrieve default reference.pptx.
getDefaultReferencePptx :: PandocMonad m
                        => (FilePath -> m B.ByteString) -> m Archive
getDefaultReferencePptx :: forall (m :: * -> *).
PandocMonad m =>
(FilePath -> m ByteString) -> m Archive
getDefaultReferencePptx FilePath -> m ByteString
readComponent = 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 -> LazyByteString
toLazy = [ByteString] -> LazyByteString
BL.fromChunks ([ByteString] -> LazyByteString)
-> (ByteString -> [ByteString]) -> ByteString -> LazyByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (ByteString -> [ByteString] -> [ByteString]
forall a. a -> [a] -> [a]
:[])
  let pathToEntry :: FilePath -> m Entry
pathToEntry FilePath
path = do
        epochtime <- POSIXTime -> Integer
forall b. Integral b => POSIXTime -> b
forall a b. (RealFrac a, Integral b) => a -> b
floor (POSIXTime -> Integer) -> m POSIXTime -> m Integer
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> m POSIXTime
forall (m :: * -> *). PandocMonad m => m POSIXTime
getPOSIXTime
        contents <- toLazy <$> readComponent ("pptx/" ++ path)
        return $ toEntry path epochtime contents
  (Entry -> Archive -> Archive) -> Archive -> [Entry] -> Archive
forall a b. (a -> b -> b) -> b -> [a] -> b
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr Entry -> Archive -> Archive
addEntryToArchive Archive
emptyArchive ([Entry] -> Archive) -> m [Entry] -> m Archive
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (FilePath -> m Entry) -> [FilePath] -> m [Entry]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]
mapM FilePath -> m Entry
pathToEntry [FilePath]
paths

getDataFileNames :: IO [FilePath]
getDataFileNames :: IO [FilePath]
getDataFileNames = do
#ifdef EMBED_DATA_FILES
  let allDataFiles :: [FilePath]
allDataFiles = ((FilePath, ByteString) -> FilePath)
-> [(FilePath, ByteString)] -> [FilePath]
forall a b. (a -> b) -> [a] -> [b]
map (FilePath, ByteString) -> FilePath
forall a b. (a, b) -> a
fst [(FilePath, ByteString)]
dataFiles
#else
  let listDirectoryRecursive d = do
        xs <- listDirectory d
        concat <$>
           mapM (\f -> do
                      isdir <- doesDirectoryExist (d </> f)
                      if isdir
                         then map (f </>) <$> listDirectoryRecursive (d </> f)
                         else pure [f]) xs
  ddir <- getDataDir
  allDataFiles <- ("MANUAL.txt" :) <$>
                     listDirectoryRecursive (ddir </> "data")
#endif
  [FilePath] -> IO [FilePath]
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return ([FilePath] -> IO [FilePath]) -> [FilePath] -> IO [FilePath]
forall a b. (a -> b) -> a -> b
$ [FilePath] -> [FilePath]
forall a. Ord a => [a] -> [a]
sort ([FilePath] -> [FilePath]) -> [FilePath] -> [FilePath]
forall a b. (a -> b) -> a -> b
$
    FilePath
"reference.docx" FilePath -> [FilePath] -> [FilePath]
forall a. a -> [a] -> [a]
: FilePath
"reference.odt" FilePath -> [FilePath] -> [FilePath]
forall a. a -> [a] -> [a]
: FilePath
"reference.pptx" FilePath -> [FilePath] -> [FilePath]
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
  xdgDir <- IO FilePath -> (SomeException -> IO FilePath) -> IO FilePath
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) -> FilePath -> IO FilePath
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return FilePath
forall a. Monoid a => a
mempty)
  xdgExists <- doesDirectoryExist xdgDir
  legacyDir <- E.catch (getAppUserDataDirectory "pandoc")
                (\(SomeException
_ :: E.SomeException) -> FilePath -> IO FilePath
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return FilePath
forall a. Monoid a => a
mempty)
  legacyDirExists <- doesDirectoryExist legacyDir
  if not xdgExists && legacyDirExists
     then return legacyDir
     else return xdgDir