diff options
author | Dmitry Jemerov <yole@jetbrains.com> | 2016-07-01 17:47:03 +0200 |
---|---|---|
committer | Dmitry Jemerov <yole@jetbrains.com> | 2016-07-01 17:47:03 +0200 |
commit | 625ea7d5d679399a24877d4f6988d58ce2662a8c (patch) | |
tree | 1009a6b208dfa84ca2213fd57a1b4f519007e897 /core/src/main/kotlin | |
parent | cb97c45aa0b0204ebc61a149794518d50b262814 (diff) | |
download | dokka-625ea7d5d679399a24877d4f6988d58ce2662a8c.tar.gz dokka-625ea7d5d679399a24877d4f6988d58ce2662a8c.tar.bz2 dokka-625ea7d5d679399a24877d4f6988d58ce2662a8c.zip |
extract stateful builder out of FormatService
Diffstat (limited to 'core/src/main/kotlin')
-rw-r--r-- | core/src/main/kotlin/Formats/FormatService.kt | 12 | ||||
-rw-r--r-- | core/src/main/kotlin/Formats/GFMFormatService.kt | 21 | ||||
-rw-r--r-- | core/src/main/kotlin/Formats/HtmlFormatService.kt | 51 | ||||
-rw-r--r-- | core/src/main/kotlin/Formats/JekyllFormatService.kt | 36 | ||||
-rw-r--r-- | core/src/main/kotlin/Formats/KotlinWebsiteFormatService.kt | 19 | ||||
-rw-r--r-- | core/src/main/kotlin/Formats/MarkdownFormatService.kt | 25 | ||||
-rw-r--r-- | core/src/main/kotlin/Formats/StructuredFormatService.kt | 34 |
7 files changed, 137 insertions, 61 deletions
diff --git a/core/src/main/kotlin/Formats/FormatService.kt b/core/src/main/kotlin/Formats/FormatService.kt index aa4e925c..e281f2fc 100644 --- a/core/src/main/kotlin/Formats/FormatService.kt +++ b/core/src/main/kotlin/Formats/FormatService.kt @@ -11,12 +11,18 @@ interface FormatService { /** Returns extension for output files */ val extension: String - /** Appends formatted content to [StringBuilder](to) using specified [location] */ - fun appendNodes(location: Location, to: StringBuilder, nodes: Iterable<DocumentationNode>) + fun createOutputBuilder(to: StringBuilder, location: Location): FormattedOutputBuilder fun enumerateSupportFiles(callback: (resource: String, targetPath: String) -> Unit) { } } +interface FormattedOutputBuilder { + /** Appends formatted content to [StringBuilder](to) using specified [location] */ + fun appendNodes(nodes: Iterable<DocumentationNode>) +} + /** Format content to [String] using specified [location] */ -fun FormatService.format(location: Location, nodes: Iterable<DocumentationNode>): String = StringBuilder().apply { appendNodes(location, this, nodes) }.toString() +fun FormatService.format(location: Location, nodes: Iterable<DocumentationNode>): String = StringBuilder().apply { + createOutputBuilder(this, location).appendNodes(nodes) +}.toString() diff --git a/core/src/main/kotlin/Formats/GFMFormatService.kt b/core/src/main/kotlin/Formats/GFMFormatService.kt index 7b1eab85..b57fa932 100644 --- a/core/src/main/kotlin/Formats/GFMFormatService.kt +++ b/core/src/main/kotlin/Formats/GFMFormatService.kt @@ -2,6 +2,20 @@ package org.jetbrains.dokka import com.google.inject.Inject +open class GFMOutputBuilder(to: StringBuilder, + location: Location, + locationService: LocationService, + languageService: LanguageService, + extension: String) + : MarkdownOutputBuilder(to, location, locationService, languageService, extension) +{ + override fun appendTable(to: StringBuilder, vararg columns: String, body: () -> Unit) { + to.appendln(columns.joinToString(" | ", "| ", " |")) + to.appendln("|" + "---|".repeat(columns.size)) + body() + } +} + open class GFMFormatService(locationService: LocationService, signatureGenerator: LanguageService, linkExtension: String) @@ -10,9 +24,6 @@ open class GFMFormatService(locationService: LocationService, @Inject constructor(locationService: LocationService, signatureGenerator: LanguageService) : this(locationService, signatureGenerator, "md") - override fun appendTable(to: StringBuilder, vararg columns: String, body: () -> Unit) { - to.appendln(columns.joinToString(" | ", "| ", " ")) - to.appendln("|" + "---|".repeat(columns.size)) - body() - } + override fun createOutputBuilder(to: StringBuilder, location: Location): FormattedOutputBuilder = + GFMOutputBuilder(to, location, locationService, languageService, extension) } diff --git a/core/src/main/kotlin/Formats/HtmlFormatService.kt b/core/src/main/kotlin/Formats/HtmlFormatService.kt index c7443128..fff8f553 100644 --- a/core/src/main/kotlin/Formats/HtmlFormatService.kt +++ b/core/src/main/kotlin/Formats/HtmlFormatService.kt @@ -6,10 +6,14 @@ import java.io.File import java.nio.file.Path import java.nio.file.Paths -open class HtmlFormatService @Inject constructor(@Named("folders") locationService: LocationService, - signatureGenerator: LanguageService, - val templateService: HtmlTemplateService) -: StructuredFormatService(locationService, signatureGenerator, "html"), OutlineFormatService { +open class HtmlOutputBuilder(to: StringBuilder, + location: Location, + locationService: LocationService, + languageService: LanguageService, + extension: String, + val templateService: HtmlTemplateService) + : StructuredOutputBuilder(to, location, locationService, languageService, extension) +{ override fun formatText(text: String): String { return text.htmlEscape() } @@ -105,23 +109,33 @@ open class HtmlFormatService @Inject constructor(@Named("folders") locationServi } - override fun appendNodes(location: Location, to: StringBuilder, nodes: Iterable<DocumentationNode>) { - templateService.appendHeader(to, getPageTitle(nodes), calcPathToRoot(location)) - super.appendNodes(location, to, nodes) + override fun appendNodes(nodes: Iterable<DocumentationNode>) { + templateService.appendHeader(to, getPageTitle(nodes), locationService.calcPathToRoot(location)) + super.appendNodes(nodes) templateService.appendFooter(to) } + override fun formatNonBreakingSpace(): String = " " +} + +open class HtmlFormatService @Inject constructor(@Named("folders") locationService: LocationService, + signatureGenerator: LanguageService, + val templateService: HtmlTemplateService) +: StructuredFormatService(locationService, signatureGenerator, "html"), OutlineFormatService { + + override fun enumerateSupportFiles(callback: (String, String) -> Unit) { + callback("/dokka/styles/style.css", "style.css") + } + + override fun createOutputBuilder(to: StringBuilder, location: Location) = + HtmlOutputBuilder(to, location, locationService, languageService, extension, templateService) + override fun appendOutline(location: Location, to: StringBuilder, nodes: Iterable<DocumentationNode>) { - templateService.appendHeader(to, "Module Contents", calcPathToRoot(location)) + templateService.appendHeader(to, "Module Contents", locationService.calcPathToRoot(location)) super.appendOutline(location, to, nodes) templateService.appendFooter(to) } - private fun calcPathToRoot(location: Location): Path { - val path = Paths.get(location.path) - return path.parent?.relativize(Paths.get(locationService.root.path + '/')) ?: path - } - override fun getOutlineFileName(location: Location): File { return File("${location.path}-outline.html") } @@ -129,7 +143,7 @@ open class HtmlFormatService @Inject constructor(@Named("folders") locationServi override fun appendOutlineHeader(location: Location, node: DocumentationNode, to: StringBuilder) { val link = ContentNodeDirectLink(node) link.append(languageService.render(node, LanguageService.RenderMode.FULL)) - val signature = formatText(location, link) + val signature = createOutputBuilder(to, location).formatText(location, link) to.appendln("<a href=\"${location.path}\">${signature}</a><br/>") } @@ -138,12 +152,11 @@ open class HtmlFormatService @Inject constructor(@Named("folders") locationServi body() to.appendln("</ul>") } +} - override fun formatNonBreakingSpace(): String = " " - - override fun enumerateSupportFiles(callback: (String, String) -> Unit) { - callback("/dokka/styles/style.css", "style.css") - } +private fun LocationService.calcPathToRoot(location: Location): Path { + val path = Paths.get(location.path) + return path.parent?.relativize(Paths.get(root.path + '/')) ?: path } fun getPageTitle(nodes: Iterable<DocumentationNode>): String? { diff --git a/core/src/main/kotlin/Formats/JekyllFormatService.kt b/core/src/main/kotlin/Formats/JekyllFormatService.kt index 9271e3a9..bab73379 100644 --- a/core/src/main/kotlin/Formats/JekyllFormatService.kt +++ b/core/src/main/kotlin/Formats/JekyllFormatService.kt @@ -2,23 +2,37 @@ package org.jetbrains.dokka import com.google.inject.Inject -open class JekyllFormatService(locationService: LocationService, - signatureGenerator: LanguageService, - linkExtension: String) -: MarkdownFormatService(locationService, signatureGenerator, linkExtension) { - - @Inject constructor(locationService: LocationService, - signatureGenerator: LanguageService): this(locationService, signatureGenerator, "md") - - override fun appendNodes(location: Location, to: StringBuilder, nodes: Iterable<DocumentationNode>) { +open class JekyllOutputBuilder(to: StringBuilder, + location: Location, + locationService: LocationService, + languageService: LanguageService, + extension: String) + : MarkdownOutputBuilder(to, location, locationService, languageService, extension) +{ + override fun appendNodes(nodes: Iterable<DocumentationNode>) { to.appendln("---") appendFrontMatter(nodes, to) to.appendln("---") to.appendln("") - super.appendNodes(location, to, nodes) + super.appendNodes(nodes) } protected open fun appendFrontMatter(nodes: Iterable<DocumentationNode>, to: StringBuilder) { to.appendln("title: ${getPageTitle(nodes)}") } -}
\ No newline at end of file +} + + +open class JekyllFormatService(locationService: LocationService, + signatureGenerator: LanguageService, + linkExtension: String) +: MarkdownFormatService(locationService, signatureGenerator, linkExtension) { + + @Inject constructor(locationService: LocationService, + signatureGenerator: LanguageService): this(locationService, signatureGenerator, "md") { + } + + override fun createOutputBuilder(to: StringBuilder, location: Location): FormattedOutputBuilder = + JekyllOutputBuilder(to, location, locationService, languageService, extension) + +} diff --git a/core/src/main/kotlin/Formats/KotlinWebsiteFormatService.kt b/core/src/main/kotlin/Formats/KotlinWebsiteFormatService.kt index 1f3a3750..e3229523 100644 --- a/core/src/main/kotlin/Formats/KotlinWebsiteFormatService.kt +++ b/core/src/main/kotlin/Formats/KotlinWebsiteFormatService.kt @@ -2,9 +2,14 @@ package org.jetbrains.dokka import com.google.inject.Inject -class KotlinWebsiteFormatService @Inject constructor(locationService: LocationService, - signatureGenerator: LanguageService) -: JekyllFormatService(locationService, signatureGenerator, "html") { + +class KotlinWebsiteOutputBuilder(to: StringBuilder, + location: Location, + locationService: LocationService, + languageService: LanguageService, + extension: String) + : JekyllOutputBuilder(to, location, locationService, languageService, extension) +{ private var needHardLineBreaks = false private var insideDiv = 0 @@ -140,3 +145,11 @@ class KotlinWebsiteFormatService @Inject constructor(locationService: LocationSe else -> "identifier" } } + +class KotlinWebsiteFormatService @Inject constructor(locationService: LocationService, + signatureGenerator: LanguageService) + : JekyllFormatService(locationService, signatureGenerator, "html") +{ + override fun createOutputBuilder(to: StringBuilder, location: Location) = + KotlinWebsiteOutputBuilder(to, location, locationService, languageService, extension) +} diff --git a/core/src/main/kotlin/Formats/MarkdownFormatService.kt b/core/src/main/kotlin/Formats/MarkdownFormatService.kt index 79177239..5ddb7f1f 100644 --- a/core/src/main/kotlin/Formats/MarkdownFormatService.kt +++ b/core/src/main/kotlin/Formats/MarkdownFormatService.kt @@ -3,13 +3,13 @@ package org.jetbrains.dokka import com.google.inject.Inject -open class MarkdownFormatService(locationService: LocationService, - signatureGenerator: LanguageService, - linkExtension: String) -: StructuredFormatService(locationService, signatureGenerator, "md", linkExtension) { - @Inject constructor(locationService: LocationService, - signatureGenerator: LanguageService): this(locationService, signatureGenerator, "md") - +open class MarkdownOutputBuilder(to: StringBuilder, + location: Location, + locationService: LocationService, + languageService: LanguageService, + extension: String) + : StructuredOutputBuilder(to, location, locationService, languageService, extension) +{ override fun formatBreadcrumbs(items: Iterable<FormatLink>): String { return items.map { formatLink(it) }.joinToString(" / ") } @@ -99,3 +99,14 @@ open class MarkdownFormatService(locationService: LocationService, override fun formatNonBreakingSpace(): String = " " } + +open class MarkdownFormatService(locationService: LocationService, + signatureGenerator: LanguageService, + linkExtension: String) +: StructuredFormatService(locationService, signatureGenerator, "md", linkExtension) { + @Inject constructor(locationService: LocationService, + signatureGenerator: LanguageService): this(locationService, signatureGenerator, "md") + + override fun createOutputBuilder(to: StringBuilder, location: Location): FormattedOutputBuilder = + MarkdownOutputBuilder(to, location, locationService, languageService, extension) +} diff --git a/core/src/main/kotlin/Formats/StructuredFormatService.kt b/core/src/main/kotlin/Formats/StructuredFormatService.kt index 72b6833d..407c7018 100644 --- a/core/src/main/kotlin/Formats/StructuredFormatService.kt +++ b/core/src/main/kotlin/Formats/StructuredFormatService.kt @@ -10,11 +10,11 @@ enum class ListKind { Unordered } -abstract class StructuredFormatService(locationService: LocationService, +abstract class StructuredOutputBuilder(val to: StringBuilder, + val location: Location, + val locationService: LocationService, val languageService: LanguageService, - override val extension: String, - val linkExtension: String = extension) : FormatService { - val locationService: LocationService = locationService.withExtension(linkExtension) + val extension: String) : FormattedOutputBuilder { abstract fun appendBlockCode(to: StringBuilder, lines: List<String>, language: String) abstract fun appendHeader(to: StringBuilder, text: String, level: Int = 1) @@ -141,7 +141,7 @@ abstract class StructuredFormatService(locationService: LocationService, } } - open inner class PageBuilder(val location: Location, val to: StringBuilder, val nodes: Iterable<DocumentationNode>) { + open inner class PageBuilder(val nodes: Iterable<DocumentationNode>) { open fun build() { val breakdownByLocation = nodes.groupBy { node -> formatBreadcrumbs(node.path.filterNot { it.name.isEmpty() }.map { link(node, it) }) @@ -285,8 +285,8 @@ abstract class StructuredFormatService(locationService: LocationService, } } - inner class SingleNodePageBuilder(location: Location, to: StringBuilder, val node: DocumentationNode) - : PageBuilder(location, to, listOf(node)) { + inner class SingleNodePageBuilder(val node: DocumentationNode) + : PageBuilder(listOf(node)) { override fun build() { super.build() @@ -397,8 +397,8 @@ abstract class StructuredFormatService(locationService: LocationService, } } - inner class AllTypesNodeBuilder(location: Location, to: StringBuilder, val node: DocumentationNode) - : PageBuilder(location, to, listOf(node)) { + inner class AllTypesNodeBuilder(val node: DocumentationNode) + : PageBuilder(listOf(node)) { override fun build() { to.append(formatText(location, node.owner!!.summary)) @@ -429,18 +429,26 @@ abstract class StructuredFormatService(locationService: LocationService, } } - override fun appendNodes(location: Location, to: StringBuilder, nodes: Iterable<DocumentationNode>) { + override fun appendNodes(nodes: Iterable<DocumentationNode>) { val singleNode = nodes.singleOrNull() if (singleNode != null) { if (singleNode.kind == NodeKind.AllTypes) { - AllTypesNodeBuilder(location, to, singleNode).build() + AllTypesNodeBuilder(singleNode).build() } else { - SingleNodePageBuilder(location, to, singleNode).build() + SingleNodePageBuilder(singleNode).build() } } else { - PageBuilder(location, to, nodes).build() + PageBuilder(nodes).build() } } + +} + +abstract class StructuredFormatService(locationService: LocationService, + val languageService: LanguageService, + override val extension: String, + val linkExtension: String = extension) : FormatService { + val locationService: LocationService = locationService.withExtension(linkExtension) } |