diff --git a/REQUIRE b/REQUIRE index 12d0ac8..c06f962 100644 --- a/REQUIRE +++ b/REQUIRE @@ -1,4 +1,5 @@ julia 0.6 Compat 0.17.0 +URIParser FilePathsBase 0.1.0 -Reexport 0.1.0 \ No newline at end of file +Reexport 0.1.0 diff --git a/src/FilePaths.jl b/src/FilePaths.jl index 8d508d9..3cb771e 100644 --- a/src/FilePaths.jl +++ b/src/FilePaths.jl @@ -2,7 +2,9 @@ __precompile__() module FilePaths -using Reexport +using Reexport, URIParser @reexport using FilePathsBase +include("uri.jl") + end # end of module diff --git a/src/uri.jl b/src/uri.jl new file mode 100644 index 0000000..8b1fc23 --- /dev/null +++ b/src/uri.jl @@ -0,0 +1,20 @@ +function URIParser.URI(p::AbstractPath) + if isempty(root(p)) + throw(ArgumentError("$p is not an absolute path")) + end + + b = IOBuffer() + print(b, "file://") + + if !isempty(drive(p)) + print(b, "/") + print(b, drive(p)) + end + + for i=2:length(p.parts) + print(b, "/") + print(b, URIParser.escape(p.parts[i])) + end + + return URIParser.URI(String(take!(b))) +end diff --git a/test/runtests.jl b/test/runtests.jl index 173b594..05f04d0 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -3,5 +3,6 @@ using Base.Test @testset "FilePaths" begin +include("test_uri.jl") end diff --git a/test/test_uri.jl b/test/test_uri.jl new file mode 100644 index 0000000..93eb733 --- /dev/null +++ b/test/test_uri.jl @@ -0,0 +1,9 @@ +using URIParser +using FilePaths + +@testset "URI" begin + @test string(URI(p"/foo/bar")) == "file:///foo/bar" + @test string(URI(p"/foo foo/bar")) == "file:///foo%20foo/bar" + @test_throws ArgumentError URI(p"foo/bar") + @test string(URI(WindowsPath("C:\\foo\\bar"))) == "file:///C:/foo/bar" +end