2670ebde9ab8f63761a4bac17a9f109f864af6ca
[nit.git] / src / doc / commands / commands_md.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 # Render commands results as Markdown
16 module commands_md
17
18 import commands::commands_graph
19 import commands::commands_usage
20 import commands::commands_ini
21 import commands::commands_main
22
23 import doc_down
24
25 redef class DocCommand
26
27 # Render results as a Markdown string
28 fun to_md: Writable do return "**Not yet implemented**"
29 end
30
31 redef class CmdMessage
32
33 # Render the message as a HTML string
34 fun to_md: Writable is abstract
35 end
36
37 redef class CmdError
38 redef fun to_md do return "**Error: {to_s}**"
39 end
40
41 redef class CmdWarning
42 redef fun to_md do return "**Warning: {to_s}**"
43 end
44
45 # Model commands
46
47 redef class CmdEntity
48 redef fun to_md do
49 var mentity = self.mentity
50 if mentity == null then return ""
51 return "`{mentity.name}`"
52 end
53 end
54
55 redef class CmdEntities
56 redef fun to_md do
57 var mentities = self.results
58 if mentities == null then return ""
59
60 var tpl = new Template
61 for mentity in mentities do
62 var mdoc = mentity.mdoc_or_fallback
63 tpl.add "* `{mentity}`"
64 if mdoc != null then
65 tpl.add " - "
66 tpl.add mdoc.synopsis
67 end
68 tpl.add "\n"
69 end
70 return tpl.write_to_string
71 end
72 end
73
74 redef class CmdComment
75 redef fun to_md do
76 var mentity = self.mentity
77 if mentity == null then return ""
78
79 var mdoc = self.mdoc
80 var tpl = new Template
81 tpl.add "### `{mentity}`"
82 if mdoc != null then
83 tpl.add " - "
84 tpl.add mdoc.synopsis
85 end
86 tpl.add "\n"
87 if mdoc != null then
88 tpl.add mdoc.comment
89 end
90 return tpl.write_to_string
91 end
92
93 redef fun render_comment do
94 var mdoc = self.mdoc
95 if mdoc == null then return null
96
97 if format == "md" then
98 if full_doc then return mdoc.md_documentation
99 return mdoc.md_synopsis
100 end
101 return super
102 end
103 end
104
105 redef class CmdEntityLink
106 redef fun to_md do
107 var mentity = self.mentity
108 if mentity == null then return ""
109 return "`{mentity}`"
110 end
111 end
112
113 redef class CmdCode
114 redef fun to_md do
115 var node = self.node
116 if node == null then return ""
117
118 var code = render_code(node)
119 var tpl = new Template
120 tpl.addn "~~~nit"
121 tpl.add code.write_to_string
122 tpl.addn "~~~"
123 return tpl.write_to_string
124 end
125
126 redef fun render_code(node) do
127 if format == "ansi" then
128 var hl = new AnsiHighlightVisitor
129 hl.highlight_node node
130 return hl.result
131 end
132 return super
133 end
134 end
135
136 redef class CmdAncestors
137 redef fun to_md do return super # FIXME lin
138 end
139
140 redef class CmdParents
141 redef fun to_md do return super # FIXME lin
142 end
143
144 redef class CmdChildren
145 redef fun to_md do return super # FIXME lin
146 end
147
148 redef class CmdDescendants
149 redef fun to_md do return super # FIXME lin
150 end
151
152 redef class CmdFeatures
153 redef fun to_md do return super # FIXME lin
154 end
155
156 redef class CmdLinearization
157 redef fun to_md do return super # FIXME lin
158 end
159
160 # Usage commands
161
162 redef class CmdNew
163 redef fun to_md do return super # FIXME lin
164 end
165
166 redef class CmdCall
167 redef fun to_md do return super # FIXME lin
168 end
169
170 redef class CmdReturn
171 redef fun to_md do return super # FIXME lin
172 end
173
174 redef class CmdParam
175 redef fun to_md do return super # FIXME lin
176 end
177
178 # Graph commands
179
180 redef class CmdGraph
181 redef fun to_md do
182 var output = render
183 if output == null then return ""
184 return output.write_to_string
185 end
186 end
187
188 # Ini commands
189
190 redef class CmdIniDescription
191 redef fun to_md do
192 var desc = self.desc
193 if desc == null then return ""
194
195 return desc
196 end
197 end
198
199 redef class CmdIniGitUrl
200 redef fun to_md do
201 var url = self.url
202 if url == null then return ""
203 return "[{url}]({url})"
204 end
205 end
206
207 redef class CmdIniCloneCommand
208 redef fun to_md do
209 var command = self.command
210 if command == null then return ""
211
212 var tpl = new Template
213 tpl.addn "~~~sh"
214 tpl.addn command
215 tpl.addn "~~~"
216 return tpl.write_to_string
217 end
218 end
219
220 redef class CmdIniIssuesUrl
221 redef fun to_md do
222 var url = self.url
223 if url == null then return ""
224 return "[{url}]({url})"
225 end
226 end
227
228 redef class CmdIniMaintainer
229 redef fun to_md do
230 var name = self.maintainer
231 if name == null then return ""
232 return "**{name}**"
233 end
234 end
235
236 redef class CmdIniContributors
237 redef fun to_md do
238 var names = self.contributors
239 if names == null or names.is_empty then return ""
240
241 var tpl = new Template
242 for name in names do
243 tpl.addn "* **{name}**"
244 end
245 return tpl.write_to_string
246 end
247 end
248
249 redef class CmdIniLicense
250 redef fun to_md do
251 var license = self.license
252 if license == null then return ""
253 return "[{license}](https://opensource.org/licenses/{license})"
254 end
255 end
256
257 redef class CmdEntityFile
258
259 # URL to the file
260 #
261 # Can be refined in subtools.
262 var file_url: nullable String = file is lazy, writable
263
264 redef fun to_md do
265 var file = self.file
266 if file == null then return ""
267 return "[{file.basename}]({file_url or else ""})"
268 end
269 end
270
271 redef class CmdEntityFileContent
272 redef fun to_md do
273 var content = self.content
274 if content == null then return ""
275
276 var tpl = new Template
277 tpl.addn "~~~"
278 tpl.add content
279 tpl.addn "~~~"
280 return tpl.write_to_string
281 end
282 end
283
284 # Main commands
285
286 redef class CmdMains
287 redef fun to_md do return super # FIXME lin
288 end
289
290 redef class CmdMainCompile
291 redef fun to_md do
292 var command = self.command
293 if command == null then return ""
294
295 var tpl = new Template
296 tpl.addn "~~~sh"
297 tpl.addn command
298 tpl.addn "~~~"
299 return tpl.write_to_string
300 end
301 end
302
303 redef class CmdManSynopsis
304 redef fun to_md do
305 var synopsis = self.synopsis
306 if synopsis == null then return ""
307
308 var tpl = new Template
309 tpl.addn "~~~"
310 tpl.addn synopsis
311 tpl.addn "~~~"
312 return tpl.write_to_string
313 end
314 end
315
316 redef class CmdManOptions
317 redef fun to_md do
318 var options = self.options
319 if options == null or options.is_empty then return ""
320
321 var tpl = new Template
322 tpl.addn "~~~"
323 for opt, desc in options do
324 tpl.addn "* {opt}\t\t{desc}"
325 end
326 tpl.addn "~~~"
327
328 return tpl.write_to_string
329 end
330 end
331
332 redef class CmdTesting
333 redef fun to_md do
334 var command = self.command
335 if command == null then return ""
336
337 var tpl = new Template
338 tpl.addn "~~~sh"
339 tpl.addn command
340 tpl.addn "~~~"
341 return tpl.write_to_string
342 end
343 end