diff options
author | John MacFarlane <fiddlosopher@gmail.com> | 2012-12-29 17:44:02 -0800 |
---|---|---|
committer | John MacFarlane <fiddlosopher@gmail.com> | 2012-12-29 17:54:07 -0800 |
commit | 1864bb0994cc70b99ea8207b3a22438c343348c0 (patch) | |
tree | 79c773cad15876d1f37f3907c8eec45ac278b6b5 /src/Text/Pandoc/Shared.hs | |
parent | 32c5a8e2dcc1262a8181d0f9b6ffe208e7499481 (diff) | |
download | pandoc-1864bb0994cc70b99ea8207b3a22438c343348c0.tar.gz |
Data files changes.
* Added `embed_data_files` flag. (not yet used)
* Shared no longer exports `findDataFile`.
* `readDataFile` now returns a strict bytestring.
* Shared now exports `readDataFileUTF8` which returns a string like
the old `readDataFile`.
* Rewrote modules to use new data file functions and to avoid
using functions from Paths_pandoc directly.
Diffstat (limited to 'src/Text/Pandoc/Shared.hs')
-rw-r--r-- | src/Text/Pandoc/Shared.hs | 41 |
1 files changed, 27 insertions, 14 deletions
diff --git a/src/Text/Pandoc/Shared.hs b/src/Text/Pandoc/Shared.hs index dee10cf9b..71bdca9dd 100644 --- a/src/Text/Pandoc/Shared.hs +++ b/src/Text/Pandoc/Shared.hs @@ -1,4 +1,4 @@ -{-# LANGUAGE DeriveDataTypeable #-} +{-# LANGUAGE DeriveDataTypeable, CPP #-} {- Copyright (C) 2006-2010 John MacFarlane <jgm@berkeley.edu> @@ -64,8 +64,8 @@ module Text.Pandoc.Shared ( renderTags', -- * File handling inDirectory, - findDataFile, readDataFile, + readDataFileUTF8, -- * Error handling err, warn, @@ -89,13 +89,18 @@ import System.FilePath ( (</>) ) import Data.Generics (Typeable, Data) import qualified Control.Monad.State as S import Control.Monad (msum) -import Paths_pandoc (getDataFileName) import Text.Pandoc.Pretty (charWidth) import System.Locale (defaultTimeLocale) import Data.Time import System.IO (stderr) import Text.HTML.TagSoup (renderTagsOptions, RenderOptions(..), Tag(..), renderOptions) +import qualified Data.ByteString as B +#ifdef EMBED_DATA_FILES +import Data.FileEmbed +#else +import Paths_pandoc (getDataFileName) +#endif -- -- List processing @@ -499,20 +504,28 @@ inDirectory path action = do setCurrentDirectory oldDir return result --- | Get file path for data file, either from specified user data directory, --- or, if not found there, from Cabal data directory. -findDataFile :: Maybe FilePath -> FilePath -> IO FilePath -findDataFile Nothing f = getDataFileName f -findDataFile (Just u) f = do - ex <- doesFileExist (u </> f) - if ex - then return (u </> f) - else getDataFileName f +readDefaultDataFile :: FilePath -> IO B.ByteString +readDefaultDataFile fname = +#ifdef EMBED_DATA_FILES + TODO +#else + getDataFileName fname >>= B.readFile +#endif -- | Read file from specified user data directory or, if not found there, from -- Cabal data directory. -readDataFile :: Maybe FilePath -> FilePath -> IO String -readDataFile userDir fname = findDataFile userDir fname >>= UTF8.readFile +readDataFile :: Maybe FilePath -> FilePath -> IO B.ByteString +readDataFile Nothing fname = readDefaultDataFile fname +readDataFile (Just userDir) fname = do + exists <- doesFileExist (userDir </> fname) + if exists + then B.readFile (userDir </> fname) + else readDefaultDataFile fname + +-- | Same as 'readDataFile' but returns a String instead of a ByteString. +readDataFileUTF8 :: Maybe FilePath -> FilePath -> IO String +readDataFileUTF8 userDir fname = + UTF8.toString `fmap` readDataFile userDir fname -- -- Error reporting |