diff options
author | Dmitry Jemerov <yole@jetbrains.com> | 2015-02-19 18:59:00 +0100 |
---|---|---|
committer | Dmitry Jemerov <yole@jetbrains.com> | 2015-02-19 18:59:00 +0100 |
commit | d9bfa029b0ecf300ae47cf1033db2d5cb323d705 (patch) | |
tree | ff8a9f7d4b0b1233d3d7b1e89a74047c64c8dbd7 /src | |
parent | 2822a3eee07a27495d5af4fc36304d483756d3a6 (diff) | |
download | dokka-d9bfa029b0ecf300ae47cf1033db2d5cb323d705.tar.gz dokka-d9bfa029b0ecf300ae47cf1033db2d5cb323d705.tar.bz2 dokka-d9bfa029b0ecf300ae47cf1033db2d5cb323d705.zip |
remove dependency of Location on a File; use more meaningful locations in tests
Diffstat (limited to 'src')
-rw-r--r-- | src/Formats/HtmlFormatService.kt | 6 | ||||
-rw-r--r-- | src/Formats/KotlinWebsiteFormatService.kt | 3 | ||||
-rw-r--r-- | src/Formats/MarkdownFormatService.kt | 4 | ||||
-rw-r--r-- | src/Formats/StructuredFormatService.kt | 13 | ||||
-rw-r--r-- | src/Generation/ConsoleGenerator.kt | 2 | ||||
-rw-r--r-- | src/Generation/FileGenerator.kt | 2 | ||||
-rw-r--r-- | src/Locations/FoldersLocationService.kt | 25 | ||||
-rw-r--r-- | src/Locations/LocationService.kt | 36 | ||||
-rw-r--r-- | src/Locations/SingleFolderLocationService.kt | 6 |
9 files changed, 52 insertions, 45 deletions
diff --git a/src/Formats/HtmlFormatService.kt b/src/Formats/HtmlFormatService.kt index 3415bfd7..8442c66f 100644 --- a/src/Formats/HtmlFormatService.kt +++ b/src/Formats/HtmlFormatService.kt @@ -82,10 +82,6 @@ public open class HtmlFormatService(locationService: LocationService, to.appendln("</td>") } - override fun formatLink(text: String, location: Location): String { - return "<a href=\"${location.path}\">${text}</a>" - } - override fun formatLink(text: String, href: String): String { return "<a href=\"${href}\">${text}</a>" } @@ -139,7 +135,7 @@ public open class HtmlFormatService(locationService: LocationService, val link = ContentNodeLink(node) link.append(languageService.render(node, LanguageService.RenderMode.FULL)) val signature = formatText(location, link) - to.appendln("${formatLink(signature, location)}<br/>") + to.appendln("<a href=\"${location.path}\">${signature}</a><br/>") } override fun appendOutlineLevel(to: StringBuilder, body: () -> Unit) { diff --git a/src/Formats/KotlinWebsiteFormatService.kt b/src/Formats/KotlinWebsiteFormatService.kt index 4f7a013a..5e616fad 100644 --- a/src/Formats/KotlinWebsiteFormatService.kt +++ b/src/Formats/KotlinWebsiteFormatService.kt @@ -22,6 +22,8 @@ public class KotlinWebsiteFormatService(locationService: LocationService, return "" } + /* + TODO this should be a LocationService override fun formatLink(text: String, location: Location): String { val href = location.path.replace("\\", "/") .replaceAfterLast(".", "html") @@ -29,6 +31,7 @@ public class KotlinWebsiteFormatService(locationService: LocationService, return "<a href=\"${href}\">${text}</a>" } + */ override fun formatLink(text: String, href: String): String { return "<a href=\"${href}\">${text}</a>" diff --git a/src/Formats/MarkdownFormatService.kt b/src/Formats/MarkdownFormatService.kt index 3598f29d..b855ec9c 100644 --- a/src/Formats/MarkdownFormatService.kt +++ b/src/Formats/MarkdownFormatService.kt @@ -50,10 +50,6 @@ public open class MarkdownFormatService(locationService: LocationService, return "~~$text~~" } - override public fun formatLink(text: String, location: Location): String { - return "[$text](${location.path})" - } - override fun formatLink(text: String, href: String): String { return "[$text]($href)" } diff --git a/src/Formats/StructuredFormatService.kt b/src/Formats/StructuredFormatService.kt index 10650ab2..32e22fd7 100644 --- a/src/Formats/StructuredFormatService.kt +++ b/src/Formats/StructuredFormatService.kt @@ -3,7 +3,7 @@ package org.jetbrains.dokka import java.util.LinkedHashMap import org.jetbrains.dokka.LanguageService.RenderMode -public data class FormatLink(val text: String, val location: Location) +public data class FormatLink(val text: String, val href: String) public abstract class StructuredFormatService(val locationService: LocationService, val languageService: LanguageService) : FormatService { @@ -25,9 +25,8 @@ public abstract class StructuredFormatService(val locationService: LocationServi public abstract fun formatSymbol(text: String): String public abstract fun formatKeyword(text: String): String public abstract fun formatIdentifier(text: String): String - public abstract fun formatLink(text: String, location: Location): String public abstract fun formatLink(text: String, href: String): String - public open fun formatLink(link: FormatLink): String = formatLink(formatText(link.text), link.location) + public open fun formatLink(link: FormatLink): String = formatLink(formatText(link.text), link.href) public abstract fun formatStrong(text: String): String public abstract fun formatStrikethrough(text: String): String public abstract fun formatEmphasis(text: String): String @@ -55,7 +54,7 @@ public abstract class StructuredFormatService(val locationService: LocationServi is ContentListItem -> append(formatListItem(formatText(location, content.children))) is ContentNodeLink -> { - val linkTo = locationService.relativeLocation(location, content.node, extension) + val linkTo = location.relativePathTo(locationService.location(content.node), extension) val linkText = formatText(location, content.children) append(formatLink(linkText, linkTo)) } @@ -77,7 +76,7 @@ public abstract class StructuredFormatService(val locationService: LocationServi open public fun link(from: DocumentationNode, to: DocumentationNode): FormatLink = link(from, to, extension) open public fun link(from: DocumentationNode, to: DocumentationNode, extension: String): FormatLink { - return FormatLink(to.name, locationService.relativeLocation(from, to, extension)) + return FormatLink(to.name, locationService.relativePathToLocation(from, to, extension)) } fun appendDocumentation(location: Location, to: StringBuilder, overloads: Iterable<DocumentationNode>) { @@ -133,7 +132,7 @@ public abstract class StructuredFormatService(val locationService: LocationServi private fun DocumentationNode.appendOverrides(to: StringBuilder) { overrides.forEach { to.append("Overrides ") - val location = locationService.relativeLocation(this, it, extension) + val location = locationService.relativePathToLocation(this, it, extension) appendLine(to, formatLink(FormatLink(it.owner!!.name + "." + it.name, location))) } } @@ -210,7 +209,7 @@ public abstract class StructuredFormatService(val locationService: LocationServi override fun appendNodes(location: Location, to: StringBuilder, nodes: Iterable<DocumentationNode>) { val breakdownByLocation = nodes.groupBy { node -> - formatBreadcrumbs(node.path.map { link(node, it) }) + formatBreadcrumbs(node.path.filterNot { it.name.isEmpty() }.map { link(node, it) }) } for ((breadcrumbs, items) in breakdownByLocation) { diff --git a/src/Generation/ConsoleGenerator.kt b/src/Generation/ConsoleGenerator.kt index f52c6f4b..2f2daa55 100644 --- a/src/Generation/ConsoleGenerator.kt +++ b/src/Generation/ConsoleGenerator.kt @@ -4,7 +4,7 @@ public class ConsoleGenerator(val signatureGenerator: LanguageService, val locat val IndentStep = " " public fun generate(node: DocumentationNode, indent: String = "") { - println("@${locationService.location(node).file}") + println("@${locationService.location(node).path}") generateHeader(node, indent) //generateDetails(node, indent) generateMembers(node, indent) diff --git a/src/Generation/FileGenerator.kt b/src/Generation/FileGenerator.kt index 128f3260..22090937 100644 --- a/src/Generation/FileGenerator.kt +++ b/src/Generation/FileGenerator.kt @@ -4,7 +4,7 @@ import java.io.FileOutputStream import java.io.OutputStreamWriter public class FileGenerator(val signatureGenerator: LanguageService, - val locationService: LocationService, + val locationService: FileLocationService, val formatService: FormatService, val outlineService: OutlineFormatService?) { 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 |