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
|
package formatter
import (
"sort"
log "github.com/sirupsen/logrus"
"github.com/zelenin/go-tdlib/client"
)
// Insertion is a piece of text in given position
type Insertion struct {
Offset int32
Runes []rune
}
// InsertionStack contains the sequence of insertions
// from the start or from the end
type InsertionStack []*Insertion
var boldRunes = []rune("**")
var italicRunes = []rune("_")
var codeRunes = []rune("\n```\n")
var urlRuneL = []rune("[")
// rebalance pumps all the values at 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 := entities[i]
entity2 := entities[j]
if entity1.Offset < entity2.Offset {
return true
} else if entity1.Offset == entity2.Offset {
return entity1.Length > entity2.Length
}
return false
})
return sortedEntities
}
func markupBraces(entity *client.TextEntity, lbrace, rbrace []rune) (*Insertion, *Insertion) {
return &Insertion{
Offset: entity.Offset,
Runes: lbrace,
}, &Insertion{
Offset: entity.Offset + entity.Length,
Runes: rbrace,
}
}
// EntityToMarkdown generates the wrapping Markdown tags
func EntityToMarkdown(entity *client.TextEntity) (*Insertion, *Insertion) {
switch entity.Type.TextEntityTypeType() {
case client.TypeTextEntityTypeBold:
return markupBraces(entity, boldRunes, boldRunes)
case client.TypeTextEntityTypeItalic:
return markupBraces(entity, italicRunes, italicRunes)
case client.TypeTextEntityTypeCode, client.TypeTextEntityTypePre:
return markupBraces(entity, codeRunes, codeRunes)
case client.TypeTextEntityTypePreCode:
preCode, _ := entity.Type.(*client.TextEntityTypePreCode)
return markupBraces(entity, []rune("\n```"+preCode.Language+"\n"), codeRunes)
case client.TypeTextEntityTypeTextUrl:
textURL, _ := entity.Type.(*client.TextEntityTypeTextUrl)
return markupBraces(entity, urlRuneL, []rune("]("+textURL.Url+")"))
}
return nil, nil
}
// Format traverses an already sorted list of entities and wraps the text in Markdown
func Format(
sourceText string,
entities []*client.TextEntity,
entityToMarkup func(*client.TextEntity) (*Insertion, *Insertion),
) string {
if len(entities) == 0 {
return sourceText
}
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 entities {
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)
startInsertion, endInsertion := entityToMarkup(entity)
if startInsertion != nil {
startStack = append(startStack, startInsertion)
}
if endInsertion != nil {
endStack = append(endStack, endInsertion)
}
}
// flush the closing brackets that still remain in endStack
startStack, endStack = startStack.rebalance(endStack, maxEndOffset)
// merge brackets into text
markupRunes := make([]rune, 0, len(sourceText))
nextInsertion := startStack.NewIterator()
insertion := nextInsertion()
var runeI int32
for _, cp := range sourceText {
for insertion != nil && insertion.Offset <= runeI {
markupRunes = append(markupRunes, insertion.Runes...)
insertion = nextInsertion()
}
markupRunes = append(markupRunes, cp)
// skip two UTF-16 code units (not points actually!) if needed
if cp > 0x0000ffff {
runeI += 2
} else {
runeI++
}
}
for insertion != nil {
markupRunes = append(markupRunes, insertion.Runes...)
insertion = nextInsertion()
}
return string(markupRunes)
}
|