aboutsummaryrefslogtreecommitdiff
path: root/src/Locations
diff options
context:
space:
mode:
Diffstat (limited to 'src/Locations')
-rw-r--r--src/Locations/FoldersLocationService.kt25
-rw-r--r--src/Locations/LocationService.kt36
-rw-r--r--src/Locations/SingleFolderLocationService.kt6
3 files changed, 40 insertions, 27 deletions
diff --git a/src/Locations/FoldersLocationService.kt b/src/Locations/FoldersLocationService.kt
index db06dc2c..6f3954c4 100644
--- a/src/Locations/FoldersLocationService.kt
+++ b/src/Locations/FoldersLocationService.kt
@@ -3,15 +3,18 @@ package org.jetbrains.dokka
import java.io.File
public fun FoldersLocationService(root: String): FoldersLocationService = FoldersLocationService(File(root))
-public class FoldersLocationService(val root: File) : LocationService {
- override fun location(node: DocumentationNode): Location {
- val parts = node.path.map { identifierToFilename(it.name) }
- val folder = if (node.members.none()) {
- // leaf node, use file in owner's folder
- parts.joinToString("/", limit = parts.size - 1, truncated = "") + "/" + parts.last()
- } else {
- parts.joinToString("/") + (if (parts.none()) "" else "/") + "index"
- }
- return Location(File(root, folder))
+public class FoldersLocationService(val root: File) : FileLocationService {
+ override fun location(node: DocumentationNode): FileLocation {
+ return FileLocation(File(root, relativePathToNode(node)))
}
-} \ No newline at end of file
+}
+
+fun relativePathToNode(node: DocumentationNode): String {
+ val parts = node.path.map { identifierToFilename(it.name) }.filterNot { it.isEmpty() }
+ return if (node.members.none()) {
+ // leaf node, use file in owner's folder
+ parts.joinToString("/")
+ } else {
+ parts.joinToString("/") + (if (parts.none()) "" else "/") + "index"
+ }
+}
diff --git a/src/Locations/LocationService.kt b/src/Locations/LocationService.kt
index 26a67382..d364c511 100644
--- a/src/Locations/LocationService.kt
+++ b/src/Locations/LocationService.kt
@@ -2,6 +2,11 @@ package org.jetbrains.dokka
import java.io.File
+public trait Location {
+ val path: String get
+ fun relativePathTo(other: Location, extension: String): String
+}
+
/**
* Represents locations in the documentation in the form of [path](File).
*
@@ -10,9 +15,18 @@ import java.io.File
* $file: [File] for this location
* $path: [String] representing path of this location
*/
-public data class Location(val file: File) {
- public val path : String
+public data class FileLocation(val file: File): Location {
+ override val path : String
get() = file.path
+
+ override fun relativePathTo(other: Location, extension: String): String {
+ if (other !is FileLocation) {
+ throw IllegalArgumentException("$other is not a FileLocation")
+ }
+ val ownerFolder = file.getParentFile()!!
+ val memberPath = other.file.appendExtension(extension)
+ return ownerFolder.getRelativePath(memberPath).path
+ }
}
/**
@@ -32,6 +46,11 @@ public trait LocationService {
}
+public trait FileLocationService: LocationService {
+ override fun location(node: DocumentationNode): FileLocation
+}
+
+
public fun identifierToFilename(path: String): String {
val escaped = path.replace('<', '-').replace('>', '-')
val lowercase = escaped.replaceAll("[A-Z]") { matchResult -> "-" + matchResult.group().toLowerCase() }
@@ -41,15 +60,6 @@ public fun identifierToFilename(path: String): String {
/**
* Returns relative location between two nodes. Used for relative links in documentation.
*/
-fun LocationService.relativeLocation(owner: DocumentationNode, node: DocumentationNode, extension: String): Location {
- return relativeLocation(location(owner), node, extension)
-}
-
-/**
- * Returns relative location between base location and a node. Used for relative links in documentation.
- */
-fun LocationService.relativeLocation(owner: Location, node: DocumentationNode, extension: String): Location {
- val ownerFolder = owner.file.getParentFile()!!
- val memberPath = location(node).file.appendExtension(extension)
- return Location(ownerFolder.getRelativePath(memberPath))
+fun LocationService.relativePathToLocation(owner: DocumentationNode, node: DocumentationNode, extension: String): String {
+ return location(owner).relativePathTo(location(node), extension)
}
diff --git a/src/Locations/SingleFolderLocationService.kt b/src/Locations/SingleFolderLocationService.kt
index 4f926959..aefab450 100644
--- a/src/Locations/SingleFolderLocationService.kt
+++ b/src/Locations/SingleFolderLocationService.kt
@@ -3,9 +3,9 @@ package org.jetbrains.dokka
import java.io.File
public fun SingleFolderLocationService(root: String): SingleFolderLocationService = SingleFolderLocationService(File(root))
-public class SingleFolderLocationService(val root: File) : LocationService {
- override fun location(node: DocumentationNode): Location {
+public class SingleFolderLocationService(val root: File) : FileLocationService {
+ override fun location(node: DocumentationNode): FileLocation {
val filename = node.path.map { identifierToFilename(it.name) }.joinToString("-")
- return Location(File(root, filename))
+ return FileLocation(File(root, filename))
}
} \ No newline at end of file