src/doc/commands: refactor `CmdCode` and `CmdEntityCode`
[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 import commands::commands_ini
23 import commands::commands_main
24
25 import templates::templates_json
26 import catalog::catalog_json
27
28 redef class DocCommand
29 # Return a JSON Serializable representation of `self` results
30 fun to_json: nullable Serializable is abstract
31 end
32
33 # Message handling
34
35 redef class CmdMessage
36 # Return a JSON Serializable representation of `self`
37 fun to_json: nullable Serializable do
38 var obj = new JsonObject
39 obj["status"] = class_name
40 obj["message"] = to_s
41 return obj
42 end
43 end
44
45 redef class CmdEntity
46 redef fun to_json do return mentity
47 end
48
49 redef class CmdList
50 redef fun to_json do
51 var obj = new JsonObject
52 obj["results"] = results
53 obj["page"] = page
54 obj["count"] = count
55 obj["limit"] = limit
56 obj["max"] = max
57 return obj
58 end
59 end
60
61 redef class CmdEntityList
62 redef fun to_json do return super
63 end
64
65 # Model commands
66
67 redef class CmdComment
68 redef fun to_json do
69 var obj = new JsonObject
70 var render = self.render_comment
71 if render != null then
72 obj["documentation"] = render.write_to_string
73 end
74 return obj
75 end
76 end
77
78 redef class CmdEntityLink
79 redef fun to_json do
80 var obj = new JsonObject
81 var text = self.text
82 if text != null then obj["text"] = text
83 var title = self.title
84 if title != null then obj["title"] = title
85 return obj
86 end
87 end
88
89 redef class CmdCode
90 redef fun to_json do
91 var obj = new JsonObject
92 var node = self.node
93 if node != null then
94 obj["location"] = node.location
95 end
96 var output = render_code(node)
97 if output != null then
98 obj["code"] = output.write_to_string
99 end
100 return obj
101 end
102 end
103
104 redef class CmdGraph
105 redef fun to_json do
106 var obj = new JsonObject
107 var output = render
108 if output != null then
109 obj["graph"] = output.write_to_string
110 end
111 return obj
112 end
113 end
114
115 redef class CmdMetadata
116 redef fun to_json do return metadata
117 end
118
119 # CmdCatalog
120
121 redef class CmdCatalogStats
122 redef fun to_json do return stats
123 end
124
125 redef class CmdCatalogTags
126 redef fun to_json do return packages_count_by_tags
127 end
128
129 redef class CmdCatalogTag
130 redef fun to_json do
131 var obj = super.as(JsonObject)
132 obj["tag"] = tag
133 return obj
134 end
135 end
136
137 redef class CmdCatalogPerson
138 redef fun to_json do return person
139 end
140
141 redef class CmdCatalogMaintaining
142 redef fun to_json do
143 var obj = new JsonObject
144 obj["person"] = person
145 obj["results"] = results
146 obj["page"] = page
147 obj["count"] = count
148 obj["limit"] = limit
149 obj["max"] = max
150 return obj
151 end
152 end
153
154 redef class CmdCatalogContributing
155 redef fun to_json do
156 var obj = new JsonObject
157 obj["person"] = person
158 obj["results"] = results
159 obj["page"] = page
160 obj["count"] = count
161 obj["limit"] = limit
162 obj["max"] = max
163 return obj
164 end
165 end
166
167 # CmdIni
168
169 redef class CmdIniDescription
170 redef fun to_json do
171 var obj = new JsonObject
172 obj["desc"] = desc
173 return obj
174 end
175 end
176
177 redef class CmdIniGitUrl
178 redef fun to_json do
179 var obj = new JsonObject
180 obj["url"] = url
181 return obj
182 end
183 end
184
185 redef class CmdIniCloneCommand
186 redef fun to_json do
187 var obj = new JsonObject
188 obj["command"] = command
189 return obj
190 end
191 end
192
193 redef class CmdIniIssuesUrl
194 redef fun to_json do
195 var obj = new JsonObject
196 obj["url"] = url
197 return obj
198 end
199 end
200
201 redef class CmdIniMaintainer
202 redef fun to_json do
203 var obj = new JsonObject
204 obj["maintainer"] = maintainer
205 return obj
206 end
207 end
208
209 redef class CmdIniContributors
210 redef fun to_json do
211 var obj = new JsonObject
212 obj["contributors"] = contributors
213 return obj
214 end
215 end
216
217 redef class CmdIniLicense
218 redef fun to_json do
219 var obj = new JsonObject
220 obj["license"] = license
221 return obj
222 end
223 end
224
225 redef class CmdLicenseFile
226 redef fun to_json do
227 var obj = new JsonObject
228 obj["file"] = file
229 return obj
230 end
231 end
232
233 redef class CmdLicenseFileContent
234 redef fun to_json do
235 var obj = super.as(JsonObject)
236 obj["content"] = content
237 return obj
238 end
239 end
240
241 redef class CmdContribFile
242 redef fun to_json do
243 var obj = new JsonObject
244 obj["file"] = file
245 return obj
246 end
247 end
248
249 redef class CmdContribFileContent
250 redef fun to_json do
251 var obj = super.as(JsonObject)
252 obj["content"] = content
253 return obj
254 end
255 end
256
257 # CmdMain
258
259 redef class CmdMains
260 redef fun to_json do
261 var obj = new JsonObject
262 obj["results"] = results
263 return obj
264 end
265 end
266
267 redef class CmdMainCompile
268 redef fun to_json do
269 var obj = new JsonObject
270 obj["command"] = command
271 return obj
272 end
273 end
274
275 redef class CmdTesting
276 redef fun to_json do
277 var obj = new JsonObject
278 obj["command"] = command
279 return obj
280 end
281 end
282
283 redef class CmdManSynopsis
284 redef fun to_json do
285 var obj = new JsonObject
286 obj["synopsis"] = synopsis
287 return obj
288 end
289 end
290
291 redef class CmdManOptions
292 redef fun to_json do
293 var obj = new JsonObject
294 obj["options"] = options
295 return obj
296 end
297 end