-
Notifications
You must be signed in to change notification settings - Fork 2
/
markdown_create_requests.go
169 lines (154 loc) · 4.29 KB
/
markdown_create_requests.go
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
package slidesutil
import (
"regexp"
"strings"
slides "google.golang.org/api/slides/v1"
)
type CommonMarkData struct {
text string
lines []TextLine
}
func NewCommonMarkData(cm string) CommonMarkData {
cmd := CommonMarkData{
text: cm,
lines: Split(cm)}
cmd.Inflate()
return cmd
}
func (cmd *CommonMarkData) Inflate() {
prefix := int64(0)
for i, line := range cmd.lines {
line = InflateCommonMarkToGoogleSlides(line)
// Set Line Start and End
line.GoogleIndexStart = prefix
line.GoogleIndexEnd = prefix + int64(len(line.GoogleSlideText))
prefix += int64(len(line.GoogleSlideText)) + 1
line.Inflated = true
cmd.lines[i] = line
}
}
func (cmd *CommonMarkData) GoogleSlideTextString() string {
googTexts := []string{}
for _, line := range cmd.lines {
googTexts = append(googTexts, line.GoogleSlideText)
}
return strings.Join(googTexts, "\n")
}
func (cmd *CommonMarkData) Lines() []TextLine {
return cmd.lines
}
func (cmd *CommonMarkData) LineCount() int {
return len(cmd.lines)
}
func Split(cm string) []TextLine {
lines := []TextLine{}
raw := strings.Split(cm, "\n")
// fmt.Println(cm)
// fmtutil.PrintJSON(raw)
for _, line := range raw {
lines = append(lines, TextLine{CommonMarkText: line})
}
return lines
}
type TextLine struct {
CommonMarkText string
GoogleSlideText string
IsBullet bool
IsBold bool
Links []LinkInfo
Inflated bool
GoogleIndexStart int64
GoogleIndexEnd int64
}
var (
rxLink *regexp.Regexp = regexp.MustCompile(`\[(.*?)\]\((.*?)\)`)
rxBulletFirst *regexp.Regexp = regexp.MustCompile(`^[*-]\s+`)
rxBullet *regexp.Regexp = regexp.MustCompile(`^(\s\s\s\s)?[*-]\s+`)
rxBold *regexp.Regexp = regexp.MustCompile(`^(\s*)\*\*(.*)\*\*\s*$`)
)
func InflateCommonMarkToGoogleSlides(cml TextLine) TextLine {
googLine := cml.CommonMarkText
links := rxLink.FindAllStringSubmatch(googLine, -1)
if len(links) > 0 {
for _, mx := range links {
fullMatch := mx[0]
textDisp := mx[1]
fullMatchRx := regexp.MustCompile(regexp.QuoteMeta(fullMatch))
googLine = fullMatchRx.ReplaceAllString(googLine, textDisp)
}
}
if rxBulletFirst.MatchString(googLine) {
cml.IsBullet = true
googLine = rxBulletFirst.ReplaceAllString(googLine, "")
} else if rxBullet.MatchString(googLine) {
cml.IsBullet = true
googLine = rxBullet.ReplaceAllString(googLine, "\t")
}
if rxBold.MatchString(googLine) {
cml.IsBold = true
googLine = rxBold.ReplaceAllString(googLine, "$1$2")
}
if len(links) > 0 {
linksInfos := []LinkInfo{}
for _, link := range links {
if len(link) < 3 {
continue
}
linkText := link[1]
linkText = rxBold.ReplaceAllString(linkText, "$1$2")
linkURL := link[2]
idxStart := strings.Index(googLine, linkText)
if idxStart >= 0 {
startIndex := cml.GoogleIndexStart + int64(idxStart)
endIndex := cml.GoogleIndexStart + int64(idxStart+len(linkText))
linkRange := &slides.Range{
Type: RangeTypeFixedRange,
StartIndex: &startIndex,
EndIndex: &endIndex}
linksInfos = append(linksInfos, LinkInfo{
URL: linkURL,
Range: linkRange})
}
}
cml.Links = linksInfos
}
cml.GoogleSlideText = googLine
return cml
}
func LineStartEndIndexes(cml TextLine, priorLength int64) (int64, int64) {
start := int64(0) + priorLength
finish := int64(len(cml.GoogleSlideText)) + priorLength
return start, finish
}
func CommonMarkDataToRequests(textboxID string, cmd CommonMarkData, underlineLinks bool) []*slides.Request {
reqs := []*slides.Request{
{
InsertText: &slides.InsertTextRequest{
ObjectId: textboxID,
Text: cmd.GoogleSlideTextString(),
},
},
}
lines := cmd.Lines()
for _, line := range lines {
if line.IsBold {
reqs = append(reqs, UpdateTextStyleRequestBold(textboxID, line.GoogleIndexStart, line.GoogleIndexEnd))
}
if line.IsBullet {
reqs = append(reqs, UpdateTextStyleRequestBullet(textboxID, line.GoogleIndexStart, line.GoogleIndexEnd))
}
for _, linkInfo := range line.Links {
reqs = append(reqs, UpdateTextStyleRequestLinkURL(textboxID, linkInfo.URL, linkInfo.Range, underlineLinks))
}
}
if 1 == 0 {
reqs = append(reqs, UpdateTextStyleRequestFontSize(textboxID, slides.Dimension{
Magnitude: float64(14),
Unit: "PT"}))
}
return reqs
}
type LinkInfo struct {
URL string
Range *slides.Range
}