wobsite.url.relative_to_absolute
Show source codeHide source code
fn pub relative_to_absolute(
current: String,
url: String,
as_file: Bool,
) -> String {
let steps = url.split('/').take_while(fn (v) { v == '..' }).count
let mut pending = steps + as_file.to_int
let chunks = current.split('/').to_array
while pending > 0 {
chunks.pop
pending -= 1
}
let base = String.join(chunks.into_iter, '/')
let rel = String.join(url.split('/').skip(steps), '/')
'${base}/${rel}'
}
fn pub static relative_to_absolute(current: String, url: String, as_file: Bool) -> String
Converts a relative URL to an absolute URL, based on the current/source URL of the document that links to the relative URL.
The current
argument specifies the URL of the document we are linking
from, while url
specifies the relative URL linked to.
If as_file
is set to true
, the last component of the target URL is treated
as if it were a file instead of a directory. This affects how many times we
have to move "up" in the returned relative URL.