forked from fortran-lang/fortran-lang.org
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpackage_search.js
173 lines (114 loc) · 5.35 KB
/
package_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
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
(function() {
if (!!document.getElementById('package-search-results')){
// Load packages from JSON asynchronously
fortranLang.loadJSON(fortranLang.baseurl+'/packages/package_index.json', search);
}
function search(data){
// Called after json data is loaded
projects = data.projects;
// In the earlier code there has been an error in the regex implemented in function fortranLang.findGetParameter('query')
// Opened a new Issue for this #388
var url = new URL(window.location.href); //converts the url from index page and extracts the parameter "query"
// Get search string
var queryString = url.searchParams.get("query");
document.getElementById('search-query').value = queryString;
results = searchProjects(queryString,projects);
resultsHTML = resultsToHTML(results);
document.getElementById('package-search-results').innerHTML = resultsHTML;
}
function getSubSentences(sentence) {
// Return all permutations of contiguous sub sentences from a sentence
var words = sentence.split(" ");
var subs = [];
var N = words.length;
var i;
for (i = 1; i <= N; i++){ // Loop over possible sentence lengths
var j;
for (j=0; j<=(N-i); j++){ // Loop over sentence locations
sub_i = words.slice(j,j+i);
subs.push(sub_i.join(" "));
}
}
return subs;
}
function searchProjects(queryString,projects) {
// Basic sub-string matching within project fields
//
// Results ranked by size of matched sub-string and field weight
//
var subs = getSubSentences(queryString);
// Sort by subsentence length descending
subs = subs.sort(function(a,b){return b.split(" ").length - a.split(" ").length})
var fields = ['name','description','tags','license','github','gitlab','url'];
var fieldWeights = [10.0,1.0,1.0,0.1,1.0,1.0,0.1];
// Loop over projects JSON
var i;
for (i = 0; i<projects.length; i++){
var j;
projects[i].score = 0;
var subMatch = [];
for (j=0; j<subs.length; j++){
subMatch[j] = false;
}
for (j=0; j<fields.length; j++){
var k;
for (k=0; k<subs.length; k++){
if (subMatch[k]){
// Don't match a sub-sentence more than once per project
continue;
}
fieldData = projects[i][fields[j]].toLowerCase();
querySubString = subs[k].toLowerCase();
queryStringLength = querySubString.split(" ").length;
if (fieldData.includes(querySubString)){
subMatch[k] = true;
projects[i].score += fieldWeights[j]*queryStringLength;
break;
}
}
}
}
return projects.sort(function(a,b){return b.score - a.score});
}
function resultsToHTML(results){
// Return HTML representation of search results
//
var out = "";
var i;
for (i=0;i<results.length;i++){
if (results[i].score > 0){
project = results[i];
if (results[i].github != "" && !project.fpm){
out += '<h3><a href="https://github.com/'+project.github+'" target="_blank">';
out += '<i class="devicon-github-plain colored"></i> '+project.name+'</a></h3>';
} else if (results[i].gitlab != "" && !project.fpm){
out += '<h3><a href="https://gitlab.com/'+results[i].gitlab+'" target="_blank">';
out += '<i class="devicon-gitlab-plain colored"></i> '+project.name+'</a></h3>';
} else {
out += '<h3><a href="'+project.url+'" target="_blank">';
if (project.fpm){
out += '<i class="fas fa-cubes"></i> ';
} else if (project.url.includes('gitlab.com')) {
out += '<i class="devicon-gitlab-plain colored"></i> ';
} else if (project.url.includes('bitbucket.org')){
out += '<i class="devicon-bitbucket-plain colored"></i> ';
}
out += project.name+'</a></h3>';
}
out += '<p> '+project.description;
var cats = project.categories.split(" ");
out += ' ('
var j;
for (j=0;j<cats.length;j++){
out += '<a href="'+fortranLang.baseurl+'/packages/'+cats[j]+'">'+cats[j]+'</a>';
if (j<cats.length-1){
out += ', ';
}
}
out += ') </p>'
out += '<p class="light small"><b>Tags: </b>'+project.tags+'</p>';
}
}
return out;
}
})();