From 2d9f0753bb925f6a6d08ab7e67d5676c196de7d0 Mon Sep 17 00:00:00 2001 From: Andrzej Ratajczak Date: Mon, 10 Feb 2020 17:05:11 +0100 Subject: Adds requested changes --- core/src/main/kotlin/parsers/MarkdownParser.kt | 31 +++++++++-------- core/src/test/kotlin/markdown/KDocTest.kt | 48 ++++++++++++++++++++++++++ core/src/test/kotlin/markdown/ParserTest.kt | 18 ++++++++-- 3 files changed, 81 insertions(+), 16 deletions(-) create mode 100644 core/src/test/kotlin/markdown/KDocTest.kt (limited to 'core/src') diff --git a/core/src/main/kotlin/parsers/MarkdownParser.kt b/core/src/main/kotlin/parsers/MarkdownParser.kt index ff27dc2e..4f4a7e18 100644 --- a/core/src/main/kotlin/parsers/MarkdownParser.kt +++ b/core/src/main/kotlin/parsers/MarkdownParser.kt @@ -19,6 +19,7 @@ import org.jetbrains.kotlin.kdoc.parser.KDocKnownTag import org.jetbrains.kotlin.kdoc.psi.impl.KDocImpl import org.jetbrains.kotlin.kdoc.psi.impl.KDocSection import org.jetbrains.kotlin.kdoc.psi.impl.KDocTag +import java.net.MalformedURLException import org.intellij.markdown.parser.MarkdownParser as IntellijMarkdownParser class MarkdownParser ( @@ -85,18 +86,20 @@ class MarkdownParser ( ) } - private fun resolveDRI(link: String): DRI? = if (link.startsWith("http") || link.startsWith("www")) { - null - } else { - resolveKDocLink( - resolutionFacade.resolveSession.bindingContext, - resolutionFacade, - declarationDescriptor, - null, - link.split('.') - ).also { if (it.size > 1) throw Error("Markdown link resolved more than one element: $it") }.firstOrNull()//.single() - ?.let { DRI.from(it) } - } + private fun resolveDRI(link: String): DRI? = + try { + java.net.URL(link) + null + } catch(e: MalformedURLException) { + resolveKDocLink( + resolutionFacade.resolveSession.bindingContext, + resolutionFacade, + declarationDescriptor, + null, + link.split('.') + ).also { if (it.size > 1) throw Error("Markdown link resolved more than one element: $it") }.firstOrNull()//.single() + ?.let { DRI.from(it) } + } private fun referenceLinksHandler(node: ASTNode): DocTag { val linkLabel = node.children.find { it.type == MarkdownElementTypes.LINK_LABEL } ?: @@ -266,8 +269,8 @@ class MarkdownParser ( } private fun String.transform() = this - .replace(Regex("\n\n+"), "") - .replace(Regex("\n>+ "), "\n") + .replace(Regex("\n\n+"), "") // Squashing new lines between paragraphs + .replace(Regex("\n>+ "), "\n") // Replacement used in blockquotes, get rid of garbage } diff --git a/core/src/test/kotlin/markdown/KDocTest.kt b/core/src/test/kotlin/markdown/KDocTest.kt new file mode 100644 index 00000000..c084154b --- /dev/null +++ b/core/src/test/kotlin/markdown/KDocTest.kt @@ -0,0 +1,48 @@ +package markdown + +import org.jetbrains.dokka.model.Package +import org.jetbrains.dokka.model.doc.DocumentationNode +import org.jetbrains.dokka.pages.ModulePageNode +import org.junit.Assert +import testApi.testRunner.AbstractCoreTest + +open class KDocTest : AbstractCoreTest() { + + private val configuration = dokkaConfiguration { + passes { + pass { + sourceRoots = listOf("src/main/kotlin/example/Test.kt") + } + } + } + + private fun interpolateKdoc(kdoc: String) = """ + |/src/main/kotlin/example/Test.kt + |package example + | /** + ${kdoc.split("\n").joinToString("") { "| * $it\n" } } + | */ + |class Test + """.trimMargin() + + private fun actualDocumentationNode(modulePageNode: ModulePageNode) = + (modulePageNode.documentable?.children?.first() as Package) + .classlikes.first() + .platformInfo.first() + .documentationNode + + + protected fun executeTest(kdoc: String, expectedDocumentationNode: DocumentationNode) { + testInline( + interpolateKdoc(kdoc), + configuration + ) { + pagesGenerationStage = { + Assert.assertEquals( + expectedDocumentationNode, + actualDocumentationNode(it) + ) + } + } + } +} \ No newline at end of file diff --git a/core/src/test/kotlin/markdown/ParserTest.kt b/core/src/test/kotlin/markdown/ParserTest.kt index 77ccd769..107940f6 100644 --- a/core/src/test/kotlin/markdown/ParserTest.kt +++ b/core/src/test/kotlin/markdown/ParserTest.kt @@ -1,12 +1,12 @@ package org.jetbrains.dokka.tests +import markdown.KDocTest import org.jetbrains.dokka.model.doc.* import org.junit.Ignore import org.junit.Test -import testApi.testRunner.AbstractKDocTest -class ParserTest : AbstractKDocTest() { +class ParserTest : KDocTest() { @Test fun `Simple text`() { val kdoc = """ @@ -897,5 +897,19 @@ class ParserTest : AbstractKDocTest() { ) executeTest(kdoc, expectedDocumentationNode) } + + @Test fun `Windows Carriage Return Line Feed`() { + val kdoc = "text\r\ntext" + val expectedDocumentationNode = DocumentationNode( + listOf( + Description( + P(listOf( + Text(body = "text\ntext") + )) + ) + ) + ) + executeTest(kdoc, expectedDocumentationNode) + } } -- cgit