1 # This file is part of NIT ( http://www.nitlanguage.org ).
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
7 # http://www.apache.org/licenses/LICENSE-2.0
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.
15 # Nitcorn actions used by the nitweb server.
19 import model
::model_collect
21 # Display the tree of all loaded mentities.
26 var view
= new HtmlHomePage(model
) is lazy
28 redef fun answer
(request
, url
) do return render_view
(view
)
31 # Display the list of mentities matching `namespace`.
35 # TODO handle more than full namespaces.
36 redef fun answer
(request
, url
) do
37 var namespace
= request
.param
("namespace")
38 if namespace
== null or namespace
.is_empty
then
39 return render_error
(400, "Missing :namespace.")
41 var mentities
= model
.collect_by_namespace
(namespace
)
42 if request
.is_json_asked
then
43 var json
= new JsonArray
44 for mentity
in mentities
do
45 json
.add mentity
.to_json
47 return render_json
(json
)
49 var view
= new HtmlResultPage(namespace
, mentities
)
50 return render_view
(view
)
54 # Display a MEntity source code.
58 # Modelbuilder used to access sources.
59 var modelbuilder
: ModelBuilder
61 # TODO handle more than full namespaces.
62 redef fun answer
(request
, url
) do
63 var namespace
= request
.param
("namespace")
64 if namespace
== null or namespace
.is_empty
then
65 return render_error
(400, "Missing :namespace.")
67 var mentities
= model
.collect_by_namespace
(namespace
)
68 if mentities
.is_empty
then
69 return render_error
(404, "No mentity matching this namespace.")
71 var view
= new HtmlSourcePage(modelbuilder
, mentities
.first
)
72 return render_view
(view
)
76 # Display the doc of a MEntity.
80 # Modelbuilder used to access sources.
81 var modelbuilder
: ModelBuilder
83 # TODO handle more than full namespaces.
84 redef fun answer
(request
, url
) do
85 var namespace
= request
.param
("namespace")
86 if namespace
== null or namespace
.is_empty
then
87 return render_error
(400, "Missing :namespace.")
89 var mentities
= model
.collect_by_namespace
(namespace
)
90 if mentities
.is_empty
then
91 return render_error
(404, "No mentity matching this namespace.")
93 var view
= new HtmlDocPage(modelbuilder
, mentities
.first
)
94 return render_view
(view
)
98 # Return a random list of MEntities.
102 # TODO handle more than full namespaces.
103 redef fun answer
(request
, url
) do
104 var n
= request
.int_arg
("n") or else 10
105 var k
= request
.string_arg
("k") or else "modules"
106 var mentities
: Array[MEntity]
107 if k
== "modules" then
108 mentities
= model
.mmodules
.to_a
109 else if k
== "classdefs" then
110 mentities
= new Array[MClassDef]
111 for mclass
in model
.mclasses
do
112 mentities
.add_all
(mclass
.mclassdefs
)
115 mentities
= new Array[MPropDef]
116 for mprop
in model
.mproperties
do
117 mentities
.add_all
(mprop
.mpropdefs
)
121 mentities
= mentities
.sub
(0, n
)
122 if request
.is_json_asked
then
123 var json
= new JsonArray
124 for mentity
in mentities
do
125 json
.add mentity
.to_json
127 return render_json
(json
)
129 var view
= new HtmlResultPage("random", mentities
)
130 return render_view
(view
)
136 # Return `self` as a JsonObject.
137 fun to_json
: JsonObject do
138 var obj
= new JsonObject
139 obj
["name"] = html_name
140 obj
["namespace"] = html_raw_namespace
143 obj
["synopsis"] = mdoc
.content
.first
.html_escape
144 obj
["mdoc"] = mdoc
.content
.join
("\n").html_escape