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.
25 # Model to explore and render.
29 var view
= new HtmlHomePage(model
) is lazy
31 redef fun answer
(request
, url
) do return render_view
(view
)
34 # Display the list of mentities matching `namespace`.
38 # Model to explore and render.
41 # TODO handle more than full namespaces.
42 redef fun answer
(request
, url
) do
43 var namespace
= request
.param
("namespace")
44 if namespace
== null or namespace
.is_empty
then
45 return render_error
(400, "Missing :namespace.")
47 var mentities
= model
.collect_by_namespace
(namespace
)
48 if request
.is_json_asked
then
49 var json
= new JsonArray
50 for mentity
in mentities
do
51 json
.add mentity
.to_json
53 return render_json
(json
)
55 var view
= new HtmlResultPage(namespace
, mentities
)
56 return render_view
(view
)
60 # Display a MEntity source code.
64 # Model to explore and render.
67 # Modelbuilder used to access sources.
68 var modelbuilder
: ModelBuilder
70 # TODO handle more than full namespaces.
71 redef fun answer
(request
, url
) do
72 var namespace
= request
.param
("namespace")
73 if namespace
== null or namespace
.is_empty
then
74 return render_error
(400, "Missing :namespace.")
76 var mentities
= model
.collect_by_namespace
(namespace
)
77 if mentities
.is_empty
then
78 return render_error
(404, "No mentity matching this namespace.")
80 var view
= new HtmlSourcePage(modelbuilder
, mentities
.first
)
81 return render_view
(view
)
85 # Display the doc of a MEntity.
89 # Model to explore and render.
92 # Modelbuilder used to access sources.
93 var modelbuilder
: ModelBuilder
95 # TODO handle more than full namespaces.
96 redef fun answer
(request
, url
) do
97 var namespace
= request
.param
("namespace")
98 if namespace
== null or namespace
.is_empty
then
99 return render_error
(400, "Missing :namespace.")
101 var mentities
= model
.collect_by_namespace
(namespace
)
102 if mentities
.is_empty
then
103 return render_error
(404, "No mentity matching this namespace.")
105 var view
= new HtmlDocPage(modelbuilder
, mentities
.first
)
106 return render_view
(view
)
110 # Return a random list of MEntities.
114 # Model to explore and render.
117 # TODO handle more than full namespaces.
118 redef fun answer
(request
, url
) do
119 var n
= request
.int_arg
("n") or else 10
120 var k
= request
.string_arg
("k") or else "modules"
121 var mentities
: Array[MEntity]
122 if k
== "modules" then
123 mentities
= model
.mmodules
.to_a
124 else if k
== "classdefs" then
125 mentities
= new Array[MClassDef]
126 for mclass
in model
.mclasses
do
127 mentities
.add_all
(mclass
.mclassdefs
)
130 mentities
= new Array[MPropDef]
131 for mprop
in model
.mproperties
do
132 mentities
.add_all
(mprop
.mpropdefs
)
136 mentities
= mentities
.sub
(0, n
)
137 if request
.is_json_asked
then
138 var json
= new JsonArray
139 for mentity
in mentities
do
140 json
.add mentity
.to_json
142 return render_json
(json
)
144 var view
= new HtmlResultPage("random", mentities
)
145 return render_view
(view
)
151 # Return `self` as a JsonObject.
152 fun to_json
: JsonObject do
153 var obj
= new JsonObject
154 obj
["name"] = html_name
155 obj
["namespace"] = html_raw_namespace
158 obj
["synopsis"] = mdoc
.content
.first
.html_escape
159 obj
["mdoc"] = mdoc
.content
.join
("\n").html_escape