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