I’ve been knocking my head against a wall trying to fix an issue in the .xspf (VLC) playlist generator I’m writing. I used “walkdir” to get the paths of the files but I can’t seem to convert them into the correct format (HTML/ANSI URL/URI?) that the .xspf file needs. I am working in Windows.
The following does the job for the strings in the post:
julia> s = "file:///C:\\Music\\Adiemus\\Adiemus IV_ The Eternal Knot\\04 The Wooing Of Etain.mp3"
"file:///C:\\Music\\Adiemus\\Adiemus IV_ The Eternal Knot\\04 The Wooing Of Etain.mp3"
julia> println(s)
file:///C:\Music\Adiemus\Adiemus IV_ The Eternal Knot\04 The Wooing Of Etain.mp3
julia> t = replace(replace(s, ' '=>"%20"),'\\'=>"/")
"file:///C:/Music/Adiemus/Adiemus%20IV_%20The%20Eternal%20Knot/04%20The%20Wooing%20Of%20Etain.mp3"
But this is a brittle solution. There are many gotchas with these encodings and depending on the level of correctness and the degree to which the inputs are exposed to manipulation, there is much more care which can be taken.
See What to use for URL encoding? - #3 by quinnj — the HTTP.jl package the URIs.jl package has a function escapeuri to escape strings for URIs. It won’t convert backslash to forward slashes, but you can apply it to each component of the path (via splitpath, which splits by \ on Windows) to escape special characters like spaces. In particular, you can define the function:
Thanks Dan and stevengj, I used both your methods to get it working. I used the path2uri function in which i did a replace for the Di which got mangled. It’s all working now.
I wanted to move this stuff out into its own package forever, but always got stuck. GitHub - davidanthoff/URIs2.jl is how far I got, should probably just finish that at some point.