Shared: readDefaultDataFile: normalize the paths.

This fixes bugs in `--self-contained` on pandoc compiled with
`embed_data_files`.  The bugs affect (a) paths containing `..`, (b)
Windows, where `\` is path separator.

Closes #833.
This commit is contained in:
John MacFarlane 2013-04-19 23:02:38 -07:00
parent 0ee081ef25
commit 6bd686a4f6

View file

@ -105,6 +105,7 @@ import Network.HTTP (findHeader, rspBody,
import Network.Browser (browse, setAllowRedirects, request)
#ifdef EMBED_DATA_FILES
import Text.Pandoc.Data (dataFiles)
import System.FilePath ( joinPath )
#else
import Paths_pandoc (getDataFileName)
#endif
@ -521,10 +522,15 @@ inDirectory path action = do
readDefaultDataFile :: FilePath -> IO B.ByteString
readDefaultDataFile fname =
#ifdef EMBED_DATA_FILES
case lookup fname dataFiles of
case lookup (makeCanonical fname) dataFiles of
Nothing -> ioError $ userError
$ "Data file `" ++ fname ++ "' does not exist"
Just contents -> return contents
where makeCanonical = joinPath . transformPathParts . splitBy (=='/')
transformPathParts = reverse . foldl go []
go as "." = as
go (_:as) ".." = as
go as x = x : as
#else
getDataFileName ("data" </> fname) >>= B.readFile
#endif