diff options
author | Błażej Kardyś <bkardys@virtuslab.com> | 2020-04-23 23:26:36 +0200 |
---|---|---|
committer | Paweł Marks <Kordyjan@users.noreply.github.com> | 2020-04-30 12:31:05 +0200 |
commit | 423e7ee17c2cb28d4a34b91231560eff0656ffc0 (patch) | |
tree | 7e02e74d40ce0807011d7663c30ae2489ae08e2d /core | |
parent | 3cd12e14d7465c197d4a6fc76d073328a46db349 (diff) | |
download | dokka-423e7ee17c2cb28d4a34b91231560eff0656ffc0.tar.gz dokka-423e7ee17c2cb28d4a34b91231560eff0656ffc0.tar.bz2 dokka-423e7ee17c2cb28d4a34b91231560eff0656ffc0.zip |
Fixing GFM components display
Diffstat (limited to 'core')
4 files changed, 46 insertions, 11 deletions
diff --git a/core/src/main/kotlin/model/doc/DocTag.kt b/core/src/main/kotlin/model/doc/DocTag.kt index 94558ca7..8a9521ab 100644 --- a/core/src/main/kotlin/model/doc/DocTag.kt +++ b/core/src/main/kotlin/model/doc/DocTag.kt @@ -71,6 +71,7 @@ class Script(children: List<DocTag> = emptyList(), params: Map<String, String> = class Section(children: List<DocTag> = emptyList(), params: Map<String, String> = emptyMap()) : DocTag(children, params) class Small(children: List<DocTag> = emptyList(), params: Map<String, String> = emptyMap()) : DocTag(children, params) class Span(children: List<DocTag> = emptyList(), params: Map<String, String> = emptyMap()) : DocTag(children, params) +class Strikethrough(children: List<DocTag> = emptyList(), params: Map<String, String> = emptyMap()) : DocTag(children, params) class Strong(children: List<DocTag> = emptyList(), params: Map<String, String> = emptyMap()) : DocTag(children, params) class Sub(children: List<DocTag> = emptyList(), params: Map<String, String> = emptyMap()) : DocTag(children, params) class Sup(children: List<DocTag> = emptyList(), params: Map<String, String> = emptyMap()) : DocTag(children, params) diff --git a/core/src/main/kotlin/pages/ContentNodes.kt b/core/src/main/kotlin/pages/ContentNodes.kt index cfcd4069..c0576006 100644 --- a/core/src/main/kotlin/pages/ContentNodes.kt +++ b/core/src/main/kotlin/pages/ContentNodes.kt @@ -185,6 +185,8 @@ enum class TextStyle : Style { Bold, Italic, Strong, Strikethrough, Paragraph, Block, Monospace, Indented } +object CommentTable: Style + fun ContentNode.dfs(predicate: (ContentNode) -> Boolean): ContentNode? = if (predicate(this)) { this } else { @@ -194,3 +196,5 @@ fun ContentNode.dfs(predicate: (ContentNode) -> Boolean): ContentNode? = if (pre null } } + +fun ContentNode.hasStyle(style: Style) = this.style.contains(style) diff --git a/core/src/main/kotlin/parsers/MarkdownParser.kt b/core/src/main/kotlin/parsers/MarkdownParser.kt index 9354a6b6..145e085c 100644 --- a/core/src/main/kotlin/parsers/MarkdownParser.kt +++ b/core/src/main/kotlin/parsers/MarkdownParser.kt @@ -8,11 +8,14 @@ import org.intellij.markdown.ast.ASTNode import org.intellij.markdown.ast.CompositeASTNode import org.intellij.markdown.ast.LeafASTNode import org.intellij.markdown.ast.impl.ListItemCompositeNode -import org.intellij.markdown.flavours.commonmark.CommonMarkFlavourDescriptor +import org.intellij.markdown.flavours.gfm.GFMElementTypes +import org.intellij.markdown.flavours.gfm.GFMFlavourDescriptor +import org.intellij.markdown.flavours.gfm.GFMTokenTypes import org.jetbrains.dokka.analysis.DokkaResolutionFacade import org.jetbrains.dokka.links.DRI import org.jetbrains.dokka.parsers.factories.DocTagsFromIElementFactory import org.jetbrains.dokka.utilities.DokkaLogger +import org.jetbrains.kotlin.descriptors.ClassDescriptor import org.jetbrains.kotlin.descriptors.DeclarationDescriptor import org.jetbrains.kotlin.idea.kdoc.resolveKDocLink import org.jetbrains.kotlin.kdoc.parser.KDocKnownTag @@ -106,15 +109,17 @@ class MarkdownParser( java.net.URL(link) null } catch (e: MalformedURLException) { - resolveKDocLink( - resolutionFacade.resolveSession.bindingContext, - resolutionFacade, - declarationDescriptor, - null, - link.split('.') - ).also { if (it.size > 1) logger.warn("Markdown link resolved more than one element: $it") } - .firstOrNull()//.single() - ?.let { DRI.from(it) } + try { + resolveKDocLink( + resolutionFacade.resolveSession.bindingContext, + resolutionFacade, + declarationDescriptor, + null, + link.split('.') + ).minBy { it is ClassDescriptor }?.let { DRI.from(it) } + } catch (e1: IllegalArgumentException) { + null + } } } @@ -249,9 +254,29 @@ class MarkdownParser( MarkdownElementTypes.MARKDOWN_FILE -> if (node.children.size == 1) visitNode(node.children.first()) else defaultHandler( node ) + GFMElementTypes.STRIKETHROUGH -> DocTagsFromIElementFactory.getInstance( + GFMElementTypes.STRIKETHROUGH, + body = text + .substring(node.startOffset, node.endOffset).transform() + ) + GFMElementTypes.TABLE -> DocTagsFromIElementFactory.getInstance( + GFMElementTypes.TABLE, + children = node.children.filterTabSeparators().evaluateChildren() + ) + GFMElementTypes.HEADER -> DocTagsFromIElementFactory.getInstance( + GFMElementTypes.HEADER, + children = node.children.filterTabSeparators().evaluateChildren() + ) + GFMElementTypes.ROW -> DocTagsFromIElementFactory.getInstance( + GFMElementTypes.ROW, + children = node.children.filterTabSeparators().evaluateChildren() + ) else -> defaultHandler(node) } + private fun List<ASTNode>.filterTabSeparators() = + this.filterNot { it.type == GFMTokenTypes.TABLE_SEPARATOR } + private fun List<ASTNode>.evaluateChildren(): List<DocTag> = this.removeUselessTokens().mergeLeafASTNodes().map { visitNode(it) } @@ -318,7 +343,7 @@ class MarkdownParser( private fun markdownToDocNode(text: String): DocTag { - val flavourDescriptor = CommonMarkFlavourDescriptor() + val flavourDescriptor = GFMFlavourDescriptor() val markdownAstRoot: ASTNode = IntellijMarkdownParser(flavourDescriptor).buildMarkdownTreeFromString(text) return MarkdownVisitor(text, getAllDestinationLinks(text, markdownAstRoot).toMap()).visitNode(markdownAstRoot) diff --git a/core/src/main/kotlin/parsers/factories/DocTagsFromIElementFactory.kt b/core/src/main/kotlin/parsers/factories/DocTagsFromIElementFactory.kt index e616b9e8..dda6c627 100644 --- a/core/src/main/kotlin/parsers/factories/DocTagsFromIElementFactory.kt +++ b/core/src/main/kotlin/parsers/factories/DocTagsFromIElementFactory.kt @@ -4,6 +4,7 @@ import org.jetbrains.dokka.model.doc.* import org.intellij.markdown.IElementType import org.intellij.markdown.MarkdownElementTypes import org.intellij.markdown.MarkdownTokenTypes +import org.intellij.markdown.flavours.gfm.GFMElementTypes import org.jetbrains.dokka.links.DRI import java.lang.NullPointerException @@ -33,6 +34,10 @@ object DocTagsFromIElementFactory { MarkdownTokenTypes.TEXT -> Text(body ?: throw NullPointerException("Text body should be at least empty string passed to DocNodes factory!"), children, params ) MarkdownTokenTypes.HORIZONTAL_RULE -> HorizontalRule MarkdownTokenTypes.HARD_LINE_BREAK -> Br + GFMElementTypes.STRIKETHROUGH -> Strikethrough(children, params) + GFMElementTypes.TABLE -> Table(children, params) + GFMElementTypes.HEADER -> Th(children, params) + GFMElementTypes.ROW -> Tr(children, params) else -> CustomDocTag(children, params) } }
\ No newline at end of file |