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