src/doc/commands: merge `catalog_json` intro `templates_json`
[nit.git] / src / doc / templates / templates_json.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 # Make model entities Serializable.
16 #
17 # To avoid cycles, every reference from a MEntity to another is replaced by a
18 # MEntityRef.
19 #
20 # How subobjects are retrieved using the MEntityRef is the responsability of the
21 # client. Json objects can be returned as this or inflated with concrete objet
22 # rather than the refs.
23 #
24 # TODO consider serialization module?
25 module templates_json
26
27 import model::model_collect
28 import json::serialization_write
29 import catalog
30 import doc_down
31
32 redef class MEntity
33 serialize
34
35 private fun core_serialize_base(v: Serializer) do
36 v.serialize_attribute("name", name)
37 var mdoc = mdoc_or_fallback
38 if mdoc != null then
39 v.serialize_attribute("synopsis", mdoc.synopsis)
40 end
41 end
42
43 redef fun core_serialize_to(v) do
44 core_serialize_base(v)
45
46 v.serialize_attribute("namespace", json_namespace)
47 v.serialize_attribute("class_name", class_name)
48 v.serialize_attribute("full_name", full_name)
49 v.serialize_attribute("visibility", visibility.to_s)
50 var mdoc = mdoc_or_fallback
51 if mdoc != null then
52 v.serialize_attribute("html_synopsis", mdoc.html_synopsis.write_to_string)
53 end
54
55 var modifiers = collect_modifiers
56 if modifiers.not_empty then
57 v.serialize_attribute("modifiers", modifiers)
58 end
59 end
60
61 # Return `self.full_name` as an object that can be serialized to json.
62 fun json_namespace: JsonNamespace is abstract
63
64 # Return a new MEntityRef to `self`.
65 fun to_json_ref: MEntityRef do return new MEntityRef(self)
66 end
67
68 redef class MDoc
69 serialize
70
71 redef fun core_serialize_to(v) do
72 var doc = html_documentation.write_to_string.trim
73 if not doc.is_empty then
74 v.serialize_attribute("html_documentation", doc)
75 end
76 end
77 end
78
79 redef class nitc::Location
80 serialize
81
82 redef fun core_serialize_to(v) do
83 v.serialize_attribute("column_end", column_end)
84 v.serialize_attribute("column_start", column_start)
85 v.serialize_attribute("line_end", line_end)
86 v.serialize_attribute("line_start", line_start)
87 var file = self.file
88 if file != null then
89 v.serialize_attribute("file", file.filename)
90 end
91 end
92 end
93
94 redef class MPackage
95 redef fun core_serialize_to(v) do
96 super
97
98 var metadata = self.metadata
99 if metadata.license != null then
100 v.serialize_attribute("license", metadata.license)
101 end
102 if metadata.maintainers.not_empty then
103 v.serialize_attribute("maintainer", metadata.maintainers.first)
104 end
105 if metadata.tags.not_empty then
106 v.serialize_attribute("tags", metadata.tags)
107 end
108 end
109
110 redef fun json_namespace do
111 var ns = new JsonNamespace
112 ns.add to_json_ref
113 return ns
114 end
115 end
116
117 redef class MGroup
118 redef fun json_namespace do
119 var ns = new JsonNamespace
120 if parent != null then
121 ns.prepend parent.as(not null).json_namespace
122 end
123 ns.add to_json_ref
124 ns.add ">"
125 return ns
126 end
127 end
128
129 redef class MModule
130 redef fun json_namespace do
131 var ns = new JsonNamespace
132 if mgroup != null then
133 ns.add_all mgroup.as(not null).mpackage.json_namespace
134 ns.add "::"
135 end
136 ns.add to_json_ref
137 return ns
138 end
139
140 private fun ns_for(visibility: MVisibility): JsonNamespace do
141 if visibility <= private_visibility then return json_namespace
142 var mgroup = self.mgroup
143 if mgroup == null then return json_namespace
144 return mgroup.mpackage.json_namespace
145 end
146 end
147
148 redef class MClass
149 redef fun core_serialize_to(v) do
150 super
151
152 if mparameters.not_empty then
153 v.serialize_attribute("mparameters", mparameters)
154 end
155 end
156
157 redef fun json_namespace do
158 var ns = new JsonNamespace
159 ns.add_all intro_mmodule.ns_for(visibility)
160 ns.add "::"
161 ns.add to_json_ref
162 return ns
163 end
164 end
165
166 redef class MClassDef
167 redef fun core_serialize_to(v) do
168 super
169 if is_intro then
170 v.serialize_attribute("is_intro", true)
171 end
172 if mclass.mparameters.not_empty then
173 v.serialize_attribute("mparameters", mclass.mparameters)
174 end
175 end
176
177 redef fun json_namespace do
178 var ns = new JsonNamespace
179 if is_intro then
180 ns.add_all mmodule.ns_for(mclass.visibility)
181 ns.add "$"
182 ns.add mclass.to_json_ref
183 else if mclass.intro_mmodule.mpackage != mmodule.mpackage then
184 ns.add_all mmodule.json_namespace
185 ns.add "$"
186 ns.add_all mclass.json_namespace
187 else if mclass.visibility > private_visibility then
188 ns.add_all mmodule.json_namespace
189 ns.add "$"
190 ns.add mclass.to_json_ref
191 else
192 ns.add_all mmodule.json_namespace
193 ns.add "$::"
194 ns.add mclass.intro_mmodule.to_json_ref
195 ns.add "::"
196 ns.add mclass.to_json_ref
197 end
198 return ns
199 end
200 end
201
202 redef class MProperty
203 redef fun json_namespace do
204 var ns = new JsonNamespace
205 if intro_mclassdef.is_intro then
206 ns.add_all intro_mclassdef.mmodule.ns_for(visibility)
207 ns.add "::"
208 ns.add intro_mclassdef.mclass.to_json_ref
209 ns.add "::"
210 ns.add to_json_ref
211 else
212 ns.add_all intro_mclassdef.mmodule.json_namespace
213 ns.add "::"
214 ns.add intro_mclassdef.mclass.to_json_ref
215 ns.add "::"
216 ns.add to_json_ref
217 end
218 return ns
219 end
220 end
221
222 redef class MMethod
223 redef fun core_serialize_to(v) do
224 super
225 if is_init then
226 v.serialize_attribute("is_init", true)
227 end
228 v.serialize_attribute("msignature", intro.msignature)
229 end
230 end
231
232 redef class MAttribute
233 redef fun core_serialize_to(v) do
234 super
235 v.serialize_attribute("static_mtype", intro.static_mtype)
236 end
237 end
238
239 redef class MVirtualTypeProp
240 redef fun core_serialize_to(v) do
241 super
242 v.serialize_attribute("bound", intro.bound)
243 end
244 end
245
246 redef class MPropDef
247 redef fun core_serialize_to(v) do
248 super
249 if is_intro then
250 v.serialize_attribute("is_intro", true)
251 end
252 end
253
254 redef fun json_namespace do
255 var res = new JsonNamespace
256 res.add_all mclassdef.json_namespace
257 res.add "$"
258
259 if mclassdef.mclass == mproperty.intro_mclassdef.mclass then
260 res.add to_json_ref
261 else
262 if mclassdef.mmodule.mpackage != mproperty.intro_mclassdef.mmodule.mpackage then
263 res.add_all mproperty.intro_mclassdef.mmodule.ns_for(mproperty.visibility)
264 res.add "::"
265 else if mproperty.visibility <= private_visibility then
266 if mclassdef.mmodule.namespace_for(mclassdef.mclass.visibility) != mproperty.intro_mclassdef.mmodule.mpackage then
267 res.add "::"
268 res.add mproperty.intro_mclassdef.mmodule.to_json_ref
269 res.add "::"
270 end
271 end
272 if mclassdef.mclass != mproperty.intro_mclassdef.mclass then
273 res.add mproperty.intro_mclassdef.to_json_ref
274 res.add "::"
275 end
276 res.add to_json_ref
277 end
278 return res
279 end
280 end
281
282 redef class MMethodDef
283 redef fun core_serialize_to(v) do
284 super
285 v.serialize_attribute("msignature", msignature)
286 end
287 end
288
289 redef class MAttributeDef
290 redef fun core_serialize_to(v) do
291 super
292 v.serialize_attribute("static_mtype", static_mtype)
293 end
294 end
295
296 redef class MVirtualTypeDef
297 redef fun core_serialize_to(v) do
298 super
299 v.serialize_attribute("bound", bound)
300 end
301 end
302
303 redef class MType
304 redef fun core_serialize_to(v) do
305 v.serialize_attribute("name", name)
306
307 var mdoc = mdoc_or_fallback
308 if mdoc != null then
309 v.serialize_attribute("synopsis", mdoc.synopsis)
310 v.serialize_attribute("html_synopsis", mdoc.html_synopsis.write_to_string)
311 end
312 end
313 end
314
315
316 redef class MSignature
317 redef fun core_serialize_to(v) do
318 v.serialize_attribute("arity", arity)
319 v.serialize_attribute("mparameters", mparameters)
320 v.serialize_attribute("return_mtype", return_mtype)
321 end
322 end
323
324 redef class MParameterType
325 redef fun core_serialize_to(v) do
326 v.serialize_attribute("name", name)
327 v.serialize_attribute("mtype", mclass.intro.bound_mtype.arguments[rank])
328 end
329 end
330
331 redef class MParameter
332 redef fun core_serialize_to(v) do
333 v.serialize_attribute("is_vararg", is_vararg)
334 v.serialize_attribute("name", name)
335 v.serialize_attribute("mtype", mtype)
336 end
337 end
338
339 # Fullname representation that can be used to build decorated links
340 #
341 # * MPackage: `mpackage_name`
342 # * MGroup: `(mpackage_name::)mgroup_name`
343 class JsonNamespace
344 super Array[nullable JsonRef]
345 serialize
346
347 redef fun to_s do return self.join("")
348 redef fun serialize_to(v) do to_a.serialize_to(v)
349 end
350
351 # Something that goes in a JsonNamespace
352 #
353 # Can be either:
354 # * a `RefToken` for tokens like `::`, `>` and `$`
355 # * a `MEntityRef` for references to mentities
356 interface JsonRef
357 super Serializable
358 end
359
360 # A reference to another mentity.
361 class MEntityRef
362 super JsonRef
363
364 # MEntity to link to.
365 var mentity: MEntity
366
367 redef fun core_serialize_to(v) do
368 mentity.core_serialize_base(v)
369 end
370 end
371
372 # A namespace token representation
373 #
374 # Used for namespace tokens like `::`, `>` and `$`
375 redef class String
376 super JsonRef
377 end
378
379 # Catalog
380
381 redef class MPackageMetadata
382 serialize
383
384 redef fun core_serialize_to(v) do
385 super
386 v.serialize_attribute("license", license)
387 v.serialize_attribute("maintainers", maintainers)
388 v.serialize_attribute("contributors", contributors)
389 v.serialize_attribute("tags", tags)
390 v.serialize_attribute("tryit", tryit)
391 v.serialize_attribute("apk", apk)
392 v.serialize_attribute("homepage", homepage)
393 v.serialize_attribute("browse", browse)
394 v.serialize_attribute("git", git)
395 v.serialize_attribute("issues", issues)
396 v.serialize_attribute("first_date", first_date)
397 v.serialize_attribute("last_date", last_date)
398 end
399 end
400
401 # Catalog statistics
402 redef class CatalogStats
403 serialize
404
405 redef fun core_serialize_to(v) do
406 super
407 v.serialize_attribute("packages", packages)
408 v.serialize_attribute("maintainers", maintainers)
409 v.serialize_attribute("contributors", contributors)
410 v.serialize_attribute("tags", tags)
411 v.serialize_attribute("modules", modules)
412 v.serialize_attribute("classes", classes)
413 v.serialize_attribute("methods", methods)
414 v.serialize_attribute("loc", loc)
415 end
416 end
417
418 # MPackage statistics for the catalog
419 redef class MPackageStats
420 serialize
421
422 redef fun core_serialize_to(v) do
423 super
424 v.serialize_attribute("mmodules", mmodules)
425 v.serialize_attribute("mclasses", mclasses)
426 v.serialize_attribute("mmethods", mmethods)
427 v.serialize_attribute("loc", loc)
428 v.serialize_attribute("errors", errors)
429 v.serialize_attribute("warnings", warnings)
430 v.serialize_attribute("warnings_per_kloc", warnings_per_kloc)
431 v.serialize_attribute("documentation_score", documentation_score)
432 v.serialize_attribute("commits", commits)
433 v.serialize_attribute("score", score)
434 end
435 end
436
437 redef class Person
438 serialize
439
440 redef fun core_serialize_to(v) do
441 super
442 v.serialize_attribute("name", name)
443 v.serialize_attribute("email", email)
444 v.serialize_attribute("gravatar", gravatar)
445 end
446 end