aboutsummaryrefslogtreecommitdiff
path: root/plugins/base/src/main/kotlin/parsers/Parser.kt
blob: 228cc88bb6354658f65725372efa1053f4741e32 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package org.jetbrains.dokka.base.parsers

import org.jetbrains.dokka.model.doc.*

abstract class Parser {

    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<TagWrapper> = list.map {
            when (it.first) {
                "description" -> Description(parseStringToDocNode(it.second))
                "author" -> Author(parseStringToDocNode(it.second))
                "version" -> Version(parseStringToDocNode(it.second))
                "since" -> Since(parseStringToDocNode(it.second))
                "see" -> See(
                    parseStringToDocNode(it.second.substringAfter(' ')),
                    it.second.substringBefore(' '),
                    null
                )
                "param" -> Param(
                    parseStringToDocNode(it.second.substringAfter(' ')),
                    it.second.substringBefore(' ')
                )
                "property" -> Property(
                    parseStringToDocNode(it.second.substringAfter(' ')),
                    it.second.substringBefore(' ')
                )
                "return" -> Return(parseStringToDocNode(it.second))
                "constructor" -> Constructor(parseStringToDocNode(it.second))
                "receiver" -> Receiver(parseStringToDocNode(it.second))
                "throws", "exception" -> Throws(
                    parseStringToDocNode(it.second.substringAfter(' ')),
                    it.second.substringBefore(' ')
                )
                "deprecated" -> Deprecated(parseStringToDocNode(it.second))
                "sample" -> Sample(
                    parseStringToDocNode(it.second.substringAfter(' ')),
                    it.second.substringBefore(' ')
                )
                "suppress" -> Suppress(parseStringToDocNode(it.second))
                else -> CustomTagWrapper(parseStringToDocNode(it.second), it.first)
            }
        }
        return DocumentationNode(mappedList)
    }

    private fun jkdocToListOfPairs(javadoc: String): List<Pair<String, String>> =
        "description $javadoc"
            .split("\n@")
            .map { content ->
                val contentWithEscapedAts = content.replace("\\@", "@")
                val (tag, body) = contentWithEscapedAts.split(" ", limit = 2)
                tag to body
            }
}