From 6bd686a4f6ab0c1584b0aaff995be57e93c8ed38 Mon Sep 17 00:00:00 2001
From: John MacFarlane <fiddlosopher@gmail.com>
Date: Fri, 19 Apr 2013 23:02:38 -0700
Subject: 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.
---
 src/Text/Pandoc/Shared.hs | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

(limited to 'src')

diff --git a/src/Text/Pandoc/Shared.hs b/src/Text/Pandoc/Shared.hs
index 4cca80eda..001af6c6d 100644
--- a/src/Text/Pandoc/Shared.hs
+++ b/src/Text/Pandoc/Shared.hs
@@ -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
-- 
cgit v1.2.3