doc/commands: generate CmdEntityLink to JSON
[nit.git] / src / doc / commands / commands_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 # Translate command results to json
16 module commands_json
17
18 import commands::commands_model
19 import commands::commands_graph
20 import commands::commands_usage
21 import commands::commands_catalog
22
23 import templates::templates_json
24 import catalog::catalog_json
25
26 redef class DocCommand
27 # Return a JSON Serializable representation of `self` results
28 fun to_json: nullable Serializable is abstract
29 end
30
31 # Message handling
32
33 redef class CmdMessage
34 # Return a JSON Serializable representation of `self`
35 fun to_json: nullable Serializable do
36 var obj = new JsonObject
37 obj["status"] = class_name
38 obj["message"] = to_s
39 return obj
40 end
41 end
42
43 redef class CmdEntity
44 redef fun to_json do return mentity
45 end
46
47 redef class CmdList
48 redef fun to_json do
49 var obj = new JsonObject
50 obj["results"] = results
51 obj["page"] = page
52 obj["count"] = count
53 obj["limit"] = limit
54 obj["max"] = max
55 return obj
56 end
57 end
58
59 redef class CmdEntityList
60 redef fun to_json do return super
61 end
62
63 # Model commands
64
65 redef class CmdComment
66 redef fun to_json do
67 var obj = new JsonObject
68 var render = self.render_comment
69 if render != null then
70 obj["documentation"] = render.write_to_string
71 end
72 return obj
73 end
74 end
75
76 redef class CmdEntityLink
77 redef fun to_json do
78 var obj = new JsonObject
79 var text = self.text
80 if text != null then obj["text"] = text
81 var title = self.title
82 if title != null then obj["title"] = title
83 return obj
84 end
85 end
86
87 redef class CmdEntityCode
88 redef fun to_json do
89 var obj = new JsonObject
90 var node = self.node
91 if node != null then
92 obj["location"] = node.location
93 end
94 var output = render_code(node)
95 if output != null then
96 obj["code"] = output.write_to_string
97 end
98 return obj
99 end
100 end
101
102 redef class CmdGraph
103 redef fun to_json do
104 var obj = new JsonObject
105 var output = render
106 if output != null then
107 obj["graph"] = output.write_to_string
108 end
109 return obj
110 end
111 end
112
113 redef class CmdMetadata
114 redef fun to_json do return metadata
115 end
116
117 # CmdCatalog
118
119 redef class CmdCatalogStats
120 redef fun to_json do return stats
121 end
122
123 redef class CmdCatalogTags
124 redef fun to_json do return packages_count_by_tags
125 end
126
127 redef class CmdCatalogTag
128 redef fun to_json do
129 var obj = super.as(JsonObject)
130 obj["tag"] = tag
131 return obj
132 end
133 end
134
135 redef class CmdCatalogPerson
136 redef fun to_json do return person
137 end
138
139 redef class CmdCatalogMaintaining
140 redef fun to_json do
141 var obj = new JsonObject
142 obj["person"] = person
143 obj["results"] = results
144 obj["page"] = page
145 obj["count"] = count
146 obj["limit"] = limit
147 obj["max"] = max
148 return obj
149 end
150 end
151
152 redef class CmdCatalogContributing
153 redef fun to_json do
154 var obj = new JsonObject
155 obj["person"] = person
156 obj["results"] = results
157 obj["page"] = page
158 obj["count"] = count
159 obj["limit"] = limit
160 obj["max"] = max
161 return obj
162 end
163 end