/
search.dart
192 lines (170 loc) · 4.51 KB
/
search.dart
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
/** Helper class */
class NavItem {
String filename;
String typ;
String title;
String _hash;
NavItem(a) {
filename = a[0];
typ = a[1];
title = a[2];
_hash = '$filename $typ $title';
}
}
/** Helper class */
class TermItem {
String filename;
String memberid;
String _hash;
TermItem(a) {
filename = a[0];
memberid = a[1];
_hash = '$filename $memberid';
}
}
/**
* Stores data to create navigation entries and search filters.
* Serializes data into a dart file.
*/
class Search {
/** Index of available documentation files. */
List filelist;
/** Inverted index, used in searchterms. */
List id;
/** List of available navigation entries. */
List<NavItem> navigation;
/** Searchindex. */
Map<String, List> searchterms;
/** Names used for navigation icon entries. */
List typ;
Search()
: filelist = [],
id = [],
navigation = [],
searchterms = {},
typ = ['library', 'class', 'interface', 'exception'];
/** Add a navigation entry. */
void addNav(List a) {
// lists are not hashable? this gets me poor performance
NavItem b = new NavItem(a);
if (navigation.length == 0) {
navigation.add(b);
} else {
if (navigation.some((e) => e._hash == b._hash) != true) {
navigation.add(b);
}
}
}
/** Add/update a searchterm entry. */
void addTerm(String a, List b) {
// remove get/set
a = a.toString().toLowerCase().split(':').last();
// remove private entries
if (a.startsWith('_') || b[0].startsWith('_') || b[0].indexOf('/_') > -1) {
return;
}
List key = [];
if (searchterms.containsKey(a)) {
key = searchterms[a];
}
key.add(new TermItem(b));
searchterms[a] = key;
}
/** Like [files/endFile], but write to current direcory. */
void endLocalFile() {
String outPath = _filePath;
world.files.createDirectory(dirname(outPath), recursive: true);
world.files.writeString(outPath, _file.toString());
_filePath = null;
_file = null;
}
/**
* Serialize a collection to a file.
* Works for String, int and List.
*/
void writelist(var l) {
write('[');
bool first = true;
for (var item in l) {
if (!first) write(', ');
if (item is String) {
// escape quotes
item = item.replaceAll("'", "\\'");
write("'$item'");
}
if (item is int) write(item);
if (item is List) writelist(item);
first = false;
}
write(']');
}
/**
* Write a dart file from a datastructure, create a sourceable file with
* contents of a Map.
*/
void contents2dart(bool enableSearch) {
var _out;
startFile('searching.dart');
// this file is used to be sourced into another dart file.
writeln('_terms() {');
if (enableSearch == true) {
writeln('var terms = {};');
// create an inverted index for navigation
_out = [];
for (NavItem i in navigation) {
if (filelist.indexOf(i.filename) == -1) {
filelist.add(i.filename);
}
_out.add(filelist.indexOf(i.filename));
_out.add(typ.indexOf(i.typ));
_out.add(i.title);
}
navigation = _out;
// searchterms is a Map of (term, [[filename, membername], ...])
// create an inverted index
_out = {};
for (String key in searchterms.getKeys()) {
List termitems = searchterms[key];
List bco = [];
for (TermItem termitem in termitems) {
if (id.indexOf(termitem.memberid) == -1) {
id.add(termitem.memberid);
}
bco.add(filelist.indexOf(termitem.filename));
bco.add(id.indexOf(termitem.memberid));
}
_out[key] = bco;
}
searchterms = _out;
// write file entires.
write("terms['nav'] = ");
writelist(navigation);
writeln(';');
write("terms['filelist'] = ");
writelist(filelist);
writeln(';');
write("terms['typ'] = ");
writelist(typ);
writeln(';');
write("terms['id'] = ");
writelist(id);
writeln(';');
writeln("terms['terms'] = {");
var v, first;
first = true;
for (String key in searchterms.getKeys()) {
if (!first) writeln(', ');
write("'$key': ");
v = searchterms[key];
writelist(v);
first = false;
}
writeln('};');
} else {
writeln('var terms = null;');
}
writeln('return terms;');
writeln('}');
endLocalFile();
}
}