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 # HTML templates for Nit model MEntities.
20 import html_components
21 import html
::bootstrap
23 import model
::model_collect
26 # URL of this entity’s Nitdoc page.
27 fun nitdoc_url
: String is abstract
29 # Returns the mentity name without short signature.
35 # * MClassDef: `Foo[E]`
36 # * MProperty: `foo(e)`
37 # * MPropdef: `foo(e)`
38 var html_name
: String is lazy
do return name
.html_escape
40 # Returns a Link to the mentity `html_url`.
42 # Example: `<a href="html_url" title="mdoc.short_comment">html_short_name</a>
43 var html_link
: Link is lazy
do
44 var tpl
= new Link(nitdoc_url
, html_name
)
45 var mdoc
= mdoc_or_fallback
47 tpl
.title
= mdoc
.synopsis
52 # Returns a Link to the mentity `nitdoc_id`.
54 # Example: `<a href="#nitdoc_id" title="mdoc.short_comment">html_short_name</a>
55 fun html_link_to_anchor
: Link do
56 var tpl
= new Link("#{nitdoc_id}", html_name
)
57 var mdoc
= mdoc_or_fallback
59 tpl
.title
= mdoc
.synopsis
64 # Returns the list of keyword used in `self` declaration.
65 fun html_modifiers
: Array[String] is abstract
67 # Returns the complete MEntity declaration decorated with HTML.
69 # * MProject: `project foo`
70 # * MGroup: `group foo`
71 # * MModule: `module foo`
72 # * MClass: `private abstract class Foo[E: Object]`
73 # * MClassDef: `redef class Foo[E]`
74 # * MProperty: `private fun foo(e: Object): Int`
75 # * MPropdef: `redef fun foo(e)`
76 fun html_declaration
: Template do
77 var tpl
= new Template
79 tpl
.add html_modifiers
.join
(" ")
86 # Returns `self` namespace decorated with HTML links.
88 # * MProject: `mproject`
89 # * MGroup: `mproject(::group)`
90 # * MModule: `mgroup::mmodule`
91 # * MClass: `mproject::mclass`
92 # * MClassDef: `mmodule::mclassdef`
93 # * MProperty: `mclass::mprop`
94 # * MPropdef: `mclassdef:mpropdef`
95 fun html_namespace
: Template is abstract
97 # Returns the synopsis and the comment of this MEntity formatted as HTML.
98 var html_documentation
: nullable Writable is lazy
do
99 var mdoc
= mdoc_or_fallback
100 if mdoc
== null then return null
101 return mdoc
.html_documentation
104 # Returns the synopsis of this MEntity formatted as HTML.
105 var html_synopsis
: nullable Writable is lazy
do
106 var mdoc
= mdoc_or_fallback
107 if mdoc
== null then return null
108 return mdoc
.html_synopsis
111 # Returns the the comment without the synopsis formatted as HTML.
112 var html_comment
: nullable Writable is lazy
do
113 var mdoc
= mdoc_or_fallback
114 if mdoc
== null then return null
115 return mdoc
.html_comment
118 # Icon that will be displayed before the title
119 fun html_icon
: BSIcon do
120 var icon
= new BSIcon("tag")
121 icon
.css_classes
.add_all
(css_classes
)
125 # CSS classes used to decorate `self`.
127 # Mainly used for icons.
128 var css_classes
= new Array[String]
130 # A li element that can go in a `HTMLList`.
131 fun html_list_item
: ListItem do
132 var tpl
= new Template
133 tpl
.add
new DocHTMLLabel.with_classes
(css_classes
)
135 var comment
= html_synopsis
136 if comment
!= null then
140 return new ListItem(tpl
)
145 redef var nitdoc_id
= name
.to_cmangle
is lazy
146 redef fun nitdoc_url
do return root
.nitdoc_url
147 redef var html_modifiers
= ["project"]
148 redef fun html_namespace
do return html_link
149 redef var css_classes
= ["public"]
153 redef var nitdoc_id
is lazy
do
154 if parent
!= null then
155 return "{parent.nitdoc_id}__{name.to_cmangle}"
157 return name
.to_cmangle
160 redef fun nitdoc_url
do return "group_{nitdoc_id}.html"
161 redef var html_modifiers
= ["group"]
163 # Depends if `self` is root or not.
165 # * If root `mproject`.
166 # * Else `mproject::self`.
167 redef fun html_namespace
do
168 var tpl
= new Template
169 tpl
.add mproject
.html_namespace
170 if mproject
.root
!= self then
177 redef var css_classes
= ["public"]
181 redef var nitdoc_id
is lazy
do
182 if mgroup
!= null then
183 if mgroup
.mmodules
.length
== 1 then
184 return "{mgroup.nitdoc_id}-"
186 return "{mgroup.nitdoc_id}__{name.to_cmangle}"
189 return name
.to_cmangle
192 redef fun nitdoc_url
do return "module_{nitdoc_id}.html"
193 redef var html_modifiers
= ["module"]
195 # Depends if `self` belongs to a MGroup.
197 # * If mgroup `mgroup::self`.
199 redef fun html_namespace
do
200 var tpl
= new Template
201 if mgroup
!= null then
202 tpl
.add mgroup
.html_namespace
209 redef var css_classes
= ["public"]
213 redef var nitdoc_id
= "{intro_mmodule.nitdoc_id}__{name.to_cmangle}" is lazy
214 redef fun nitdoc_url
do return "class_{nitdoc_id}.html"
215 redef fun mdoc_or_fallback
do return intro
.mdoc
218 redef var html_name
is lazy
do
219 var tpl
= new Template
220 tpl
.add name
.html_escape
223 var parameter_names
= new Array[String]
224 for p
in mparameters
do
225 parameter_names
.add
(p
.html_name
)
227 tpl
.add parameter_names
.join
(", ")
230 return tpl
.write_to_string
233 redef fun html_modifiers
do return intro
.html_modifiers
234 redef fun html_declaration
do return intro
.html_declaration
236 # Returns `mproject::self`.
237 redef fun html_namespace
do
238 var tpl
= new Template
239 tpl
.add intro_mmodule
.mgroup
.mproject
.html_namespace
246 # Returns `intro.html_short_signature`.
247 fun html_short_signature
: Template do return intro
.html_short_signature
249 # Returns `intro.html_signature`.
250 fun html_signature
: Template do return intro
.html_signature
252 redef fun html_icon
do return intro
.html_icon
253 redef fun css_classes
do return intro
.css_classes
256 redef class MClassDef
257 redef var nitdoc_id
= "{mmodule.nitdoc_id}__{name.to_cmangle}" is lazy
258 redef fun nitdoc_url
do return "{mclass.nitdoc_url}#{nitdoc_id}"
259 redef fun mdoc_or_fallback
do return mdoc
or else mclass
.mdoc_or_fallback
261 # Depends if `self` is an intro or not.
263 # * If intro contains the visibility and kind.
264 # * If redef contains the `redef` keyword and kind.
265 redef fun html_modifiers
do
266 var res
= new Array[String]
270 if mclass
.visibility
!= public_visibility
then
271 res
.add mclass
.visibility
.to_s
274 res
.add mclass
.kind
.to_s
278 # Depends if `self` is an intro or not.
280 # For intro: `private abstract class Foo[E: Object]`
281 # For redef: `redef class Foo[E]`
282 redef fun html_declaration
do
283 var tpl
= new Template
285 tpl
.add html_modifiers
.join
(" ")
289 tpl
.add html_signature
291 tpl
.add html_short_signature
297 # Returns `mmodule::self`
298 redef fun html_namespace
do
299 var tpl
= new Template
300 tpl
.add mmodule
.html_namespace
302 tpl
.add mclass
.html_link
307 # Returns the MClassDef generic signature without static bounds.
308 fun html_short_signature
: Template do
309 var tpl
= new Template
310 var mparameters
= mclass
.mparameters
311 if not mparameters
.is_empty
then
313 for i
in [0..mparameters
.length
[ do
314 tpl
.add mparameters
[i
].html_name
315 if i
< mparameters
.length
- 1 then tpl
.add
", "
322 # Returns the MClassDef generic signature with static bounds.
323 fun html_signature
: Template do
324 var tpl
= new Template
325 var mparameters
= mclass
.mparameters
326 if not mparameters
.is_empty
then
328 for i
in [0..mparameters
.length
[ do
329 tpl
.add
"{mparameters[i].html_name}: "
330 tpl
.add bound_mtype
.arguments
[i
].html_signature
331 if i
< mparameters
.length
- 1 then tpl
.add
", "
338 redef fun css_classes
do
339 var set
= new HashSet[String]
340 if is_intro
then set
.add
"intro"
341 for m
in mclass
.intro
.collect_modifiers
do set
.add m
.to_cmangle
342 for m
in collect_modifiers
do set
.add m
.to_cmangle
347 redef class MProperty
348 redef var nitdoc_id
= "{intro_mclassdef.mclass.nitdoc_id}__{name.to_cmangle}" is lazy
349 redef fun nitdoc_url
do return "property_{nitdoc_id}.html"
350 redef fun mdoc_or_fallback
do return intro
.mdoc
351 redef fun html_modifiers
do return intro
.html_modifiers
352 redef fun html_declaration
do return intro
.html_declaration
354 # Returns `mclass::self`.
355 redef fun html_namespace
do
356 var tpl
= new Template
357 tpl
.add intro_mclassdef
.mclass
.html_namespace
359 tpl
.add intro
.html_link
364 # Returns `intro.html_short_signature`.
365 fun html_short_signature
: Template do return intro
.html_short_signature
367 # Returns `intro.html_signature`.
368 fun html_signature
: Template do return intro
.html_signature
370 redef fun css_classes
do return intro
.css_classes
374 redef var nitdoc_id
= "{mclassdef.nitdoc_id}__{name.to_cmangle}" is lazy
375 redef fun nitdoc_url
do return "{mproperty.nitdoc_url}#{nitdoc_id}"
376 redef fun mdoc_or_fallback
do return mdoc
or else mproperty
.mdoc_or_fallback
378 # Depends if `self` is an intro or not.
380 # * If intro contains the visibility and kind.
381 # * If redef contains the `redef` keyword and kind.
382 redef fun html_modifiers
do
383 var res
= new Array[String]
387 if mproperty
.visibility
!= public_visibility
then
388 res
.add mproperty
.visibility
.to_s
394 # Depends if `self` is an intro or not.
396 # For intro: `private fun foo(e: Object): Bar is abstract`
397 # For redef: `redef fun foo(e) is cached`
398 redef fun html_declaration
do
399 var tpl
= new Template
401 tpl
.add html_modifiers
.join
(" ")
405 tpl
.add html_signature
407 tpl
.add mproperty
.intro
.html_link
408 tpl
.add html_short_signature
414 # Returns `mclassdef::self`
415 redef fun html_namespace
do
416 var tpl
= new Template
417 tpl
.add mclassdef
.html_namespace
423 # Returns the MPropdDef signature without static types.
424 fun html_short_signature
: Template is abstract
426 # Returns the MPropDef signature with static types.
427 fun html_signature
: Template is abstract
429 redef fun css_classes
do
430 var set
= new HashSet[String]
431 if is_intro
then set
.add
"intro"
432 for m
in mproperty
.intro
.collect_modifiers
do set
.add m
.to_cmangle
433 for m
in collect_modifiers
do set
.add m
.to_cmangle
438 redef class MAttributeDef
440 redef fun html_modifiers
do
446 redef fun html_short_signature
do return new Template
448 redef fun html_signature
do
449 var tpl
= new Template
450 if static_mtype
!= null then
452 tpl
.add static_mtype
.html_signature
458 redef class MMethodDef
460 # FIXME annotation should be handled in their own way
461 redef fun html_modifiers
do
462 if mproperty
.is_init
then
463 var res
= new Array[String]
464 if mproperty
.visibility
!= public_visibility
then
465 res
.add mproperty
.visibility
.to_s
472 else if is_intern
then
479 redef fun html_declaration
do
480 if mproperty
.is_init
then
481 var tpl
= new Template
483 tpl
.add html_modifiers
.join
(" ")
486 tpl
.add html_signature
493 redef fun html_short_signature
do
494 if mproperty
.is_root_init
and new_msignature
!= null then
495 return new_msignature
.html_short_signature
497 return msignature
.html_short_signature
500 redef fun html_signature
do
501 if mproperty
.is_root_init
and new_msignature
!= null then
502 return new_msignature
.html_signature
504 return msignature
.html_signature
508 redef class MVirtualTypeProp
509 redef fun html_link
do return mvirtualtype
.html_link
512 redef class MVirtualTypeDef
514 redef fun html_modifiers
do
520 redef fun html_short_signature
do return new Template
522 redef fun html_signature
do
523 var tpl
= new Template
524 if bound
== null then return tpl
526 tpl
.add bound
.html_signature
532 # Returns the signature of this type whithout bounds.
533 fun html_short_signature
: Template is abstract
535 # Returns the signature of this type.
536 fun html_signature
: Template is abstract
539 redef class MClassType
540 redef fun html_link
do return mclass
.html_link
541 redef fun html_short_signature
do return html_link
542 redef fun html_signature
do return html_link
545 redef class MNullableType
547 redef fun html_short_signature
do
548 var tpl
= new Template
550 tpl
.add mtype
.html_short_signature
554 redef fun html_signature
do
555 var tpl
= new Template
557 tpl
.add mtype
.html_signature
562 redef class MGenericType
563 redef fun html_short_signature
do
565 var tpl
= new Template
566 tpl
.add
new Link.with_title
(lnk
.href
, mclass
.name
.html_escape
, lnk
.title
)
568 for i
in [0..arguments
.length
[ do
569 tpl
.add arguments
[i
].html_short_signature
570 if i
< arguments
.length
- 1 then tpl
.add
", "
576 redef fun html_signature
do
578 var tpl
= new Template
579 tpl
.add
new Link.with_title
(lnk
.href
, mclass
.name
.html_escape
, lnk
.title
)
581 for i
in [0..arguments
.length
[ do
582 tpl
.add arguments
[i
].html_signature
583 if i
< arguments
.length
- 1 then tpl
.add
", "
590 redef class MParameterType
591 redef fun html_link
do
592 return new Link.with_title
("{mclass.nitdoc_url}#FT_{name.to_cmangle}", name
, "formal type")
595 redef fun html_short_signature
do return html_link
596 redef fun html_signature
do return html_link
599 redef class MVirtualType
600 redef fun html_link
do return mproperty
.intro
.html_link
601 redef fun html_signature
do return html_link
604 redef class MSignature
606 redef fun html_short_signature
do
607 var tpl
= new Template
608 if not mparameters
.is_empty
then
610 for i
in [0..mparameters
.length
[ do
611 tpl
.add mparameters
[i
].html_short_signature
612 if i
< mparameters
.length
- 1 then tpl
.add
", "
619 redef fun html_signature
do
620 var tpl
= new Template
621 if not mparameters
.is_empty
then
623 for i
in [0..mparameters
.length
[ do
624 tpl
.add mparameters
[i
].html_signature
625 if i
< mparameters
.length
- 1 then tpl
.add
", "
629 if return_mtype
!= null then
631 tpl
.add return_mtype
.html_signature
637 redef class MParameter
639 # Returns `self` name and ellipsys if any.
640 fun html_short_signature
: Template do
641 var tpl
= new Template
643 if is_vararg
then tpl
.add
"..."
647 # Returns `self` name with it's static type and ellipsys if any.
648 fun html_signature
: Template do
649 var tpl
= new Template
651 tpl
.add mtype
.html_signature
652 if is_vararg
then tpl
.add
"..."
657 redef class ConcernsTree
658 # Render `self` as a hierarchical UnorderedList.
659 fun html_list
: UnorderedList do
660 var lst
= new UnorderedList
661 lst
.css_classes
.add
"list-unstyled list-definition"
663 var li
= r
.html_concern_item
665 build_html_list
(r
, li
)
670 # Build the html list recursively.
671 private fun build_html_list
(e
: MConcern, li
: ListItem) do
672 if not sub
.has_key
(e
) then return
674 var lst
= new UnorderedList
675 lst
.css_classes
.add
"list-unstyled list-definition"
677 if e2
isa MGroup and e2
.is_root
then
678 build_html_list
(e2
, li
)
680 var sli
= e2
.html_concern_item
682 build_html_list
(e2
, sli
)
685 var text
= new Template
687 if not lst
.is_empty
then text
.add lst
693 # Return a li element for `self` that can be displayed in a concern list
694 private fun html_concern_item
: ListItem do
696 var tpl
= new Template
697 tpl
.add
new Link.with_title
("#{nitdoc_id}.concern", lnk
.text
, lnk
.title
)
698 var comment
= html_synopsis
699 if comment
!= null then
703 return new ListItem(tpl
)
707 ################################################################################
708 # Additions to `model_ext`.
711 redef fun html_signature
do
712 var tpl
= new Template
715 if part
.target
!= null then
716 tpl
.add part
.target
.as(not null).html_link
718 tpl
.add part
.text
.html_escape
725 redef class MInnerClass
726 redef fun nitdoc_url
do return inner
.nitdoc_url
727 redef fun html_signature
do return inner
.html_signature
730 redef class MInnerClassDef
731 redef fun nitdoc_url
do return inner
.nitdoc_url
733 redef fun html_link_to_anchor
do return inner
.html_link_to_anchor
734 redef fun html_link
do return inner
.html_link
735 redef fun html_signature
do return inner
.html_signature