diff options
author | Andrzej Ratajczak <andrzej.ratajczak98@gmail.com> | 2020-07-14 19:22:17 +0200 |
---|---|---|
committer | Sebastian Sellmair <34319766+sellmair@users.noreply.github.com> | 2020-07-15 16:01:44 +0200 |
commit | dcd4c1c5bc13e7bb058bcd055aa2b02d7d39e9c1 (patch) | |
tree | 7ff9003f55c5890ca5e7d47a0f2adc58ee363201 /plugins/base | |
parent | ac19f61e253e9d168898fe3a0f64221b697ad8be (diff) | |
download | dokka-dcd4c1c5bc13e7bb058bcd055aa2b02d7d39e9c1.tar.gz dokka-dcd4c1c5bc13e7bb058bcd055aa2b02d7d39e9c1.tar.bz2 dokka-dcd4c1c5bc13e7bb058bcd055aa2b02d7d39e9c1.zip |
Fix presenting inline code in KDoc
Diffstat (limited to 'plugins/base')
10 files changed, 49 insertions, 29 deletions
diff --git a/plugins/base/src/main/kotlin/parsers/factories/DocTagsFromIElementFactory.kt b/plugins/base/src/main/kotlin/parsers/factories/DocTagsFromIElementFactory.kt index e81eb513..277fd35e 100644 --- a/plugins/base/src/main/kotlin/parsers/factories/DocTagsFromIElementFactory.kt +++ b/plugins/base/src/main/kotlin/parsers/factories/DocTagsFromIElementFactory.kt @@ -16,9 +16,9 @@ object DocTagsFromIElementFactory { MarkdownElementTypes.INLINE_LINK -> if(dri == null) A(children, params) else DocumentationLink(dri, children, params) MarkdownElementTypes.STRONG -> B(children, params) MarkdownElementTypes.BLOCK_QUOTE -> BlockQuote(children, params) - MarkdownElementTypes.CODE_SPAN, + MarkdownElementTypes.CODE_SPAN -> CodeInline(children, params) MarkdownElementTypes.CODE_BLOCK, - MarkdownElementTypes.CODE_FENCE -> Code(children, params) + MarkdownElementTypes.CODE_FENCE -> CodeBlock(children, params) MarkdownElementTypes.ATX_1 -> H1(children, params) MarkdownElementTypes.ATX_2 -> H2(children, params) MarkdownElementTypes.ATX_3 -> H3(children, params) diff --git a/plugins/base/src/main/kotlin/parsers/factories/DocTagsFromStringFactory.kt b/plugins/base/src/main/kotlin/parsers/factories/DocTagsFromStringFactory.kt index de7c3cad..124dc3b4 100644 --- a/plugins/base/src/main/kotlin/parsers/factories/DocTagsFromStringFactory.kt +++ b/plugins/base/src/main/kotlin/parsers/factories/DocTagsFromStringFactory.kt @@ -13,7 +13,7 @@ object DocTagsFromStringFactory { "blockquote" -> BlockQuote(children, params) "br" -> Br "cite" -> Cite(children, params) - "code" -> Code(children, params) + "code" -> if(params.isEmpty()) CodeInline(children, params) else CodeBlock(children, params) "dd" -> Dd(children, params) "dfn" -> Dfn(children, params) "dir" -> Dir(children, params) diff --git a/plugins/base/src/main/kotlin/renderers/DefaultRenderer.kt b/plugins/base/src/main/kotlin/renderers/DefaultRenderer.kt index 75fbb187..afee1b33 100644 --- a/plugins/base/src/main/kotlin/renderers/DefaultRenderer.kt +++ b/plugins/base/src/main/kotlin/renderers/DefaultRenderer.kt @@ -74,7 +74,11 @@ abstract class DefaultRenderer<T>( nodes.forEach { it.build(this, pageContext, sourceSetRestriction) } } - open fun T.buildCode(code: ContentCode, pageContext: ContentPage) { + open fun T.buildCodeBlock(code: ContentCodeBlock, pageContext: ContentPage) { + code.children.forEach { it.build(this, pageContext) } + } + + open fun T.buildCodeInline(code: ContentCodeInline, pageContext: ContentPage) { code.children.forEach { it.build(this, pageContext) } } @@ -102,7 +106,8 @@ abstract class DefaultRenderer<T>( when (node) { is ContentText -> buildText(node) is ContentHeader -> buildHeader(node, pageContext, sourceSetRestriction) - is ContentCode -> buildCode(node, pageContext) + is ContentCodeBlock -> buildCodeBlock(node, pageContext) + is ContentCodeInline -> buildCodeInline(node, pageContext) is ContentDRILink -> buildLink(locationProvider.resolve(node.address, node.sourceSets, pageContext)) { buildLinkText(node.children, pageContext, sourceSetRestriction) diff --git a/plugins/base/src/main/kotlin/renderers/html/HtmlRenderer.kt b/plugins/base/src/main/kotlin/renderers/html/HtmlRenderer.kt index 8e18547c..44ca5e20 100644 --- a/plugins/base/src/main/kotlin/renderers/html/HtmlRenderer.kt +++ b/plugins/base/src/main/kotlin/renderers/html/HtmlRenderer.kt @@ -554,8 +554,8 @@ open class HtmlRenderer( override fun FlowContent.buildLink(address: String, content: FlowContent.() -> Unit) = a(href = address, block = content) - override fun FlowContent.buildCode( - code: ContentCode, + override fun FlowContent.buildCodeBlock( + code: ContentCodeBlock, pageContext: ContentPage ) { div("sample-container") { @@ -566,6 +566,15 @@ open class HtmlRenderer( } } + override fun FlowContent.buildCodeInline( + code: ContentCodeInline, + pageContext: ContentPage + ) { + code { + code.children.forEach { buildContentNode(it, pageContext) } + } + } + private fun getSymbolSignature(page: ContentPage) = page.content.dfs { it.dci.kind == ContentKind.Symbol } private fun flattenToText(node: ContentNode): String { diff --git a/plugins/base/src/main/kotlin/transformers/pages/comments/DocTagToContentConverter.kt b/plugins/base/src/main/kotlin/transformers/pages/comments/DocTagToContentConverter.kt index 1770dc6f..19817992 100644 --- a/plugins/base/src/main/kotlin/transformers/pages/comments/DocTagToContentConverter.kt +++ b/plugins/base/src/main/kotlin/transformers/pages/comments/DocTagToContentConverter.kt @@ -91,7 +91,7 @@ object DocTagToContentConverter : CommentsToContentConverter { ) ) is BlockQuote -> listOf( - ContentCode( + ContentCodeBlock( buildChildren(docTag), "", dci, @@ -99,8 +99,17 @@ object DocTagToContentConverter : CommentsToContentConverter { styles ) ) - is Code -> listOf( - ContentCode( + is CodeInline -> listOf( + ContentCodeInline( + buildChildren(docTag), + "", + dci, + sourceSets, + styles + ) + ) + is CodeBlock -> listOf( + ContentCodeBlock( buildChildren(docTag), "", dci, diff --git a/plugins/base/src/main/kotlin/transformers/pages/samples/SamplesTransformer.kt b/plugins/base/src/main/kotlin/transformers/pages/samples/SamplesTransformer.kt index 41dea1a2..695ef050 100644 --- a/plugins/base/src/main/kotlin/transformers/pages/samples/SamplesTransformer.kt +++ b/plugins/base/src/main/kotlin/transformers/pages/samples/SamplesTransformer.kt @@ -86,7 +86,7 @@ abstract class SamplesTransformer(val context: DokkaContext) : PageTransformer { | //sampleEnd |}""".trimMargin() - private fun ContentNode.dfs(fqName: String, node: ContentCode): ContentNode { + private fun ContentNode.dfs(fqName: String, node: ContentCodeBlock): ContentNode { return when (this) { is ContentHeader -> copy(children.map { it.dfs(fqName, node) }) is ContentDivergentGroup -> @Suppress("UNCHECKED_CAST") copy(children.map { @@ -96,7 +96,8 @@ abstract class SamplesTransformer(val context: DokkaContext) : PageTransformer { before.let { it?.dfs(fqName, node) }, divergent.dfs(fqName, node), after.let { it?.dfs(fqName, node) }) - is ContentCode -> copy(children.map { it.dfs(fqName, node) }) + is ContentCodeBlock -> copy(children.map { it.dfs(fqName, node) }) + is ContentCodeInline -> copy(children.map { it.dfs(fqName, node) }) is ContentDRILink -> copy(children.map { it.dfs(fqName, node) }) is ContentResolvedLink -> copy(children.map { it.dfs(fqName, node) }) is ContentEmbeddedResource -> copy(children.map { it.dfs(fqName, node) }) @@ -131,7 +132,7 @@ abstract class SamplesTransformer(val context: DokkaContext) : PageTransformer { styles: Set<Style> = emptySet(), extra: PropertyContainer<ContentNode> = PropertyContainer.empty() ) = - ContentCode( + ContentCodeBlock( children = listOf( ContentText( text = content, diff --git a/plugins/base/src/main/kotlin/translators/psi/JavadocParser.kt b/plugins/base/src/main/kotlin/translators/psi/JavadocParser.kt index 89226821..2e921e83 100644 --- a/plugins/base/src/main/kotlin/translators/psi/JavadocParser.kt +++ b/plugins/base/src/main/kotlin/translators/psi/JavadocParser.kt @@ -158,7 +158,7 @@ class JavadocParser( "strong" -> Strong(children) "i" -> I(children) "em" -> Em(children) - "code" -> Code(children) + "code" -> CodeBlock(children) "pre" -> Pre(children) "ul" -> Ul(children) "ol" -> Ol(children) @@ -196,7 +196,7 @@ class JavadocParser( tag.referenceElement()?.toDocumentationLink(tag.dataElements.firstIsInstanceOrNull<PsiDocToken>()) } "code", "literal" -> { - Code(listOf(Text(tag.text))) + CodeInline(listOf(Text(tag.text))) } else -> Text(tag.text) } diff --git a/plugins/base/src/test/kotlin/linkableContent/LinkableContentTest.kt b/plugins/base/src/test/kotlin/linkableContent/LinkableContentTest.kt index 2775fcf4..a8fc9f6d 100644 --- a/plugins/base/src/test/kotlin/linkableContent/LinkableContentTest.kt +++ b/plugins/base/src/test/kotlin/linkableContent/LinkableContentTest.kt @@ -166,7 +166,7 @@ class LinkableContentTest : AbstractCoreTest() { .cast<PlatformHintedContent>().children.single() .cast<ContentGroup>().children.single() .cast<ContentGroup>().children.single() - .cast<ContentCode>().children.single().cast<ContentText>().text + .cast<ContentCodeBlock>().children.single().cast<ContentText>().text Assertions.assertEquals( """|import p2.${name}Class |fun main() { diff --git a/plugins/base/src/test/kotlin/markdown/ParserTest.kt b/plugins/base/src/test/kotlin/markdown/ParserTest.kt index 332c9766..ee6170c2 100644 --- a/plugins/base/src/test/kotlin/markdown/ParserTest.kt +++ b/plugins/base/src/test/kotlin/markdown/ParserTest.kt @@ -808,7 +808,7 @@ class ParserTest : KDocTest() { Description( P( listOf( - Code(listOf(Text("Some code"))), + CodeInline(listOf(Text("Some code"))), Text(" Sample text") ) ) @@ -837,7 +837,7 @@ class ParserTest : KDocTest() { Description( P( listOf( - Code( + CodeBlock( listOf( Text("val x: Int = 0"), Br, Text("val y: String = \"Text\""), Br, Br, diff --git a/plugins/base/src/test/kotlin/transformers/CommentsToContentConverterTest.kt b/plugins/base/src/test/kotlin/transformers/CommentsToContentConverterTest.kt index 2fe2d2ae..5197afc6 100644 --- a/plugins/base/src/test/kotlin/transformers/CommentsToContentConverterTest.kt +++ b/plugins/base/src/test/kotlin/transformers/CommentsToContentConverterTest.kt @@ -1,17 +1,13 @@ package transformers -import org.jetbrains.dokka.DokkaConfiguration -import org.jetbrains.dokka.base.DokkaBase import org.jetbrains.dokka.base.transformers.pages.comments.DocTagToContentConverter import org.jetbrains.dokka.links.DRI import org.jetbrains.dokka.model.doc.* -import org.jetbrains.dokka.plugability.querySingle import org.junit.jupiter.api.Assertions.* import org.junit.jupiter.api.Test import matchers.content.* import org.jetbrains.dokka.pages.* import org.jetbrains.kotlin.utils.addToStdlib.assertedCast -import utils.assertIsInstance class CommentsToContentConverterTest { private val converter = DocTagToContentConverter @@ -215,11 +211,11 @@ class CommentsToContentConverterTest { ) ) executeTest(docTag) { - node<ContentCode> { + node<ContentCodeBlock> { +"Blockquotes are very handy in email to emulate reply text. This line is part of the same quote." } +"Quote break." - node<ContentCode> { + node<ContentCodeBlock> { +"Quote" } } @@ -249,15 +245,15 @@ class CommentsToContentConverterTest { ) ) executeTest(docTag) { - node<ContentCode> { + node<ContentCodeBlock> { +"text 1 text 2" - node<ContentCode> { + node<ContentCodeBlock> { +"text 3 text 4" } +"text 5" } +"Quote break." - node<ContentCode> { + node<ContentCodeBlock> { +"Quote" } } @@ -267,7 +263,7 @@ class CommentsToContentConverterTest { fun `multiline code`() { val docTag = P( listOf( - Code( + CodeBlock( listOf( Text("val x: Int = 0"), Br, Text("val y: String = \"Text\""), Br, Br, @@ -282,7 +278,7 @@ class CommentsToContentConverterTest { ) ) executeTest(docTag) { - node<ContentCode> { + node<ContentCodeBlock> { +"val x: Int = 0" node<ContentBreakLine>() +"val y: String = \"Text\"" |