org.jetbrains.kotlin.doc.templates.KDocTemplate.kt Maven / Gradle / Ivy
package org.jetbrains.kotlin.doc.templates
import org.jetbrains.kotlin.doc.*
import org.jetbrains.kotlin.doc.model.KAnnotation
import org.jetbrains.kotlin.doc.model.KClass
import org.jetbrains.kotlin.doc.model.KFunction
import org.jetbrains.kotlin.doc.model.KPackage
import org.jetbrains.kotlin.doc.model.KProperty
import org.jetbrains.kotlin.doc.model.KType
import org.jetbrains.kotlin.template.TextTemplate
abstract class KDocTemplate() : TextTemplate() {
// TODO: use same timestamp for all pages
protected val generatedComment: String = ""
open fun rootHref(pkg: KPackage): String {
return if (!pkg.useExternalLink)
relativePrefix()
else
pkg.model.config.resolveLink(pkg.name)
}
open fun href(p: KPackage): String
= "${rootHref(p)}${p.nameAsPath}/package-summary.html"
open fun href(c: KClass): String {
val postfix = if (!c.pkg.useExternalLink) "" else "?is-external=true"
return "${rootHref(c.pkg)}${c.nameAsPath}.html$postfix"
}
open fun href(f: KFunction): String {
return if (f.owner is KClass) {
"${href(f.owner)}#${f.link}"
} else {
"package-summary.html#${f.link}"
}
}
/**
* Generates a link to a package level property
*/
open fun href(pkg: KPackage, p: KProperty): String {
return "${href(pkg)}#${p.link}"
}
open fun href(f: KProperty): String {
return if (f.owner is KClass) {
"${href(f.owner)}#${f.link}"
} else {
"package-summary.html#${f.link}"
}
}
open fun extensionsHref(pkg: KPackage, c: KClass): String {
// is inside the pkg so no need to use it...
return "${c.nameAsPath}-extensions.html"
}
open fun extensionsHref(pkg: KPackage, c: KClass, f: KFunction): String {
return "${extensionsHref(pkg, c)}#${f.link}"
}
open fun extensionsHref(pkg: KPackage, c: KClass, f: KProperty): String {
return "${extensionsHref(pkg, c)}#${f.link}"
}
open fun sourceHref(klass: KClass): String {
if (klass.isLinkToSourceRepo()) {
return klass.sourceLink()
} else {
val pkg = klass.pkg
return if (klass.sourceInfo != null) {
"${pkg.nameAsRelativePath}${names.htmlSourceDirName}/${klass.sourceInfo.htmlPath}.html#${names.lineNumberLinkHref(klass.sourceLine)}"
} else {
href(klass)
}
}
}
open fun sourceHref(f: KFunction): String {
if (f.isLinkToSourceRepo()) {
return f.sourceLink()
} else {
val owner = f.owner
return if (owner is KClass) {
val pkg = owner.pkg
if (owner.sourceInfo != null) {
"${rootHref(pkg)}${names.htmlSourceDirName}/${owner.sourceInfo.htmlPath}.html#${names.lineNumberLinkHref(f.sourceLine)}"
} else {
href(f)
}
} else if (owner is KPackage) {
if (!owner.useExternalLink) {
// TODO how to find the function in a package???
"${rootHref(owner)}${names.htmlSourceDirName}/package.html#${names.lineNumberLinkHref(f.sourceLine)}"
} else {
href(owner)
}
} else href(f)
}
}
open fun sourceHref(f: KProperty): String {
if (f.isLinkToSourceRepo()) {
return f.sourceLink()
} else {
val owner = f.owner
return if (owner is KClass) {
val pkg = owner.pkg
if (owner.sourceInfo != null) {
"${rootHref(pkg)}${names.htmlSourceDirName}/${owner.sourceInfo.htmlPath}#${names.lineNumberLinkHref(f.sourceLine)}"
} else {
href(f)
}
} else if (owner is KPackage) {
if (!owner.useExternalLink) {
// TODO how to find the function in a package???
"${rootHref(owner)}${names.htmlSourceDirName}/package.html#${names.lineNumberLinkHref(f.sourceLine)}"
} else {
href(owner)
}
} else href(f)
}
}
open fun link(c: KClass, fullName: Boolean = false): String {
val prefix = if (c.isAnnotation()) "@" else ""
val name = if (fullName) c.name else c.simpleName
return "$prefix$name"
}
open fun link(t: KType, fullName: Boolean = false): String {
val c = t.klass
val arguments = t.arguments
return if (c != null) {
val prefix = if (c.isAnnotation()) "@" else ""
val cname = c.name
if ((cname.startsWith("kotlin.Function") || cname.startsWith("kotlin.ExtensionFunction")) && arguments.isNotEmpty()) {
val rt = arguments.last()
// TODO use drop()
val rest = arguments.subList(0, arguments.size - 1).orEmpty()
"${typeArguments(rest, "(", ")", "()")} -> ${link(rt)}"
} else {
val name = if (fullName) cname else c.simpleName
"$prefix$name${typeArguments(arguments)}"
}
} else {
"${t.name}${typeArguments(arguments)}"
}
}
open fun typeArguments(arguments: List, prefix: String = "<", postfix: String = ">", empty: String = ""): String {
val text = arguments.map() { link(it) }.makeString(", ")
return if (text.length() == 0) empty else "$prefix$text$postfix"
}
open fun link(p: KPackage): String {
return "${p.name}"
}
open fun link(a: KAnnotation): String {
val c = a.klass
return "@${c.simpleName}"
}
fun searchBox(): String =
"""
"""
fun stylesheets(): String {
return """
"""
}
protected open fun relativePrefix(): String = ""
}