Skip to content

Commit

Permalink
overload group formatting fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
yole committed Dec 3, 2015
1 parent 075d4ae commit 050b045
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 40 deletions.
6 changes: 1 addition & 5 deletions core/src/main/kotlin/Formats/HtmlFormatService.kt
Expand Up @@ -41,11 +41,7 @@ public open class HtmlFormatService @Inject constructor(@Named("folders") locati
}

override fun appendLine(to: StringBuilder, text: String) {
to.appendln("${text}<br/>")
}

override fun appendLine(to: StringBuilder) {
to.appendln("<br/>")
to.appendln("$text<br/>")
}

override fun appendAnchor(to: StringBuilder, anchor: String) {
Expand Down
45 changes: 35 additions & 10 deletions core/src/main/kotlin/Formats/KotlinWebsiteFormatService.kt
Expand Up @@ -6,6 +6,7 @@ public class KotlinWebsiteFormatService @Inject constructor(locationService: Loc
signatureGenerator: LanguageService)
: JekyllFormatService(locationService, signatureGenerator, "html") {
private var needHardLineBreaks = false
private var insideDiv = 0

override fun appendFrontMatter(nodes: Iterable<DocumentationNode>, to: StringBuilder) {
super.appendFrontMatter(nodes, to)
Expand All @@ -28,29 +29,53 @@ public class KotlinWebsiteFormatService @Inject constructor(locationService: Loc

override fun formatStrikethrough(text: String): String = "<s>$text</s>"

private fun div(to: StringBuilder, cssClass: String, block: () -> Unit) {
to.append("<div class=\"$cssClass\">")
insideDiv++
block()
insideDiv--
to.append("</div>\n")
}

override fun appendAsSignature(to: StringBuilder, node: ContentNode, block: () -> Unit) {
val contentLength = node.textLength
if (contentLength == 0) return
to.append("<div class=\"signature\">")
needHardLineBreaks = contentLength >= 62
try {
block()
} finally {
needHardLineBreaks = false
div(to, "signature") {
needHardLineBreaks = contentLength >= 62
try {
block()
} finally {
needHardLineBreaks = false
}
}
to.append("</div>")
}

override fun appendAsOverloadGroup(to: StringBuilder, block: () -> Unit) {
to.append("<div class=\"overload-group\">\n")
block()
to.append("</div>\n")
div(to, "overload-group", block)
}

override fun formatLink(text: String, href: String): String {
return "<a href=\"${href}\">${text}</a>"
}

override fun appendHeader(to: StringBuilder, text: String, level: Int) {
if (insideDiv > 0) {
to.appendln("<h$level>${text}</h$level>")
}
else {
super.appendHeader(to, text, level)
}
}

override fun appendLine(to: StringBuilder, text: String) {
if (insideDiv > 0) {
to.appendln("$text<br/>")
}
else {
super.appendLine(to, text)
}
}

override fun appendTable(to: StringBuilder, body: () -> Unit) {
to.appendln("<table class=\"api-docs-table\">")
body()
Expand Down
4 changes: 0 additions & 4 deletions core/src/main/kotlin/Formats/MarkdownFormatService.kt
Expand Up @@ -55,10 +55,6 @@ public open class MarkdownFormatService
return "[$text]($href)"
}

override public fun appendLine(to: StringBuilder) {
to.appendln()
}

override public fun appendLine(to: StringBuilder, text: String) {
to.appendln(text)
}
Expand Down
50 changes: 29 additions & 21 deletions core/src/main/kotlin/Formats/StructuredFormatService.kt
Expand Up @@ -19,8 +19,7 @@ abstract class StructuredFormatService(locationService: LocationService,
abstract fun appendBlockCode(to: StringBuilder, line: String, language: String)
abstract fun appendHeader(to: StringBuilder, text: String, level: Int = 1)
abstract fun appendParagraph(to: StringBuilder, text: String)
abstract fun appendLine(to: StringBuilder, text: String)
abstract fun appendLine(to: StringBuilder)
abstract fun appendLine(to: StringBuilder, text: String = "")
abstract fun appendAnchor(to: StringBuilder, anchor: String)

abstract fun appendTable(to: StringBuilder, body: () -> Unit)
Expand Down Expand Up @@ -114,30 +113,39 @@ abstract class StructuredFormatService(locationService: LocationService,
fun appendDocumentation(location: Location, to: StringBuilder, overloads: Iterable<DocumentationNode>) {
val breakdownBySummary = overloads.groupByTo(LinkedHashMap()) { node -> node.content }

for ((summary, items) in breakdownBySummary) {
appendAsOverloadGroup(to) {
items.forEach {
val rendered = languageService.render(it)
appendAsSignature(to, rendered) {
to.append(formatCode(formatText(location, rendered)))
it.appendSourceLink(to)
}
it.appendOverrides(to)
it.appendDeprecation(location, to)
}
// All items have exactly the same documentation, so we can use any item to render it
val item = items.first()
item.details(DocumentationNode.Kind.OverloadGroupNote).forEach {
to.append(formatText(location, it.content))
if (breakdownBySummary.size == 1) {
formatOverloadGroup(breakdownBySummary.values.single(), location, to)
}
else {
for ((summary, items) in breakdownBySummary) {
appendAsOverloadGroup(to) {
formatOverloadGroup(items, location, to)
}
to.append(formatText(location, item.content.summary))
appendDescription(location, to, item)
appendLine(to)
appendLine(to)
}
}
}

private fun formatOverloadGroup(items: MutableList<DocumentationNode>, location: Location, to: StringBuilder) {
items.forEach {
val rendered = languageService.render(it)
appendAsSignature(to, rendered) {
to.append(formatCode(formatText(location, rendered)))
it.appendSourceLink(to)
}
it.appendOverrides(to)
it.appendDeprecation(location, to)
}
// All items have exactly the same documentation, so we can use any item to render it
val item = items.first()
item.details(DocumentationNode.Kind.OverloadGroupNote).forEach {
to.append(formatText(location, it.content))
}
to.append(formatText(location, item.content.summary))
appendDescription(location, to, item)
appendLine(to)
appendLine(to)
}

private fun DocumentationNode.isModuleOrPackage(): Boolean =
kind == DocumentationNode.Kind.Module || kind == DocumentationNode.Kind.Package

Expand Down

0 comments on commit 050b045

Please sign in to comment.