diff options
author | Simon Ogorodnik <Simon.Ogorodnik@jetbrains.com> | 2017-12-28 20:27:28 +0300 |
---|---|---|
committer | Simon Ogorodnik <Simon.Ogorodnik@jetbrains.com> | 2018-07-13 18:29:04 +0300 |
commit | 93ce43340730111df977921e62cae93c2bcc4a5b (patch) | |
tree | 5114aa490e2962f7e88a146db57ea5a9032dea29 /core | |
parent | e4d3abee285844c002a8984efbae0bf0be23d237 (diff) | |
download | dokka-93ce43340730111df977921e62cae93c2bcc4a5b.tar.gz dokka-93ce43340730111df977921e62cae93c2bcc4a5b.tar.bz2 dokka-93ce43340730111df977921e62cae93c2bcc4a5b.zip |
[backport] Add uri.relativeTo
Original: cca63f7
Diffstat (limited to 'core')
-rw-r--r-- | core/src/main/kotlin/Utilities/Uri.kt | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/core/src/main/kotlin/Utilities/Uri.kt b/core/src/main/kotlin/Utilities/Uri.kt new file mode 100644 index 00000000..5b520188 --- /dev/null +++ b/core/src/main/kotlin/Utilities/Uri.kt @@ -0,0 +1,40 @@ +package org.jetbrains.dokka + +import java.net.URI + + +fun URI.relativeTo(uri: URI): URI { + // Normalize paths to remove . and .. segments + val base = uri.normalize() + val child = this.normalize() + + fun StringBuilder.appendRelativePath() { + // Split paths into segments + var bParts = base.path.split('/').dropLastWhile { it.isEmpty() } + val cParts = child.path.split('/').dropLastWhile { it.isEmpty() } + + // Discard trailing segment of base path + if (bParts.isNotEmpty() && !base.path.endsWith("/")) { + bParts = bParts.dropLast(1) + } + + // Compute common prefix + val commonPartsSize = bParts.zip(cParts).count { (basePart, childPart) -> basePart == childPart } + bParts.drop(commonPartsSize).joinTo(this, separator = "") { "../" } + cParts.drop(commonPartsSize).joinTo(this, separator = "/") + } + + return URI.create(buildString { + if (base.path != child.path) { + appendRelativePath() + } + child.rawQuery?.let { + append("?") + append(it) + } + child.rawFragment?.let { + append("#") + append(it) + } + }) +}
\ No newline at end of file |