aboutsummaryrefslogtreecommitdiff
path: root/telegram/formatter/formatter.go
blob: 9403198fdc81854b0e1e10c68526d24ea03596aa (plain) (blame)
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
package formatter

import (
	"sort"
	"unicode"

	log "github.com/sirupsen/logrus"
	"github.com/zelenin/go-tdlib/client"
)

type insertionType int
const (
	insertionOpening insertionType = iota
	insertionClosing
	insertionUnpaired
)

type MarkupModeType int
const (
	MarkupModeXEP0393 MarkupModeType = iota
	MarkupModeMarkdown
)

// insertion is a piece of text in given position
type insertion struct {
	Offset int32
	Runes  []rune
	Type   insertionType
}

// insertionStack contains the sequence of insertions
// from the start or from the end
type insertionStack []*insertion

var boldRunesMarkdown = []rune("**")
var boldRunesXEP0393 = []rune("*")
var italicRunes = []rune("_")
var strikeRunesMarkdown = []rune("~~")
var strikeRunesXEP0393 = []rune("~")
var codeRunes = []rune("`")
var preRuneStart = []rune("```\n")
var preRuneEnd = []rune("\n```")
var quoteRunes = []rune("> ")
var newlineRunes = []rune("\n")
var doubleNewlineRunes = []rune("\n\n")
var newlineCode = rune(0x0000000a)
var bmpCeil = rune(0x0000ffff)

// rebalance pumps all the values until the given offset to current stack (growing
// from start) from given stack (growing from end); should be called
// before any insertions to the current stack at the given offset
func (s insertionStack) rebalance(s2 insertionStack, offset int32) (insertionStack, insertionStack) {
	for len(s2) > 0 && s2[len(s2)-1].Offset <= offset {
		s = append(s, s2[len(s2)-1])
		s2 = s2[:len(s2)-1]
	}

	return s, s2
}

// NewIterator is a second order function that sequentially scans and returns
// stack elements; starts returning nil when elements are ended
func (s insertionStack) NewIterator() func() *insertion {
	i := -1

	return func() *insertion {
		i++
		if i < len(s) {
			return s[i]
		}
		return nil
	}
}

// SortEntities arranges the entities in traversal-ready order
func SortEntities(entities []*client.TextEntity) []*client.TextEntity {
	sortedEntities := make([]*client.TextEntity, len(entities))
	copy(sortedEntities, entities)

	sort.Slice(sortedEntities, func(i int, j int) bool {
		entity1 := sortedEntities[i]
		entity2 := sortedEntities[j]
		if entity1.Offset < entity2.Offset {
			return true
		} else if entity1.Offset == entity2.Offset {
			return entity1.Length > entity2.Length
		}
		return false
	})
	return sortedEntities
}

// MergeAdjacentEntities merges entities of a same kind
func MergeAdjacentEntities(entities []*client.TextEntity) []*client.TextEntity {
	mergedEntities := make([]*client.TextEntity, 0, len(entities))
	excludedIndices := make(map[int]bool)

	for i, entity := range entities {
		if excludedIndices[i] || entity.Type == nil {
			continue
		}

		typ := entity.Type.TextEntityTypeType()
		start := entity.Offset
		end := start + entity.Length
		ei := make(map[int]bool)

		// collect continuations
		for j, entity2 := range entities[i+1:] {
			if entity2.Type != nil && entity2.Type.TextEntityTypeType() == typ && entity2.Offset == end {
				end += entity2.Length
				ei[j+i+1] = true
			}
		}

		// check for intersections with other entities
		var isIntersecting bool
		if len(ei) > 0 {
			for _, entity2 := range entities {
				entity2End := entity2.Offset + entity2.Length
				if (entity2.Offset < start && entity2End > start && entity2End < end) ||
					(entity2.Offset > start && entity2.Offset < end && entity2End > end) {
					isIntersecting = true
					break
				}
			}
		}

		if !isIntersecting {
			entity.Length = end - start
			for j := range ei {
				excludedIndices[j] = true
			}
		}
		mergedEntities = append(mergedEntities, entity)
	}

	return mergedEntities
}

// ClaspDirectives to the following span as required by XEP-0393
func ClaspDirectives(doubledRunes []rune, entities []*client.TextEntity) []*client.TextEntity {
	alignedEntities := make([]*client.TextEntity, len(entities))
	copy(alignedEntities, entities)

	for i, entity := range alignedEntities {
		var dirty bool
		endOffset := entity.Offset + entity.Length

		if unicode.IsSpace(doubledRunes[entity.Offset]) {
			for j, r := range doubledRunes[entity.Offset+1 : endOffset] {
				if !unicode.IsSpace(r) {
					dirty = true
					entity.Offset += int32(j + 1)
					entity.Length -= int32(j + 1)
					break
				}
			}
		}
		if unicode.IsSpace(doubledRunes[endOffset-1]) {
			for j := endOffset - 2; j >= entity.Offset; j-- {
				if !unicode.IsSpace(doubledRunes[j]) {
					dirty = true
					entity.Length = j + 1 - entity.Offset
					break
				}
			}
		}

		if dirty {
			alignedEntities[i] = entity
		}
	}

	return alignedEntities
}

func markupBraces(entity *client.TextEntity, lbrace, rbrace []rune) []*insertion {
	return []*insertion{
		&insertion{
			Offset: entity.Offset,
			Runes:  lbrace,
			Type:   insertionOpening,
		},
		&insertion{
			Offset: entity.Offset + entity.Length,
			Runes:  rbrace,
			Type:   insertionClosing,
		},
	}
}

func quotePrependNewlines(entity *client.TextEntity, doubledRunes []rune, markupMode MarkupModeType) []*insertion {
	if len(doubledRunes) == 0 {
		return []*insertion{}
	}

	startRunes := []rune("\n> ")
	if entity.Offset == 0 || doubledRunes[entity.Offset-1] == newlineCode {
		startRunes = quoteRunes
	}
	insertions := []*insertion{
		&insertion{
			Offset: entity.Offset,
			Runes:  startRunes,
			Type:   insertionUnpaired,
		},
	}

	entityEnd := entity.Offset + entity.Length
	entityEndInt := int(entityEnd)

	var wasNewline bool
	// last newline is omitted, there's no need to put quote mark after the quote
	for i := entity.Offset; i < entityEnd-1; i++ {
		isNewline := doubledRunes[i] == newlineCode
		if (isNewline && markupMode == MarkupModeXEP0393) || (wasNewline && isNewline && markupMode == MarkupModeMarkdown) {
			insertions = append(insertions, &insertion{
				Offset: i+1,
				Runes:  quoteRunes,
				Type:   insertionUnpaired,
			})
		}

		if isNewline {
			wasNewline = true
		} else {
			wasNewline = false
		}
	}

	var rbrace []rune
	if len(doubledRunes) > entityEndInt {
		if doubledRunes[entityEnd] == newlineCode {
			if markupMode == MarkupModeMarkdown && len(doubledRunes) > entityEndInt+1 && doubledRunes[entityEndInt+1] != newlineCode {
				rbrace = newlineRunes
			}
		} else {
			if markupMode == MarkupModeMarkdown {
				rbrace = doubleNewlineRunes
			} else {
				rbrace = newlineRunes
			}
		}
	}
	insertions = append(insertions, &insertion{
		Offset: entityEnd,
		Runes:  rbrace,
		Type:   insertionClosing,
	})

	return insertions
}

// entityToMarkdown generates the wrapping Markdown tags
func entityToMarkdown(entity *client.TextEntity, doubledRunes []rune, markupMode MarkupModeType) []*insertion {
	if entity == nil || entity.Type == nil {
		return []*insertion{}
	}

	switch entity.Type.TextEntityTypeType() {
	case client.TypeTextEntityTypeBold:
		return markupBraces(entity, boldRunesMarkdown, boldRunesMarkdown)
	case client.TypeTextEntityTypeItalic:
		return markupBraces(entity, italicRunes, italicRunes)
	case client.TypeTextEntityTypeStrikethrough:
		return markupBraces(entity, strikeRunesMarkdown, strikeRunesMarkdown)
	case client.TypeTextEntityTypeCode:
		return markupBraces(entity, codeRunes, codeRunes)
	case client.TypeTextEntityTypePre:
		return markupBraces(entity, preRuneStart, preRuneEnd)
	case client.TypeTextEntityTypePreCode:
		preCode, _ := entity.Type.(*client.TextEntityTypePreCode)
		return markupBraces(entity, []rune("\n```"+preCode.Language+"\n"), codeRunes)
	case client.TypeTextEntityTypeBlockQuote:
		return quotePrependNewlines(entity, doubledRunes, MarkupModeMarkdown)
	case client.TypeTextEntityTypeTextUrl:
		textURL, _ := entity.Type.(*client.TextEntityTypeTextUrl)
		return markupBraces(entity, []rune("["), []rune("]("+textURL.Url+")"))
	}

	return []*insertion{}
}

// entityToXEP0393 generates the wrapping XEP-0393 tags
func entityToXEP0393(entity *client.TextEntity, doubledRunes []rune, markupMode MarkupModeType) []*insertion {
	if entity == nil || entity.Type == nil {
		return []*insertion{}
	}

	switch entity.Type.TextEntityTypeType() {
	case client.TypeTextEntityTypeBold:
		return markupBraces(entity, boldRunesXEP0393, boldRunesXEP0393)
	case client.TypeTextEntityTypeItalic:
		return markupBraces(entity, italicRunes, italicRunes)
	case client.TypeTextEntityTypeStrikethrough:
		return markupBraces(entity, strikeRunesXEP0393, strikeRunesXEP0393)
	case client.TypeTextEntityTypeCode:
		return markupBraces(entity, codeRunes, codeRunes)
	case client.TypeTextEntityTypePre:
		return markupBraces(entity, preRuneStart, preRuneEnd)
	case client.TypeTextEntityTypePreCode:
		preCode, _ := entity.Type.(*client.TextEntityTypePreCode)
		return markupBraces(entity, []rune("\n```"+preCode.Language+"\n"), codeRunes)
	case client.TypeTextEntityTypeBlockQuote:
		return quotePrependNewlines(entity, doubledRunes, MarkupModeXEP0393)
	case client.TypeTextEntityTypeTextUrl:
		textURL, _ := entity.Type.(*client.TextEntityTypeTextUrl)
		// non-standard, Pidgin-specific
		return markupBraces(entity, []rune{}, []rune(" <"+textURL.Url+">"))
	}

	return []*insertion{}
}

// transform the source text into a form with uniform runes and code points,
// by duplicating anything beyond the Basic Multilingual Plane
func textToDoubledRunes(text string) []rune {
	doubledRunes := make([]rune, 0, len(text)*2)
	for _, cp := range text {
		if cp > bmpCeil {
			doubledRunes = append(doubledRunes, cp, cp)
		} else {
			doubledRunes = append(doubledRunes, cp)
		}
	}

	return doubledRunes
}

// Format traverses an already sorted list of entities and wraps the text in a markup
func Format(
	sourceText string,
	entities []*client.TextEntity,
	markupMode MarkupModeType,
) string {
	if len(entities) == 0 {
		return sourceText
	}

	var entityToMarkup func(*client.TextEntity, []rune, MarkupModeType) []*insertion
	if markupMode == MarkupModeXEP0393 {
		entityToMarkup = entityToXEP0393
	} else {
		entityToMarkup = entityToMarkdown
	}

	doubledRunes := textToDoubledRunes(sourceText)

	mergedEntities := SortEntities(ClaspDirectives(doubledRunes, MergeAdjacentEntities(SortEntities(entities))))

	startStack := make(insertionStack, 0, len(sourceText))
	endStack := make(insertionStack, 0, len(sourceText))

	// convert entities to a stack of brackets
	var maxEndOffset int32
	for _, entity := range mergedEntities {
		log.Debugf("%#v", entity)
		if entity.Length <= 0 {
			continue
		}

		endOffset := entity.Offset + entity.Length
		if endOffset > maxEndOffset {
			maxEndOffset = endOffset
		}

		startStack, endStack = startStack.rebalance(endStack, entity.Offset)

		insertions := entityToMarkup(entity, doubledRunes, markupMode)
		if len(insertions) > 1 {
			startStack = append(startStack, insertions[0:len(insertions)-1]...)
		}
		if len(insertions) > 0 {
			endStack = append(endStack, insertions[len(insertions)-1])
		}
	}
	// flush the closing brackets that still remain in endStack
	startStack, endStack = startStack.rebalance(endStack, maxEndOffset)
	// sort unpaired insertions
	sort.SliceStable(startStack, func(i int, j int) bool {
		ins1 := startStack[i]
		ins2 := startStack[j]
		if ins1.Type == insertionUnpaired && ins2.Type == insertionUnpaired {
			return ins1.Offset < ins2.Offset
		}
		if ins1.Type == insertionUnpaired {
			if ins1.Offset == ins2.Offset {
				if ins2.Type == insertionOpening { // > **
					return true
				} else if ins2.Type == insertionClosing { // **> 
					return false
				}
			} else {
				return ins1.Offset < ins2.Offset
			}
		}
		if ins2.Type == insertionUnpaired {
			if ins1.Offset == ins2.Offset {
				if ins1.Type == insertionOpening { // > **
					return false
				} else if ins1.Type == insertionClosing { // **> 
					return true
				}
			} else {
				return ins1.Offset < ins2.Offset
			}
		}
		return false
	})

	// merge brackets into text
	markupRunes := make([]rune, 0, len(sourceText))

	nextInsertion := startStack.NewIterator()
	insertion := nextInsertion()
	var skipNext bool

	for i, cp := range doubledRunes {
		if skipNext {
			skipNext = false
			continue
		}

		for insertion != nil && int(insertion.Offset) <= i {
			markupRunes = append(markupRunes, insertion.Runes...)
			insertion = nextInsertion()
		}

		markupRunes = append(markupRunes, cp)
		// skip two UTF-16 code units (not points actually!) if needed
		if cp > bmpCeil {
			skipNext = true
		}
	}
	for insertion != nil {
		markupRunes = append(markupRunes, insertion.Runes...)
		insertion = nextInsertion()
	}

	return string(markupRunes)
}