aboutsummaryrefslogtreecommitdiff
path: root/core/src/main
diff options
context:
space:
mode:
Diffstat (limited to 'core/src/main')
-rw-r--r--core/src/main/kotlin/model/doc/DocTag.kt5
-rw-r--r--core/src/main/kotlin/parsers/MarkdownParser.kt49
-rw-r--r--core/src/main/kotlin/parsers/factories/DocNodesFromIElementFactory.kt5
-rw-r--r--core/src/main/kotlin/parsers/factories/DocNodesFromStringFactory.kt9
4 files changed, 38 insertions, 30 deletions
diff --git a/core/src/main/kotlin/model/doc/DocTag.kt b/core/src/main/kotlin/model/doc/DocTag.kt
index 0224634b..94558ca7 100644
--- a/core/src/main/kotlin/model/doc/DocTag.kt
+++ b/core/src/main/kotlin/model/doc/DocTag.kt
@@ -28,6 +28,7 @@ class A(children: List<DocTag> = emptyList(), params: Map<String, String> = empt
class Big(children: List<DocTag> = emptyList(), params: Map<String, String> = emptyMap()) : DocTag(children, params)
class B(children: List<DocTag> = emptyList(), params: Map<String, String> = emptyMap()) : DocTag(children, params)
class BlockQuote(children: List<DocTag> = emptyList(), params: Map<String, String> = emptyMap()) : DocTag(children, params)
+object Br : DocTag(emptyList(), emptyMap())
class Cite(children: List<DocTag> = emptyList(), params: Map<String, String> = emptyMap()) : DocTag(children, params)
class Code(children: List<DocTag> = emptyList(), params: Map<String, String> = emptyMap()) : DocTag(children, params)
class Dd(children: List<DocTag> = emptyList(), params: Map<String, String> = emptyMap()) : DocTag(children, params)
@@ -74,7 +75,7 @@ class Strong(children: List<DocTag> = emptyList(), params: Map<String, String> =
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)
class Table(children: List<DocTag> = emptyList(), params: Map<String, String> = emptyMap()) : DocTag(children, params)
-class Text(children: List<DocTag> = emptyList(), params: Map<String, String> = emptyMap(), val body: String = "") : DocTag(children, params) {
+class Text(val body: String = "", children: List<DocTag> = emptyList(), params: Map<String, String> = emptyMap()) : DocTag(children, params) {
override fun equals(other: Any?): Boolean = super.equals(other) && this.body == (other as Text).body
override fun hashCode(): Int = super.hashCode() + body.hashCode()
}
@@ -89,7 +90,7 @@ class Tt(children: List<DocTag> = emptyList(), params: Map<String, String> = emp
class U(children: List<DocTag> = emptyList(), params: Map<String, String> = emptyMap()) : DocTag(children, params)
class Ul(children: List<DocTag> = emptyList(), params: Map<String, String> = emptyMap()) : DocTag(children, params)
class Var(children: List<DocTag> = emptyList(), params: Map<String, String> = emptyMap()) : DocTag(children, params)
-class DocumentationLink(children: List<DocTag> = emptyList(), params: Map<String, String> = emptyMap(), val dri: DRI) : DocTag(children, params) {
+class DocumentationLink(val dri: DRI, children: List<DocTag> = emptyList(), params: Map<String, String> = emptyMap()) : DocTag(children, params) {
override fun equals(other: Any?): Boolean = super.equals(other) && this.dri == (other as DocumentationLink).dri
override fun hashCode(): Int = super.hashCode() + dri.hashCode()
}
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)
}