From 15ccc4c133d87866ab8cdf85a0e3e4f2e33fb603 Mon Sep 17 00:00:00 2001 From: Jens Peters Date: Mon, 23 Oct 2023 18:27:25 +0200 Subject: [PATCH] Create and keep only one Asciidoctor instance It's much cheaper to reuse an already created instance. Co-authored-by: Fabian Illner --- .../structurizr/site/generatr/site/model/Asciidoctor.kt | 7 +++++++ .../structurizr/site/generatr/site/model/ContentText.kt | 4 ---- .../structurizr/site/generatr/site/model/ContentTitle.kt | 3 +-- .../avisi/structurizr/site/generatr/site/model/ToHtml.kt | 3 +-- 4 files changed, 9 insertions(+), 8 deletions(-) create mode 100644 src/main/kotlin/nl/avisi/structurizr/site/generatr/site/model/Asciidoctor.kt 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 new file mode 100644 index 00000000..575decb7 --- /dev/null +++ b/src/main/kotlin/nl/avisi/structurizr/site/generatr/site/model/Asciidoctor.kt @@ -0,0 +1,7 @@ +package nl.avisi.structurizr.site.generatr.site.model + +import org.asciidoctor.Asciidoctor + +val asciidoctor: Asciidoctor = Asciidoctor.Factory.create().apply { + javaConverterRegistry().register(AsciiDocTextConverter::class.java) +} 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 17e98e59..eddb363c 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 @@ -50,12 +50,8 @@ private fun markdownText(content: String): String { } private fun asciidocText(content: String): String { - val asciidoctor = Asciidoctor.Factory.create() - asciidoctor.javaConverterRegistry().register(AsciiDocTextConverter::class.java) - val options = Options.builder().safe(SafeMode.SERVER).backend("text").build() val text = asciidoctor.convert(content, options) - asciidoctor.shutdown() return text.lines().joinToString(" ") } diff --git a/src/main/kotlin/nl/avisi/structurizr/site/generatr/site/model/ContentTitle.kt b/src/main/kotlin/nl/avisi/structurizr/site/generatr/site/model/ContentTitle.kt index cd33657e..91d13524 100644 --- a/src/main/kotlin/nl/avisi/structurizr/site/generatr/site/model/ContentTitle.kt +++ b/src/main/kotlin/nl/avisi/structurizr/site/generatr/site/model/ContentTitle.kt @@ -27,10 +27,9 @@ private fun Section.markdownTitle(): String { } private fun Section.asciidocTitle(): String { - val asciidoctor = Asciidoctor.Factory.create() val options = Options.builder().safe(SafeMode.SERVER).build() val document = asciidoctor.load(content, options) - asciidoctor.shutdown() + if (document.title != null && document.title.isNotEmpty()) return document.title diff --git a/src/main/kotlin/nl/avisi/structurizr/site/generatr/site/model/ToHtml.kt b/src/main/kotlin/nl/avisi/structurizr/site/generatr/site/model/ToHtml.kt index 02fbb496..91c2fc2d 100644 --- a/src/main/kotlin/nl/avisi/structurizr/site/generatr/site/model/ToHtml.kt +++ b/src/main/kotlin/nl/avisi/structurizr/site/generatr/site/model/ToHtml.kt @@ -58,7 +58,6 @@ private fun asciidocToHtml( asciidoc: String, svgFactory: (key: String, url: String) -> String? ): String { - val asciidoctor = Asciidoctor.Factory.create() val options = Options.builder() .safe(SafeMode.SERVER) // Docs dir needs to be exposed from structurizr, which is not the case at the moment. @@ -69,7 +68,7 @@ private fun asciidocToHtml( .backend("html5") .build() val html = asciidoctor.convert(asciidoc, options) - asciidoctor.shutdown() + return Jsoup.parse(html) .apply { body().transformEmbeddedDiagramElements(pageViewModel, svgFactory)