From 45efb9b7e15f7d3708ca56bb6baef1f50a3135f6 Mon Sep 17 00:00:00 2001 From: Jens Peters Date: Mon, 23 Oct 2023 18:43:07 +0200 Subject: [PATCH] Move converter class Also improve formatting. Co-authored-by: Fabian Illner --- .../site/generatr/site/model/Asciidoctor.kt | 25 +++++++++++++++++++ .../site/generatr/site/model/ContentText.kt | 24 ------------------ .../generatr/site/model/AsciidocToHtmlTest.kt | 1 + 3 files changed, 26 insertions(+), 24 deletions(-) diff --git a/src/main/kotlin/nl/avisi/structurizr/site/generatr/site/model/Asciidoctor.kt b/src/main/kotlin/nl/avisi/structurizr/site/generatr/site/model/Asciidoctor.kt index 575decb7..655cbd06 100644 --- a/src/main/kotlin/nl/avisi/structurizr/site/generatr/site/model/Asciidoctor.kt +++ b/src/main/kotlin/nl/avisi/structurizr/site/generatr/site/model/Asciidoctor.kt @@ -1,7 +1,32 @@ package nl.avisi.structurizr.site.generatr.site.model import org.asciidoctor.Asciidoctor +import org.asciidoctor.ast.ContentNode +import org.asciidoctor.ast.Document +import org.asciidoctor.ast.StructuralNode +import org.asciidoctor.converter.ConverterFor +import org.asciidoctor.converter.StringConverter val asciidoctor: Asciidoctor = Asciidoctor.Factory.create().apply { javaConverterRegistry().register(AsciiDocTextConverter::class.java) } + +@ConverterFor("text") +class AsciiDocTextConverter( + backend: String?, + opts: Map? +) : StringConverter(backend, opts) { + // based on https://docs.asciidoctor.org/asciidoctorj/latest/write-converter/ + + override fun convert(node: ContentNode, transform: String?, o: Map?): String? { + val transform1 = transform ?: node.nodeName + return if (node is Document) + node.content.toString() + else if (node is org.asciidoctor.ast.Section) + "${node.title}\n${node.content}" + else if (transform1 == "preamble" || transform1 == "paragraph") + (node as StructuralNode).content as String + else + null + } +} diff --git a/src/main/kotlin/nl/avisi/structurizr/site/generatr/site/model/ContentText.kt b/src/main/kotlin/nl/avisi/structurizr/site/generatr/site/model/ContentText.kt index eddb363c..6bfff62a 100644 --- a/src/main/kotlin/nl/avisi/structurizr/site/generatr/site/model/ContentText.kt +++ b/src/main/kotlin/nl/avisi/structurizr/site/generatr/site/model/ContentText.kt @@ -6,14 +6,8 @@ import com.structurizr.documentation.Section import com.vladsch.flexmark.ast.Heading import com.vladsch.flexmark.ast.Paragraph import com.vladsch.flexmark.parser.Parser -import org.asciidoctor.Asciidoctor import org.asciidoctor.Options import org.asciidoctor.SafeMode -import org.asciidoctor.ast.ContentNode -import org.asciidoctor.ast.Document -import org.asciidoctor.ast.StructuralNode -import org.asciidoctor.converter.ConverterFor -import org.asciidoctor.converter.StringConverter private val parser = Parser.builder().build() @@ -55,21 +49,3 @@ private fun asciidocText(content: String): String { return text.lines().joinToString(" ") } - -@ConverterFor("text") -class AsciiDocTextConverter( - backend: String?, - opts: Map? -) : StringConverter(backend, opts) { - override fun convert(node: ContentNode, transform: String?, o: Map?): String? { - val transform1 = transform ?: node.nodeName - return if (node is Document) - node.content.toString() - else if (node is org.asciidoctor.ast.Section) - "${node.title}\n${node.content}" - else if (transform1 == "preamble" || transform1 == "paragraph") - (node as StructuralNode).content as String - else - null - } -} diff --git a/src/test/kotlin/nl/avisi/structurizr/site/generatr/site/model/AsciidocToHtmlTest.kt b/src/test/kotlin/nl/avisi/structurizr/site/generatr/site/model/AsciidocToHtmlTest.kt index aa2e0b44..d1c817b6 100644 --- a/src/test/kotlin/nl/avisi/structurizr/site/generatr/site/model/AsciidocToHtmlTest.kt +++ b/src/test/kotlin/nl/avisi/structurizr/site/generatr/site/model/AsciidocToHtmlTest.kt @@ -6,6 +6,7 @@ import com.structurizr.documentation.Format import org.junit.jupiter.api.Test class AsciidocToHtmlTest : ViewModelTest() { + @Test fun `translates asciidoc`() { val generatorContext = generatorContext()