7a51f0692b1083e33b789ab3cc3e46c172c9b93c
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_url
do return "package_{html_id}.html"
108 redef fun html_namespace
do return html_link
109 redef fun html_icon
do return new BSIcon("book", ["text-muted"])
110 redef var css_classes
= ["public"]
114 redef fun html_url
do return "group_{html_id}.html"
115 redef fun html_icon
do return new BSIcon("folder-close", ["text-muted"])
117 redef fun html_namespace
do
118 var tpl
= new Template
119 var parent
= self.parent
120 if parent
!= null then
121 tpl
.add parent
.html_namespace
130 redef fun html_url
do return "module_{html_id}.html"
131 redef fun html_icon
do return new BSIcon("file", ["text-muted"])
133 redef fun html_namespace
do
134 var mpackage
= self.mpackage
135 var tpl
= new Template
136 if mpackage
!= null then
137 tpl
.add mpackage
.html_namespace
146 redef fun html_url
do return "class_{html_id}.html"
147 redef fun html_icon
do return new BSIcon("stop", css_classes
)
148 redef fun html_signature
(short
) do return intro
.html_signature
(short
)
149 redef fun css_classes
do return super + [visibility
.to_s
]
151 redef fun html_namespace
do
152 var mgroup
= intro_mmodule
.mgroup
153 var tpl
= new Template
154 if mgroup
!= null then
155 tpl
.add mgroup
.mpackage
.html_namespace
165 redef class MClassDef
166 redef fun html_url
do return "{mclass.html_url}#{html_id}"
167 redef fun css_classes
do return super + mclass
.css_classes
169 redef fun html_namespace
do
170 var tpl
= new Template
171 var mpackage
= mmodule
.mpackage
172 if mpackage
!= null and is_intro
then
174 tpl
.add mpackage
.html_namespace
177 tpl
.add mmodule
.html_namespace
179 var intro_mpackage
= mclass
.intro
.mmodule
.mpackage
180 if intro_mpackage
!= null and mpackage
!= intro_mpackage
then
181 tpl
.add intro_mpackage
.html_namespace
186 tpl
.add mmodule
.html_namespace
193 redef fun html_icon
do
195 return new BSIcon("plus", css_classes
)
197 return new BSIcon("asterisk", css_classes
)
200 redef fun html_signature
(short
) do
201 var tpl
= new Template
202 var mparameters
= mclass
.mparameters
203 if not mparameters
.is_empty
then
205 for i
in [0..mparameters
.length
[ do
206 tpl
.add mparameters
[i
].html_name
207 if short
== null or not short
then
209 tpl
.add bound_mtype
.arguments
[i
].html_signature
(short
)
211 if i
< mparameters
.length
- 1 then tpl
.add
", "
219 redef class MProperty
220 redef fun html_url
do return "property_{html_id}.html"
221 redef fun html_declaration
do return intro
.html_declaration
222 redef fun html_signature
(short
) do return intro
.html_signature
(short
)
223 redef fun html_icon
do return new BSIcon("tag", css_classes
)
224 redef fun css_classes
do return super + [visibility
.to_s
]
226 redef fun html_namespace
do
227 var tpl
= new Template
228 tpl
.add intro_mclassdef
.mclass
.html_namespace
230 tpl
.add intro
.html_link
236 redef fun html_url
do return "{mproperty.html_url}#{html_id}"
237 redef fun css_classes
do return super + mproperty
.css_classes
239 redef fun html_namespace
do
240 var tpl
= new Template
241 tpl
.add mclassdef
.html_namespace
247 redef fun html_icon
do
249 return new BSIcon("plus", css_classes
)
251 return new BSIcon("asterisk", css_classes
)
255 redef class MAttributeDef
256 redef fun html_signature
(short
) do
257 var static_mtype
= self.static_mtype
258 var tpl
= new Template
259 if static_mtype
!= null then
261 tpl
.add static_mtype
.html_signature
(short
)
267 redef class MMethodDef
268 redef fun html_signature
(short
) do
269 var new_msignature
= self.new_msignature
270 if mproperty
.is_root_init
and new_msignature
!= null then
271 return new_msignature
.html_signature
(short
)
273 return msignature
.as(not null).html_signature
(short
)
277 redef class MVirtualTypeProp
278 redef fun html_link
(text
, title
) do return mvirtualtype
.html_link
(text
, title
)
281 redef class MVirtualTypeDef
282 redef fun html_signature
(short
) do
283 var bound
= self.bound
284 var tpl
= new Template
285 if bound
== null then return tpl
287 tpl
.add bound
.html_signature
(short
)
293 redef fun html_signature
(short
) do return html_link
296 redef class MClassType
297 redef fun html_link
(text
, title
) do return mclass
.html_link
(text
, title
)
300 redef class MNullableType
301 redef fun html_signature
(short
) do
302 var tpl
= new Template
304 tpl
.add mtype
.html_signature
(short
)
309 redef class MGenericType
310 redef fun html_signature
(short
) do
312 var tpl
= new Template
313 tpl
.add
new Link(lnk
.href
, mclass
.name
.html_escape
, lnk
.title
)
315 for i
in [0..arguments
.length
[ do
316 tpl
.add arguments
[i
].html_signature
(short
)
317 if i
< arguments
.length
- 1 then tpl
.add
", "
324 redef class MParameterType
325 redef fun html_link
(text
, title
) do
326 if text
== null then text
= name
327 if title
== null then title
= "formal type"
328 return new Link("{mclass.html_url}#FT_{name.to_cmangle}", text
, title
)
332 redef class MVirtualType
333 redef fun html_link
(text
, title
) do return mproperty
.intro
.html_link
(text
, title
)
336 redef class MSignature
337 redef fun html_signature
(short
) do
338 var tpl
= new Template
339 if not mparameters
.is_empty
then
341 for i
in [0..mparameters
.length
[ do
342 tpl
.add mparameters
[i
].html_signature
(short
)
343 if i
< mparameters
.length
- 1 then tpl
.add
", "
347 if short
== null or not short
then
348 var return_mtype
= self.return_mtype
349 if return_mtype
!= null then
351 tpl
.add return_mtype
.html_signature
(short
)
358 redef class MParameter
359 redef fun html_signature
(short
) do
360 var tpl
= new Template
362 if short
== null or not short
then
364 tpl
.add mtype
.html_signature
(short
)
366 if is_vararg
then tpl
.add
"..."