diff options
| author | John MacFarlane <jgm@berkeley.edu> | 2022-10-19 10:48:47 -0700 |
|---|---|---|
| committer | John MacFarlane <jgm@berkeley.edu> | 2022-10-19 10:48:49 -0700 |
| commit | 2e1f424daa859b163d85ac1452b1478af618169b (patch) | |
| tree | 539c106bedb6987c5397b44243a36762643cba5d /src/Text | |
| parent | e10d6d8ff6fde50269511f53681333e5f5c2dc08 (diff) | |
Move `defaultUserDataDir` from T.P.Shared to T.P.Data [API change].
Diffstat (limited to 'src/Text')
| -rw-r--r-- | src/Text/Pandoc/App.hs | 2 | ||||
| -rw-r--r-- | src/Text/Pandoc/App/CommandLineOptions.hs | 2 | ||||
| -rw-r--r-- | src/Text/Pandoc/App/Opt.hs | 3 | ||||
| -rw-r--r-- | src/Text/Pandoc/Data.hs | 23 | ||||
| -rw-r--r-- | src/Text/Pandoc/Shared.hs | 25 |
5 files changed, 27 insertions, 28 deletions
diff --git a/src/Text/Pandoc/App.hs b/src/Text/Pandoc/App.hs index 4bf0a520d..c5fe382c8 100644 --- a/src/Text/Pandoc/App.hs +++ b/src/Text/Pandoc/App.hs @@ -61,7 +61,7 @@ import Text.Pandoc.PDF (makePDF) import Text.Pandoc.Scripting (ScriptingEngine (..)) import Text.Pandoc.SelfContained (makeSelfContained) import Text.Pandoc.Shared (eastAsianLineBreakFilter, - headerShift, filterIpynbOutput, defaultUserDataDir, tshow) + headerShift, filterIpynbOutput, tshow) import Text.Pandoc.URI (isURI) import Text.Pandoc.Writers.Shared (lookupMetaString) import Text.Pandoc.Readers.Markdown (yamlToMeta) diff --git a/src/Text/Pandoc/App/CommandLineOptions.hs b/src/Text/Pandoc/App/CommandLineOptions.hs index fda83d7c6..771569f9b 100644 --- a/src/Text/Pandoc/App/CommandLineOptions.hs +++ b/src/Text/Pandoc/App/CommandLineOptions.hs @@ -50,7 +50,7 @@ import Text.Pandoc.App.Opt (Opt (..), LineEnding (..), IpynbOutput (..), fullDefaultsPath) import Text.Pandoc.Filter (Filter (..)) import Text.Pandoc.Highlighting (highlightingStyles, lookupHighlightingStyle) -import Text.Pandoc.Shared (ordNub, safeStrRead, defaultUserDataDir) +import Text.Pandoc.Shared (ordNub, safeStrRead) import Text.Printf import qualified Control.Exception as E import qualified Data.ByteString as BS diff --git a/src/Text/Pandoc/App/Opt.hs b/src/Text/Pandoc/App/Opt.hs index d5e38b253..9ecf7e7ae 100644 --- a/src/Text/Pandoc/App/Opt.hs +++ b/src/Text/Pandoc/App/Opt.hs @@ -43,7 +43,8 @@ import Text.Pandoc.Options (TopLevelDivision (TopLevelDefault), import Text.Pandoc.Class (readFileStrict, fileExists, setVerbosity, report, PandocMonad(lookupEnv), getUserDataDir) import Text.Pandoc.Error (PandocError (PandocParseError, PandocSomeError)) -import Text.Pandoc.Shared (defaultUserDataDir, findM, ordNub) +import Text.Pandoc.Shared (findM, ordNub) +import Text.Pandoc.Data (defaultUserDataDir) import qualified Text.Pandoc.Parsing as P import Text.Pandoc.Readers.Metadata (yamlMap) import Text.Pandoc.Class.PandocPure diff --git a/src/Text/Pandoc/Data.hs b/src/Text/Pandoc/Data.hs index 8a8b51e60..66821f5b8 100644 --- a/src/Text/Pandoc/Data.hs +++ b/src/Text/Pandoc/Data.hs @@ -1,4 +1,5 @@ {-# LANGUAGE CPP #-} +{-# LANGUAGE ScopedTypeVariables #-} #ifdef EMBED_DATA_FILES {-# LANGUAGE TemplateHaskell #-} #endif @@ -16,6 +17,7 @@ Access to pandoc's data files. module Text.Pandoc.Data ( readDefaultDataFile , readDataFile , getDataFileNames + , defaultUserDataDir ) where import Text.Pandoc.Class (PandocMonad(..), checkUserDataDir, getTimestamp, getUserDataDir, getPOSIXTime) @@ -27,12 +29,13 @@ 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) -import System.Directory (getDirectoryContents) #endif -- | Read file from from the default data files. @@ -224,3 +227,21 @@ getDataFileNames = do (getDataDir >>= getDirectoryContents) #endif return $ "reference.docx" : "reference.odt" : "reference.pptx" : 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 = do + xdgDir <- E.catch (getXdgDirectory XdgData "pandoc") + (\(_ :: E.SomeException) -> return mempty) + legacyDir <- getAppUserDataDirectory "pandoc" + xdgExists <- doesDirectoryExist xdgDir + legacyDirExists <- doesDirectoryExist legacyDir + if not xdgExists && legacyDirExists + then return legacyDir + else return xdgDir diff --git a/src/Text/Pandoc/Shared.hs b/src/Text/Pandoc/Shared.hs index 34b67f0b7..75d26816f 100644 --- a/src/Text/Pandoc/Shared.hs +++ b/src/Text/Pandoc/Shared.hs @@ -83,9 +83,7 @@ module Text.Pandoc.Shared ( defaultBlocksSeparator, -- * Safe read safeRead, - safeStrRead, - -- * User data directory - defaultUserDataDir, + safeStrRead ) where import Codec.Archive.Zip @@ -902,24 +900,3 @@ safeStrRead s = case reads s of (d,x):_ | all isSpace x -> return d _ -> mzero --- --- User data directory --- - --- | 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 = do - xdgDir <- E.catch (getXdgDirectory XdgData "pandoc") - (\(_ :: E.SomeException) -> return mempty) - legacyDir <- getAppUserDataDirectory "pandoc" - xdgExists <- doesDirectoryExist xdgDir - legacyDirExists <- doesDirectoryExist legacyDir - if not xdgExists && legacyDirExists - then return legacyDir - else return xdgDir |
