9e9ef3cca19f83710d4a46c8292cb419cd994505
[nit.git] / src / doc / doc_phases / doc_indexing.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 doc_indexing
17
18 import doc_extract
19 private import json::static
20
21 # Generate the index for then Nitdoc QuickSearch field.
22 #
23 # Create a JSON object containing links to:
24 # * modules
25 # * mclasses
26 # * mpropdefs
27 # All entities are grouped by name to make the research easier.
28 #
29 # TODO Add a way to change the output and use it from Vim or whatever.
30 class IndexingPhase
31 super DocPhase
32
33 redef fun apply do
34 for mmodule in doc.mmodules do
35 add_result_for(mmodule.name, mmodule.full_name, mmodule.nitdoc_url)
36 end
37 for mclass in doc.mclasses do
38 add_result_for(mclass.name, mclass.full_name, mclass.nitdoc_url)
39 end
40 for mproperty in doc.mproperties do
41 for mpropdef in mproperty.mpropdefs do
42 if not doc.mpropdefs.has(mpropdef) then continue
43 var full_name = mpropdef.mclassdef.mclass.full_name
44 var cls_url = mpropdef.mclassdef.mclass.nitdoc_url
45 var def_url = "{cls_url}#{mpropdef.mproperty.nitdoc_id}"
46 add_result_for(mproperty.name, full_name, def_url)
47 end
48 end
49 # FIXME hack, generation should be done by the render phase
50 # create destination dir if it's necessary
51 var output_dir = ctx.output_dir
52 if not output_dir.file_exists then output_dir.mkdir
53
54 render.write_to_file("{ctx.output_dir.to_s}/quicksearch-list.js")
55 end
56
57 private var table = new QuickSearchTable
58
59 private fun add_result_for(query: String, txt: String, url: String) do
60 table[query].add new QuickSearchResult(txt, url)
61 end
62
63 # Render the index content.
64 fun render: Template do
65 var tpl = new Template
66 var buffer = new RopeBuffer
67 tpl.add buffer
68 buffer.append "var nitdocQuickSearchRawList="
69 table.append_json buffer
70 buffer.append ";"
71 return tpl
72 end
73 end
74
75 # The result map for QuickSearch.
76 private class QuickSearchTable
77 super JsonMapRead[String, QuickSearchResultList]
78 super HashMap[String, QuickSearchResultList]
79
80 redef fun provide_default_value(key) do
81 var v = new QuickSearchResultList
82 self[key] = v
83 return v
84 end
85 end
86
87 # A QuickSearch result list.
88 private class QuickSearchResultList
89 super JsonSequenceRead[QuickSearchResult]
90 super Array[QuickSearchResult]
91 end
92
93 # A QuickSearch result.
94 private class QuickSearchResult
95 super Jsonable
96
97 # The text of the link.
98 var txt: String
99
100 # The destination of the link.
101 var url: String
102
103 redef fun to_json do
104 return "\{\"txt\":{txt.to_json},\"url\":{url.to_json}\}"
105 end
106 end