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 # Translate mentities to html blocks.
18 import model
::model_collect
20 import html
::bootstrap
24 # The MEntity unique ID in the HTML output
25 var html_id
: String is lazy
do return full_name
.to_cmangle
27 # The MEntity URL in the HTML output
29 # You MUST redefine this method.
30 # Depending on your implementation, this URL can be a page URL or an anchor.
31 var html_url
: String is lazy
do return html_id
33 # The MEntity name escaped for HTML
34 var html_name
: String is lazy
do return name
.html_escape
36 # The MEntity `full_name` escaped for HTML
37 var html_full_name
: String is lazy
do return full_name
.html_escape
39 # Link to the MEntity in the HTML output
41 # You should redefine this method depending on the organization or your
43 fun html_link
(text
, title
: nullable String): Link do
47 var mdoc
= self.mdoc_or_fallback
48 if title
== null and mdoc
!= null then
49 title
= mdoc
.synopsis
.html_escape
51 return new Link(html_url
, text
, title
)
54 # Returns the complete MEntity declaration decorated with HTML
57 # * MPackage: `package foo`
58 # * MGroup: `group foo`
59 # * MModule: `module foo`
60 # * MClass: `private abstract class Foo[E: Object]`
61 # * MClassDef: `redef class Foo[E]`
62 # * MProperty: `private fun foo(e: Object): Int`
63 # * MPropdef: `redef fun foo(e)`
64 fun html_declaration
: Template do
65 var tpl
= new Template
66 tpl
.add
"<span class='signature'>"
67 for modifier
in collect_modifiers
do
68 tpl
.add
"<span class='modifier'>{modifier}</span> "
70 tpl
.add
"<span class='name'>{html_link.write_to_string}</span>"
71 tpl
.add html_signature
(false)
76 # Returns the MEntity signature decorated with HTML
78 # This function only returns the parenthesis and return types.
79 # See `html_declaration` for the full declaration including modifiers and name.
80 fun html_signature
(short
: nullable Bool): Template do return new Template
82 # Returns `full_name` decorated with HTML links
83 fun html_namespace
: Template is abstract
85 # An icon representative of the mentity
86 fun html_icon
: BSIcon do return new BSIcon("tag", ["text-muted"])
88 # A li element that can go in a `HTMLList`
89 fun html_list_item
: ListItem do
90 var tpl
= new Template
91 tpl
.add html_namespace
92 var comment
= mdoc_or_fallback
93 if comment
!= null then
95 tpl
.add comment
.html_synopsis
97 return new ListItem(tpl
)
100 # CSS classes used to decorate `self`
102 # Mainly used for icons.
103 var css_classes
: Array[String] = collect_modifiers
is lazy
107 redef fun html_namespace
do return html_link
108 redef fun html_icon
do return new BSIcon("book", ["text-muted"])
109 redef var css_classes
= ["public"]
113 redef fun html_icon
do return new BSIcon("folder-close", ["text-muted"])
115 redef fun html_namespace
do
116 var tpl
= new Template
117 var parent
= self.parent
118 if parent
!= null then
119 tpl
.add parent
.html_namespace
128 redef fun html_icon
do return new BSIcon("file", ["text-muted"])
130 redef fun html_namespace
do
131 var mpackage
= self.mpackage
132 var tpl
= new Template
133 if mpackage
!= null then
134 tpl
.add mpackage
.html_namespace
143 redef fun html_icon
do return new BSIcon("stop", css_classes
)
144 redef fun html_signature
(short
) do return intro
.html_signature
(short
)
145 redef fun css_classes
do return super + [visibility
.to_s
]
147 redef fun html_namespace
do
148 var mgroup
= intro_mmodule
.mgroup
149 var tpl
= new Template
150 if mgroup
!= null then
151 tpl
.add mgroup
.mpackage
.html_namespace
161 redef class MClassDef
162 redef fun css_classes
do return super + mclass
.css_classes
164 redef fun html_namespace
do
165 var tpl
= new Template
166 var mpackage
= mmodule
.mpackage
167 if mpackage
!= null and is_intro
then
169 tpl
.add mpackage
.html_namespace
172 tpl
.add mmodule
.html_namespace
174 var intro_mpackage
= mclass
.intro
.mmodule
.mpackage
175 if intro_mpackage
!= null and mpackage
!= intro_mpackage
then
176 tpl
.add intro_mpackage
.html_namespace
181 tpl
.add mmodule
.html_namespace
188 redef fun html_icon
do
190 return new BSIcon("plus", css_classes
)
192 return new BSIcon("asterisk", css_classes
)
195 redef fun html_signature
(short
) do
196 var tpl
= new Template
197 var mparameters
= mclass
.mparameters
198 if not mparameters
.is_empty
then
200 for i
in [0..mparameters
.length
[ do
201 tpl
.add mparameters
[i
].html_name
202 if short
== null or not short
then
204 tpl
.add bound_mtype
.arguments
[i
].html_signature
(short
)
206 if i
< mparameters
.length
- 1 then tpl
.add
", "
214 redef class MProperty
215 redef fun html_declaration
do return intro
.html_declaration
216 redef fun html_signature
(short
) do return intro
.html_signature
(short
)
217 redef fun html_icon
do return new BSIcon("tag", css_classes
)
218 redef fun css_classes
do return super + [visibility
.to_s
]
220 redef fun html_namespace
do
221 var tpl
= new Template
222 tpl
.add intro_mclassdef
.mclass
.html_namespace
224 tpl
.add intro
.html_link
230 redef fun css_classes
do return super + mproperty
.css_classes
232 redef fun html_namespace
do
233 var tpl
= new Template
234 tpl
.add mclassdef
.html_namespace
240 redef fun html_icon
do
242 return new BSIcon("plus", css_classes
)
244 return new BSIcon("asterisk", css_classes
)
248 redef class MAttributeDef
249 redef fun html_signature
(short
) do
250 var static_mtype
= self.static_mtype
251 var tpl
= new Template
252 if static_mtype
!= null then
254 tpl
.add static_mtype
.html_signature
(short
)
260 redef class MMethodDef
261 redef fun html_signature
(short
) do
262 var new_msignature
= self.new_msignature
263 if mproperty
.is_root_init
and new_msignature
!= null then
264 return new_msignature
.html_signature
(short
)
266 return msignature
.as(not null).html_signature
(short
)
270 redef class MVirtualTypeProp
271 redef fun html_link
(text
, title
) do return mvirtualtype
.html_link
(text
, title
)
274 redef class MVirtualTypeDef
275 redef fun html_signature
(short
) do
276 var bound
= self.bound
277 var tpl
= new Template
278 if bound
== null then return tpl
280 tpl
.add bound
.html_signature
(short
)
286 redef fun html_signature
(short
) do return html_link
289 redef class MClassType
290 redef fun html_link
(text
, title
) do return mclass
.html_link
(text
, title
)
293 redef class MNullableType
294 redef fun html_signature
(short
) do
295 var tpl
= new Template
297 tpl
.add mtype
.html_signature
(short
)
302 redef class MGenericType
303 redef fun html_signature
(short
) do
305 var tpl
= new Template
306 tpl
.add
new Link(lnk
.href
, mclass
.name
.html_escape
, lnk
.title
)
308 for i
in [0..arguments
.length
[ do
309 tpl
.add arguments
[i
].html_signature
(short
)
310 if i
< arguments
.length
- 1 then tpl
.add
", "
317 redef class MParameterType
318 redef fun html_link
(text
, title
) do
319 if text
== null then text
= name
320 if title
== null then title
= "formal type"
321 return new Link("{mclass.html_url}#FT_{name.to_cmangle}", text
, title
)
325 redef class MVirtualType
326 redef fun html_link
(text
, title
) do return mproperty
.intro
.html_link
(text
, title
)
329 redef class MSignature
330 redef fun html_signature
(short
) do
331 var tpl
= new Template
332 if not mparameters
.is_empty
then
334 for i
in [0..mparameters
.length
[ do
335 tpl
.add mparameters
[i
].html_signature
(short
)
336 if i
< mparameters
.length
- 1 then tpl
.add
", "
340 if short
== null or not short
then
341 var return_mtype
= self.return_mtype
342 if return_mtype
!= null then
344 tpl
.add return_mtype
.html_signature
(short
)
351 redef class MParameter
352 redef fun html_signature
(short
) do
353 var tpl
= new Template
355 if short
== null or not short
then
357 tpl
.add mtype
.html_signature
(short
)
359 if is_vararg
then tpl
.add
"..."