diff options
Diffstat (limited to 'plugins/gfm/gfm-template-processing/src/main')
-rw-r--r-- | plugins/gfm/gfm-template-processing/src/main/kotlin/org/jetbrains/dokka/gfm/templateProcessing/GfmTemplateProcessingStrategy.kt | 46 |
1 files changed, 33 insertions, 13 deletions
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<AllModulesPagePlugin>().querySingle { externalModuleLinkResolver } + private val externalModuleLinkResolver = + context.plugin<AllModulesPagePlugin>().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<GfmCommand>(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<GfmCommand>(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)" |