package org.jetbrains.dokka import java.io.File public open class HtmlFormatService(locationService: LocationService, signatureGenerator: LanguageService, val templateService: HtmlTemplateService = HtmlTemplateService.default()) : StructuredFormatService(locationService, signatureGenerator, "html"), OutlineFormatService { override public fun formatText(text: String): String { return text.htmlEscape() } override fun formatSymbol(text: String): String { return "${formatText(text)}" } override fun formatKeyword(text: String): String { return "${formatText(text)}" } override fun formatIdentifier(text: String): String { return "${formatText(text)}" } override fun appendBlockCode(to: StringBuilder, line: String) { to.append("
")
        to.append(line)
        to.append("
") } override fun appendBlockCode(to: StringBuilder, lines: Iterable) { to.append("
")
        to.append(lines.join("\n"))
        to.append("
") } override fun appendHeader(to: StringBuilder, text: String, level: Int) { to.appendln("${text}") } override fun appendParagraph(to: StringBuilder, text: String) { to.appendln("

${text}

") } override fun appendLine(to: StringBuilder, text: String) { to.appendln("${text}
") } override fun appendLine(to: StringBuilder) { to.appendln("
") } override fun appendAnchor(to: StringBuilder, anchor: String) { to.appendln("") } override fun appendTable(to: StringBuilder, body: () -> Unit) { to.appendln("") body() to.appendln("
") } override fun appendTableHeader(to: StringBuilder, body: () -> Unit) { to.appendln("") body() to.appendln("") } override fun appendTableBody(to: StringBuilder, body: () -> Unit) { to.appendln("") body() to.appendln("") } override fun appendTableRow(to: StringBuilder, body: () -> Unit) { to.appendln("") body() to.appendln("") } override fun appendTableCell(to: StringBuilder, body: () -> Unit) { to.appendln("") body() to.appendln("") } override fun formatLink(text: String, href: String): String { return "${text}" } override fun formatStrong(text: String): String { return "${text}" } override fun formatEmphasis(text: String): String { return "${text}" } override fun formatStrikethrough(text: String): String { return "${text}" } override fun formatCode(code: String): String { return "${code}" } override fun formatList(text: String): String { return "" } override fun formatListItem(text: String): String { return "
  • ${text}
  • " } override fun formatBreadcrumbs(items: Iterable): String { return items.map { formatLink(it) }.joinToString(" / ") } override fun appendNodes(location: Location, to: StringBuilder, nodes: Iterable) { templateService.appendHeader(to, getPageTitle(nodes)) super.appendNodes(location, to, nodes) templateService.appendFooter(to) } override fun appendOutline(location: Location, to: StringBuilder, nodes: Iterable) { templateService.appendHeader(to, "Module Contents") super.appendOutline(location, to, nodes) templateService.appendFooter(to) } override fun getOutlineFileName(location: Location): File { return File("${location.path}-outline.html") } 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) to.appendln("${signature}
    ") } override fun appendOutlineLevel(to: StringBuilder, body: () -> Unit) { to.appendln("
      ") body() to.appendln("
    ") } } fun getPageTitle(nodes: Iterable): String? { val breakdownByLocation = nodes.groupBy { node -> formatPageTitle(node) } return breakdownByLocation.keySet().singleOrNull() } fun formatPageTitle(node: DocumentationNode): String { val path = node.path if (path.size() == 1) { return path.first().name } val qualifiedName = path.drop(1).map { it.name }.filter { it.length() > 0 }.join(".") if (qualifiedName.length() == 0 && path.size() == 2) { return path.first().name + " / root package" } return path.first().name + " / " + qualifiedName }