aboutsummaryrefslogtreecommitdiff
path: root/core/src/main/kotlin/Locations/Location.kt
diff options
context:
space:
mode:
authorSimon Ogorodnik <Simon.Ogorodnik@jetbrains.com>2017-12-05 18:25:08 +0300
committerSimon Ogorodnik <Simon.Ogorodnik@jetbrains.com>2017-12-05 18:25:08 +0300
commit214e16e07d93b3565c433ed67b78796e80f43ec5 (patch)
tree6b350d92958cdeaf3e25479f95e8ce8d33e075e0 /core/src/main/kotlin/Locations/Location.kt
parent955a4f706c54a271473424c817daaeb02bea5cc5 (diff)
downloaddokka-214e16e07d93b3565c433ed67b78796e80f43ec5.tar.gz
dokka-214e16e07d93b3565c433ed67b78796e80f43ec5.tar.bz2
dokka-214e16e07d93b3565c433ed67b78796e80f43ec5.zip
Tidy around
Diffstat (limited to 'core/src/main/kotlin/Locations/Location.kt')
-rw-r--r--core/src/main/kotlin/Locations/Location.kt59
1 files changed, 59 insertions, 0 deletions
diff --git a/core/src/main/kotlin/Locations/Location.kt b/core/src/main/kotlin/Locations/Location.kt
new file mode 100644
index 00000000..17538ff5
--- /dev/null
+++ b/core/src/main/kotlin/Locations/Location.kt
@@ -0,0 +1,59 @@
+package org.jetbrains.dokka
+
+import java.io.File
+
+interface Location {
+ val path: String get
+ fun relativePathTo(other: Location, anchor: String? = null): String
+}
+
+/**
+ * Represents locations in the documentation in the form of [path](File).
+ *
+ * $file: [File] for this location
+ * $path: [String] representing path of this location
+ */
+data class FileLocation(val file: File): Location {
+ override val path : String
+ get() = file.path
+
+ override fun relativePathTo(other: Location, anchor: String?): String {
+ if (other !is FileLocation) {
+ throw IllegalArgumentException("$other is not a FileLocation")
+ }
+ if (file.path.substringBeforeLast(".") == other.file.path.substringBeforeLast(".") && anchor == null) {
+ return "./${file.name}"
+ }
+ val ownerFolder = file.parentFile!!
+ val relativePath = ownerFolder.toPath().relativize(other.file.toPath()).toString().replace(File.separatorChar, '/')
+ return if (anchor == null) relativePath else relativePath + "#" + anchor
+ }
+}
+
+
+fun relativePathToNode(qualifiedName: List<String>, hasMembers: Boolean): String {
+ val parts = qualifiedName.map { identifierToFilename(it) }.filterNot { it.isEmpty() }
+ return if (!hasMembers) {
+ // leaf node, use file in owner's folder
+ parts.joinToString("/")
+ } else {
+ parts.joinToString("/") + (if (parts.none()) "" else "/") + "index"
+ }
+}
+
+fun relativePathToNode(node: DocumentationNode) = relativePathToNode(node.path.map { it.name }, node.members.any())
+
+fun identifierToFilename(path: String): String {
+ val escaped = path.replace('<', '-').replace('>', '-')
+ val lowercase = escaped.replace("[A-Z]".toRegex()) { matchResult -> "-" + matchResult.value.toLowerCase() }
+ return if (lowercase == "index") "--index--" else lowercase
+}
+
+fun NodeLocationAwareGenerator.relativePathToLocation(owner: DocumentationNode, node: DocumentationNode): String {
+ return location(owner).relativePathTo(location(node), null)
+}
+
+fun NodeLocationAwareGenerator.relativeToRoot(from: Location): File {
+ val file = File(from.path)
+ return file.relativeTo(root)
+} \ No newline at end of file