<lambda>null1 package org.jetbrains.dokka
2
3 import java.io.File
4
5 interface Location {
6 val path: String get
7 fun relativePathTo(other: Location, anchor: String? = null): String
8 }
9
10 /**
11 * Represents locations in the documentation in the form of [path](File).
12 *
13 * $file: [File] for this location
14 * $path: [String] representing path of this location
15 */
16 data class FileLocation(val file: File) : Location {
17 override val path: String
18 get() = file.path
19
relativePathTonull20 override fun relativePathTo(other: Location, anchor: String?): String {
21 if (other !is FileLocation) {
22 throw IllegalArgumentException("$other is not a FileLocation")
23 }
24 if (file.path.substringBeforeLast(".") == other.file.path.substringBeforeLast(".") && anchor == null) {
25 return "./${file.name}"
26 }
27 val ownerFolder = file.parentFile!!
28 val relativePath = ownerFolder.toPath().relativize(other.file.toPath()).toString().replace(File.separatorChar, '/')
29 return if (anchor == null) relativePath else relativePath + "#" + anchor
30 }
31 }
32
relativePathToNodenull33 fun relativePathToNode(qualifiedName: List<String>, hasMembers: Boolean): String {
34 val parts = qualifiedName.map { identifierToFilename(it) }.filterNot { it.isEmpty() }
35 return if (!hasMembers) {
36 // leaf node, use file in owner's folder
37 parts.joinToString("/")
38 } else {
39 parts.joinToString("/") + (if (parts.none()) "" else "/") + "index"
40 }
41 }
42
43
<lambda>null44 fun relativePathToNode(node: DocumentationNode) = relativePathToNode(node.path.map { it.name }, node.members.any())
45
identifierToFilenamenull46 fun identifierToFilename(path: String): String {
47 val escaped = path.replace('<', '-').replace('>', '-')
48 val lowercase = escaped.replace("[A-Z]".toRegex()) { matchResult -> "-" + matchResult.value.toLowerCase() }
49 return if (lowercase == "index") "--index--" else lowercase
50 }
51
relativePathToLocationnull52 fun NodeLocationAwareGenerator.relativePathToLocation(owner: DocumentationNode, node: DocumentationNode): String {
53 return location(owner).relativePathTo(location(node), null)
54 }
55
relativePathToRootnull56 fun NodeLocationAwareGenerator.relativePathToRoot(from: Location): File {
57 val file = File(from.path).parentFile
58 return root.relativeTo(file)
59 }
60
toUnixStringnull61 fun File.toUnixString() = toString().replace(File.separatorChar, '/')
62