-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnote.go
More file actions
168 lines (148 loc) · 3.31 KB
/
note.go
File metadata and controls
168 lines (148 loc) · 3.31 KB
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
package main
import (
"fmt"
"regexp"
"strings"
"github.com/chentaihan/phpNote/util"
)
const (
DOWNLOAD_URL = "http://php.net/manual/zh/%s.%s.php"
URL_PREFIX = "http://php.net/manual/zh/"
LINK_TAG = "<a href=\""
)
type Note struct {
funName string
funUrl string
funNote string
}
func NewNote(funName string) *Note {
note := &Note{
funName: strings.ToLower(funName),
}
note.GetFunUrl(note.funName)
return note
}
func (note *Note) GetNote(content string) string {
reg := regexp.MustCompile("dc-title\">[\\s\\S]+")
ret := reg.FindAllString(content, -1)
noteContent := ""
if len(ret) > 0 {
noteContent = note.parseNote(ret[0])
if noteContent != "" {
return note.updateLink(noteContent)
}
}
return noteContent
}
func (note *Note) parseNote(content string) string {
index := strings.Index(content, ">")
if index < 0 {
return ""
}
preEnd := 0
end := 0
for {
end = IndexEx(content, "</span", end)
if end > 0 {
tmpIndex := IndexEx(content, "<span", preEnd)
if tmpIndex > end || tmpIndex < 0 {
return content[index+1 : end]
}
end += 6
preEnd = end
} else {
break
}
}
if end > index {
return content[index+1 : end]
}
return ""
}
func (note *Note) updateLink(content string) string {
var buffer *util.BufferWriter = nil
offset := 0
index := 0
for {
tmpIndex := IndexEx(content, LINK_TAG, offset)
if tmpIndex >= 0 {
if buffer == nil {
buffer = util.NewBufferWriter(len(content) + len(URL_PREFIX))
}
tmpIndex += len(LINK_TAG)
buffer.WriteString(content[offset:tmpIndex])
buffer.WriteString(URL_PREFIX)
offset = tmpIndex
index = tmpIndex
} else {
break
}
}
if buffer == nil {
return content
}
return buffer.ToString() + content[index:]
}
func IndexEx(s, substr string, offset int) int {
if len(s) < offset+len(substr) || offset < 0 {
return -1
}
index := strings.Index(s[offset:], substr)
if index < 0 {
return index
}
return index + offset
}
func (note *Note) GetFunUrl(funName string) string {
arr := strings.SplitN(funName, "::", 2)
if len(arr) != 2 {
funName = strings.Replace(funName, "_", "-", -1)
note.funUrl = fmt.Sprintf(DOWNLOAD_URL, "function", funName)
} else {
arr[1] = strings.Replace(arr[1], "_", "-", -1)
note.funUrl = fmt.Sprintf(DOWNLOAD_URL, arr[0], arr[1])
}
note.funUrl = strings.ToLower(note.funUrl)
return note.funUrl
}
type NoteSort []*Note
func (s NoteSort) Len() int {
return len(s)
}
func (s NoteSort) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}
func (s NoteSort) Less(i, j int) bool {
return s[i].funName < s[j].funName
}
type NoteList struct {
NoteMap map[string]*Note
}
var noteList *NoteList = nil
func NewNoteList() *NoteList {
noteList = &NoteList{
NoteMap: make(map[string]*Note, 4096),
}
return noteList
}
func GetNoteList() *NoteList {
return noteList
}
func (noteList *NoteList) AddNote(note *Note) {
noteList.NoteMap[note.funUrl] = note
}
func (noteList *NoteList) SetFunNote(url string, data interface{}) {
if funNote, isOK := data.([]byte); isOK {
if _, isOK1 := noteList.NoteMap[url]; isOK1 {
note := noteList.NoteMap[url]
note.funNote = note.GetNote(string(funNote))
}
}
}
func (noteList *NoteList) getUrlList() []string {
list := make([]string, 0, len(noteList.NoteMap))
for _, note := range noteList.NoteMap {
list = append(list, note.funUrl)
}
return list
}