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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
|
package org.jetbrains.dokka.pages
import org.intellij.markdown.MarkdownElementTypes
import org.intellij.markdown.MarkdownTokenTypes
import org.jetbrains.dokka.DokkaLogger
import org.jetbrains.dokka.DokkaResolutionFacade
import org.jetbrains.dokka.MarkdownNode
import org.jetbrains.dokka.Model.DocumentationNode
import org.jetbrains.dokka.links.DRI
import org.jetbrains.kotlin.idea.kdoc.resolveKDocLink
class MarkdownToContentConverter(
private val resolutionFacade: DokkaResolutionFacade,
private val logger: DokkaLogger
) {
fun buildContent(
node: MarkdownNode,
dci: DCI,
documentationNode: DocumentationNode<*>
): List<ContentNode> {
// println(tree.toTestString())
fun buildChildren(node: MarkdownNode) = node.children.flatMap {
buildContent(it, dci, documentationNode)
}.coalesceText()
return when (node.type) {
MarkdownElementTypes.ATX_1 -> listOf(ContentHeader(buildChildren(node), 1, dci))
MarkdownElementTypes.ATX_2 -> listOf(ContentHeader(buildChildren(node), 2, dci))
MarkdownElementTypes.ATX_3 -> listOf(ContentHeader(buildChildren(node), 3, dci))
MarkdownElementTypes.ATX_4 -> listOf(ContentHeader(buildChildren(node), 4, dci))
MarkdownElementTypes.ATX_5 -> listOf(ContentHeader(buildChildren(node), 5, dci))
MarkdownElementTypes.ATX_6 -> listOf(ContentHeader(buildChildren(node), 6, dci))
MarkdownElementTypes.UNORDERED_LIST -> listOf(ContentList(buildChildren(node), false, dci))
MarkdownElementTypes.ORDERED_LIST -> listOf(ContentList(buildChildren(node), true, dci))
MarkdownElementTypes.LIST_ITEM -> TODO()
MarkdownElementTypes.EMPH -> listOf(
ContentStyle(
buildChildren(node),
Style.Emphasis,
dci
)
)// TODO
MarkdownElementTypes.STRONG -> listOf(
ContentStyle(
buildChildren(node),
Style.Strong,
dci
)
) // TODO
MarkdownElementTypes.CODE_SPAN -> TODO()
// val startDelimiter = node.child(MarkdownTokenTypes.BACKTICK)?.text
// if (startDelimiter != null) {
// val text = node.text.substring(startDelimiter.length).removeSuffix(startDelimiter)
// val codeSpan = ContentCode().apply { append(ContentText(text)) }
// parent.append(codeSpan)
// }
MarkdownElementTypes.CODE_BLOCK,
MarkdownElementTypes.CODE_FENCE -> {
val language = node.child(MarkdownTokenTypes.FENCE_LANG)?.text?.trim() ?: ""
listOf(ContentCode(buildChildren(node).toString(), language, dci)) // TODO
}
MarkdownElementTypes.PARAGRAPH -> listOf(
ContentStyle(
buildChildren(node),
Style.Paragraph,
dci
)
) // TODO
MarkdownElementTypes.INLINE_LINK -> {
// val linkTextNode = node.child(MarkdownElementTypes.LINK_TEXT)
// val destination = node.child(MarkdownElementTypes.LINK_DESTINATION)
// if (linkTextNode != null) {
// if (destination != null) {
// val link = ContentExternalLink(destination.text)
// renderLinkTextTo(linkTextNode, link, linkResolver)
// parent.append(link)
// } else {
// val link = ContentExternalLink(linkTextNode.getLabelText())
// renderLinkTextTo(linkTextNode, link, linkResolver)
// parent.append(link)
// }
// }
//TODO: Linking!!!
// ContentLink()
TODO()
}
MarkdownElementTypes.SHORT_REFERENCE_LINK,
MarkdownElementTypes.FULL_REFERENCE_LINK -> {
if (documentationNode.descriptors.isNotEmpty()) {
val destinationNode = node.children.find { it.type == MarkdownElementTypes.LINK_DESTINATION }
?: node.children.first { it.type == MarkdownElementTypes.LINK_LABEL }
val destination = destinationNode.children.find { it.type == MarkdownTokenTypes.TEXT }?.text
?: destinationNode.text
documentationNode.descriptors.flatMap {
resolveKDocLink(
resolutionFacade.resolveSession.bindingContext,
resolutionFacade,
it,
null,
destination.split('.')
)
}
.firstOrNull()
?.let { ContentLink(destination, DRI.from(it), dci) }
.let(::listOfNotNull)
} else {
logger.error("Apparently descriptor for $documentationNode was needed in model")
emptyList()
}
}
MarkdownTokenTypes.WHITE_SPACE -> {
// Don't append first space if start of header (it is added during formatting later)
// v
// #### Some Heading
// if (nodeStack.peek() !is ContentHeading || node.parent?.children?.first() != node) {
// parent.append(ContentText(node.text))
// }
listOf(ContentText(" ", dci))
}
MarkdownTokenTypes.EOL -> {
// if ((keepEol(nodeStack.peek()) && node.parent?.children?.last() != node) ||
// // Keep extra blank lines when processing lists (affects Markdown formatting)
// (processingList(nodeStack.peek()) && node.previous?.type == MarkdownTokenTypes.EOL)) {
// parent.append(ContentText(node.text))
// }
emptyList()
}
MarkdownTokenTypes.CODE_LINE -> {
listOf(ContentText(node.text, dci)) // TODO check
// if (parent is ContentBlockCode) {
// parent.append(content)
// } else {
// parent.append(ContentBlockCode().apply { append(content) })
// }
}
MarkdownTokenTypes.TEXT ->
// fun createEntityOrText(text: String): ContentNode {
// if (text == "&" || text == """ || text == "<" || text == ">") {
// return ContentEntity(text)
// }
// if (text == "&") {
// return ContentEntity("&")
// }
// val decodedText = EntityConverter.replaceEntities(text, true, true)
// if (decodedText != text) {
// return ContentEntity(text)
// }
// return ContentText(text)
// }
//
// parent.append(createEntityOrText(node.text))
listOf(ContentText(node.text, dci)) // TODO
MarkdownTokenTypes.EMPH ->
// val parentNodeType = node.parent?.type
// if (parentNodeType != MarkdownElementTypes.EMPH && parentNodeType != MarkdownElementTypes.STRONG) {
// parent.append(ContentText(node.text))
// }
listOf(ContentStyle(buildChildren(node), Style.Emphasis, dci)) // TODO
MarkdownTokenTypes.COLON,
MarkdownTokenTypes.SINGLE_QUOTE,
MarkdownTokenTypes.DOUBLE_QUOTE,
MarkdownTokenTypes.LT,
MarkdownTokenTypes.GT,
MarkdownTokenTypes.LPAREN,
MarkdownTokenTypes.RPAREN,
MarkdownTokenTypes.LBRACKET,
MarkdownTokenTypes.RBRACKET,
MarkdownTokenTypes.EXCLAMATION_MARK,
MarkdownTokenTypes.BACKTICK,
MarkdownTokenTypes.CODE_FENCE_CONTENT -> {
listOf(ContentText(node.text, dci))
}
MarkdownElementTypes.LINK_DEFINITION -> TODO()
MarkdownTokenTypes.EMAIL_AUTOLINK ->
listOf(ContentResolvedLink(node.text, "mailto:${node.text}", dci))
else -> buildChildren(node)
}
}
private fun Collection<ContentNode>.coalesceText() =
this
.sliceWhen { prev, next -> prev::class != next::class }
.flatMap { nodes ->
when (nodes.first()) {
is ContentText -> listOf(
ContentText(
nodes.joinToString("") { (it as ContentText).text },
nodes.first().dci
)
)
else -> nodes
}
}
}
fun <T> Collection<T>.sliceWhen(predicate: (before: T, after: T) -> Boolean): Collection<Collection<T>> {
val newCollection = mutableListOf<Collection<T>>()
var currentSlice = mutableListOf<T>()
for ((prev, next) in this.windowed(2, 1, false)) {
currentSlice.add(prev)
if (predicate(prev, next)) {
newCollection.add(currentSlice)
currentSlice = mutableListOf<T>()
}
}
if (this.isNotEmpty()) {
currentSlice.add(this.last())
newCollection.add(currentSlice)
}
return newCollection
}
|