From bfd9ffd13ed6b6916790f5f0de5f9523db71b22e Mon Sep 17 00:00:00 2001 From: Dmitry Jemerov Date: Fri, 30 Jan 2015 17:59:15 +0100 Subject: load sections from KDoc PSI, not through Markdown extensions --- src/Model/Content.kt | 71 ++++++++++++++++-------------------------- src/Model/DocumentationNode.kt | 6 ++-- 2 files changed, 29 insertions(+), 48 deletions(-) (limited to 'src/Model') diff --git a/src/Model/Content.kt b/src/Model/Content.kt index 8491fd88..2fae317d 100644 --- a/src/Model/Content.kt +++ b/src/Model/Content.kt @@ -1,7 +1,5 @@ package org.jetbrains.dokka -import kotlin.properties.Delegates - public abstract class ContentNode { val children = arrayListOf() @@ -34,7 +32,7 @@ public class ContentNodeLink(val node : DocumentationNode) : ContentBlock() public class ContentExternalLink(val href : String) : ContentBlock() public class ContentList() : ContentBlock() public class ContentListItem() : ContentBlock() -public class ContentSection(public val label: String) : ContentBlock() +public class ContentSection(public val label: String, public val subjectName: String?) : ContentBlock() fun content(body: ContentNode.() -> Unit): ContentNode { val block = ContentBlock() @@ -54,53 +52,38 @@ fun ContentNode.link(to: DocumentationNode, body: ContentNode.() -> Unit) { } public class Content() : ContentNode() { - public val sections: Map by Delegates.lazy { - val map = linkedMapOf() - for (child in children) { - if (child is ContentSection) - map.put(child.label, child) - } - - if ("\$summary" !in map && "\$description" !in map) { - // no explicit summary and description, convert anonymous section - val anonymous = map[""] - if (anonymous != null) { - map.remove("") - val summary = ContentSection("\$summary") - val description = ContentSection("\$description") - - val summaryNodes = anonymous.children.take(1) - val descriptionNodes = anonymous.children.drop(1) - - if (summaryNodes.any()) { - summary.children.addAll(summaryNodes) - map.put("\$summary", summary) - } - - if (descriptionNodes.any()) { - description.children.addAll(descriptionNodes) - map.put("\$description", description) - } - } - } - map + private val sectionList = arrayListOf() + public val sections: List + get() = sectionList + + fun addSection(name: String?, subjectName: String?): ContentSection { + val section = ContentSection(name ?: "", subjectName) + sectionList.add(section) + return section } - public val summary: ContentNode get() { - return sections["\$summary"] ?: ContentNode.empty + fun findSectionByName(name: String): ContentSection? = + sections.firstOrNull { it.label == name } + + fun getSectionsWithSubjects(): Map> = + sections.filter { it.subjectName != null }.groupBy { it.label } + + public val summary: ContentNode get() = children.firstOrNull() ?: ContentEmpty + + public val description: ContentNode get() { + val descriptionNodes = children.drop(1) + if (descriptionNodes.isEmpty()) { + return ContentEmpty + } + val result = ContentSection("\$description", null) + result.children.addAll(descriptionNodes) + return result } - public val description: ContentNode get() = sections["\$description"] ?: ContentNode.empty override fun equals(other: Any?): Boolean { if (other !is Content) return false - if (sections.size != other.sections.size) - return false - for (keys in sections.keySet()) - if (sections[keys] != other.sections[keys]) - return false - - return true + return sections == other.sections && children == other.children } override fun hashCode(): Int { @@ -110,7 +93,7 @@ public class Content() : ContentNode() { override fun toString(): String { if (sections.isEmpty()) return "" - return sections.values().joinToString() + return sections.joinToString() } val isEmpty: Boolean diff --git a/src/Model/DocumentationNode.kt b/src/Model/DocumentationNode.kt index a666b486..3a61b8fb 100644 --- a/src/Model/DocumentationNode.kt +++ b/src/Model/DocumentationNode.kt @@ -8,13 +8,11 @@ public open class DocumentationNode(val name: String, private val references = LinkedHashSet() + public val summary: ContentNode get() { - val contentSection = content.sections["\$summary"] + val contentSection = content.summary if (contentSection != null) return contentSection - val ownerSection = owner?.content?.sections?.get(name) - if (ownerSection != null) - return ownerSection return ContentNode.empty } -- cgit