-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmod_inline.go
239 lines (196 loc) · 6.22 KB
/
mod_inline.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
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
package white600
import (
"strings"
)
type InlineType int
const (
inlineText InlineType = iota
inlineCode
inlineBold
inlineItalic
inlineCancel
inlineLink
inlineBreak
)
type inlineObject struct {
inlineType InlineType
content string
child *[]inlineObject
}
// inlineConv ...インライン要素の解析
func (data *MarkdownInfo) inlineConv(text string) string {
// 初期値の生成
inline := []inlineObject{{inlineType: inlineText, content: text}}
// リンクの解析(画像リンクのためにデータを文字列に変換する)
inline = inlineLinkX(inline, [3]string{""}, "<img alt='$1' src='$2'>")
inline = inlineLinkX([]inlineObject{{inlineType: inlineText, content: inlineConvert(inline)}}, [3]string{"[", "](", ")"}, "<a href='$2'>$1</a>")
// 再帰的にその他のインライン要素の解析
inline = inlineTag(inline)
// 解析結果を文字列に
return inlineConvert(inline)
}
// inlineLink ...インラインのリンクを解析(リンク要素は前優先ではなく字句優先)
func inlineLinkX(inline []inlineObject, mdTemplate [3]string, htmlTemplate string) []inlineObject {
// 全てのテキスト要素をチェック
isLinkGenerated := true
for isLinkGenerated {
isLinkGenerated = false
for i, v := range inline {
// テキスト要素以外は処理しない
if v.inlineType != inlineText {
continue
}
// 分解して解析
var mdPoint [4]int // 先頭の参照先が無いので1個ずらす
for j, w := range mdTemplate {
mdPoint[j+1] = strings.Index(v.content[mdPoint[j]:], w)
if mdPoint[j+1] == -1 {
break
}
mdPoint[j+1] += mdPoint[j]
}
// 変換が必要ない場合は処理しない
if mdPoint[1] == -1 || mdPoint[2] == -1 || mdPoint[3] == -1 {
continue
}
// インライン要素の解析
alt := inlineTag([]inlineObject{{inlineType: inlineText, content: v.content[mdPoint[1]+len(mdTemplate[0]) : mdPoint[2]], child: nil}})
// 置き換えテキストを作成
s := strings.NewReplacer(
"$1", inlineConvert(alt),
"$2", v.content[mdPoint[2]+len(mdTemplate[1]):mdPoint[3]],
).Replace(htmlTemplate)
// オブジェクトを更新
isLinkGenerated = true
nextInline := append(inline[:i], inlineObject{inlineType: inlineText, content: v.content[:mdPoint[1]]})
nextInline = append(nextInline, inlineObject{inlineType: inlineLink, content: s})
nextInline = append(nextInline, inlineObject{inlineType: inlineText, content: v.content[len(mdTemplate[2])+mdPoint[3]:]})
nextInline = append(nextInline, inline[i+1:]...)
inline = nextInline
break
}
}
return inline
}
// inlineConvert ...インライン要素の解析結果を文字列に変換
func inlineConvert(inline []inlineObject) string {
text := []string{}
for _, v := range inline {
// 開きタグを入れる
text = append(text, inlineTagOpen(v.inlineType))
// 内部テキストを入れる
text = append(text, v.content)
// 子要素を処理
if v.child != nil {
text = append(text, inlineConvert(*v.child))
}
// 閉じタグを入れる
text = append(text, inlineTagClose(v.inlineType))
}
// まとめる
return strings.Join(text, "")
}
// inlineTagClose ...閉じタグ
func inlineTagClose(inlineType InlineType) string {
switch inlineType {
case inlineCode:
return "</code>"
case inlineBold:
return "</strong>"
case inlineItalic:
return "</em>"
case inlineCancel:
return "</s>"
default:
return ""
}
}
// inlineTagOpen ...開きタグ
func inlineTagOpen(inlineType InlineType) string {
switch inlineType {
case inlineCode:
return "<code>"
case inlineBold:
return "<strong>"
case inlineItalic:
return "<em>"
case inlineCancel:
return "<s>"
default:
return ""
}
}
// 辞書用の型
type dictInfo struct {
tag string
inlineType InlineType
recursion bool
}
// テキストのパターンと対応するタイプ(場所移動する)
var dictionary = []dictInfo{
{tag: "`", inlineType: inlineCode, recursion: false},
{tag: "**", inlineType: inlineBold, recursion: true},
{tag: "__", inlineType: inlineBold, recursion: true},
{tag: "~~", inlineType: inlineCancel, recursion: true},
{tag: "*", inlineType: inlineItalic, recursion: true},
{tag: "_", inlineType: inlineItalic, recursion: true},
}
// inlineTag ...インラインの要素を解析
func inlineTag(inline []inlineObject) []inlineObject {
for i := 0; i < len(inline); i++ {
v := inline[i]
// 解析しない物を除外
if v.inlineType == inlineLink || v.inlineType == inlineCode {
continue
}
// 現在の要素を解析
for j := 0; j < len(v.content); j++ {
for _, x := range dictionary {
// 開始終了タグが存在するかをチェック
flg, ct, af := lexerInlineStart(v.content[j:], x.tag)
bf := v.content[:j]
if !flg {
// 改行が存在するかをチェック
if len(v.content[j:]) >= 2 && v.content[j:j+2] == " " {
newContent := v.content[:j]
newContent += "<br>"
newContent += v.content[j+2:]
v.content = newContent
inline[i] = v
}
continue
}
// 中のテキストを処理
chobj := []inlineObject{{inlineType: inlineText, content: ct}}
if x.recursion {
chobj = inlineTag(chobj)
}
// 要素を間に追加
nextInline := append(inline[:i], inlineObject{inlineType: inlineText, content: bf})
nextInline = append(nextInline, inlineObject{inlineType: x.inlineType, content: "", child: &chobj})
nextInline = append(nextInline, inlineObject{inlineType: inlineText, content: af})
nextInline = append(nextInline, inline[i+1:]...)
inline = nextInline
// 現在の要素の解析を抜ける
j = len(v.content)
break
}
}
}
return inline
}
// lexerInlineStart ...インラインの開始であるかをチェック(終了が存在しないなら開始扱いしない)
func lexerInlineStart(text string, tag string) (bool, string, string) {
// そもそも違う
if len(text) < len(tag)*2 || text[:len(tag)] != tag {
return false, text, ""
}
// 終了が存在するか?
end := strings.Index(text[len(tag):], tag)
if end == -1 {
return false, text, ""
}
inner := text[len(tag) : end+len(tag)]
after := text[end+len(tag)*2:]
return true, inner, after
}