Skip to content

Commit

Permalink
Move converter class
Browse files Browse the repository at this point in the history
Also improve formatting.

Co-authored-by: Fabian Illner <[email protected]>
  • Loading branch information
jp7677 and illnr committed Oct 23, 2023
1 parent 8c7c1de commit 45efb9b
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 24 deletions.
Original file line number Diff line number Diff line change
@@ -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<String?, Any?>?
) : StringConverter(backend, opts) {
// based on https://docs.asciidoctor.org/asciidoctorj/latest/write-converter/

override fun convert(node: ContentNode, transform: String?, o: Map<Any?, Any?>?): 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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -55,21 +49,3 @@ private fun asciidocText(content: String): String {

return text.lines().joinToString(" ")
}

@ConverterFor("text")
class AsciiDocTextConverter(
backend: String?,
opts: Map<String?, Any?>?
) : StringConverter(backend, opts) {
override fun convert(node: ContentNode, transform: String?, o: Map<Any?, Any?>?): 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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 45efb9b

Please sign in to comment.