diff options
author | Ilya Ryzhenkov <orangy@jetbrains.com> | 2014-09-19 22:25:27 +0300 |
---|---|---|
committer | Ilya Ryzhenkov <orangy@jetbrains.com> | 2014-09-19 22:25:27 +0300 |
commit | 455d74a6a6245969ebd7ec20071b691ed20a2628 (patch) | |
tree | 601e80982ddc289d1745a5570347a15512f4fe7d /src/Model/DocumentationContent.kt | |
parent | ff0c8d7514742b505acff2f2ba51dc21aedbd949 (diff) | |
download | dokka-455d74a6a6245969ebd7ec20071b691ed20a2628.tar.gz dokka-455d74a6a6245969ebd7ec20071b691ed20a2628.tar.bz2 dokka-455d74a6a6245969ebd7ec20071b691ed20a2628.zip |
Convert content to RichString and establish resolution service for links.
Diffstat (limited to 'src/Model/DocumentationContent.kt')
-rw-r--r-- | src/Model/DocumentationContent.kt | 65 |
1 files changed, 52 insertions, 13 deletions
diff --git a/src/Model/DocumentationContent.kt b/src/Model/DocumentationContent.kt index 5ea745c5..e8f32a33 100644 --- a/src/Model/DocumentationContent.kt +++ b/src/Model/DocumentationContent.kt @@ -3,13 +3,15 @@ package org.jetbrains.dokka import org.jetbrains.jet.lang.descriptors.* import org.jetbrains.jet.lang.resolve.BindingContext -class DocumentationContentSection(val label: String, val text: String) { +public class DocumentationContentSection(public val label: String, public val text: RichString) { override fun toString(): String { return "$label = $text" } } -class DocumentationContent(val summary: String, val description: String, val sections: List<DocumentationContentSection>) { +public class DocumentationContent(public val summary: RichString, + public val description: RichString, + public val sections: List<DocumentationContentSection>) { override fun equals(other: Any?): Boolean { if (other !is DocumentationContent) @@ -31,15 +33,15 @@ class DocumentationContent(val summary: String, val description: String, val sec override fun toString(): String { if (sections.isEmpty()) - return summary + return summary.toString() return "$summary | " + sections.joinToString() } - val hasDescription : Boolean - get() = !description.isEmpty() || sections.any() + val isEmpty: Boolean + get() = description.isEmpty() && sections.none() class object { - val Empty = DocumentationContent("", "", listOf()) + val Empty = DocumentationContent(RichString.empty, RichString.empty, listOf()) } } @@ -47,8 +49,27 @@ class DocumentationContent(val summary: String, val description: String, val sec fun BindingContext.getDocumentation(descriptor: DeclarationDescriptor): DocumentationContent { val docText = getDocumentationElements(descriptor).map { it.extractText() }.join("\n") val sections = docText.parseSections() - val content = sections.first().text - return DocumentationContent(content.substringBefore('\n'), content.substringAfter('\n'), sections.drop(1)) + val (summary, description) = sections.extractSummaryAndDescription() + return DocumentationContent(summary, description, sections.drop(1)) +} + +fun List<DocumentationContentSection>.extractSummaryAndDescription() : Pair<RichString, RichString> { + val summary = firstOrNull { it.label == "\$summary" } + if (summary != null) { + val description = firstOrNull { it.label == "\$description" } + return Pair(summary.text, description?.text ?: RichString.empty) + } + + val description = firstOrNull { it.label == "\$description" } + if (description != null) { + return Pair(RichString.empty, description.text) + } + + val default = firstOrNull { it.label == "" }?.text + if (default == null) + return Pair(RichString.empty, RichString.empty) + + return default.splitBy("\n") } fun String.parseLabel(index: Int): Pair<String, Int> { @@ -62,9 +83,17 @@ fun String.parseLabel(index: Int): Pair<String, Int> { } return substring(index, length) to length } + c == '$' -> { + for (end in index + 1..length - 1) { + if (Character.isWhitespace(get(end))) { + return substring(index, end) to end + } + } + return substring(index, length) to length + } c == '{' -> { val end = indexOf('}', index + 1) - return substring(index + 1, end) to end + 2 + return substring(index + 1, end) to end + 1 } } return "" to -1 @@ -79,12 +108,14 @@ fun String.parseSections(): List<DocumentationContentSection> { while (currentIndex < length) { if (get(currentIndex) == '$') { val (label, index) = parseLabel(currentIndex + 1) - if (index != -1) { + if (index != -1 && index < length() && get(index) == ':') { // section starts, add previous section - sections.add(DocumentationContentSection(currentLabel, substring(currentSectionStart, currentIndex).trim())) + val currentContent = substring(currentSectionStart, currentIndex).trim() + val section = DocumentationContentSection(currentLabel, currentContent.toRichString()) + sections.add(section) currentLabel = label - currentIndex = index + currentIndex = index + 1 currentSectionStart = currentIndex continue } @@ -93,6 +124,14 @@ fun String.parseSections(): List<DocumentationContentSection> { } - sections.add(DocumentationContentSection(currentLabel, substring(currentSectionStart, currentIndex).trim())) + val currentContent = substring(currentSectionStart, currentIndex).trim() + val section = DocumentationContentSection(currentLabel, currentContent.toRichString()) + sections.add(section) return sections +} + +fun String.toRichString() : RichString { + val content = RichString() + content.addSlice(this, NormalStyle) + return content }
\ No newline at end of file |