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 # Base classes used by `nitweb`.
18 import model
::model_views
19 import model
::model_json
23 # Specific nitcorn Action that uses a Model
30 # MModule used to flatten model.
31 var mainmodule
: MModule
33 # Find the MEntity ` with `full_name`.
34 fun find_mentity
(model
: ModelView, full_name
: nullable String): nullable MEntity do
35 if full_name
== null then return null
36 return model
.mentity_by_full_name
(full_name
.from_percent_encoding
)
39 # Init the model view from the `req` uri parameters.
40 fun init_model_view
(req
: HttpRequest): ModelView do
41 var view
= new ModelView(model
)
42 var show_private
= req
.bool_arg
("private") or else false
43 if not show_private
then view
.min_visibility
= protected_visibility
45 view
.include_fictive
= req
.bool_arg
("fictive") or else false
46 view
.include_empty_doc
= req
.bool_arg
("empty-doc") or else true
47 view
.include_test_suite
= req
.bool_arg
("test-suite") or else false
48 view
.include_attribute
= req
.bool_arg
("attributes") or else true
54 # Specific handler for nitweb API.
55 abstract class APIHandler
58 # The JSON API does not filter anything by default.
60 # So we can cache the model view.
61 var view
: ModelView is lazy
do
62 var view
= new ModelView(model
)
63 view
.min_visibility
= private_visibility
64 view
.include_fictive
= true
65 view
.include_empty_doc
= true
66 view
.include_attribute
= true
67 view
.include_test_suite
= true
71 # Try to load the mentity from uri with `/:id`.
73 # Send 400 if `:id` is null.
74 # Send 404 if no entity is found.
75 # Return null in both cases.
76 fun mentity_from_uri
(req
: HttpRequest, res
: HttpResponse): nullable MEntity do
77 var id
= req
.param
("id")
82 var mentity
= find_mentity
(view
, id
)
83 if mentity
== null then
92 # URL to `self` within the web interface.
93 fun web_url
: String do return "/doc/" / full_name
95 # URL to `self` within the JSON api.
96 fun api_url
: String do return "/api/entity/" / full_name
100 obj
["web_url"] = web_url
101 obj
["api_url"] = api_url
105 # Get the full json repesentation of `self` with MEntityRefs resolved.
106 fun api_json
(handler
: ModelHandler): JsonObject do return json
109 redef class MEntityRef
112 obj
["web_url"] = mentity
.web_url
113 obj
["api_url"] = mentity
.api_url
114 obj
["name"] = mentity
.name
115 obj
["mdoc"] = mentity
.mdoc_or_fallback
116 obj
["visibility"] = mentity
.visibility
117 obj
["location"] = mentity
.location
118 var modifiers
= new JsonArray
119 for modifier
in mentity
.collect_modifiers
do
120 modifiers
.add modifier
122 obj
["modifiers"] = modifiers
123 var mentity
= self.mentity
124 if mentity
isa MMethod then
125 obj
["msignature"] = mentity
.intro
.msignature
126 else if mentity
isa MMethodDef then
127 obj
["msignature"] = mentity
.msignature
128 else if mentity
isa MVirtualTypeProp then
129 obj
["bound"] = to_mentity_ref
(mentity
.intro
.bound
)
130 else if mentity
isa MVirtualTypeDef then
131 obj
["bound"] = to_mentity_ref
(mentity
.bound
)
139 # Add doc down processing
142 obj
["synopsis"] = synopsis
143 obj
["documentation"] = documentation
144 obj
["comment"] = comment
145 obj
["html_synopsis"] = html_synopsis
.write_to_string
146 obj
["html_documentation"] = html_documentation
.write_to_string
147 obj
["html_comment"] = html_comment
.write_to_string
153 redef fun api_json
(handler
) do
155 obj
["intro_mclassdefs"] = to_mentity_refs
(collect_intro_mclassdefs
(private_view
))
156 obj
["redef_mclassdefs"] = to_mentity_refs
(collect_redef_mclassdefs
(private_view
))
157 obj
["imports"] = to_mentity_refs
(in_importation
.direct_greaters
)
163 redef fun api_json
(handler
) do
165 obj
["all_mproperties"] = to_mentity_refs
(collect_accessible_mproperties
(private_view
))
166 obj
["intro_mproperties"] = to_mentity_refs
(collect_intro_mproperties
(private_view
))
167 obj
["redef_mproperties"] = to_mentity_refs
(collect_redef_mproperties
(private_view
))
168 obj
["parents"] = to_mentity_refs
(collect_parents
(private_view
))
173 redef class MClassDef
176 obj
["intro"] = to_mentity_ref
(mclass
.intro
)
177 obj
["mpackage"] = to_mentity_ref
(mmodule
.mpackage
)
181 redef fun api_json
(handler
) do
183 obj
["intro_mpropdefs"] = to_mentity_refs
(collect_intro_mpropdefs
(private_view
))
184 obj
["redef_mpropdefs"] = to_mentity_refs
(collect_redef_mpropdefs
(private_view
))
189 redef class MProperty
192 obj
["intro_mclass"] = to_mentity_ref
(intro_mclassdef
.mclass
)
193 obj
["mpackage"] = to_mentity_ref
(intro_mclassdef
.mmodule
.mpackage
)
201 obj
["intro"] = to_mentity_ref
(mproperty
.intro
)
202 obj
["intro_mclassdef"] = to_mentity_ref
(mproperty
.intro
.mclassdef
)
203 obj
["mmodule"] = to_mentity_ref
(mclassdef
.mmodule
)
204 obj
["mgroup"] = to_mentity_ref
(mclassdef
.mmodule
.mgroup
)
205 obj
["mpackage"] = to_mentity_ref
(mclassdef
.mmodule
.mpackage
)
210 redef class MClassType
211 redef var web_url
= mclass
.web_url
is lazy
214 redef class MNullableType
215 redef var web_url
= mtype
.web_url
is lazy
218 redef class MParameterType
219 redef var web_url
= mclass
.web_url
is lazy
222 redef class MVirtualType
223 redef var web_url
= mproperty
.web_url
is lazy
226 redef class POSetElement[E
]
229 # Return JSON representation of `self`.
230 fun json
: JsonObject do
231 assert self isa POSetElement[MEntity]
232 var obj
= new JsonObject
233 obj
["greaters"] = to_mentity_refs
(greaters
)
234 obj
["direct_greaters"] = to_mentity_refs
(direct_greaters
)
235 obj
["direct_smallers"] = to_mentity_refs
(direct_smallers
)
236 obj
["smallers"] = to_mentity_refs
(smallers
)
240 redef fun to_json
do return json
.to_json