diff --git a/convert/src/main/kotlin/com/github/zeldigas/text2confl/convert/asciidoc/AsciidocParser.kt b/convert/src/main/kotlin/com/github/zeldigas/text2confl/convert/asciidoc/AsciidocParser.kt index ec38be72..13388cc4 100644 --- a/convert/src/main/kotlin/com/github/zeldigas/text2confl/convert/asciidoc/AsciidocParser.kt +++ b/convert/src/main/kotlin/com/github/zeldigas/text2confl/convert/asciidoc/AsciidocParser.kt @@ -5,6 +5,7 @@ import com.vladsch.flexmark.util.sequence.Escaping.unescapeHtml import org.asciidoctor.* import org.asciidoctor.ast.Document import java.nio.file.Path +import java.nio.file.Paths import kotlin.io.path.Path import kotlin.io.path.div @@ -14,7 +15,7 @@ class AsciidocParser( ) { companion object { - private val TEMPLATES_LOCATION = "/com/github/zeldigas/text2confl/asciidoc" + private val TEMPLATES_LOCATION = "com/github/zeldigas/text2confl/asciidoc" } private val ADOC: Asciidoctor by lazy { @@ -27,9 +28,10 @@ class AsciidocParser( } private val templatesLocation: Path by lazy { - val templateResources = AsciidocParser::class.java.getResource(TEMPLATES_LOCATION)!!.toURI() + val templateResources = AsciidocParser::class.java.classLoader.getResource(TEMPLATES_LOCATION)!!.toURI() if (templateResources.scheme == "file") { - Path(templateResources.path) + val mainPath: String = Paths.get(templateResources).toString() + Path(mainPath) } else { val dest = config.workdir / "templates" diff --git a/convert/src/main/kotlin/com/github/zeldigas/text2confl/convert/confluence/ReferenceProvider.kt b/convert/src/main/kotlin/com/github/zeldigas/text2confl/convert/confluence/ReferenceProvider.kt index 7a372ce0..9b34908e 100644 --- a/convert/src/main/kotlin/com/github/zeldigas/text2confl/convert/confluence/ReferenceProvider.kt +++ b/convert/src/main/kotlin/com/github/zeldigas/text2confl/convert/confluence/ReferenceProvider.kt @@ -1,7 +1,12 @@ package com.github.zeldigas.text2confl.convert.confluence import com.github.zeldigas.text2confl.convert.PageHeader +import io.github.oshai.kotlinlogging.KotlinLogging +import java.net.URL +import java.nio.file.InvalidPathException import java.nio.file.Path +import java.util.regex.Matcher +import java.util.regex.Pattern import kotlin.io.path.relativeTo interface ReferenceProvider { @@ -39,24 +44,41 @@ class ReferenceProviderImpl(private val basePath: Path, documents: Map path.relativeTo(basePath).normalize() to header }.toMap() override fun resolveReference(source: Path, refTo: String): Reference? { - if (URI_DETECTOR.matches(refTo)) return null + + if (refTo.startsWith(MAILTO_DETECTOR)) return null + if (refTo.startsWith(LOCALHOST_DETECTOR)) return null + if (isValid(refTo)) return null if (refTo.startsWith("#")) return Anchor(refTo.substring(1)) val parts = refTo.split("#", limit = 2) val ref = parts[0] val anchor = parts.getOrNull(1) - val targetPath = source.resolveSibling(ref).relativeTo(basePath).normalize() + try { - val document = normalizedDocs[targetPath]?.title ?: return null - return Xref(document, anchor) + val targetPath = source.resolveSibling(ref).relativeTo(basePath).normalize() + val document = normalizedDocs[targetPath]?.title ?: return null + return Xref(document, anchor) + } catch (ex: InvalidPathException) { + logger.error { "Failed to resolve : $refTo from $source" } + throw ex + } } override fun pathFromDocsRoot(source: Path): Path { @@ -83,4 +105,3 @@ class ReferenceProviderImpl(private val basePath: Path, documents: Map

hello world

diff --git a/convert/src/test/kotlin/com/github/zeldigas/text2confl/convert/confluence/ReferenceProviderImplTest.kt b/convert/src/test/kotlin/com/github/zeldigas/text2confl/convert/confluence/ReferenceProviderImplTest.kt index 8e1192bf..21b90b6b 100644 --- a/convert/src/test/kotlin/com/github/zeldigas/text2confl/convert/confluence/ReferenceProviderImplTest.kt +++ b/convert/src/test/kotlin/com/github/zeldigas/text2confl/convert/confluence/ReferenceProviderImplTest.kt @@ -3,6 +3,7 @@ package com.github.zeldigas.text2confl.convert.confluence import assertk.assertThat import assertk.assertions.isEqualTo import assertk.assertions.isNotNull +import assertk.assertions.isNull import com.github.zeldigas.text2confl.convert.PageHeader import org.junit.jupiter.api.Test import org.junit.jupiter.params.ParameterizedTest @@ -68,4 +69,49 @@ internal class ReferenceProviderImplTest { assertThat(result).isNotNull().isEqualTo(Xref("Sub Title One", "test")) } + + + @Test + internal fun `Http resolution`() { + val result = providerImpl.resolveReference(Path("docs/one.md"), "http://github.com") + + assertThat(result).isNull() + } + + @Test + internal fun `Https resolution`() { + val result = providerImpl.resolveReference(Path("docs/one.md"), "https://github.com") + + assertThat(result).isNull() + } + + @Test + internal fun `Mailto resolution`() { + val result = providerImpl.resolveReference(Path("docs/one.md"), "mailto:john.doe@github.com") + + assertThat(result).isNull() + } + + + @Test + internal fun `French url resolution`() { + val result = providerImpl.resolveReference(Path("docs/one.md"), "http://github.com/handle'case") + + assertThat(result).isNull() + } + + @Test + internal fun `Parenthesis url resolution`() { + val result = providerImpl.resolveReference(Path("docs/one.md"), "http://github.com/handle()case") + + assertThat(result).isNull() + } + + @Test + internal fun `localhost url resolution`() { + val result = providerImpl.resolveReference(Path("docs/one.md"), "localhost:9000") + + assertThat(result).isNull() + } + } \ No newline at end of file