Skip to content

Instantly share code, notes, and snippets.

@rgchris
Last active December 20, 2020 18:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rgchris/1bb355d337e4727d37b934e1d53544e3 to your computer and use it in GitHub Desktop.
Save rgchris/1bb355d337e4727d37b934e1d53544e3 to your computer and use it in GitHub Desktop.
Updated Clean-Path function for Ren-C
Rebol [
Title: "Modified Clean-Path function"
Date: 20-Dec-2020
Author: "Christopher Ross-Gill"
]
clean-path: func [
{Returns new directory path with `.` and `..` processed.}
path [file! url! text!]
/only "Do not prepend current directory"
/dir "Add a trailing / if missing"
<local> scheme current target count part
][
scheme: _
case [
url? path [
scheme: make make object! [scheme: user: pass: host: port-id: path: _] decode-url path
target: either scheme/path [
to file! scheme/path
][
copy %/
]
]
any [
only
text? path
#"/" = first path
][
target: copy path
]
file? path [
if url? current: what-dir [
scheme: make make object! [scheme: user: pass: host: port-id: path: _] decode-url current
current: any [
scheme/path
copy %/
]
]
target: to file! unspaced [current path]
]
]
if all [
dir
not #"/" = last target
][
append target #"/"
]
path: make type of target length of target
count: 0
parse reverse target [
some [
"../"
(count: me + 1)
|
"./"
|
"/"
(
if any [
not file? target
#"/" <> last path
][
append path #"/"
]
)
|
copy part: [to "/" | to end] (
either count > 0 [
count: me - 1
][
if not find ["" "." ".."] as text! part [
append path part
]
]
)
]
end
]
if all [
#"/" = last path
#"/" <> last target
][
remove back tail of path
]
reverse path
either scheme [
to url! head insert path unspaced [
form scheme/scheme "://"
if scheme/user [
unspaced [
scheme/user
if scheme/pass [
unspaced [":" scheme/pass]
]
"@"
]
]
scheme/host
if scheme/port-id [
unspaced [":" scheme/port-id]
]
]
][
path
]
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment