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
.short_comment
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
.short_comment
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 comment of this MEntity formatted as HTML.
98 var html_comment
: nullable Writable is lazy
do
99 var mdoc
= mdoc_or_fallback
100 if mdoc
== null then return null
101 return mdoc
.tpl_comment
104 # Returns the comment of this MEntity formatted as HTML.
105 var html_short_comment
: nullable Writable is lazy
do
106 var mdoc
= mdoc_or_fallback
107 if mdoc
== null then return null
108 return mdoc
.tpl_short_comment
111 # Icon that will be displayed before the title
112 fun html_icon
: BSIcon do
113 var icon
= new BSIcon("tag")
114 icon
.css_classes
.add_all
(css_classes
)
118 # CSS classes used to decorate `self`.
120 # Mainly used for icons.
121 var css_classes
= new Array[String]
123 # A li element that can go in a `HTMLList`.
124 fun html_list_item
: ListItem do
125 var tpl
= new Template
126 tpl
.add
new DocHTMLLabel.with_classes
(css_classes
)
128 var comment
= html_short_comment
129 if comment
!= null then
133 return new ListItem(tpl
)
138 redef var nitdoc_id
= name
.to_cmangle
is lazy
139 redef fun nitdoc_url
do return root
.nitdoc_url
140 redef var html_modifiers
= ["project"]
141 redef fun html_namespace
do return html_link
142 redef var css_classes
= ["public"]
146 redef var nitdoc_id
is lazy
do
147 if parent
!= null then
148 return "{parent.nitdoc_id}__{name.to_cmangle}"
150 return name
.to_cmangle
153 redef fun nitdoc_url
do return "group_{nitdoc_id}.html"
154 redef var html_modifiers
= ["group"]
156 # Depends if `self` is root or not.
158 # * If root `mproject`.
159 # * Else `mproject::self`.
160 redef fun html_namespace
do
161 var tpl
= new Template
162 tpl
.add mproject
.html_namespace
163 if mproject
.root
!= self then
170 redef var css_classes
= ["public"]
174 redef var nitdoc_id
is lazy
do
175 if mgroup
!= null then
176 if mgroup
.mmodules
.length
== 1 then
177 return "{mgroup.nitdoc_id}-"
179 return "{mgroup.nitdoc_id}__{name.to_cmangle}"
182 return name
.to_cmangle
185 redef fun nitdoc_url
do return "module_{nitdoc_id}.html"
186 redef var html_modifiers
= ["module"]
188 # Depends if `self` belongs to a MGroup.
190 # * If mgroup `mgroup::self`.
192 redef fun html_namespace
do
193 var tpl
= new Template
194 if mgroup
!= null then
195 tpl
.add mgroup
.html_namespace
202 redef var css_classes
= ["public"]
206 redef var nitdoc_id
= "{intro_mmodule.nitdoc_id}__{name.to_cmangle}" is lazy
207 redef fun nitdoc_url
do return "class_{nitdoc_id}.html"
208 redef fun mdoc_or_fallback
do return intro
.mdoc
211 redef var html_name
is lazy
do
212 var tpl
= new Template
213 tpl
.add name
.html_escape
216 var parameter_names
= new Array[String]
217 for p
in mparameters
do
218 parameter_names
.add
(p
.html_name
)
220 tpl
.add parameter_names
.join
(", ")
223 return tpl
.write_to_string
226 redef fun html_modifiers
do return intro
.html_modifiers
227 redef fun html_declaration
do return intro
.html_declaration
229 # Returns `mproject::self`.
230 redef fun html_namespace
do
231 var tpl
= new Template
232 tpl
.add intro_mmodule
.mgroup
.mproject
.html_namespace
239 # Returns `intro.html_short_signature`.
240 fun html_short_signature
: Template do return intro
.html_short_signature
242 # Returns `intro.html_signature`.
243 fun html_signature
: Template do return intro
.html_signature
245 redef fun html_icon
do return intro
.html_icon
246 redef fun css_classes
do return intro
.css_classes
249 redef class MClassDef
250 redef var nitdoc_id
= "{mmodule.nitdoc_id}__{name.to_cmangle}" is lazy
251 redef fun nitdoc_url
do return "{mclass.nitdoc_url}#{nitdoc_id}"
252 redef fun mdoc_or_fallback
do return mdoc
or else mclass
.mdoc_or_fallback
254 # Depends if `self` is an intro or not.
256 # * If intro contains the visibility and kind.
257 # * If redef contains the `redef` keyword and kind.
258 redef fun html_modifiers
do
259 var res
= new Array[String]
263 if mclass
.visibility
!= public_visibility
then
264 res
.add mclass
.visibility
.to_s
267 res
.add mclass
.kind
.to_s
271 # Depends if `self` is an intro or not.
273 # For intro: `private abstract class Foo[E: Object]`
274 # For redef: `redef class Foo[E]`
275 redef fun html_declaration
do
276 var tpl
= new Template
278 tpl
.add html_modifiers
.join
(" ")
282 tpl
.add html_signature
284 tpl
.add html_short_signature
290 # Returns `mmodule::self`
291 redef fun html_namespace
do
292 var tpl
= new Template
293 tpl
.add mmodule
.html_namespace
295 tpl
.add mclass
.html_link
300 # Returns the MClassDef generic signature without static bounds.
301 fun html_short_signature
: Template do
302 var tpl
= new Template
303 var mparameters
= mclass
.mparameters
304 if not mparameters
.is_empty
then
306 for i
in [0..mparameters
.length
[ do
307 tpl
.add mparameters
[i
].html_name
308 if i
< mparameters
.length
- 1 then tpl
.add
", "
315 # Returns the MClassDef generic signature with static bounds.
316 fun html_signature
: Template do
317 var tpl
= new Template
318 var mparameters
= mclass
.mparameters
319 if not mparameters
.is_empty
then
321 for i
in [0..mparameters
.length
[ do
322 tpl
.add
"{mparameters[i].html_name}: "
323 tpl
.add bound_mtype
.arguments
[i
].html_signature
324 if i
< mparameters
.length
- 1 then tpl
.add
", "
331 redef fun css_classes
do
332 var set
= new HashSet[String]
333 if is_intro
then set
.add
"intro"
334 for m
in mclass
.intro
.collect_modifiers
do set
.add m
.to_cmangle
335 for m
in collect_modifiers
do set
.add m
.to_cmangle
340 redef class MProperty
341 redef var nitdoc_id
= "{intro_mclassdef.mclass.nitdoc_id}__{name.to_cmangle}" is lazy
342 redef fun nitdoc_url
do return "property_{nitdoc_id}.html"
343 redef fun mdoc_or_fallback
do return intro
.mdoc
344 redef fun html_modifiers
do return intro
.html_modifiers
345 redef fun html_declaration
do return intro
.html_declaration
347 # Returns `mclass::self`.
348 redef fun html_namespace
do
349 var tpl
= new Template
350 tpl
.add intro_mclassdef
.mclass
.html_namespace
352 tpl
.add intro
.html_link
357 # Returns `intro.html_short_signature`.
358 fun html_short_signature
: Template do return intro
.html_short_signature
360 # Returns `intro.html_signature`.
361 fun html_signature
: Template do return intro
.html_signature
363 redef fun css_classes
do return intro
.css_classes
367 redef var nitdoc_id
= "{mclassdef.nitdoc_id}__{name.to_cmangle}" is lazy
368 redef fun nitdoc_url
do return "{mproperty.nitdoc_url}#{nitdoc_id}"
369 redef fun mdoc_or_fallback
do return mdoc
or else mproperty
.mdoc_or_fallback
371 # Depends if `self` is an intro or not.
373 # * If intro contains the visibility and kind.
374 # * If redef contains the `redef` keyword and kind.
375 redef fun html_modifiers
do
376 var res
= new Array[String]
380 if mproperty
.visibility
!= public_visibility
then
381 res
.add mproperty
.visibility
.to_s
387 # Depends if `self` is an intro or not.
389 # For intro: `private fun foo(e: Object): Bar is abstract`
390 # For redef: `redef fun foo(e) is cached`
391 redef fun html_declaration
do
392 var tpl
= new Template
394 tpl
.add html_modifiers
.join
(" ")
398 tpl
.add html_signature
400 tpl
.add mproperty
.intro
.html_link
401 tpl
.add html_short_signature
407 # Returns `mclassdef::self`
408 redef fun html_namespace
do
409 var tpl
= new Template
410 tpl
.add mclassdef
.html_namespace
416 # Returns the MPropdDef signature without static types.
417 fun html_short_signature
: Template is abstract
419 # Returns the MPropDef signature with static types.
420 fun html_signature
: Template is abstract
422 redef fun css_classes
do
423 var set
= new HashSet[String]
424 if is_intro
then set
.add
"intro"
425 for m
in mproperty
.intro
.collect_modifiers
do set
.add m
.to_cmangle
426 for m
in collect_modifiers
do set
.add m
.to_cmangle
431 redef class MAttributeDef
433 redef fun html_modifiers
do
439 redef fun html_short_signature
do return new Template
441 redef fun html_signature
do
442 var tpl
= new Template
443 if static_mtype
!= null then
445 tpl
.add static_mtype
.html_signature
451 redef class MMethodDef
453 # FIXME annotation should be handled in their own way
454 redef fun html_modifiers
do
455 if mproperty
.is_init
then
456 var res
= new Array[String]
457 if mproperty
.visibility
!= public_visibility
then
458 res
.add mproperty
.visibility
.to_s
465 else if is_intern
then
472 redef fun html_declaration
do
473 if mproperty
.is_init
then
474 var tpl
= new Template
476 tpl
.add html_modifiers
.join
(" ")
479 tpl
.add html_signature
486 redef fun html_short_signature
do
487 if mproperty
.is_root_init
and new_msignature
!= null then
488 return new_msignature
.html_short_signature
490 return msignature
.html_short_signature
493 redef fun html_signature
do
494 if mproperty
.is_root_init
and new_msignature
!= null then
495 return new_msignature
.html_signature
497 return msignature
.html_signature
501 redef class MVirtualTypeProp
502 redef fun html_link
do return mvirtualtype
.html_link
505 redef class MVirtualTypeDef
507 redef fun html_modifiers
do
513 redef fun html_short_signature
do return new Template
515 redef fun html_signature
do
516 var tpl
= new Template
517 if bound
== null then return tpl
519 tpl
.add bound
.html_signature
525 # Returns the signature of this type whithout bounds.
526 fun html_short_signature
: Template is abstract
528 # Returns the signature of this type.
529 fun html_signature
: Template is abstract
532 redef class MClassType
533 redef fun html_link
do return mclass
.html_link
534 redef fun html_short_signature
do return html_link
535 redef fun html_signature
do return html_link
538 redef class MNullableType
540 redef fun html_short_signature
do
541 var tpl
= new Template
543 tpl
.add mtype
.html_short_signature
547 redef fun html_signature
do
548 var tpl
= new Template
550 tpl
.add mtype
.html_signature
555 redef class MGenericType
556 redef fun html_short_signature
do
558 var tpl
= new Template
559 tpl
.add
new Link.with_title
(lnk
.href
, mclass
.name
.html_escape
, lnk
.title
)
561 for i
in [0..arguments
.length
[ do
562 tpl
.add arguments
[i
].html_short_signature
563 if i
< arguments
.length
- 1 then tpl
.add
", "
569 redef fun html_signature
do
571 var tpl
= new Template
572 tpl
.add
new Link.with_title
(lnk
.href
, mclass
.name
.html_escape
, lnk
.title
)
574 for i
in [0..arguments
.length
[ do
575 tpl
.add arguments
[i
].html_signature
576 if i
< arguments
.length
- 1 then tpl
.add
", "
583 redef class MParameterType
584 redef fun html_link
do
585 return new Link.with_title
("{mclass.nitdoc_url}#FT_{name.to_cmangle}", name
, "formal type")
588 redef fun html_short_signature
do return html_link
589 redef fun html_signature
do return html_link
592 redef class MVirtualType
593 redef fun html_link
do return mproperty
.intro
.html_link
594 redef fun html_signature
do return html_link
597 redef class MSignature
599 redef fun html_short_signature
do
600 var tpl
= new Template
601 if not mparameters
.is_empty
then
603 for i
in [0..mparameters
.length
[ do
604 tpl
.add mparameters
[i
].html_short_signature
605 if i
< mparameters
.length
- 1 then tpl
.add
", "
612 redef fun html_signature
do
613 var tpl
= new Template
614 if not mparameters
.is_empty
then
616 for i
in [0..mparameters
.length
[ do
617 tpl
.add mparameters
[i
].html_signature
618 if i
< mparameters
.length
- 1 then tpl
.add
", "
622 if return_mtype
!= null then
624 tpl
.add return_mtype
.html_signature
630 redef class MParameter
632 # Returns `self` name and ellipsys if any.
633 fun html_short_signature
: Template do
634 var tpl
= new Template
636 if is_vararg
then tpl
.add
"..."
640 # Returns `self` name with it's static type and ellipsys if any.
641 fun html_signature
: Template do
642 var tpl
= new Template
644 tpl
.add mtype
.html_signature
645 if is_vararg
then tpl
.add
"..."
650 redef class ConcernsTree
651 # Render `self` as a hierarchical UnorderedList.
652 fun html_list
: UnorderedList do
653 var lst
= new UnorderedList
654 lst
.css_classes
.add
"list-unstyled list-definition"
656 var li
= r
.html_concern_item
658 build_html_list
(r
, li
)
663 # Build the html list recursively.
664 private fun build_html_list
(e
: MConcern, li
: ListItem) do
665 if not sub
.has_key
(e
) then return
667 var lst
= new UnorderedList
668 lst
.css_classes
.add
"list-unstyled list-definition"
670 if e2
isa MGroup and e2
.is_root
then
671 build_html_list
(e2
, li
)
673 var sli
= e2
.html_concern_item
675 build_html_list
(e2
, sli
)
678 var text
= new Template
680 if not lst
.is_empty
then text
.add lst
686 # Return a li element for `self` that can be displayed in a concern list
687 private fun html_concern_item
: ListItem do
689 var tpl
= new Template
690 tpl
.add
new Link.with_title
("#{nitdoc_id}.concern", lnk
.text
, lnk
.title
)
691 var comment
= html_short_comment
692 if comment
!= null then
696 return new ListItem(tpl
)
700 ################################################################################
701 # Additions to `model_ext`.
704 redef fun html_signature
do
705 var tpl
= new Template
708 if part
.target
!= null then
709 tpl
.add part
.target
.as(not null).html_link
711 tpl
.add part
.text
.html_escape
718 redef class MInnerClass
719 redef fun nitdoc_url
do return inner
.nitdoc_url
720 redef fun html_signature
do return inner
.html_signature
723 redef class MInnerClassDef
724 redef fun nitdoc_url
do return inner
.nitdoc_url
726 redef fun html_link_to_anchor
do return inner
.html_link_to_anchor
727 redef fun html_link
do return inner
.html_link
728 redef fun html_signature
do return inner
.html_signature