-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch.js
141 lines (135 loc) · 4.19 KB
/
search.js
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
"use strict"
const SearchMethod = {
currentSearchString: "",
patternDivision: function(src, re) {
const result = []
const length = src.length
re.lastIndex = 0
let matched
let lastIndex = 0
let found
let prefixLength
while (matched = re.exec(src)) {
found = matched[0]
prefixLength = re.lastIndex - lastIndex - found.length
if (prefixLength > 0) {
result.push(["u", src.substring(lastIndex, lastIndex + prefixLength)])
}
lastIndex = re.lastIndex
if (found.length === 0) {
re.lastIndex += 1
}
result.push(["m", found])
}
if (lastIndex < length) {
result.push(["u", src.substring(lastIndex, length)])
}
return result
},
methodNameSpan: function(name, reKeywords) {
const result = document.createElement("span")
SearchMethod.patternDivision(name, /(?:::|\.#?|#|(?=\$))/g).forEach(function(chunk) {
if (chunk[0] === "m") {
const elem = document.createElement("span")
elem.classList.add("method-links__delimiter")
elem.innerText = chunk[1]
result.appendChild(elem)
} else {
const elem = document.createElement("span")
SearchMethod.patternDivision(chunk[1], reKeywords).forEach(function(cnk) {
const e = document.createElement("span")
e.innerText = cnk[1]
if (cnk[0] === "m") {
e.classList.add("method-links__hit")
}
elem.appendChild(e)
})
result.appendChild(elem)
}
})
return result
},
normalizeSearchString: function(str) {
const charShift = function(offset) {
return function(char) {
return String.fromCharCode(char.charCodeAt(0) + offset)
}
}
return str.
replace(/[\uFF01-\uFF5E]/g, charShift(-0xFEE0)).
replace(/[A-Z]/g, charShift(0x20)).
replace(/[^\u0021-\u007E]+/g, " ").
replace(/^ | $/g, "")
},
retrieve: function(keywords) {
return methodEntries.filter(function(item) {
const str = item.s
return keywords.every(function(kw) {
return str.indexOf(kw) >= 0
})
})
},
search: function(searchStr, renderElem) {
searchStr = SearchMethod.normalizeSearchString(searchStr)
if (searchStr !== SearchMethod.currentSearchStr) {
if (searchStr === "") {
renderElem.innerHTML = ""
return
}
SearchMethod.currentSearchStr = searchStr
const keywords = searchStr.split(" ")
const reKeywords = new RegExp(
keywords.map(function(s) {
return s.replace(/[\\^$.*+?()[\]{}|]/g, '\\$&')
}).join("|"),
"gi"
)
const entries = SearchMethod.retrieve(keywords)
const ul = document.createElement("ul")
ul.classList.add("method-links")
let count = 0
entries.forEach(function(entry) {
count += 1
if (count > 100) {
return
}
const li = document.createElement("li")
ul.appendChild(li)
const a = document.createElement("a")
li.appendChild(a)
a.href = "/method/" + entry.p
a.href = "https://docs.ruby-lang.org/ja/2.7.0/method/" + entry.p + ".html"
console.log(reKeywords)
a.appendChild(SearchMethod.methodNameSpan(entry.k, reKeywords))
})
renderElem.innerHTML = ""
renderElem.appendChild(ul)
const info = document.createElement("div")
info.classList.add("method-links__info")
if (entries.length === 0) {
info.innerText = "見つかりませんでした。"
} else {
info.innerText = ((entries.length > 100) ? "ほか" : "") +
"(計 " + entries.length + " 件)"
}
renderElem.appendChild(info)
}
},
setSearch: function(params) {
if (params.searchField) {
const sc = document.createElement("script")
sc.src = "method-entries.js"
document.body.appendChild(sc)
setInterval(function() {
SearchMethod.search(params.searchField.value, params.resultElem)
}, params.intervalMs)
}
}
};
window.onload = function() {
SearchMethod.setSearch({
resultElem: document.getElementById("searchResult"),
searchField: document.getElementById("searchMethodField"),
intervalMs: 750,
})
}