diff options
author | Andrzej Ratajczak <andrzej.ratajczak98@gmail.com> | 2020-02-12 14:22:11 +0100 |
---|---|---|
committer | Paweł Marks <Kordyjan@users.noreply.github.com> | 2020-02-12 14:29:06 +0100 |
commit | 135a3118cf9d08bb50ff0c438b60508ecf1994c6 (patch) | |
tree | 7e53776267524ca2bd27b7d8fb08c9a83fe1e1b6 /core/src/main/kotlin/parsers | |
parent | 193d8259c234c3b73d4f89f10aa0e2cdfc217e6c (diff) | |
download | dokka-135a3118cf9d08bb50ff0c438b60508ecf1994c6.tar.gz dokka-135a3118cf9d08bb50ff0c438b60508ecf1994c6.tar.bz2 dokka-135a3118cf9d08bb50ff0c438b60508ecf1994c6.zip |
Added Br handling
Diffstat (limited to 'core/src/main/kotlin/parsers')
3 files changed, 35 insertions, 28 deletions
diff --git a/core/src/main/kotlin/parsers/MarkdownParser.kt b/core/src/main/kotlin/parsers/MarkdownParser.kt index 4f4a7e18..d70a2239 100644 --- a/core/src/main/kotlin/parsers/MarkdownParser.kt +++ b/core/src/main/kotlin/parsers/MarkdownParser.kt @@ -1,6 +1,8 @@ package org.jetbrains.dokka.parsers import com.intellij.psi.PsiElement +import org.intellij.markdown.IElementType +import org.intellij.markdown.MarkdownElementType import org.jetbrains.dokka.model.doc.* import org.intellij.markdown.MarkdownElementTypes import org.intellij.markdown.MarkdownTokenTypes @@ -47,7 +49,8 @@ class MarkdownParser ( private fun blockquotesHandler(node: ASTNode): DocTag = DocNodesFromIElementFactory.getInstance(node.type, children = node.children - .filterIsInstance<CompositeASTNode>().evaluateChildren()) + .filterIsInstance<CompositeASTNode>() + .evaluateChildren()) private fun listsHandler(node: ASTNode): DocTag { @@ -166,11 +169,14 @@ class MarkdownParser ( node.type, children = node .children - .let { listOf(Text( body = text.substring( - it.find { it.type == MarkdownTokenTypes.CODE_FENCE_CONTENT }?.startOffset ?: 0, - it.findLast { it.type == MarkdownTokenTypes.CODE_FENCE_CONTENT }?.endOffset ?: 0 //TODO: Problem with empty code fence - )) - ) }, + .dropWhile { it.type != MarkdownTokenTypes.CODE_FENCE_CONTENT } + .dropLastWhile { it.type != MarkdownTokenTypes.CODE_FENCE_CONTENT } + .map { + if(it.type == MarkdownTokenTypes.EOL) + LeafASTNode(MarkdownTokenTypes.HARD_LINE_BREAK, 0, 0) + else + it + }.evaluateChildren(), params = node .children .find { it.type == MarkdownTokenTypes.FENCE_LANG } @@ -208,6 +214,7 @@ class MarkdownParser ( MarkdownElementTypes.CODE_FENCE -> codeFencesHandler(node) MarkdownElementTypes.CODE_SPAN -> codeSpansHandler(node) MarkdownElementTypes.IMAGE -> imagesHandler(node) + MarkdownTokenTypes.HARD_LINE_BREAK -> DocNodesFromIElementFactory.getInstance(node.type) MarkdownTokenTypes.CODE_FENCE_CONTENT, MarkdownTokenTypes.CODE_LINE, MarkdownTokenTypes.TEXT -> DocNodesFromIElementFactory.getInstance( @@ -223,19 +230,16 @@ class MarkdownParser ( this.removeUselessTokens().mergeLeafASTNodes().map { visitNode(it) } private fun List<ASTNode>.removeUselessTokens(): List<ASTNode> = - this.filterIndexed { index, _ -> - !(this[index].type == MarkdownTokenTypes.EOL && - this.isLeaf(index - 1) && this.getOrNull(index - 1)?.type !in leafNodes && - this.isLeaf(index + 1) && this.getOrNull(index + 1)?.type !in leafNodes) && - this[index].type != MarkdownElementTypes.LINK_DEFINITION - } + this.filterIndexed { index, node -> !(node.type == MarkdownElementTypes.LINK_DEFINITION || ( + node.type == MarkdownTokenTypes.EOL && + this.getOrNull(index - 1)?.type == MarkdownTokenTypes.HARD_LINE_BREAK + )) } - private val notLeafNodes = listOf(MarkdownTokenTypes.HORIZONTAL_RULE) - private val leafNodes = listOf(MarkdownElementTypes.STRONG, MarkdownElementTypes.EMPH) + private val notLeafNodes = listOf(MarkdownTokenTypes.HORIZONTAL_RULE, MarkdownTokenTypes.HARD_LINE_BREAK) - private fun List<ASTNode>.isLeaf(index: Int): Boolean = + private fun List<ASTNode>.isNotLeaf(index: Int): Boolean = if(index in 0..this.lastIndex) - (this[index] is CompositeASTNode)|| this[index].type in notLeafNodes + (this[index] is CompositeASTNode) || this[index].type in notLeafNodes else false @@ -243,15 +247,15 @@ class MarkdownParser ( val children: MutableList<ASTNode> = mutableListOf() var index = 0 while(index <= this.lastIndex) { - if(this.isLeaf(index)) { + if(this.isNotLeaf(index)) { children += this[index] } else { val startOffset = this[index].startOffset - while(index < this.lastIndex) { - if(this.isLeaf(index + 1)) { + while(index < this.lastIndex ) { + if(this.isNotLeaf(index + 1) || this[index+1].startOffset != this[index].endOffset) { val endOffset = this[index].endOffset - if(text.substring(startOffset, endOffset).transform().isNotEmpty()) + if(text.substring(startOffset, endOffset).transform().trim().isNotEmpty()) children += LeafASTNode(MarkdownTokenTypes.TEXT, startOffset, endOffset) break } @@ -259,7 +263,7 @@ class MarkdownParser ( } if(index == this.lastIndex) { val endOffset = this[index].endOffset - if(text.substring(startOffset, endOffset).transform().isNotEmpty()) + if(text.substring(startOffset, endOffset).transform().trim().isNotEmpty()) children += LeafASTNode(MarkdownTokenTypes.TEXT, startOffset, endOffset) } } @@ -270,7 +274,8 @@ class MarkdownParser ( private fun String.transform() = this .replace(Regex("\n\n+"), "") // Squashing new lines between paragraphs - .replace(Regex("\n>+ "), "\n") // Replacement used in blockquotes, get rid of garbage + .replace(Regex("\n"), " ") + .replace(Regex(" >+ +"), " ") // Replacement used in blockquotes, get rid of garbage } diff --git a/core/src/main/kotlin/parsers/factories/DocNodesFromIElementFactory.kt b/core/src/main/kotlin/parsers/factories/DocNodesFromIElementFactory.kt index b899679d..d4c6e752 100644 --- a/core/src/main/kotlin/parsers/factories/DocNodesFromIElementFactory.kt +++ b/core/src/main/kotlin/parsers/factories/DocNodesFromIElementFactory.kt @@ -12,7 +12,7 @@ object DocNodesFromIElementFactory { when(type) { MarkdownElementTypes.SHORT_REFERENCE_LINK, MarkdownElementTypes.FULL_REFERENCE_LINK, - MarkdownElementTypes.INLINE_LINK -> if(dri == null) A(children, params) else DocumentationLink(children, params, dri) + 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, @@ -30,8 +30,9 @@ object DocNodesFromIElementFactory { MarkdownElementTypes.ORDERED_LIST -> Ol(children, params) MarkdownElementTypes.UNORDERED_LIST -> Ul(children, params) MarkdownElementTypes.PARAGRAPH -> P(children, params) - MarkdownTokenTypes.TEXT -> Text(children, params, body ?: throw NullPointerException("Text body should be at least empty string passed to DocNodes factory!")) + 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 else -> CustomDocTag(children, params) } }
\ No newline at end of file diff --git a/core/src/main/kotlin/parsers/factories/DocNodesFromStringFactory.kt b/core/src/main/kotlin/parsers/factories/DocNodesFromStringFactory.kt index dc74ecc1..4ff9a9d4 100644 --- a/core/src/main/kotlin/parsers/factories/DocNodesFromStringFactory.kt +++ b/core/src/main/kotlin/parsers/factories/DocNodesFromStringFactory.kt @@ -11,8 +11,9 @@ object DocNodesFromStringFactory { "big" -> Big(children, params) "b" -> B(children, params) "blockquote" -> BlockQuote(children, params) - "bite" -> Cite(children, params) - "bode" -> Code(children, params) + "br" -> Br + "cite" -> Cite(children, params) + "code" -> Code(children, params) "dd" -> Dd(children, params) "dfn" -> Dfn(children, params) "dir" -> Dir(children, params) @@ -57,7 +58,7 @@ object DocNodesFromStringFactory { "sub" -> Sub(children, params) "sup" -> Sup(children, params) "table" -> Table(children, params) - "#text" -> Text(children, params, body ?: throw NullPointerException("Text body should be at least empty string passed to DocNodes factory!")) + "#text" -> Text(body ?: throw NullPointerException("Text body should be at least empty string passed to DocNodes factory!"), children, params) "tBody" -> TBody(children, params) "td" -> Td(children, params) "tFoot" -> TFoot(children, params) @@ -69,7 +70,7 @@ object DocNodesFromStringFactory { "u" -> U(children, params) "ul" -> Ul(children, params) "var" -> Var(children, params) - "documentationlink" -> DocumentationLink(children, params, dri ?: throw NullPointerException("DRI cannot be passed null while constructing documentation link!")) + "documentationlink" -> DocumentationLink(dri ?: throw NullPointerException("DRI cannot be passed null while constructing documentation link!"), children, params) "hr" -> HorizontalRule else -> CustomDocTag(children, params) } |