e00a02486e8b6fdbb388c96fa6e0634503140a0d
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 HTML
18 import commands_catalog
22 import commands_parser
25 import templates
::templates_html
26 intrude import markdown
::wikilinks
28 redef class DocCommand
30 # Render results as a HTML string
31 fun to_html
: Writable do return "<p class='text-danger'>Not yet implemented</p>"
34 redef class CmdMessage
36 # Render the message as a HTML string
37 fun to_html
: Writable is abstract
41 redef fun to_html
do return "<p class='text-danger'>Error: {to_s}</p>"
44 redef class CmdWarning
45 redef fun to_html
do return "<p class='text-warning'>Warning: {to_s}</p>"
52 var mentity
= self.mentity
53 if mentity
== null then return ""
54 return mentity
.html_link
.write_to_string
58 redef class CmdEntities
60 var mentities
= self.results
61 if mentities
== null then return ""
63 var tpl
= new Template
65 for mentity
in mentities
do
66 var mdoc
= mentity
.mdoc_or_fallback
68 tpl
.add mentity
.html_link
71 tpl
.add mdoc
.html_synopsis
76 return tpl
.write_to_string
80 redef class CmdComment
82 var mentity
= self.mentity
83 if mentity
== null then return ""
86 var tpl
= new Template
88 # FIXME comments left here until I figure out what to do about the presentation options
89 # if not opts.has_key("no-link") then
90 tpl
.add mentity
.html_link
93 # if not opts.has_key("no-link") and not opts.has_key("no-synopsis") then
96 # if not opts.has_key("no-synopsis") then
97 tpl
.add mdoc
.html_synopsis
102 # if not opts.has_key("no-comment") then
103 tpl
.add mdoc
.html_comment
106 return tpl
.write_to_string
109 redef fun render_comment
do
111 if mdoc
== null then return null
113 if format
== "html" then
114 if full_doc
then return mdoc
.html_documentation
115 return mdoc
.html_synopsis
121 redef class CmdEntityLink
123 var mentity
= self.mentity
124 if mentity
== null then return ""
125 return mentity
.html_link
(text
, title
).write_to_string
132 if node
== null then return ""
134 var code
= render_code
(node
)
135 return "<pre>{code.write_to_string}</pre>"
138 redef fun render_code
(node
) do
139 if format
== "html" then
140 var hl
= new CmdHtmlightVisitor
141 hl
.show_infobox
= false
142 hl
.highlight_node node
149 # Custom HtmlightVisitor for commands
151 # We create a new subclass so its behavior can be refined in clients without
152 # breaking the main implementation.
153 class CmdHtmlightVisitor
154 super HtmlightVisitor
156 redef fun hrefto
(mentity
) do
157 if mentity
isa MClassDef then return mentity
.mclass
.html_url
158 if mentity
isa MPropDef then return mentity
.mproperty
.html_url
159 return mentity
.html_url
163 redef class CmdAncestors
164 redef fun to_html
do return super # FIXME lin
167 redef class CmdParents
168 redef fun to_html
do return super # FIXME lin
171 redef class CmdChildren
172 redef fun to_html
do return super # FIXME lin
175 redef class CmdDescendants
176 redef fun to_html
do return super # FIXME lin
179 redef class CmdFeatures
180 redef fun to_html
do return super # FIXME lin
183 redef class CmdLinearization
184 redef fun to_html
do return super # FIXME lin
190 redef fun to_html
do return super # FIXME lin
194 redef fun to_html
do return super # FIXME lin
197 redef class CmdReturn
198 redef fun to_html
do return super # FIXME lin
202 redef fun to_html
do return super # FIXME lin
210 if output
== null then return ""
211 return output
.write_to_string
217 redef class CmdIniDescription
220 if desc
== null then return ""
222 return "<p>{desc}</p>"
226 redef class CmdIniGitUrl
229 if url
== null then return ""
231 return "<a href=\"{url}\
">{url}</a>"
235 redef class CmdIniCloneCommand
237 var command
= self.command
238 if command
== null then return ""
240 return "<pre>{command}</pre>"
244 redef class CmdIniIssuesUrl
247 if url
== null then return ""
249 return "<a href=\"{url}\
">{url}</a>"
253 redef class CmdIniMaintainer
255 var name
= self.maintainer
256 if name
== null then return ""
258 return "<b>{name.html_escape}</b>"
262 redef class CmdIniContributors
264 var names
= self.contributors
265 if names
== null or names
.is_empty
then return ""
267 var tpl
= new Template
270 tpl
.add
"<li><b>{name.html_escape}</b></li>"
273 return tpl
.write_to_string
277 redef class CmdIniLicense
279 var license
= self.license
280 if license
== null then return ""
282 return "<a href=\"https
://opensource
.org
/licenses
/{license}\
">{license}</a>"
286 redef class CmdEntityFile
290 # Can be refined in subtools.
291 var file_url
: nullable String = file
is lazy
, writable
295 if file
== null then return ""
297 return "<a href=\"{file_url or else ""}\
">{file.basename}</a>"
301 redef class CmdEntityFileContent
303 var content
= self.content
304 if content
== null then return ""
306 return "<pre>{content}</pre>"
313 redef fun to_html
do return super # FIXME lin
316 redef class CmdMainCompile
318 var command
= self.command
319 if command
== null then return ""
321 return "<pre>{command}</pre>"
325 redef class CmdManSynopsis
327 var synopsis
= self.synopsis
328 if synopsis
== null then return ""
330 return "<pre>{synopsis}</pre>"
334 redef class CmdManOptions
336 var options
= self.options
337 if options
== null or options
.is_empty
then return ""
339 var tpl
= new Template
341 tpl
.addn
"<table width='100%'>"
342 for opt
, desc
in options
do
344 tpl
.addn
"<th valign='top' width='30%'>{opt}</th>"
345 tpl
.addn
"<td width='70%'>{desc}</td>"
351 return tpl
.write_to_string
355 redef class CmdTesting
357 var command
= self.command
358 if command
== null then return ""
360 return "<pre>{command}</pre>"
366 # Custom Markdown processor able to process doc commands
368 super NitdocDecorator
370 redef type PROCESSOR: CmdMarkdownProcessor
372 # Model used by wikilink commands to find entities
375 # Filter to apply if any
376 var filter
: nullable ModelFilter
378 redef fun add_span_code
(v
, buffer
, from
, to
) do
379 var text
= new FlatBuffer
380 buffer
.read
(text
, from
, to
)
381 var name
= text
.write_to_string
382 name
= name
.replace
("nullable ", "")
383 var mentity
= try_find_mentity
(name
)
384 if mentity
== null then
388 v
.emit_text mentity
.html_link
.write_to_string
393 private fun try_find_mentity
(text
: String): nullable MEntity do
394 var mentity
= model
.mentity_by_full_name
(text
, filter
)
395 if mentity
!= null then return mentity
397 var mentities
= model
.mentities_by_name
(text
, filter
)
398 if mentities
.is_empty
then
400 else if mentities
.length
> 1 then
401 # TODO smart resolve conflicts
403 return mentities
.first
406 redef fun add_wikilink
(v
, token
) do
407 v
.render_wikilink
(token
, model
)
411 # Same as `InlineDecorator` but with wikilink commands handling
412 class CmdInlineDecorator
413 super InlineDecorator
415 redef type PROCESSOR: CmdMarkdownProcessor
417 # Model used by wikilink commands to find entities
420 redef fun add_wikilink
(v
, token
) do
421 v
.render_wikilink
(token
, model
)
425 # Custom MarkdownEmitter for commands
426 class CmdMarkdownProcessor
427 super MarkdownProcessor
429 # Parser used to process doc commands
430 var parser
: CommandParser
433 fun render_wikilink
(token
: TokenWikiLink, model
: Model) do
434 var link
= token
.link
435 if link
== null then return
436 var name
= token
.name
437 if name
!= null then link
= "{name} | {link}"
439 var command
= parser
.parse
(link
.write_to_string
)
440 var error
= parser
.error
442 if error
isa CmdError then
443 emit_text error
.to_html
.write_to_string
446 if error
isa CmdWarning then
447 emit_text error
.to_html
.write_to_string
449 add command
.as(not null).to_html
454 # Read `self` between `nstart` and `nend` (excluded) and writte chars to `out`.
455 private fun read
(out
: FlatBuffer, nstart
, nend
: Int): Int do
457 while pos
< length
and pos
< nend
do
461 if pos
== length
then return -1