git.iko.soy/typeable/pandoc
on master help sign in
Shared: Make collapseFilePath OS-agnostic
cc07d0cmpickering
src/Text/Pandoc/Shared.hs+9−5
@@ -1,5 +1,6 @@
1{-# LANGUAGE DeriveDataTypeable, CPP, MultiParamTypeClasses,
1{-# LANGUAGE DeriveDataTypeable, CPP, MultiParamTypeClasses,
2− FlexibleContexts, ScopedTypeVariables, PatternGuards #-}
2+ FlexibleContexts, ScopedTypeVariables, PatternGuards,
3+ ViewPatterns #-}
3{-
4{-
4Copyright (C) 2006-2014 John MacFarlane <jgm@berkeley.edu>
5Copyright (C) 2006-2014 John MacFarlane <jgm@berkeley.edu>
5
6
@@ -106,7 +107,7 @@ import Network.URI ( escapeURIString, isURI, nonStrictRelativeTo,
106 unEscapeString, parseURIReference, isAllowedInURI )
107 unEscapeString, parseURIReference, isAllowedInURI )
107import qualified Data.Set as Set
108import qualified Data.Set as Set
108import System.Directory
109import System.Directory
109−import System.FilePath (joinPath, splitDirectories)
110+import System.FilePath (joinPath, splitDirectories, pathSeparator, isPathSeparator)
110import Text.Pandoc.MIME (MimeType, getMimeType)
111import Text.Pandoc.MIME (MimeType, getMimeType)
111import System.FilePath ( (</>), takeExtension, dropExtension)
112import System.FilePath ( (</>), takeExtension, dropExtension)
112import Data.Generics (Typeable, Data)
113import Data.Generics (Typeable, Data)
@@ -871,11 +872,14 @@ collapseFilePath = joinPath . reverse . foldl go [] . splitDirectories
871 go rs "." = rs
872 go rs "." = rs
872 go r@(p:rs) ".." = case p of
873 go r@(p:rs) ".." = case p of
873 ".." -> ("..":r)
874 ".." -> ("..":r)
874− "/" -> ("..":r)
875+ (checkPathSeperator -> Just True) -> ("..":r)
875 _ -> rs
876 _ -> rs
876− go _ "/" = ["/"]
877+ go _ (checkPathSeperator -> Just True) = [[pathSeparator]]
877 go rs x = x:rs
878 go rs x = x:rs
878−
879+ isSingleton [] = Nothing
880+ isSingleton [x] = Just x
881+ isSingleton _ = Nothing
882+ checkPathSeperator = fmap isPathSeparator . isSingleton
879
883
880--
884--
881-- Safe read
885-- Safe read
tests/Tests/Shared.hs+19−18
@@ -9,6 +9,7 @@ import Test.Framework.Providers.HUnit
9import Test.HUnit ( assertBool, (@?=) )
9import Test.HUnit ( assertBool, (@?=) )
10import Text.Pandoc.Builder
10import Text.Pandoc.Builder
11import Data.Monoid
11import Data.Monoid
12+import System.FilePath (joinPath)
12
13
13tests :: [Test]
14tests :: [Test]
14tests = [ testGroup "normalize"
15tests = [ testGroup "normalize"
@@ -40,21 +41,21 @@ p_normalize_no_trailing_spaces ils = null ils' || last ils' /= Space
40
41
41testCollapse :: [Test]
42testCollapse :: [Test]
42testCollapse = map (testCase "collapse")
43testCollapse = map (testCase "collapse")
43− [ (collapseFilePath "" @?= "")
44+ [ (collapseFilePath (joinPath [ ""]) @?= (joinPath [ ""]))
44− , (collapseFilePath "./foo" @?= "foo")
45+ , (collapseFilePath (joinPath [ ".","foo"]) @?= (joinPath [ "foo"]))
45− , (collapseFilePath "././../foo" @?= "../foo")
46+ , (collapseFilePath (joinPath [ ".",".","..","foo"]) @?= (joinPath [ joinPath ["..", "foo"]]))
46− , (collapseFilePath "../foo" @?= "../foo")
47+ , (collapseFilePath (joinPath [ "..","foo"]) @?= (joinPath [ "..","foo"]))
47− , (collapseFilePath "/bar/../baz" @?= "/baz")
48+ , (collapseFilePath (joinPath [ "","bar","..","baz"]) @?= (joinPath [ "","baz"]))
48− , (collapseFilePath "/../baz" @?= "/../baz")
49+ , (collapseFilePath (joinPath [ "","..","baz"]) @?= (joinPath [ "","..","baz"]))
49− , (collapseFilePath "./foo/.././bar/../././baz" @?= "baz")
50+ , (collapseFilePath (joinPath [ ".","foo","..",".","bar","..",".",".","baz"]) @?= (joinPath [ "baz"]))
50− , (collapseFilePath "./" @?= "")
51+ , (collapseFilePath (joinPath [ ".",""]) @?= (joinPath [ ""]))
51− , (collapseFilePath "././" @?= "")
52+ , (collapseFilePath (joinPath [ ".",".",""]) @?= (joinPath [ ""]))
52− , (collapseFilePath "../" @?= "..")
53+ , (collapseFilePath (joinPath [ "..",""]) @?= (joinPath [ ".."]))
53− , (collapseFilePath ".././" @?= "..")
54+ , (collapseFilePath (joinPath [ "..",".",""]) @?= (joinPath [ ".."]))
54− , (collapseFilePath "./../" @?= "..")
55+ , (collapseFilePath (joinPath [ ".","..",""]) @?= (joinPath [ ".."]))
55− , (collapseFilePath "../../" @?= "../..")
56+ , (collapseFilePath (joinPath [ "..","..",""]) @?= (joinPath [ "..",".."]))
56− , (collapseFilePath "parent/foo/baz/../bar" @?= "parent/foo/bar")
57+ , (collapseFilePath (joinPath [ "parent","foo","baz","..","bar"]) @?= (joinPath [ "parent","foo","bar"]))
57− , (collapseFilePath "parent/foo/baz/../../bar" @?= "parent/bar")
58+ , (collapseFilePath (joinPath [ "parent","foo","baz","..","..","bar"]) @?= (joinPath [ "parent","bar"]))
58− , (collapseFilePath "parent/foo/.." @?= "parent")
59+ , (collapseFilePath (joinPath [ "parent","foo",".."]) @?= (joinPath [ "parent"]))
59− , (collapseFilePath "/parent/foo/../../bar" @?= "/bar")
60+ , (collapseFilePath (joinPath [ "","parent","foo","..","..","bar"]) @?= (joinPath [ "","bar"]))
60− , (collapseFilePath "/./parent/foo" @?= "/parent/foo")]
61+ , (collapseFilePath (joinPath [ "",".","parent","foo"]) @?= (joinPath [ "","parent","foo"]))]