From 288431f21b18f5311de77c9afcb29346cb7c6498 Mon Sep 17 00:00:00 2001 From: Andrzej Ratajczak Date: Fri, 13 Dec 2019 16:24:16 +0100 Subject: Changes naming and applies pull request's requirements --- core/src/main/kotlin/parsers/HtmlParser.kt | 10 +-- core/src/main/kotlin/parsers/MarkdownParser.kt | 91 ++++++++++++---------- core/src/main/kotlin/parsers/Parser.kt | 12 +-- .../factories/DocNodesFromIElementFactory.kt | 6 +- .../parsers/factories/DocNodesFromStringFactory.kt | 6 +- 5 files changed, 69 insertions(+), 56 deletions(-) (limited to 'core/src/main/kotlin/parsers') diff --git a/core/src/main/kotlin/parsers/HtmlParser.kt b/core/src/main/kotlin/parsers/HtmlParser.kt index 30f882b1..aebdee41 100644 --- a/core/src/main/kotlin/parsers/HtmlParser.kt +++ b/core/src/main/kotlin/parsers/HtmlParser.kt @@ -1,6 +1,6 @@ -package parsers +package org.jetbrains.dokka.parsers -import model.doc.* +import org.jetbrains.dokka.model.doc.* import org.jetbrains.dokka.parsers.factories.DocNodesFromStringFactory import org.jsoup.Jsoup import org.jsoup.nodes.Node @@ -11,10 +11,10 @@ class HtmlParser : Parser() { inner class NodeFilterImpl : NodeFilter { - private val nodesCache: MutableMap> = mutableMapOf() + private val nodesCache: MutableMap> = mutableMapOf() private var currentDepth = 0 - fun collect(): DocNode = nodesCache[currentDepth]!![0] + fun collect(): DocTag = nodesCache[currentDepth]!![0] override fun tail(node: Node?, depth: Int): NodeFilter.FilterResult { val nodeName = node!!.nodeName() @@ -65,7 +65,7 @@ class HtmlParser : Parser() { } - private fun htmlToDocNode(string: String): DocNode { + private fun htmlToDocNode(string: String): DocTag { val document = Jsoup.parse(string) val nodeFilterImpl = NodeFilterImpl() NodeTraversor.filter(nodeFilterImpl, document.root()) diff --git a/core/src/main/kotlin/parsers/MarkdownParser.kt b/core/src/main/kotlin/parsers/MarkdownParser.kt index 44c917e3..b4ebdac7 100644 --- a/core/src/main/kotlin/parsers/MarkdownParser.kt +++ b/core/src/main/kotlin/parsers/MarkdownParser.kt @@ -1,9 +1,10 @@ -package parsers +package org.jetbrains.dokka.parsers -import model.doc.* +import org.jetbrains.dokka.model.doc.* import org.intellij.markdown.MarkdownElementTypes import org.intellij.markdown.MarkdownTokenTypes import org.intellij.markdown.ast.ASTNode +import org.intellij.markdown.ast.CompositeASTNode import org.intellij.markdown.ast.impl.ListItemCompositeNode import org.intellij.markdown.flavours.commonmark.CommonMarkFlavourDescriptor import org.jetbrains.dokka.analysis.DokkaResolutionFacade @@ -22,19 +23,25 @@ class MarkdownParser ( inner class MarkdownVisitor(val text: String) { - private fun headersHandler(node: ASTNode): DocNode = - DocNodesFromIElementFactory.getInstance(node.type, visitNode(node.children.find { it.type == MarkdownTokenTypes.ATX_CONTENT }!!).children.drop(1)) + private fun headersHandler(node: ASTNode): DocTag = + DocNodesFromIElementFactory.getInstance( + node.type, + visitNode(node.children.find { it.type == MarkdownTokenTypes.ATX_CONTENT }!!).children + ) - private fun horizontalRulesHandler(node: ASTNode): DocNode = + private fun horizontalRulesHandler(node: ASTNode): DocTag = DocNodesFromIElementFactory.getInstance(MarkdownTokenTypes.HORIZONTAL_RULE) - private fun emphasisHandler(node: ASTNode): DocNode = - DocNodesFromIElementFactory.getInstance(node.type, children = listOf(visitNode(node.children[node.children.size/2]))) + private fun emphasisHandler(node: ASTNode): DocTag = + DocNodesFromIElementFactory.getInstance( + node.type, + children = listOf(visitNode(node.children[node.children.size / 2])) + ) - private fun blockquotesHandler(node: ASTNode): DocNode = + private fun blockquotesHandler(node: ASTNode): DocTag = DocNodesFromIElementFactory.getInstance(node.type, children = node.children.drop(1).map { visitNode(it) }) - private fun listsHandler(node: ASTNode): DocNode { + private fun listsHandler(node: ASTNode): DocTag { val children = node.children.filterIsInstance().flatMap { if( it.children.last().type in listOf(MarkdownElementTypes.ORDERED_LIST, MarkdownElementTypes.UNORDERED_LIST) ) { @@ -57,8 +64,7 @@ class MarkdownParser ( children = it .children .drop(1) - .filter { it.type !in listOf(MarkdownTokenTypes.WHITE_SPACE, MarkdownTokenTypes.EOL) } - .map { visitNode(it) } + .evaluateChildren() ) else visitNode(it) @@ -72,7 +78,7 @@ class MarkdownParser ( ) } - private fun linksHandler(node: ASTNode): DocNode { + private fun linksHandler(node: ASTNode): DocTag { val linkNode = node.children.find { it.type == MarkdownElementTypes.LINK_LABEL }!! val link = text.substring(linkNode.startOffset+1, linkNode.endOffset-1) @@ -91,19 +97,20 @@ class MarkdownParser ( } val href = mapOf("href" to link) return when (node.type) { - MarkdownElementTypes.FULL_REFERENCE_LINK -> DocNodesFromIElementFactory.getInstance(node.type, params = href, children = node.children.find { it.type == MarkdownElementTypes.LINK_TEXT }!!.children.drop(1).dropLast(1).map { visitNode(it) }, dri = dri) + MarkdownElementTypes.FULL_REFERENCE_LINK -> DocNodesFromIElementFactory.getInstance(node.type, params = href, children = node.children.find { it.type == MarkdownElementTypes.LINK_TEXT }!!.children.drop(1).dropLast(1).evaluateChildren(), dri = dri) else -> DocNodesFromIElementFactory.getInstance(node.type, params = href, children = listOf(visitNode(linkNode)), dri = dri) } } - private fun imagesHandler(node: ASTNode): DocNode { - val linkNode = node.children.last().children.find { it.type == MarkdownElementTypes.LINK_LABEL }!!.children[1] + private fun imagesHandler(node: ASTNode): DocTag { + val linkNode = + node.children.last().children.find { it.type == MarkdownElementTypes.LINK_LABEL }!!.children[1] val link = text.substring(linkNode.startOffset, linkNode.endOffset) val src = mapOf("src" to link) return DocNodesFromIElementFactory.getInstance(node.type, params = src, children = listOf(visitNode(node.children.last().children.find { it.type == MarkdownElementTypes.LINK_TEXT }!!))) } - private fun codeSpansHandler(node: ASTNode): DocNode = + private fun codeSpansHandler(node: ASTNode): DocTag = DocNodesFromIElementFactory.getInstance( node.type, children = listOf( @@ -115,7 +122,7 @@ class MarkdownParser ( ) ) - private fun codeFencesHandler(node: ASTNode): DocNode = + private fun codeFencesHandler(node: ASTNode): DocTag = DocNodesFromIElementFactory.getInstance( node.type, children = node @@ -129,13 +136,15 @@ class MarkdownParser ( ?: emptyMap() ) - private fun codeBlocksHandler(node: ASTNode): DocNode = - DocNodesFromIElementFactory.getInstance(node.type, children = node.children.map { visitNode(it) }) + private fun codeBlocksHandler(node: ASTNode): DocTag = + DocNodesFromIElementFactory.getInstance(node.type, children = node.children.evaluateChildren()) - private fun defaultHandler(node: ASTNode): DocNode = - DocNodesFromIElementFactory.getInstance(MarkdownElementTypes.PARAGRAPH, children = node.children.map { visitNode(it) }) + private fun defaultHandler(node: ASTNode): DocTag = + DocNodesFromIElementFactory.getInstance( + MarkdownElementTypes.PARAGRAPH, + children = node.children.evaluateChildren()) - fun visitNode(node: ASTNode): DocNode = + fun visitNode(node: ASTNode): DocTag = when (node.type) { MarkdownElementTypes.ATX_1, MarkdownElementTypes.ATX_2, @@ -155,16 +164,17 @@ class MarkdownParser ( MarkdownElementTypes.CODE_FENCE -> codeFencesHandler(node) MarkdownElementTypes.CODE_SPAN -> codeSpansHandler(node) MarkdownElementTypes.IMAGE -> imagesHandler(node) - MarkdownTokenTypes.EOL -> DocNodesFromIElementFactory.getInstance(MarkdownTokenTypes.TEXT, body = "\n") - MarkdownTokenTypes.WHITE_SPACE -> DocNodesFromIElementFactory.getInstance(MarkdownTokenTypes.TEXT, body = " ") MarkdownTokenTypes.CODE_FENCE_CONTENT, MarkdownTokenTypes.CODE_LINE, MarkdownTokenTypes.TEXT -> DocNodesFromIElementFactory.getInstance(MarkdownTokenTypes.TEXT, body = text.substring(node.startOffset, node.endOffset)) else -> defaultHandler(node) } + + private fun List.evaluateChildren(): List = + this.filter { it is CompositeASTNode || it.type == MarkdownTokenTypes.TEXT }.map { visitNode(it) } } - private fun markdownToDocNode(text: String): DocNode { + private fun markdownToDocNode(text: String): DocTag { val flavourDescriptor = CommonMarkFlavourDescriptor() val markdownAstRoot: ASTNode = IntellijMarkdownParser(flavourDescriptor).buildMarkdownTreeFromString(text) @@ -181,20 +191,23 @@ class MarkdownParser ( else DocumentationNode( (listOf(kDocTag) + kDocTag.children).filterIsInstance().map { - when( it.knownTag ) { - null -> Description(parseStringToDocNode(it.getContent())) - KDocKnownTag.AUTHOR -> Author(parseStringToDocNode(it.getContent())) - KDocKnownTag.THROWS -> Throws(parseStringToDocNode(it.getContent()), it.getSubjectName()!!) - KDocKnownTag.EXCEPTION -> Throws(parseStringToDocNode(it.getContent()), it.getSubjectName()!!) - KDocKnownTag.PARAM -> Param(parseStringToDocNode(it.getContent()), it.getSubjectName()!!) - KDocKnownTag.RECEIVER -> Receiver(parseStringToDocNode(it.getContent())) - KDocKnownTag.RETURN -> Return(parseStringToDocNode(it.getContent())) - KDocKnownTag.SEE -> See(parseStringToDocNode(it.getContent()), it.getSubjectName()!!) - KDocKnownTag.SINCE -> Since(parseStringToDocNode(it.getContent())) - KDocKnownTag.CONSTRUCTOR -> Constructor(parseStringToDocNode(it.getContent())) - KDocKnownTag.PROPERTY -> Property(parseStringToDocNode(it.getContent()), it.getSubjectName()!!) - KDocKnownTag.SAMPLE -> Sample(parseStringToDocNode(it.getContent()), it.getSubjectName()!!) - KDocKnownTag.SUPPRESS -> Suppress(parseStringToDocNode(it.getContent())) + when (it.knownTag) { + null -> if (it.name == null) Description(parseStringToDocNode(it.getContent())) else CustomWrapperTag( + parseStringToDocNode(it.getContent()), + it.name!! + ) + KDocKnownTag.AUTHOR -> Author(parseStringToDocNode(it.getContent())) + KDocKnownTag.THROWS -> Throws(parseStringToDocNode(it.getContent()), it.getSubjectName()!!) + KDocKnownTag.EXCEPTION -> Throws(parseStringToDocNode(it.getContent()), it.getSubjectName()!!) + KDocKnownTag.PARAM -> Param(parseStringToDocNode(it.getContent()), it.getSubjectName()!!) + KDocKnownTag.RECEIVER -> Receiver(parseStringToDocNode(it.getContent())) + KDocKnownTag.RETURN -> Return(parseStringToDocNode(it.getContent())) + KDocKnownTag.SEE -> See(parseStringToDocNode(it.getContent()), it.getSubjectName()!!) + KDocKnownTag.SINCE -> Since(parseStringToDocNode(it.getContent())) + KDocKnownTag.CONSTRUCTOR -> Constructor(parseStringToDocNode(it.getContent())) + KDocKnownTag.PROPERTY -> Property(parseStringToDocNode(it.getContent()), it.getSubjectName()!!) + KDocKnownTag.SAMPLE -> Sample(parseStringToDocNode(it.getContent()), it.getSubjectName()!!) + KDocKnownTag.SUPPRESS -> Suppress(parseStringToDocNode(it.getContent())) } } ) diff --git a/core/src/main/kotlin/parsers/Parser.kt b/core/src/main/kotlin/parsers/Parser.kt index a2a90dcd..daa9f38d 100644 --- a/core/src/main/kotlin/parsers/Parser.kt +++ b/core/src/main/kotlin/parsers/Parser.kt @@ -1,19 +1,19 @@ -package parsers +package org.jetbrains.dokka.parsers -import model.doc.* -import model.doc.Deprecated +import org.jetbrains.dokka.model.doc.* +import org.jetbrains.dokka.model.doc.Deprecated abstract class Parser { - abstract fun parseStringToDocNode(extractedString: String): DocNode + abstract fun parseStringToDocNode(extractedString: String): DocTag abstract fun preparse(text: String): String fun parse(text: String): DocumentationNode { val list = jkdocToListOfPairs(preparse(text)) - val mappedList: List = list.map { + val mappedList: List = list.map { when(it.first) { "description" -> Description(parseStringToDocNode(it.second)) "author" -> Author(parseStringToDocNode(it.second)) @@ -29,7 +29,7 @@ abstract class Parser { "deprecated" -> Deprecated(parseStringToDocNode(it.second)) "sample" -> Sample(parseStringToDocNode(it.second.substringAfter(' ')), it.second.substringBefore(' ')) "suppress" -> Suppress(parseStringToDocNode(it.second)) - else -> CustomTag(parseStringToDocNode(it.second), it.first) + else -> CustomWrapperTag(parseStringToDocNode(it.second), it.first) } } return DocumentationNode(mappedList) diff --git a/core/src/main/kotlin/parsers/factories/DocNodesFromIElementFactory.kt b/core/src/main/kotlin/parsers/factories/DocNodesFromIElementFactory.kt index a93be0d3..1a302176 100644 --- a/core/src/main/kotlin/parsers/factories/DocNodesFromIElementFactory.kt +++ b/core/src/main/kotlin/parsers/factories/DocNodesFromIElementFactory.kt @@ -1,6 +1,6 @@ package org.jetbrains.dokka.parsers.factories -import model.doc.* +import org.jetbrains.dokka.model.doc.* import org.intellij.markdown.IElementType import org.intellij.markdown.MarkdownElementTypes import org.intellij.markdown.MarkdownTokenTypes @@ -8,7 +8,7 @@ import org.jetbrains.dokka.links.DRI import java.lang.NullPointerException object DocNodesFromIElementFactory { - fun getInstance(type: IElementType, children: List = emptyList(), params: Map = emptyMap(), body: String? = null, dri: DRI? = null) = + fun getInstance(type: IElementType, children: List = emptyList(), params: Map = emptyMap(), body: String? = null, dri: DRI? = null) = when(type) { MarkdownElementTypes.SHORT_REFERENCE_LINK, MarkdownElementTypes.FULL_REFERENCE_LINK -> if(dri == null) A(children, params) else DocumentationLink(children, params, dri) @@ -31,6 +31,6 @@ object DocNodesFromIElementFactory { 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.HORIZONTAL_RULE -> HorizontalRule() - else -> CustomNode(children, params) + 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 49102ed0..6090cdb4 100644 --- a/core/src/main/kotlin/parsers/factories/DocNodesFromStringFactory.kt +++ b/core/src/main/kotlin/parsers/factories/DocNodesFromStringFactory.kt @@ -1,11 +1,11 @@ package org.jetbrains.dokka.parsers.factories -import model.doc.* +import org.jetbrains.dokka.model.doc.* import org.jetbrains.dokka.links.DRI import java.lang.NullPointerException object DocNodesFromStringFactory { - fun getInstance(name: String, children: List = emptyList(), params: Map = emptyMap(), body: String? = null, dri: DRI? = null) = + fun getInstance(name: String, children: List = emptyList(), params: Map = emptyMap(), body: String? = null, dri: DRI? = null) = when(name) { "a" -> A(children, params) "big" -> Big(children, params) @@ -71,6 +71,6 @@ object DocNodesFromStringFactory { "var" -> Var(children, params) "documentationlink" -> DocumentationLink(children, params, dri ?: throw NullPointerException("DRI cannot be passed null while constructing documentation link!")) "hr" -> HorizontalRule() - else -> CustomNode(children, params) + else -> CustomDocTag(children, params) } } \ No newline at end of file -- cgit