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.
20 import html
::bootstrap
24 # Returns the MEntity name escaped for html.
30 # * MClassDef: `Foo[E]`
31 # * MProperty: `foo(e)`
32 # * MPropdef: `foo(e)`
33 var html_name
: String is lazy
do return name
.html_escape
35 # Returns the MEntity full_name escaped for html.
36 var html_full_name
: String is lazy
do return full_name
.html_escape
38 # Link to MEntity in the web server.
39 # TODO this should be parameterizable... but how?
40 fun html_link
: Link do return new Link("/doc/{full_name}", html_name
)
42 # Returns the list of keyword used in `self` declaration.
43 fun html_modifiers
: Array[String] is abstract
45 # Returns the complete MEntity declaration decorated with HTML.
47 # * MPackage: `package foo`
48 # * MGroup: `group foo`
49 # * MModule: `module foo`
50 # * MClass: `private abstract class Foo[E: Object]`
51 # * MClassDef: `redef class Foo[E]`
52 # * MProperty: `private fun foo(e: Object): Int`
53 # * MPropdef: `redef fun foo(e)`
54 fun html_declaration
: Template do
55 var tpl
= new Template
57 tpl
.add html_modifiers
.join
(" ")
64 # Returns `self` namespace decorated with HTML links.
66 # * MPackage: `mpackage`
67 # * MGroup: `mpackage(::group)`
68 # * MModule: `mgroup::mmodule`
69 # * MClass: `mpackage::mclass`
70 # * MClassDef: `mmodule::mclassdef`
71 # * MProperty: `mclass::mprop`
72 # * MPropdef: `mclassdef:mpropdef`
73 fun html_namespace
: Template is abstract
75 # Returns the synopsis and the comment of this MEntity formatted as HTML.
76 var html_documentation
: nullable Writable is lazy
do
77 var mdoc
= mdoc_or_fallback
78 if mdoc
== null then return null
79 return mdoc
.html_documentation
82 # Returns the synopsis of this MEntity formatted as HTML.
83 var html_synopsis
: nullable Writable is lazy
do
84 var mdoc
= mdoc_or_fallback
85 if mdoc
== null then return null
86 return mdoc
.html_synopsis
89 # Returns the the comment without the synopsis formatted as HTML.
90 var html_comment
: nullable Writable is lazy
do
91 var mdoc
= mdoc_or_fallback
92 if mdoc
== null then return null
93 return mdoc
.html_comment
96 # Icon that will be displayed before the title
97 fun html_icon
: BSIcon do
98 var icon
= new BSIcon("tag")
99 icon
.css_classes
.add_all
(css_classes
)
103 # CSS classes used to decorate `self`.
105 # Mainly used for icons.
106 var css_classes
= new Array[String]
110 redef var html_modifiers
= ["package"]
111 redef fun html_namespace
do return html_link
112 redef var css_classes
= ["public"]
116 redef var html_modifiers
= ["group"]
118 # Depends if `self` is root or not.
120 # * If root `mpackage`.
121 # * Else `mpackage::self`.
122 redef fun html_namespace
do
123 var tpl
= new Template
124 tpl
.add mpackage
.html_namespace
125 if mpackage
.root
!= self then
132 redef var css_classes
= ["public"]
137 redef var html_modifiers
= ["module"]
139 # Depends if `self` belongs to a MGroup.
141 # * If mgroup `mgroup::self`.
143 redef fun html_namespace
do
144 var mgroup
= self.mgroup
145 var tpl
= new Template
146 if mgroup
!= null then
147 tpl
.add mgroup
.html_namespace
154 redef var css_classes
= ["public"]
159 redef var html_name
is lazy
do
160 var tpl
= new Template
161 tpl
.add name
.html_escape
164 var parameter_names
= new Array[String]
165 for p
in mparameters
do
166 parameter_names
.add
(p
.html_name
)
168 tpl
.add parameter_names
.join
(", ")
171 return tpl
.write_to_string
174 redef fun html_modifiers
do return intro
.html_modifiers
175 redef fun html_declaration
do return intro
.html_declaration
177 # Returns `mpackage::self`.
178 redef fun html_namespace
do
179 var mgroup
= intro_mmodule
.mgroup
180 var tpl
= new Template
181 if mgroup
!= null then
182 tpl
.add mgroup
.mpackage
.html_namespace
191 # Returns `intro.html_short_signature`.
192 fun html_short_signature
: Template do return intro
.html_short_signature
194 # Returns `intro.html_signature`.
195 fun html_signature
: Template do return intro
.html_signature
197 redef fun html_icon
do return intro
.html_icon
198 redef fun css_classes
do return intro
.css_classes
201 redef class MClassDef
202 # Depends if `self` is an intro or not.
204 # * If intro contains the visibility and kind.
205 # * If redef contains the `redef` keyword and kind.
206 redef fun html_modifiers
do
207 var res
= new Array[String]
211 if mclass
.visibility
!= public_visibility
then
212 res
.add mclass
.visibility
.to_s
215 res
.add mclass
.kind
.to_s
219 # Depends if `self` is an intro or not.
221 # For intro: `private abstract class Foo[E: Object]`
222 # For redef: `redef class Foo[E]`
223 redef fun html_declaration
do
224 var tpl
= new Template
226 tpl
.add html_modifiers
.join
(" ")
230 tpl
.add html_signature
232 tpl
.add html_short_signature
238 # Returns `mmodule::self`
239 redef fun html_namespace
do
240 var tpl
= new Template
241 tpl
.add mmodule
.html_namespace
243 tpl
.add mclass
.html_link
248 # Returns the MClassDef generic signature without static bounds.
249 fun html_short_signature
: Template do
250 var tpl
= new Template
251 var mparameters
= mclass
.mparameters
252 if not mparameters
.is_empty
then
254 for i
in [0..mparameters
.length
[ do
255 tpl
.add mparameters
[i
].html_name
256 if i
< mparameters
.length
- 1 then tpl
.add
", "
263 # Returns the MClassDef generic signature with static bounds.
264 fun html_signature
: Template do
265 var tpl
= new Template
266 var mparameters
= mclass
.mparameters
267 if not mparameters
.is_empty
then
269 for i
in [0..mparameters
.length
[ do
270 tpl
.add
"{mparameters[i].html_name}: "
271 tpl
.add bound_mtype
.arguments
[i
].html_signature
272 if i
< mparameters
.length
- 1 then tpl
.add
", "
279 redef fun css_classes
do
280 var set
= new HashSet[String]
281 if is_intro
then set
.add
"intro"
282 for m
in mclass
.intro
.modifiers
do set
.add m
.to_cmangle
283 for m
in modifiers
do set
.add m
.to_cmangle
288 # List of all modifiers like redef, private etc.
289 var modifiers
: Array[String] is lazy
do
290 var res
= new Array[String]
294 res
.add mclass
.visibility
.to_s
296 res
.add mclass
.kind
.to_s
301 redef class MProperty
302 redef fun html_modifiers
do return intro
.html_modifiers
303 redef fun html_declaration
do return intro
.html_declaration
305 # Returns `mclass::self`.
306 redef fun html_namespace
do
307 var tpl
= new Template
308 tpl
.add intro_mclassdef
.mclass
.html_namespace
310 tpl
.add intro
.html_link
315 # Returns `intro.html_short_signature`.
316 fun html_short_signature
: Template do return intro
.html_short_signature
318 # Returns `intro.html_signature`.
319 fun html_signature
: Template do return intro
.html_signature
321 redef fun css_classes
do return intro
.css_classes
325 # Depends if `self` is an intro or not.
327 # * If intro contains the visibility and kind.
328 # * If redef contains the `redef` keyword and kind.
329 redef fun html_modifiers
do
330 var res
= new Array[String]
334 if mproperty
.visibility
!= public_visibility
then
335 res
.add mproperty
.visibility
.to_s
341 # Depends if `self` is an intro or not.
343 # For intro: `private fun foo(e: Object): Bar is abstract`
344 # For redef: `redef fun foo(e) is cached`
345 redef fun html_declaration
do
346 var tpl
= new Template
348 tpl
.add html_modifiers
.join
(" ")
352 tpl
.add html_signature
354 tpl
.add mproperty
.intro
.html_link
355 tpl
.add html_short_signature
361 # Returns `mclassdef::self`
362 redef fun html_namespace
do
363 var tpl
= new Template
364 tpl
.add mclassdef
.html_namespace
370 # Returns the MPropdDef signature without static types.
371 fun html_short_signature
: Template is abstract
373 # Returns the MPropDef signature with static types.
374 fun html_signature
: Template is abstract
376 redef fun css_classes
do
377 var set
= new HashSet[String]
378 if is_intro
then set
.add
"intro"
379 for m
in mproperty
.intro
.modifiers
do set
.add m
.to_cmangle
380 for m
in modifiers
do set
.add m
.to_cmangle
384 # List of all modifiers like redef, private, abstract, intern, fun etc.
385 var modifiers
: Array[String] is lazy
do
386 var res
= new Array[String]
390 res
.add mproperty
.visibility
.to_s
393 if mprop
isa MVirtualTypeDef then
395 else if mprop
isa MMethodDef then
396 if mprop
.is_abstract
then
398 else if mprop
.is_intern
then
401 if mprop
.mproperty
.is_init
then
411 redef class MAttributeDef
413 redef fun html_modifiers
do
419 redef fun html_short_signature
do return new Template
421 redef fun html_signature
do
422 var static_mtype
= self.static_mtype
423 var tpl
= new Template
424 if static_mtype
!= null then
426 tpl
.add static_mtype
.html_signature
432 redef class MMethodDef
434 # FIXME annotation should be handled in their own way
435 redef fun html_modifiers
do
436 if mproperty
.is_init
then
437 var res
= new Array[String]
438 if mproperty
.visibility
!= public_visibility
then
439 res
.add mproperty
.visibility
.to_s
446 else if is_intern
then
453 redef fun html_declaration
do
454 if mproperty
.is_init
then
455 var tpl
= new Template
457 tpl
.add html_modifiers
.join
(" ")
460 tpl
.add html_signature
467 redef fun html_short_signature
do
468 var new_msignature
= self.new_msignature
469 if mproperty
.is_root_init
and new_msignature
!= null then
470 return new_msignature
.html_short_signature
472 return msignature
.as(not null).html_short_signature
475 redef fun html_signature
do
476 var new_msignature
= self.new_msignature
477 if mproperty
.is_root_init
and new_msignature
!= null then
478 return new_msignature
.html_signature
480 return msignature
.as(not null).html_signature
484 redef class MVirtualTypeProp
485 redef fun html_link
do return mvirtualtype
.html_link
488 redef class MVirtualTypeDef
490 redef fun html_modifiers
do
496 redef fun html_short_signature
do return new Template
498 redef fun html_signature
do
499 var bound
= self.bound
500 var tpl
= new Template
501 if bound
== null then return tpl
503 tpl
.add bound
.html_signature
509 # Returns the signature of this type whithout bounds.
510 fun html_short_signature
: Template is abstract
512 # Returns the signature of this type.
513 fun html_signature
: Template is abstract
516 redef class MClassType
517 redef fun html_link
do return mclass
.html_link
518 redef fun html_short_signature
do return html_link
519 redef fun html_signature
do return html_link
522 redef class MNullableType
523 redef fun html_short_signature
do
524 var tpl
= new Template
526 tpl
.add mtype
.html_short_signature
530 redef fun html_signature
do
531 var tpl
= new Template
533 tpl
.add mtype
.html_signature
538 redef class MGenericType
539 redef fun html_short_signature
do
541 var tpl
= new Template
542 tpl
.add
new Link(lnk
.href
, mclass
.name
.html_escape
, lnk
.title
)
544 for i
in [0..arguments
.length
[ do
545 tpl
.add arguments
[i
].html_short_signature
546 if i
< arguments
.length
- 1 then tpl
.add
", "
552 redef fun html_signature
do
554 var tpl
= new Template
555 tpl
.add
new Link(lnk
.href
, mclass
.name
.html_escape
, lnk
.title
)
557 for i
in [0..arguments
.length
[ do
558 tpl
.add arguments
[i
].html_signature
559 if i
< arguments
.length
- 1 then tpl
.add
", "
566 redef class MParameterType
567 redef fun html_short_signature
do return html_link
568 redef fun html_signature
do return html_link
571 redef class MVirtualType
572 redef fun html_signature
do return html_link
575 redef class MSignature
576 redef fun html_short_signature
do
577 var tpl
= new Template
578 if not mparameters
.is_empty
then
580 for i
in [0..mparameters
.length
[ do
581 tpl
.add mparameters
[i
].html_short_signature
582 if i
< mparameters
.length
- 1 then tpl
.add
", "
589 redef fun html_signature
do
590 var tpl
= new Template
591 if not mparameters
.is_empty
then
593 for i
in [0..mparameters
.length
[ do
594 tpl
.add mparameters
[i
].html_signature
595 if i
< mparameters
.length
- 1 then tpl
.add
", "
599 var return_mtype
= self.return_mtype
600 if return_mtype
!= null then
602 tpl
.add return_mtype
.html_signature
608 redef class MParameter
610 # Returns `self` name and ellipsys if any.
611 fun html_short_signature
: Template do
612 var tpl
= new Template
614 if is_vararg
then tpl
.add
"..."
618 # Returns `self` name with it's static type and ellipsys if any.
619 fun html_signature
: Template do
620 var tpl
= new Template
622 tpl
.add mtype
.html_signature
623 if is_vararg
then tpl
.add
"..."
628 redef class MEntityTree
629 # Render `self` as a hierarchical UnorderedList.
630 fun html_list
: UnorderedList do
631 var lst
= new_unordered_list
633 var li
= new_mentity_item
(r
)
635 build_html_list
(r
, li
)
640 # Build the html list recursively.
641 private fun build_html_list
(e
: MEntity, li
: ListItem) do
642 if not sub
.has_key
(e
) then return
644 var lst
= new_unordered_list
646 if e2
isa MGroup and e2
.is_root
then
647 build_html_list
(e2
, li
)
649 var sli
= new_mentity_item
(e2
)
651 build_html_list
(e2
, sli
)
654 var text
= new Template
656 if not lst
.is_empty
then text
.add lst
660 # HTML unordered List used to compose the tree.
662 # Redefine this method to add custom CSS classes or other html attributes.
663 protected fun new_unordered_list
: UnorderedList do return new UnorderedList
665 # Return a li element for `mconcern` that can be displayed in a concern list
666 protected fun new_mentity_item
(mentity
: MEntity): ListItem do
667 var tpl
= new Template
668 tpl
.add mentity
.html_link
669 var comment
= mentity
.html_synopsis
670 if comment
!= null then
674 return new ListItem(tpl
)