1 # This file is part of NIT ( http://www.nitlanguage.org ).
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
7 # http://www.apache.org/licenses/LICENSE-2.0
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.
15 # Render commands results as Markdown
18 import commands_catalog
26 redef class DocCommand
28 # Render results as a Markdown string
29 fun to_md
: Writable do return "**Not yet implemented**"
32 redef class CmdMessage
34 # Render the message as a HTML string
35 fun to_md
: Writable is abstract
39 redef fun to_md
do return "**Error: {to_s}**"
42 redef class CmdWarning
43 redef fun to_md
do return "**Warning: {to_s}**"
50 var mentity
= self.mentity
51 if mentity
== null then return ""
52 return "`{mentity.name}`"
56 redef class CmdEntities
58 var mentities
= self.results
59 if mentities
== null then return ""
61 var tpl
= new Template
62 for mentity
in mentities
do
63 var mdoc
= mentity
.mdoc_or_fallback
64 tpl
.add
"* `{mentity}`"
71 return tpl
.write_to_string
75 redef class CmdComment
77 var mentity
= self.mentity
78 if mentity
== null then return ""
81 var tpl
= new Template
82 tpl
.add
"### `{mentity}`"
91 return tpl
.write_to_string
94 redef fun render_comment
do
96 if mdoc
== null then return null
98 if format
== "md" then
99 if full_doc
then return mdoc
.md_documentation
100 return mdoc
.md_synopsis
106 redef class CmdEntityLink
108 var mentity
= self.mentity
109 if mentity
== null then return ""
117 if node
== null then return ""
119 var code
= render_code
(node
)
120 var tpl
= new Template
122 tpl
.add code
.write_to_string
124 return tpl
.write_to_string
127 redef fun render_code
(node
) do
128 if format
== "ansi" then
129 var hl
= new AnsiHighlightVisitor
130 hl
.highlight_node node
137 redef class CmdAncestors
138 redef fun to_md
do return super # FIXME lin
141 redef class CmdParents
142 redef fun to_md
do return super # FIXME lin
145 redef class CmdChildren
146 redef fun to_md
do return super # FIXME lin
149 redef class CmdDescendants
150 redef fun to_md
do return super # FIXME lin
153 redef class CmdFeatures
154 redef fun to_md
do return super # FIXME lin
157 redef class CmdLinearization
158 redef fun to_md
do return super # FIXME lin
164 redef fun to_md
do return super # FIXME lin
168 redef fun to_md
do return super # FIXME lin
171 redef class CmdReturn
172 redef fun to_md
do return super # FIXME lin
176 redef fun to_md
do return super # FIXME lin
184 if output
== null then return ""
185 return output
.write_to_string
191 redef class CmdIniDescription
194 if desc
== null then return ""
200 redef class CmdIniGitUrl
203 if url
== null then return ""
204 return "[{url}]({url})"
208 redef class CmdIniCloneCommand
210 var command
= self.command
211 if command
== null then return ""
213 var tpl
= new Template
217 return tpl
.write_to_string
221 redef class CmdIniIssuesUrl
224 if url
== null then return ""
225 return "[{url}]({url})"
229 redef class CmdIniMaintainer
231 var name
= self.maintainer
232 if name
== null then return ""
237 redef class CmdIniContributors
239 var names
= self.contributors
240 if names
== null or names
.is_empty
then return ""
242 var tpl
= new Template
244 tpl
.addn
"* **{name}**"
246 return tpl
.write_to_string
250 redef class CmdIniLicense
252 var license
= self.license
253 if license
== null then return ""
254 return "[{license}](https://opensource.org/licenses/{license})"
258 redef class CmdEntityFile
262 # Can be refined in subtools.
263 var file_url
: nullable String = file
is lazy
, writable
267 if file
== null then return ""
268 return "[{file.basename}]({file_url or else ""})"
272 redef class CmdEntityFileContent
274 var content
= self.content
275 if content
== null then return ""
277 var tpl
= new Template
281 return tpl
.write_to_string
288 redef fun to_md
do return super # FIXME lin
291 redef class CmdMainCompile
293 var command
= self.command
294 if command
== null then return ""
296 var tpl
= new Template
300 return tpl
.write_to_string
304 redef class CmdManSynopsis
306 var synopsis
= self.synopsis
307 if synopsis
== null then return ""
309 var tpl
= new Template
313 return tpl
.write_to_string
317 redef class CmdManOptions
319 var options
= self.options
320 if options
== null or options
.is_empty
then return ""
322 var tpl
= new Template
324 for opt
, desc
in options
do
325 tpl
.addn
"* {opt}\t\t{desc}"
329 return tpl
.write_to_string
333 redef class CmdTesting
335 var command
= self.command
336 if command
== null then return ""
338 var tpl
= new Template
342 return tpl
.write_to_string
350 # Renders the synopsis as a HTML comment block.
351 var md_synopsis
: Writable is lazy
do
352 if content
.is_empty
then return ""
357 var md_comment
: Writable is lazy
do
358 if content
.is_empty
then return ""
359 var lines
= content
.to_a
361 return lines
.join
("\n")
364 # Renders the synopsis and the comment as a HTML comment block.
365 var md_documentation
: Writable is lazy
do return lines_to_md
(content
.to_a
)
367 private fun lines_to_md
(lines
: Array[String]): Writable do
368 var res
= new Template
369 if not lines
.is_empty
then
370 var syn
= lines
.first
371 if not syn
.has_prefix
(" ") and not syn
.has_prefix
("\t") and
372 not syn
.trim
.has_prefix
("#") then
377 res
.add lines
.join
("\n")