git.iko.soy/typeable/pandoc
on master help sign in
Shared: Make collapseFilePath OS-agnostic
commitcc07d0c6bfac7081118d3cbf0baea197593adc11 authormpickering <matthewtpickering@gmail.com> dateThu 25 Sep 2014, 12:42 (12 years ago) parent56e4eca
2 files changed+28−23
src/Text/Pandoc/Shared.hs+9−5 tests/Tests/Shared.hs+19−18
src/Text/Pandoc/Shared.hs+9−5
@@ -1,5 +1,6 @@
11{-# LANGUAGE DeriveDataTypeable, CPP, MultiParamTypeClasses,
2− FlexibleContexts, ScopedTypeVariables, PatternGuards #-}
2+ FlexibleContexts, ScopedTypeVariables, PatternGuards,
3+ ViewPatterns #-}
34{-
45Copyright (C) 2006-2014 John MacFarlane <jgm@berkeley.edu>
56
@@ -106,7 +107,7 @@ import Network.URI ( escapeURIString, isURI, nonStrictRelativeTo,
106107 unEscapeString, parseURIReference, isAllowedInURI )
107108import qualified Data.Set as Set
108109import System.Directory
109−import System.FilePath (joinPath, splitDirectories)
110+import System.FilePath (joinPath, splitDirectories, pathSeparator, isPathSeparator)
110111import Text.Pandoc.MIME (MimeType, getMimeType)
111112import System.FilePath ( (</>), takeExtension, dropExtension)
112113import Data.Generics (Typeable, Data)
@@ -871,11 +872,14 @@ collapseFilePath = joinPath . reverse . foldl go [] . splitDirectories
871872 go rs "." = rs
872873 go r@(p:rs) ".." = case p of
873874 ".." -> ("..":r)
874− "/" -> ("..":r)
875+ (checkPathSeperator -> Just True) -> ("..":r)
875876 _ -> rs
876− go _ "/" = ["/"]
877+ go _ (checkPathSeperator -> Just True) = [[pathSeparator]]
877878 go rs x = x:rs
878−
879+ isSingleton [] = Nothing
880+ isSingleton [x] = Just x
881+ isSingleton _ = Nothing
882+ checkPathSeperator = fmap isPathSeparator . isSingleton
879883
880884--
881885-- Safe read
tests/Tests/Shared.hs+19−18
@@ -9,6 +9,7 @@ import Test.Framework.Providers.HUnit
99import Test.HUnit ( assertBool, (@?=) )
1010import Text.Pandoc.Builder
1111import Data.Monoid
12+import System.FilePath (joinPath)
1213
1314tests :: [Test]
1415tests = [ testGroup "normalize"
@@ -40,21 +41,21 @@ p_normalize_no_trailing_spaces ils = null ils' || last ils' /= Space
4041
4142testCollapse :: [Test]
4243testCollapse = map (testCase "collapse")
43− [ (collapseFilePath "" @?= "")
44− , (collapseFilePath "./foo" @?= "foo")
45− , (collapseFilePath "././../foo" @?= "../foo")
46− , (collapseFilePath "../foo" @?= "../foo")
47− , (collapseFilePath "/bar/../baz" @?= "/baz")
48− , (collapseFilePath "/../baz" @?= "/../baz")
49− , (collapseFilePath "./foo/.././bar/../././baz" @?= "baz")
50− , (collapseFilePath "./" @?= "")
51− , (collapseFilePath "././" @?= "")
52− , (collapseFilePath "../" @?= "..")
53− , (collapseFilePath ".././" @?= "..")
54− , (collapseFilePath "./../" @?= "..")
55− , (collapseFilePath "../../" @?= "../..")
56− , (collapseFilePath "parent/foo/baz/../bar" @?= "parent/foo/bar")
57− , (collapseFilePath "parent/foo/baz/../../bar" @?= "parent/bar")
58− , (collapseFilePath "parent/foo/.." @?= "parent")
59− , (collapseFilePath "/parent/foo/../../bar" @?= "/bar")
60− , (collapseFilePath "/./parent/foo" @?= "/parent/foo")]
44+ [ (collapseFilePath (joinPath [ ""]) @?= (joinPath [ ""]))
45+ , (collapseFilePath (joinPath [ ".","foo"]) @?= (joinPath [ "foo"]))
46+ , (collapseFilePath (joinPath [ ".",".","..","foo"]) @?= (joinPath [ joinPath ["..", "foo"]]))
47+ , (collapseFilePath (joinPath [ "..","foo"]) @?= (joinPath [ "..","foo"]))
48+ , (collapseFilePath (joinPath [ "","bar","..","baz"]) @?= (joinPath [ "","baz"]))
49+ , (collapseFilePath (joinPath [ "","..","baz"]) @?= (joinPath [ "","..","baz"]))
50+ , (collapseFilePath (joinPath [ ".","foo","..",".","bar","..",".",".","baz"]) @?= (joinPath [ "baz"]))
51+ , (collapseFilePath (joinPath [ ".",""]) @?= (joinPath [ ""]))
52+ , (collapseFilePath (joinPath [ ".",".",""]) @?= (joinPath [ ""]))
53+ , (collapseFilePath (joinPath [ "..",""]) @?= (joinPath [ ".."]))
54+ , (collapseFilePath (joinPath [ "..",".",""]) @?= (joinPath [ ".."]))
55+ , (collapseFilePath (joinPath [ ".","..",""]) @?= (joinPath [ ".."]))
56+ , (collapseFilePath (joinPath [ "..","..",""]) @?= (joinPath [ "..",".."]))
57+ , (collapseFilePath (joinPath [ "parent","foo","baz","..","bar"]) @?= (joinPath [ "parent","foo","bar"]))
58+ , (collapseFilePath (joinPath [ "parent","foo","baz","..","..","bar"]) @?= (joinPath [ "parent","bar"]))
59+ , (collapseFilePath (joinPath [ "parent","foo",".."]) @?= (joinPath [ "parent"]))
60+ , (collapseFilePath (joinPath [ "","parent","foo","..","..","bar"]) @?= (joinPath [ "","bar"]))
61+ , (collapseFilePath (joinPath [ "",".","parent","foo"]) @?= (joinPath [ "","parent","foo"]))]