diff --git a/src/Electron.jl b/src/Electron.jl index f3cfc960..82f1d7cf 100644 --- a/src/Electron.jl +++ b/src/Electron.jl @@ -4,6 +4,7 @@ module Electron using JSON, URIParser export Application, Window, URI, windows, applications +export @LOCAL const OptDict = Dict{String, Any} struct JSError @@ -309,4 +310,34 @@ function Base.close(win::Window) return nothing end +""" + URI_file(base, filespec) + +Construct an absolute URI to `filespec` relative to `base`. +""" +URI_file(base, filespec) = URI_file(base, URI("file:///$filespec")) +function URI_file(base, filespec::URI) + base = join(map(URIParser.escape, split(base, Base.Filesystem.path_separator_re)), "/") + return URI(filespec, path = base * filespec.path) +end + +""" + pwd(URI) + +Return `pwd()` as a `URI` resource. +""" +Base.pwd(::Type{URI}) = URI_file(pwd(), "") + +""" + @LOCAL(filespec) + +Construct an absolute URI to `filespec` relative to the source file containing the macro call. +""" +macro LOCAL(filespec) + # v0.7: base = String(__source__.file) + # filespec isa String && return URI_file(base, filespec) # can construct eagerly + # return :(URI_file($base, $(esc(filespec)))) + return :(URI_file(@__DIR__, $(esc(filespec)))) +end + end diff --git a/test/runtests.jl b/test/runtests.jl index ae6123e9..9e64572d 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -2,6 +2,21 @@ using Electron using URIParser using Base.Test +@testset "local URI" begin + dir = pwd(URI) + @test unescape(dir.path) == join(push!(split(pwd(), Base.Filesystem.path_separator_re), ""), "/") + @test dir.query == dir.fragment == dir.host == "" + @test string(dir) == "file://$(dir.path)" + + __dirname = @__DIR__ + dir = Electron.URI_file(__dirname, "") + @test URI(dir, path = dir.path * "test.html", query = "a", fragment = "b") == + @LOCAL("test.html?a#b") == + @LOCAL(begin; "test.html?a#b"; end) == + Electron.URI_file(__dirname, "test.html?a#b") +end + + @testset "Electron" begin w = Window(URI("file://test.html")) @@ -25,7 +40,7 @@ close(w) @test length(applications()) == 1 @test isempty(windows(a)) == 1 -w2 = Window(URI("file://test.html")) +w2 = Window(@LOCAL("test.html")) close(a) @test length(applications()) == 1 @@ -35,9 +50,9 @@ sleep(1) @test isempty(applications()) @test isempty(windows(a)) -w3 = Window(Dict("url" => string(URI("file://test.html")))) +w3 = Window(Dict("url" => string(@LOCAL("test.html")))) -w4 = Window(URI("file://test.html"), options=Dict("title" => "Window title")) +w4 = Window(@LOCAL("test.html"), options=Dict("title" => "Window title")) w5 = Window("", options=Dict("title" => "Window title"))