From fea7c42733a339ed19fc7471bb064f53de71cc6b Mon Sep 17 00:00:00 2001 From: Marcin Aman Date: Thu, 17 Dec 2020 12:14:40 +0100 Subject: Multimodule tests (#1670) * Multimodule tests * Multimodule tests --- .../GfmTemplateProcessingStrategy.kt | 46 ++++++++++++++++------ 1 file changed, 33 insertions(+), 13 deletions(-) (limited to 'plugins/gfm/gfm-template-processing/src') diff --git a/plugins/gfm/gfm-template-processing/src/main/kotlin/org/jetbrains/dokka/gfm/templateProcessing/GfmTemplateProcessingStrategy.kt b/plugins/gfm/gfm-template-processing/src/main/kotlin/org/jetbrains/dokka/gfm/templateProcessing/GfmTemplateProcessingStrategy.kt index b2ef4d06..3f2bbd3e 100644 --- a/plugins/gfm/gfm-template-processing/src/main/kotlin/org/jetbrains/dokka/gfm/templateProcessing/GfmTemplateProcessingStrategy.kt +++ b/plugins/gfm/gfm-template-processing/src/main/kotlin/org/jetbrains/dokka/gfm/templateProcessing/GfmTemplateProcessingStrategy.kt @@ -15,28 +15,35 @@ import org.jetbrains.dokka.links.DRI import org.jetbrains.dokka.plugability.DokkaContext import org.jetbrains.dokka.plugability.plugin import org.jetbrains.dokka.plugability.querySingle +import java.io.BufferedWriter import java.io.File -class GfmTemplateProcessingStrategy(context: DokkaContext) : TemplateProcessingStrategy { +class GfmTemplateProcessingStrategy(val context: DokkaContext) : TemplateProcessingStrategy { - private val externalModuleLinkResolver = context.plugin().querySingle { externalModuleLinkResolver } + private val externalModuleLinkResolver = + context.plugin().querySingle { externalModuleLinkResolver } override suspend fun process(input: File, output: File): Boolean = coroutineScope { if (input.extension == "md") { launch(IO) { input.bufferedReader().use { reader -> - output.bufferedWriter().use { writer -> - do { - val line = reader.readLine() - if (line != null) { - writer.write(line.replace(templateCommandRegex) { - when (val command = parseJson(it.command)) { - is ResolveLinkGfmCommand -> resolveLink(output, command.dri, it.label) - } - }) - writer.newLine() + //This should also work whenever we have a misconfigured dokka and output is pointing to the input + //the same way that html processing does + if (input.absolutePath == output.absolutePath) { + context.logger.info("Attempting to process GFM templates in place for directory $input, this suggests miss configuration.") + val lines = reader.readLines() + output.bufferedWriter().use { writer -> + lines.forEach { line -> + writer.processAndWrite(line, output) } - } while (line != null) + + } + } else { + output.bufferedWriter().use { writer -> + reader.lineSequence().forEach { line -> + writer.processAndWrite(line, output) + } + } } } } @@ -44,6 +51,19 @@ class GfmTemplateProcessingStrategy(context: DokkaContext) : TemplateProcessingS } else false } + private fun BufferedWriter.processAndWrite(line: String, output: File) = + processLine(line, output).run { + write(this) + newLine() + } + + private fun processLine(line: String, output: File): String = + line.replace(templateCommandRegex) { + when (val command = parseJson(it.command)) { + is ResolveLinkGfmCommand -> resolveLink(output, command.dri, it.label) + } + } + private fun resolveLink(fileContext: File, dri: DRI, label: String): String = externalModuleLinkResolver.resolve(dri, fileContext)?.let { address -> "[$label]($address)" -- cgit