Merge: src/model/model_index: model index uses BKTree
[nit.git] / src / doc / static / static_index.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 # Manage indexing of Nit model for Nitdoc QuickSearch.
16 module static_index
17
18 import static::static_html
19 import json
20
21 # Generate the index for then Nitdoc QuickSearch field.
22 #
23 # Create a JSON object containing links to:
24 # * mpackages
25 # * modules
26 # * mclasses
27 # * mpropdefs
28 # All entities are grouped by name to make the research easier.
29 #
30 # TODO Merge with model_index
31 redef class DocModel
32
33 # Build the nitdoc quick search index
34 fun create_index_file(file: String) do
35 var table = new QuickSearchTable(self)
36 var tpl = new Template
37 tpl.add "var nitdocQuickSearchRawList="
38 tpl.add table.to_json
39 tpl.add ";"
40 tpl.write_to_file(file)
41 end
42 end
43
44 # The result map for QuickSearch.
45 private class QuickSearchTable
46 super HashMap[String, Array[QuickSearchResult]]
47
48 var doc: DocModel
49
50 init do
51 var model = doc.model
52 var filter = doc.filter
53
54 index_mentities model.collect_mpackages(filter)
55 index_mentities model.collect_mmodules(filter)
56 index_mentities model.collect_mclasses(filter)
57 index_mentities model.collect_mproperties(filter)
58 end
59
60 fun index_mentities(mentities: Collection[MEntity]) do
61 for mentity in mentities do index_mentity mentity
62 end
63
64 fun index_mentity(mentity: MEntity) do
65 var key = mentity.name
66 if not has_key(key) then
67 self[key] = new Array[QuickSearchResult]
68 end
69 self[key].add new QuickSearchResult(mentity.full_name, mentity.html_url)
70 end
71 end
72
73 # A QuickSearch result.
74 private class QuickSearchResult
75 serialize
76
77 # The text of the link.
78 var txt: String
79
80 # The destination of the link.
81 var url: String
82 end