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