-
Notifications
You must be signed in to change notification settings - Fork 79
/
GeneratingProviders.kt
326 lines (268 loc) · 12.6 KB
/
GeneratingProviders.kt
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
package org.intellij.markdown.html
import org.intellij.markdown.IElementType
import org.intellij.markdown.MarkdownElementTypes
import org.intellij.markdown.MarkdownTokenTypes
import org.intellij.markdown.ast.*
import org.intellij.markdown.ast.impl.ListCompositeNode
import org.intellij.markdown.ast.impl.ListItemCompositeNode
import org.intellij.markdown.html.entities.EntityConverter
import org.intellij.markdown.lexer.Compat.assert
import org.intellij.markdown.parser.LinkMap
abstract class OpenCloseGeneratingProvider : GeneratingProvider {
abstract fun openTag(visitor: HtmlGenerator.HtmlGeneratingVisitor, text: String, node: ASTNode)
abstract fun closeTag(visitor: HtmlGenerator.HtmlGeneratingVisitor, text: String, node: ASTNode)
override fun processNode(visitor: HtmlGenerator.HtmlGeneratingVisitor, text: String, node: ASTNode) {
openTag(visitor, text, node)
node.acceptChildren(visitor)
closeTag(visitor, text, node)
}
}
abstract class InlineHolderGeneratingProvider : OpenCloseGeneratingProvider() {
open fun childrenToRender(node: ASTNode): List<ASTNode> {
return node.children
}
override fun processNode(visitor: HtmlGenerator.HtmlGeneratingVisitor, text: String, node: ASTNode) {
openTag(visitor, text, node)
for (child in childrenToRender(node)) {
if (child is LeafASTNode) {
visitor.visitLeaf(child)
} else {
child.accept(visitor)
}
}
closeTag(visitor, text, node)
}
}
open class SimpleTagProvider(val tagName: String) : OpenCloseGeneratingProvider() {
override fun openTag(visitor: HtmlGenerator.HtmlGeneratingVisitor, text: String, node: ASTNode) {
visitor.consumeTagOpen(node, tagName)
}
override fun closeTag(visitor: HtmlGenerator.HtmlGeneratingVisitor, text: String, node: ASTNode) {
visitor.consumeTagClose(tagName)
}
}
open class SimpleInlineTagProvider(val tagName: String, val renderFrom: Int = 0, val renderTo: Int = 0)
: InlineHolderGeneratingProvider() {
override fun openTag(visitor: HtmlGenerator.HtmlGeneratingVisitor, text: String, node: ASTNode) {
visitor.consumeTagOpen(node, tagName)
}
override fun closeTag(visitor: HtmlGenerator.HtmlGeneratingVisitor, text: String, node: ASTNode) {
visitor.consumeTagClose(tagName)
}
override fun childrenToRender(node: ASTNode): List<ASTNode> {
return node.children.subList(renderFrom, node.children.size + renderTo)
}
}
/**
* Trims an equal number of delimiters from the start and end off the children.
* Meant to be used to trim surrounding tildes ('~') for strikethroughs.
*/
open class EqualDelimiterTrimmingInlineTagProvider(val tagName: String, val delimiterType: IElementType)
: InlineHolderGeneratingProvider() {
override fun openTag(visitor: HtmlGenerator.HtmlGeneratingVisitor, text: String, node: ASTNode) {
visitor.consumeTagOpen(node, tagName)
}
override fun closeTag(visitor: HtmlGenerator.HtmlGeneratingVisitor, text: String, node: ASTNode) {
visitor.consumeTagClose(tagName)
}
override fun childrenToRender(node: ASTNode): List<ASTNode> {
if (node.children.isEmpty()) return node.children
var left = 0
var right = node.children.size - 1
while (
node.children[left].type == delimiterType &&
node.children[right].type == delimiterType
) {
left += 1
right -= 1
if (left >= right) break
}
return node.children.subList(left, right + 1)
}
}
class CodeSpanGeneratingProvider: GeneratingProvider {
override fun processNode(visitor: HtmlGenerator.HtmlGeneratingVisitor, text: String, node: ASTNode) {
val nodes = node.children.subList(1, node.children.size - 1)
val output = nodes.joinToString(separator = "") { HtmlGenerator.leafText(text, it, false) }.trim()
visitor.consumeTagOpen(node, "code")
visitor.consumeHtml(output)
visitor.consumeTagClose("code")
}
}
open class TransparentInlineHolderProvider(renderFrom: Int = 0, renderTo: Int = 0)
: SimpleInlineTagProvider("", renderFrom, renderTo) {
override fun openTag(visitor: HtmlGenerator.HtmlGeneratingVisitor, text: String, node: ASTNode) {
}
override fun closeTag(visitor: HtmlGenerator.HtmlGeneratingVisitor, text: String, node: ASTNode) {
}
}
open class TrimmingInlineHolderProvider : InlineHolderGeneratingProvider() {
override fun openTag(visitor: HtmlGenerator.HtmlGeneratingVisitor, text: String, node: ASTNode) {
}
override fun closeTag(visitor: HtmlGenerator.HtmlGeneratingVisitor, text: String, node: ASTNode) {
}
override fun childrenToRender(node: ASTNode): List<ASTNode> {
val children = node.children
var from = 0
while (from < children.size && children[from].type == MarkdownTokenTypes.WHITE_SPACE) {
from++
}
var to = children.size
while (to > from && children[to - 1].type == MarkdownTokenTypes.WHITE_SPACE) {
to--
}
return children.subList(from, to)
}
}
internal class ListItemGeneratingProvider : SimpleTagProvider("li") {
override fun processNode(visitor: HtmlGenerator.HtmlGeneratingVisitor, text: String, node: ASTNode) {
assert(node is ListItemCompositeNode)
openTag(visitor, text, node)
val listNode = node.parent
assert(listNode is ListCompositeNode)
val isLoose = (listNode as ListCompositeNode).loose
for (child in node.children) {
if (child.type == MarkdownElementTypes.PARAGRAPH && !isLoose) {
SilentParagraphGeneratingProvider.processNode(visitor, text, child)
} else {
child.accept(visitor)
}
}
closeTag(visitor, text, node)
}
object SilentParagraphGeneratingProvider : InlineHolderGeneratingProvider() {
override fun openTag(visitor: HtmlGenerator.HtmlGeneratingVisitor, text: String, node: ASTNode) {
}
override fun closeTag(visitor: HtmlGenerator.HtmlGeneratingVisitor, text: String, node: ASTNode) {
}
}
}
internal class HtmlBlockGeneratingProvider : GeneratingProvider {
override fun processNode(visitor: HtmlGenerator.HtmlGeneratingVisitor, text: String, node: ASTNode) {
for (child in node.children) {
if (child.type in listOf(MarkdownTokenTypes.EOL, MarkdownTokenTypes.HTML_BLOCK_CONTENT)) {
visitor.consumeHtml(child.getTextInNode(text))
}
}
visitor.consumeHtml("\n")
}
}
internal class CodeFenceGeneratingProvider : GeneratingProvider {
override fun processNode(visitor: HtmlGenerator.HtmlGeneratingVisitor, text: String, node: ASTNode) {
val indentBefore = node.getTextInNode(text).commonPrefixWith(" ".repeat(10)).length
visitor.consumeHtml("<pre>")
var state = 0
var childrenToConsider = node.children
if (childrenToConsider.last().type == MarkdownTokenTypes.CODE_FENCE_END) {
childrenToConsider = childrenToConsider.subList(0, childrenToConsider.size - 1)
}
var lastChildWasContent = false
val attributes = ArrayList<String>()
for (child in childrenToConsider) {
if (state == 1 && child.type in listOf(MarkdownTokenTypes.CODE_FENCE_CONTENT,
MarkdownTokenTypes.EOL)) {
visitor.consumeHtml(HtmlGenerator.trimIndents(HtmlGenerator.leafText(text, child, false), indentBefore))
lastChildWasContent = child.type == MarkdownTokenTypes.CODE_FENCE_CONTENT
}
if (state == 0 && child.type == MarkdownTokenTypes.FENCE_LANG) {
attributes.add("class=\"language-${
HtmlGenerator.leafText(text, child).toString().trim().split(' ')[0]
}\"")
}
if (state == 0 && child.type == MarkdownTokenTypes.EOL) {
visitor.consumeTagOpen(node, "code", *attributes.toTypedArray())
state = 1
}
}
if (state == 0) {
visitor.consumeTagOpen(node, "code", *attributes.toTypedArray())
}
if (lastChildWasContent) {
visitor.consumeHtml("\n")
}
visitor.consumeHtml("</code></pre>")
}
}
abstract class LinkGeneratingProvider(val baseURI: URI?, val resolveAnchors: Boolean = false) : GeneratingProvider {
final override fun processNode(visitor: HtmlGenerator.HtmlGeneratingVisitor, text: String, node: ASTNode) {
val info = getRenderInfo(text, node)
?: return fallbackProvider.processNode(visitor, text, node)
renderLink(visitor, text, node, info)
}
protected fun makeAbsoluteUrl(destination : CharSequence) : CharSequence {
if (!resolveAnchors && destination.startsWith('#')) {
return destination
}
return baseURI?.resolveToStringSafe(destination.toString()) ?: destination
}
open fun renderLink(visitor: HtmlGenerator.HtmlGeneratingVisitor, text: String, node: ASTNode, info: RenderInfo) {
visitor.consumeTagOpen(node, "a", "href=\"${makeAbsoluteUrl(info.destination)}\"", info.title?.let { "title=\"$it\"" })
labelProvider.processNode(visitor, text, info.label)
visitor.consumeTagClose("a")
}
abstract fun getRenderInfo(text: String, node: ASTNode): RenderInfo?
data class RenderInfo(val label: ASTNode, val destination: CharSequence, val title: CharSequence?)
companion object {
val fallbackProvider = TransparentInlineHolderProvider()
val labelProvider = TransparentInlineHolderProvider(1, -1)
}
}
open class InlineLinkGeneratingProvider(baseURI: URI?, resolveAnchors: Boolean = false)
: LinkGeneratingProvider(baseURI, resolveAnchors) {
override fun getRenderInfo(text: String, node: ASTNode): RenderInfo? {
val label = node.findChildOfType(MarkdownElementTypes.LINK_TEXT)
?: return null
return RenderInfo(
label,
node.findChildOfType(MarkdownElementTypes.LINK_DESTINATION)?.getTextInNode(text)?.let {
LinkMap.normalizeDestination(it, true)
} ?: "",
node.findChildOfType(MarkdownElementTypes.LINK_TITLE)?.getTextInNode(text)?.let {
LinkMap.normalizeTitle(it)
}
)
}
}
open class ReferenceLinksGeneratingProvider(private val linkMap: LinkMap, baseURI: URI?, resolveAnchors: Boolean = false)
: LinkGeneratingProvider(baseURI, resolveAnchors) {
override fun getRenderInfo(text: String, node: ASTNode): RenderInfo? {
val label = node.children.firstOrNull { it.type == MarkdownElementTypes.LINK_LABEL }
?: return null
val linkInfo = linkMap.getLinkInfo(label.getTextInNode(text))
?: return null
val linkTextNode = node.children.firstOrNull { it.type == MarkdownElementTypes.LINK_TEXT }
return RenderInfo(
linkTextNode ?: label,
EntityConverter.replaceEntities(linkInfo.destination, processEntities = true, processEscapes = true),
linkInfo.title?.let { EntityConverter.replaceEntities(it, processEntities = true, processEscapes = true) }
)
}
}
open class ImageGeneratingProvider(linkMap: LinkMap, baseURI: URI?) : LinkGeneratingProvider(baseURI) {
protected val referenceLinkProvider = ReferenceLinksGeneratingProvider(linkMap, baseURI)
protected val inlineLinkProvider = InlineLinkGeneratingProvider(baseURI)
override fun getRenderInfo(text: String, node: ASTNode): RenderInfo? {
node.findChildOfType(MarkdownElementTypes.INLINE_LINK)?.let { linkNode ->
return inlineLinkProvider.getRenderInfo(text, linkNode)
}
(node.findChildOfType(MarkdownElementTypes.FULL_REFERENCE_LINK)
?: node.findChildOfType(MarkdownElementTypes.SHORT_REFERENCE_LINK))
?.let { linkNode ->
return referenceLinkProvider.getRenderInfo(text, linkNode)
}
return null
}
override fun renderLink(visitor: HtmlGenerator.HtmlGeneratingVisitor, text: String, node: ASTNode, info: RenderInfo) {
visitor.consumeTagOpen(node, "img",
"src=\"${makeAbsoluteUrl(info.destination)}\"",
"alt=\"${getPlainTextFrom(info.label, text)}\"",
info.title?.let { "title=\"$it\"" },
autoClose = true)
}
private fun getPlainTextFrom(node: ASTNode, text: String): CharSequence {
return REGEX.replace(node.getTextInNode(text), "")
}
companion object {
val REGEX = Regex("[^a-zA-Z0-9 ]")
}
}