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
: Link do
45 var mdoc
= self.mdoc_or_fallback
47 title
= mdoc
.synopsis
.html_escape
49 return new Link(html_url
, html_name
, title
)
52 # Returns the complete MEntity declaration decorated with HTML
55 # * MPackage: `package foo`
56 # * MGroup: `group foo`
57 # * MModule: `module foo`
58 # * MClass: `private abstract class Foo[E: Object]`
59 # * MClassDef: `redef class Foo[E]`
60 # * MProperty: `private fun foo(e: Object): Int`
61 # * MPropdef: `redef fun foo(e)`
62 fun html_declaration
: Template do
63 var tpl
= new Template
64 tpl
.add
"<span class='signature'>"
65 for modifier
in collect_modifiers
do
66 tpl
.add
"<span class='modifier'>{modifier}</span> "
68 tpl
.add
"<span class='name'>{html_link.write_to_string}</span>"
69 tpl
.add html_signature
(false)
74 # Returns the MEntity signature decorated with HTML
76 # This function only returns the parenthesis and return types.
77 # See `html_declaration` for the full declaration including modifiers and name.
78 fun html_signature
(short
: nullable Bool): Template do return new Template
80 # Returns `full_name` decorated with HTML links
81 fun html_namespace
: Template is abstract
83 # An icon representative of the mentity
84 fun html_icon
: BSIcon do return new BSIcon("tag", ["text-muted"])
86 # A li element that can go in a `HTMLList`
87 fun html_list_item
: ListItem do
88 var tpl
= new Template
89 tpl
.add html_namespace
90 var comment
= mdoc_or_fallback
91 if comment
!= null then
93 tpl
.add comment
.html_synopsis
95 return new ListItem(tpl
)
98 # CSS classes used to decorate `self`
100 # Mainly used for icons.
101 var css_classes
: Array[String] = collect_modifiers
is lazy
105 redef fun html_url
do return "package_{html_id}.html"
106 redef fun html_namespace
do return html_link
107 redef fun html_icon
do return new BSIcon("book", ["text-muted"])
108 redef var css_classes
= ["public"]
112 redef fun html_url
do return "group_{html_id}.html"
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_url
do return "module_{html_id}.html"
129 redef fun html_icon
do return new BSIcon("file", ["text-muted"])
131 redef fun html_namespace
do
132 var mpackage
= self.mpackage
133 var tpl
= new Template
134 if mpackage
!= null then
135 tpl
.add mpackage
.html_namespace
144 redef fun html_url
do return "class_{html_id}.html"
145 redef fun html_icon
do return new BSIcon("stop", css_classes
)
146 redef fun html_signature
(short
) do return intro
.html_signature
(short
)
147 redef fun css_classes
do return super + [visibility
.to_s
]
149 redef fun html_namespace
do
150 var mgroup
= intro_mmodule
.mgroup
151 var tpl
= new Template
152 if mgroup
!= null then
153 tpl
.add mgroup
.mpackage
.html_namespace
163 redef class MClassDef
164 redef fun html_url
do return "{mclass.html_url}#{html_id}"
165 redef fun css_classes
do return super + mclass
.css_classes
167 redef fun html_namespace
do
168 var tpl
= new Template
169 var mpackage
= mmodule
.mpackage
170 if mpackage
!= null and is_intro
then
172 tpl
.add mpackage
.html_namespace
175 tpl
.add mmodule
.html_namespace
177 var intro_mpackage
= mclass
.intro
.mmodule
.mpackage
178 if intro_mpackage
!= null and mpackage
!= intro_mpackage
then
179 tpl
.add intro_mpackage
.html_namespace
184 tpl
.add mmodule
.html_namespace
191 redef fun html_icon
do
193 return new BSIcon("plus", css_classes
)
195 return new BSIcon("asterisk", css_classes
)
198 redef fun html_signature
(short
) do
199 var tpl
= new Template
200 var mparameters
= mclass
.mparameters
201 if not mparameters
.is_empty
then
203 for i
in [0..mparameters
.length
[ do
204 tpl
.add mparameters
[i
].html_name
205 if short
== null or not short
then
207 tpl
.add bound_mtype
.arguments
[i
].html_signature
(short
)
209 if i
< mparameters
.length
- 1 then tpl
.add
", "
217 redef class MProperty
218 redef fun html_url
do return "property_{html_id}.html"
219 redef fun html_declaration
do return intro
.html_declaration
220 redef fun html_signature
(short
) do return intro
.html_signature
(short
)
221 redef fun html_icon
do return new BSIcon("tag", css_classes
)
222 redef fun css_classes
do return super + [visibility
.to_s
]
224 redef fun html_namespace
do
225 var tpl
= new Template
226 tpl
.add intro_mclassdef
.mclass
.html_namespace
228 tpl
.add intro
.html_link
234 redef fun html_url
do return "{mproperty.html_url}#{html_id}"
235 redef fun css_classes
do return super + mproperty
.css_classes
237 redef fun html_namespace
do
238 var tpl
= new Template
239 tpl
.add mclassdef
.html_namespace
245 redef fun html_icon
do
247 return new BSIcon("plus", css_classes
)
249 return new BSIcon("asterisk", css_classes
)
253 redef class MAttributeDef
254 redef fun html_signature
(short
) do
255 var static_mtype
= self.static_mtype
256 var tpl
= new Template
257 if static_mtype
!= null then
259 tpl
.add static_mtype
.html_signature
(short
)
265 redef class MMethodDef
266 redef fun html_signature
(short
) do
267 var new_msignature
= self.new_msignature
268 if mproperty
.is_root_init
and new_msignature
!= null then
269 return new_msignature
.html_signature
(short
)
271 return msignature
.as(not null).html_signature
(short
)
275 redef class MVirtualTypeProp
276 redef fun html_link
do return mvirtualtype
.html_link
279 redef class MVirtualTypeDef
280 redef fun html_signature
(short
) do
281 var bound
= self.bound
282 var tpl
= new Template
283 if bound
== null then return tpl
285 tpl
.add bound
.html_signature
(short
)
291 redef fun html_signature
(short
) do return html_link
294 redef class MClassType
295 redef fun html_link
do return mclass
.html_link
298 redef class MNullableType
299 redef fun html_signature
(short
) do
300 var tpl
= new Template
302 tpl
.add mtype
.html_signature
(short
)
307 redef class MGenericType
308 redef fun html_signature
(short
) do
310 var tpl
= new Template
311 tpl
.add
new Link(lnk
.href
, mclass
.name
.html_escape
, lnk
.title
)
313 for i
in [0..arguments
.length
[ do
314 tpl
.add arguments
[i
].html_signature
(short
)
315 if i
< arguments
.length
- 1 then tpl
.add
", "
322 redef class MParameterType
323 redef fun html_link
do
324 return new Link("{mclass.html_url}#FT_{name.to_cmangle}", name
, "formal type")
328 redef class MVirtualType
329 redef fun html_link
do return mproperty
.intro
.html_link
332 redef class MSignature
333 redef fun html_signature
(short
) do
334 var tpl
= new Template
335 if not mparameters
.is_empty
then
337 for i
in [0..mparameters
.length
[ do
338 tpl
.add mparameters
[i
].html_signature
(short
)
339 if i
< mparameters
.length
- 1 then tpl
.add
", "
343 if short
== null or not short
then
344 var return_mtype
= self.return_mtype
345 if return_mtype
!= null then
347 tpl
.add return_mtype
.html_signature
(short
)
354 redef class MParameter
355 redef fun html_signature
(short
) do
356 var tpl
= new Template
358 if short
== null or not short
then
360 tpl
.add mtype
.html_signature
(short
)
362 if is_vararg
then tpl
.add
"..."