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
25 # URL of this entity’s Nitdoc page.
26 fun nitdoc_url
: String is abstract
28 # Returns the mentity name without short signature.
34 # * MClassDef: `Foo[E]`
35 # * MProperty: `foo(e)`
36 # * MPropdef: `foo(e)`
37 var html_name
: String is lazy
do return name
.html_escape
39 # Returns a Link to the mentity `html_url`.
41 # Example: `<a href="html_url" title="mdoc.short_comment">html_short_name</a>
42 var html_link
: Link is lazy
do
43 var tpl
= new Link(nitdoc_url
, html_name
)
44 var mdoc
= mdoc_or_fallback
46 tpl
.title
= mdoc
.short_comment
51 # Returns a Link to the mentity `nitdoc_id`.
53 # Example: `<a href="#nitdoc_id" title="mdoc.short_comment">html_short_name</a>
54 fun html_link_to_anchor
: Link do
55 var tpl
= new Link("#{nitdoc_id}", html_name
)
56 var mdoc
= mdoc_or_fallback
58 tpl
.title
= mdoc
.short_comment
63 # Returns the list of keyword used in `self` declaration.
64 fun html_modifiers
: Array[String] is abstract
66 # Returns the complete MEntity declaration decorated with HTML.
68 # * MProject: `project foo`
69 # * MGroup: `group foo`
70 # * MModule: `module foo`
71 # * MClass: `private abstract class Foo[E: Object]`
72 # * MClassDef: `redef class Foo[E]`
73 # * MProperty: `private fun foo(e: Object): Int`
74 # * MPropdef: `redef fun foo(e)`
75 fun html_declaration
: Template do
76 var tpl
= new Template
78 tpl
.add html_modifiers
.join
(" ")
85 # Returns `self` namespace decorated with HTML links.
87 # * MProject: `mproject`
88 # * MGroup: `mproject(::group)`
89 # * MModule: `mgroup::mmodule`
90 # * MClass: `mproject::mclass`
91 # * MClassDef: `mmodule::mclassdef`
92 # * MProperty: `mclass::mprop`
93 # * MPropdef: `mclassdef:mpropdef`
94 fun html_namespace
: Template is abstract
96 # Returns the comment of this MEntity formatted as HTML.
97 var html_comment
: nullable Writable is lazy
do
98 var mdoc
= mdoc_or_fallback
99 if mdoc
== null then return null
100 return mdoc
.tpl_comment
103 # Returns the comment of this MEntity formatted as HTML.
104 var html_short_comment
: nullable Writable is lazy
do
105 var mdoc
= mdoc_or_fallback
106 if mdoc
== null then return null
107 return mdoc
.tpl_short_comment
110 # Icon that will be displayed before the title
111 fun html_icon
: BSIcon do
112 var icon
= new BSIcon("tag")
113 icon
.css_classes
.add_all
(css_classes
)
117 # CSS classes used to decorate `self`.
119 # Mainly used for icons.
120 var css_classes
= new Array[String]
122 # A li element that can go in a `HTMLList`.
123 fun html_list_item
: ListItem do
124 var tpl
= new Template
125 tpl
.add
new DocHTMLLabel.with_classes
(css_classes
)
127 var comment
= html_short_comment
128 if comment
!= null then
132 return new ListItem(tpl
)
137 redef var nitdoc_id
= name
.to_cmangle
is lazy
138 redef fun nitdoc_url
do return root
.nitdoc_url
139 redef var html_modifiers
= ["project"]
140 redef fun html_namespace
do return html_link
141 redef var css_classes
= ["public"]
145 redef var nitdoc_id
is lazy
do
146 if parent
!= null then
147 return "{parent.nitdoc_id}__{name.to_cmangle}"
149 return name
.to_cmangle
152 redef fun nitdoc_url
do return "group_{nitdoc_id}.html"
153 redef var html_modifiers
= ["group"]
155 # Depends if `self` is root or not.
157 # * If root `mproject`.
158 # * Else `mproject::self`.
159 redef fun html_namespace
do
160 var tpl
= new Template
161 tpl
.add mproject
.html_namespace
162 if mproject
.root
!= self then
169 redef var css_classes
= ["public"]
173 redef var nitdoc_id
is lazy
do
174 if mgroup
!= null then
175 if mgroup
.mmodules
.length
== 1 then
176 return "{mgroup.nitdoc_id}-"
178 return "{mgroup.nitdoc_id}__{name.to_cmangle}"
181 return name
.to_cmangle
184 redef fun nitdoc_url
do return "module_{nitdoc_id}.html"
185 redef var html_modifiers
= ["module"]
187 # Depends if `self` belongs to a MGroup.
189 # * If mgroup `mgroup::self`.
191 redef fun html_namespace
do
192 var tpl
= new Template
193 if mgroup
!= null then
194 tpl
.add mgroup
.html_namespace
201 redef var css_classes
= ["public"]
205 redef var nitdoc_id
= "{intro_mmodule.nitdoc_id}__{name.to_cmangle}" is lazy
206 redef fun nitdoc_url
do return "class_{nitdoc_id}.html"
207 redef fun mdoc_or_fallback
do return intro
.mdoc
210 redef var html_name
is lazy
do
211 var tpl
= new Template
212 tpl
.add name
.html_escape
215 var parameter_names
= new Array[String]
216 for p
in mparameters
do
217 parameter_names
.add
(p
.html_name
)
219 tpl
.add parameter_names
.join
(", ")
222 return tpl
.write_to_string
225 redef fun html_modifiers
do return intro
.html_modifiers
226 redef fun html_declaration
do return intro
.html_declaration
228 # Returns `mproject::self`.
229 redef fun html_namespace
do
230 var tpl
= new Template
231 tpl
.add intro_mmodule
.mgroup
.mproject
.html_namespace
238 # Returns `intro.html_short_signature`.
239 fun html_short_signature
: Template do return intro
.html_short_signature
241 # Returns `intro.html_signature`.
242 fun html_signature
: Template do return intro
.html_signature
244 redef fun html_icon
do return intro
.html_icon
245 redef fun css_classes
do return intro
.css_classes
248 redef class MClassDef
249 redef var nitdoc_id
= "{mmodule.nitdoc_id}__{name.to_cmangle}" is lazy
250 redef fun nitdoc_url
do return "{mclass.nitdoc_url}#{nitdoc_id}"
251 redef fun mdoc_or_fallback
do return mdoc
or else mclass
.mdoc_or_fallback
253 # Depends if `self` is an intro or not.
255 # * If intro contains the visibility and kind.
256 # * If redef contains the `redef` keyword and kind.
257 redef fun html_modifiers
do
258 var res
= new Array[String]
262 if mclass
.visibility
!= public_visibility
then
263 res
.add mclass
.visibility
.to_s
266 res
.add mclass
.kind
.to_s
270 # Depends if `self` is an intro or not.
272 # For intro: `private abstract class Foo[E: Object]`
273 # For redef: `redef class Foo[E]`
274 redef fun html_declaration
do
275 var tpl
= new Template
277 tpl
.add html_modifiers
.join
(" ")
281 tpl
.add html_signature
283 tpl
.add html_short_signature
289 # Returns `mmodule::self`
290 redef fun html_namespace
do
291 var tpl
= new Template
292 tpl
.add mmodule
.html_namespace
294 tpl
.add mclass
.html_link
299 # Returns the MClassDef generic signature without static bounds.
300 fun html_short_signature
: Template do
301 var tpl
= new Template
302 var mparameters
= mclass
.mparameters
303 if not mparameters
.is_empty
then
305 for i
in [0..mparameters
.length
[ do
306 tpl
.add mparameters
[i
].html_name
307 if i
< mparameters
.length
- 1 then tpl
.add
", "
314 # Returns the MClassDef generic signature with static bounds.
315 fun html_signature
: Template do
316 var tpl
= new Template
317 var mparameters
= mclass
.mparameters
318 if not mparameters
.is_empty
then
320 for i
in [0..mparameters
.length
[ do
321 tpl
.add
"{mparameters[i].html_name}: "
322 tpl
.add bound_mtype
.arguments
[i
].html_signature
323 if i
< mparameters
.length
- 1 then tpl
.add
", "
330 redef fun css_classes
do
331 var set
= new HashSet[String]
332 if is_intro
then set
.add
"intro"
333 for m
in mclass
.intro
.modifiers
do set
.add m
.to_cmangle
334 for m
in modifiers
do set
.add m
.to_cmangle
339 redef class MProperty
340 redef var nitdoc_id
= "{intro_mclassdef.mclass.nitdoc_id}__{name.to_cmangle}" is lazy
341 redef fun nitdoc_url
do return "property_{nitdoc_id}.html"
342 redef fun mdoc_or_fallback
do return intro
.mdoc
343 redef fun html_modifiers
do return intro
.html_modifiers
344 redef fun html_declaration
do return intro
.html_declaration
346 # Returns `mclass::self`.
347 redef fun html_namespace
do
348 var tpl
= new Template
349 tpl
.add intro_mclassdef
.mclass
.html_namespace
351 tpl
.add intro
.html_link
356 # Returns `intro.html_short_signature`.
357 fun html_short_signature
: Template do return intro
.html_short_signature
359 # Returns `intro.html_signature`.
360 fun html_signature
: Template do return intro
.html_signature
362 redef fun css_classes
do return intro
.css_classes
366 redef var nitdoc_id
= "{mclassdef.nitdoc_id}__{name.to_cmangle}" is lazy
367 redef fun nitdoc_url
do return "{mproperty.nitdoc_url}#{nitdoc_id}"
368 redef fun mdoc_or_fallback
do return mdoc
or else mproperty
.mdoc_or_fallback
370 # Depends if `self` is an intro or not.
372 # * If intro contains the visibility and kind.
373 # * If redef contains the `redef` keyword and kind.
374 redef fun html_modifiers
do
375 var res
= new Array[String]
379 if mproperty
.visibility
!= public_visibility
then
380 res
.add mproperty
.visibility
.to_s
386 # Depends if `self` is an intro or not.
388 # For intro: `private fun foo(e: Object): Bar is abstract`
389 # For redef: `redef fun foo(e) is cached`
390 redef fun html_declaration
do
391 var tpl
= new Template
393 tpl
.add html_modifiers
.join
(" ")
397 tpl
.add html_signature
399 tpl
.add mproperty
.intro
.html_link
400 tpl
.add html_short_signature
406 # Returns `mclassdef::self`
407 redef fun html_namespace
do
408 var tpl
= new Template
409 tpl
.add mclassdef
.html_namespace
415 # Returns the MPropdDef signature without static types.
416 fun html_short_signature
: Template is abstract
418 # Returns the MPropDef signature with static types.
419 fun html_signature
: Template is abstract
421 redef fun css_classes
do
422 var set
= new HashSet[String]
423 if is_intro
then set
.add
"intro"
424 for m
in mproperty
.intro
.modifiers
do set
.add m
.to_cmangle
425 for m
in modifiers
do set
.add m
.to_cmangle
430 redef class MAttributeDef
432 redef fun html_modifiers
do
438 redef fun html_short_signature
do return new Template
440 redef fun html_signature
do
441 var tpl
= new Template
442 if static_mtype
!= null then
444 tpl
.add static_mtype
.html_signature
450 redef class MMethodDef
452 # FIXME annotation should be handled in their own way
453 redef fun html_modifiers
do
454 if mproperty
.is_init
then
455 var res
= new Array[String]
456 if mproperty
.visibility
!= public_visibility
then
457 res
.add mproperty
.visibility
.to_s
464 else if is_intern
then
471 redef fun html_declaration
do
472 if mproperty
.is_init
then
473 var tpl
= new Template
475 tpl
.add html_modifiers
.join
(" ")
478 tpl
.add html_signature
485 redef fun html_short_signature
do
486 if mproperty
.is_root_init
and new_msignature
!= null then
487 return new_msignature
.html_short_signature
489 return msignature
.html_short_signature
492 redef fun html_signature
do
493 if mproperty
.is_root_init
and new_msignature
!= null then
494 return new_msignature
.html_signature
496 return msignature
.html_signature
500 redef class MVirtualTypeProp
501 redef fun html_link
do return mvirtualtype
.html_link
504 redef class MVirtualTypeDef
506 redef fun html_modifiers
do
512 redef fun html_short_signature
do return new Template
514 redef fun html_signature
do
515 var tpl
= new Template
516 if bound
== null then return tpl
518 tpl
.add bound
.html_signature
524 # Returns the signature of this type whithout bounds.
525 fun html_short_signature
: Template is abstract
527 # Returns the signature of this type.
528 fun html_signature
: Template is abstract
531 redef class MClassType
532 redef fun html_link
do return mclass
.html_link
533 redef fun html_short_signature
do return html_link
534 redef fun html_signature
do return html_link
537 redef class MNullableType
539 redef fun html_short_signature
do
540 var tpl
= new Template
542 tpl
.add mtype
.html_short_signature
546 redef fun html_signature
do
547 var tpl
= new Template
549 tpl
.add mtype
.html_signature
554 redef class MGenericType
555 redef fun html_short_signature
do
557 var tpl
= new Template
558 tpl
.add
new Link.with_title
(lnk
.href
, mclass
.name
.html_escape
, lnk
.title
)
560 for i
in [0..arguments
.length
[ do
561 tpl
.add arguments
[i
].html_short_signature
562 if i
< arguments
.length
- 1 then tpl
.add
", "
568 redef fun html_signature
do
570 var tpl
= new Template
571 tpl
.add
new Link.with_title
(lnk
.href
, mclass
.name
.html_escape
, lnk
.title
)
573 for i
in [0..arguments
.length
[ do
574 tpl
.add arguments
[i
].html_signature
575 if i
< arguments
.length
- 1 then tpl
.add
", "
582 redef class MParameterType
583 redef fun html_link
do
584 return new Link.with_title
("{mclass.nitdoc_url}#FT_{name.to_cmangle}", name
, "formal type")
587 redef fun html_short_signature
do return html_link
588 redef fun html_signature
do return html_link
591 redef class MVirtualType
592 redef fun html_link
do return mproperty
.intro
.html_link
593 redef fun html_signature
do return html_link
596 redef class MSignature
598 redef fun html_short_signature
do
599 var tpl
= new Template
600 if not mparameters
.is_empty
then
602 for i
in [0..mparameters
.length
[ do
603 tpl
.add mparameters
[i
].html_short_signature
604 if i
< mparameters
.length
- 1 then tpl
.add
", "
611 redef fun html_signature
do
612 var tpl
= new Template
613 if not mparameters
.is_empty
then
615 for i
in [0..mparameters
.length
[ do
616 tpl
.add mparameters
[i
].html_signature
617 if i
< mparameters
.length
- 1 then tpl
.add
", "
621 if return_mtype
!= null then
623 tpl
.add return_mtype
.html_signature
629 redef class MParameter
631 # Returns `self` name and ellipsys if any.
632 fun html_short_signature
: Template do
633 var tpl
= new Template
635 if is_vararg
then tpl
.add
"..."
639 # Returns `self` name with it's static type and ellipsys if any.
640 fun html_signature
: Template do
641 var tpl
= new Template
643 tpl
.add mtype
.html_signature
644 if is_vararg
then tpl
.add
"..."
649 redef class ConcernsTree
650 # Render `self` as a hierarchical UnorderedList.
651 fun html_list
: UnorderedList do
652 var lst
= new UnorderedList
653 lst
.css_classes
.add
"list-unstyled list-definition"
655 var li
= r
.html_concern_item
657 build_html_list
(r
, li
)
662 # Build the html list recursively.
663 private fun build_html_list
(e
: MConcern, li
: ListItem) do
664 if not sub
.has_key
(e
) then return
666 var lst
= new UnorderedList
667 lst
.css_classes
.add
"list-unstyled list-definition"
669 if e2
isa MGroup and e2
.is_root
then
670 build_html_list
(e2
, li
)
672 var sli
= e2
.html_concern_item
674 build_html_list
(e2
, sli
)
677 var text
= new Template
679 if not lst
.is_empty
then text
.add lst
685 # Return a li element for `self` that can be displayed in a concern list
686 private fun html_concern_item
: ListItem do
688 var tpl
= new Template
689 tpl
.add
new Link.with_title
("#concern:{nitdoc_id}", lnk
.text
, lnk
.title
)
690 var comment
= html_short_comment
691 if comment
!= null then
695 return new ListItem(tpl
)
699 ################################################################################
700 # Additions to `model_ext`.
703 redef fun html_signature
do
704 var tpl
= new Template
707 if part
.target
!= null then
708 tpl
.add part
.target
.as(not null).html_link
710 tpl
.add part
.text
.html_escape
717 redef class MInnerClass
718 redef fun nitdoc_url
do return inner
.nitdoc_url
719 redef fun html_signature
do return inner
.html_signature
722 redef class MInnerClassDef
723 redef fun nitdoc_url
do return inner
.nitdoc_url
725 redef fun html_link_to_anchor
do return inner
.html_link_to_anchor
726 redef fun html_link
do return inner
.html_link
727 redef fun html_signature
do return inner
.html_signature