-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
Add filesystem func to transform a path to a URI #55454
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -613,3 +613,59 @@ relpath(path::AbstractString, startpath::AbstractString) = | |
for f in (:isdirpath, :splitdir, :splitdrive, :splitext, :normpath, :abspath) | ||
@eval $f(path::AbstractString) = $f(String(path)) | ||
end | ||
|
||
""" | ||
uripath(path::AbstractString) | ||
|
||
Encode `path` as a URI as per [RFC8089: The "file" URI | ||
Scheme](https://www.rfc-editor.org/rfc/rfc8089), [RFC3986: Uniform Resource | ||
Identifier (URI): Generic Syntax](https://www.rfc-editor.org/rfc/rfc3986), and | ||
the [Freedesktop File URI spec](https://www.freedesktop.org/wiki/Specifications/file-uri-spec/). | ||
|
||
## Examples | ||
|
||
```julia-repl | ||
julia> uripath("/home/user/example file.jl") # On a unix machine | ||
"file://<hostname>/home/user/example%20file.jl" | ||
|
||
juila> uripath("C:\\Users\\user\\example file.jl") # On a windows machine | ||
"file:///C:/Users/user/example%20file.jl" | ||
``` | ||
""" | ||
function uripath end | ||
|
||
@static if Sys.iswindows() | ||
function uripath(path::String) | ||
percent_escape(s) = # RFC3986 Section 2.1 | ||
'%' * join(map(b -> uppercase(string(b, base=16)), codeunits(s)), '%') | ||
encode_uri_component(s) = # RFC3986 Section 2.3 | ||
replace(s, r"[^A-Za-z0-9\-_.~/]+" => percent_escape) | ||
path = abspath(path) | ||
if startswith(path, "\\\\") # UNC path, RFC8089 Appendix E.3 | ||
unixpath = join(eachsplit(path, path_separator_re, keepempty=false), '/') | ||
string("file://", encode_uri_component(unixpath)) # RFC8089 Section 2 | ||
else | ||
drive, localpath = splitdrive(path) # Assuming that non-UNC absolute paths on Windows always have a drive component | ||
unixpath = join(eachsplit(localpath, path_separator_re, keepempty=false), '/') | ||
encdrive = replace(encode_uri_component(drive), "%3A" => ':', "%7C" => '|') # RFC8089 Appendices D.2, E.2.1, and E.2.2 | ||
string("file:///", encdrive, '/', encode_uri_component(unixpath)) # RFC8089 Section 2 | ||
end | ||
end | ||
else | ||
function uripath(path::String) | ||
percent_escape(s) = # RFC3986 Section 2.1 | ||
'%' * join(map(b -> uppercase(string(b, base=16)), codeunits(s)), '%') | ||
encode_uri_component(s) = # RFC3986 Section 2.3 | ||
replace(s, r"[^A-Za-z0-9\-_.~/]+" => percent_escape) | ||
localpath = join(eachsplit(abspath(path), path_separator_re, keepempty=false), '/') | ||
host = if ispath("/proc/sys/fs/binfmt_misc/WSLInterop") # WSL sigil | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. xref #36425 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, that's a relevant xref. Checking for |
||
distro = get(ENV, "WSL_DISTRO_NAME", "") # See <https://patrickwu.space/wslconf/> | ||
"wsl\$/$distro" # See <https://github.com/microsoft/terminal/pull/14993> and <https://learn.microsoft.com/en-us/windows/wsl/filesystems> | ||
else | ||
gethostname() # Freedesktop File URI Spec, Hostnames section | ||
end | ||
string("file://", encode_uri_component(host), '/', encode_uri_component(localpath)) # RFC8089 Section 2 | ||
end | ||
end | ||
|
||
uripath(path::AbstractString) = uripath(String(path)) |
Uh oh!
There was an error while loading. Please reload this page.