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 # MEntity namespace escaped for html.
36 fun html_raw_namespace
: String is abstract
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/{html_raw_namespace}", 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 fun html_raw_namespace
do return html_name
112 redef var html_modifiers
= ["package"]
113 redef fun html_namespace
do return html_link
114 redef var css_classes
= ["public"]
118 redef fun html_raw_namespace
do
119 var parent
= self.parent
120 if parent
!= null then
121 return "{parent.html_raw_namespace}::{html_name}"
123 return "{mpackage.html_raw_namespace}::{html_name}"
126 redef var html_modifiers
= ["group"]
128 # Depends if `self` is root or not.
130 # * If root `mpackage`.
131 # * Else `mpackage::self`.
132 redef fun html_namespace
do
133 var tpl
= new Template
134 tpl
.add mpackage
.html_namespace
135 if mpackage
.root
!= self then
142 redef var css_classes
= ["public"]
147 redef var html_modifiers
= ["module"]
149 # Depends if `self` belongs to a MGroup.
151 # * If mgroup `mgroup::self`.
153 redef fun html_namespace
do
154 var mgroup
= self.mgroup
155 var tpl
= new Template
156 if mgroup
!= null then
157 tpl
.add mgroup
.html_namespace
164 redef fun html_raw_namespace
do
165 var mpackage
= self.mpackage
166 var mgroup
= self.mgroup
167 if mgroup
!= null then
168 return "{mgroup.html_raw_namespace}::{html_name}"
169 else if mpackage
!= null then
170 return "{mpackage.html_raw_namespace}::{html_name}"
175 redef var css_classes
= ["public"]
179 redef fun mdoc_or_fallback
do return intro
.mdoc
182 redef var html_name
is lazy
do
183 var tpl
= new Template
184 tpl
.add name
.html_escape
187 var parameter_names
= new Array[String]
188 for p
in mparameters
do
189 parameter_names
.add
(p
.html_name
)
191 tpl
.add parameter_names
.join
(", ")
194 return tpl
.write_to_string
197 redef fun html_modifiers
do return intro
.html_modifiers
198 redef fun html_declaration
do return intro
.html_declaration
200 # Returns `mpackage::self`.
201 redef fun html_namespace
do
202 var mgroup
= intro_mmodule
.mgroup
203 var tpl
= new Template
204 if mgroup
!= null then
205 tpl
.add mgroup
.mpackage
.html_namespace
214 redef fun html_raw_namespace
do return intro
.html_raw_namespace
216 # Returns `intro.html_short_signature`.
217 fun html_short_signature
: Template do return intro
.html_short_signature
219 # Returns `intro.html_signature`.
220 fun html_signature
: Template do return intro
.html_signature
222 redef fun html_icon
do return intro
.html_icon
223 redef fun css_classes
do return intro
.css_classes
226 redef class MClassDef
227 redef fun html_raw_namespace
do return "{mmodule.html_raw_namespace}::{html_name}"
229 redef fun mdoc_or_fallback
do return mdoc
or else mclass
.mdoc_or_fallback
231 # Depends if `self` is an intro or not.
233 # * If intro contains the visibility and kind.
234 # * If redef contains the `redef` keyword and kind.
235 redef fun html_modifiers
do
236 var res
= new Array[String]
240 if mclass
.visibility
!= public_visibility
then
241 res
.add mclass
.visibility
.to_s
244 res
.add mclass
.kind
.to_s
248 # Depends if `self` is an intro or not.
250 # For intro: `private abstract class Foo[E: Object]`
251 # For redef: `redef class Foo[E]`
252 redef fun html_declaration
do
253 var tpl
= new Template
255 tpl
.add html_modifiers
.join
(" ")
259 tpl
.add html_signature
261 tpl
.add html_short_signature
267 # Returns `mmodule::self`
268 redef fun html_namespace
do
269 var tpl
= new Template
270 tpl
.add mmodule
.html_namespace
272 tpl
.add mclass
.html_link
277 # Returns the MClassDef generic signature without static bounds.
278 fun html_short_signature
: Template do
279 var tpl
= new Template
280 var mparameters
= mclass
.mparameters
281 if not mparameters
.is_empty
then
283 for i
in [0..mparameters
.length
[ do
284 tpl
.add mparameters
[i
].html_name
285 if i
< mparameters
.length
- 1 then tpl
.add
", "
292 # Returns the MClassDef generic signature with static bounds.
293 fun html_signature
: Template do
294 var tpl
= new Template
295 var mparameters
= mclass
.mparameters
296 if not mparameters
.is_empty
then
298 for i
in [0..mparameters
.length
[ do
299 tpl
.add
"{mparameters[i].html_name}: "
300 tpl
.add bound_mtype
.arguments
[i
].html_signature
301 if i
< mparameters
.length
- 1 then tpl
.add
", "
308 redef fun css_classes
do
309 var set
= new HashSet[String]
310 if is_intro
then set
.add
"intro"
311 for m
in mclass
.intro
.modifiers
do set
.add m
.to_cmangle
312 for m
in modifiers
do set
.add m
.to_cmangle
317 # List of all modifiers like redef, private etc.
318 var modifiers
: Array[String] is lazy
do
319 var res
= new Array[String]
323 res
.add mclass
.visibility
.to_s
325 res
.add mclass
.kind
.to_s
330 redef class MProperty
331 redef fun mdoc_or_fallback
do return intro
.mdoc
332 redef fun html_modifiers
do return intro
.html_modifiers
333 redef fun html_declaration
do return intro
.html_declaration
335 # Returns `mclass::self`.
336 redef fun html_namespace
do
337 var tpl
= new Template
338 tpl
.add intro_mclassdef
.mclass
.html_namespace
340 tpl
.add intro
.html_link
345 redef fun html_raw_namespace
do return intro
.html_raw_namespace
347 # Returns `intro.html_short_signature`.
348 fun html_short_signature
: Template do return intro
.html_short_signature
350 # Returns `intro.html_signature`.
351 fun html_signature
: Template do return intro
.html_signature
353 redef fun css_classes
do return intro
.css_classes
357 redef fun html_raw_namespace
do return "{mclassdef.html_raw_namespace}::{html_name}"
358 redef fun mdoc_or_fallback
do return mdoc
or else mproperty
.mdoc_or_fallback
360 # Depends if `self` is an intro or not.
362 # * If intro contains the visibility and kind.
363 # * If redef contains the `redef` keyword and kind.
364 redef fun html_modifiers
do
365 var res
= new Array[String]
369 if mproperty
.visibility
!= public_visibility
then
370 res
.add mproperty
.visibility
.to_s
376 # Depends if `self` is an intro or not.
378 # For intro: `private fun foo(e: Object): Bar is abstract`
379 # For redef: `redef fun foo(e) is cached`
380 redef fun html_declaration
do
381 var tpl
= new Template
383 tpl
.add html_modifiers
.join
(" ")
387 tpl
.add html_signature
389 tpl
.add mproperty
.intro
.html_link
390 tpl
.add html_short_signature
396 # Returns `mclassdef::self`
397 redef fun html_namespace
do
398 var tpl
= new Template
399 tpl
.add mclassdef
.html_namespace
405 # Returns the MPropdDef signature without static types.
406 fun html_short_signature
: Template is abstract
408 # Returns the MPropDef signature with static types.
409 fun html_signature
: Template is abstract
411 redef fun css_classes
do
412 var set
= new HashSet[String]
413 if is_intro
then set
.add
"intro"
414 for m
in mproperty
.intro
.modifiers
do set
.add m
.to_cmangle
415 for m
in modifiers
do set
.add m
.to_cmangle
419 # List of all modifiers like redef, private, abstract, intern, fun etc.
420 var modifiers
: Array[String] is lazy
do
421 var res
= new Array[String]
425 res
.add mproperty
.visibility
.to_s
428 if mprop
isa MVirtualTypeDef then
430 else if mprop
isa MMethodDef then
431 if mprop
.is_abstract
then
433 else if mprop
.is_intern
then
436 if mprop
.mproperty
.is_init
then
446 redef class MAttributeDef
448 redef fun html_modifiers
do
454 redef fun html_short_signature
do return new Template
456 redef fun html_signature
do
457 var static_mtype
= self.static_mtype
458 var tpl
= new Template
459 if static_mtype
!= null then
461 tpl
.add static_mtype
.html_signature
467 redef class MMethodDef
469 # FIXME annotation should be handled in their own way
470 redef fun html_modifiers
do
471 if mproperty
.is_init
then
472 var res
= new Array[String]
473 if mproperty
.visibility
!= public_visibility
then
474 res
.add mproperty
.visibility
.to_s
481 else if is_intern
then
488 redef fun html_declaration
do
489 if mproperty
.is_init
then
490 var tpl
= new Template
492 tpl
.add html_modifiers
.join
(" ")
495 tpl
.add html_signature
502 redef fun html_short_signature
do
503 var new_msignature
= self.new_msignature
504 if mproperty
.is_root_init
and new_msignature
!= null then
505 return new_msignature
.html_short_signature
507 return msignature
.as(not null).html_short_signature
510 redef fun html_signature
do
511 var new_msignature
= self.new_msignature
512 if mproperty
.is_root_init
and new_msignature
!= null then
513 return new_msignature
.html_signature
515 return msignature
.as(not null).html_signature
519 redef class MVirtualTypeProp
520 redef fun html_link
do return mvirtualtype
.html_link
523 redef class MVirtualTypeDef
525 redef fun html_modifiers
do
531 redef fun html_short_signature
do return new Template
533 redef fun html_signature
do
534 var bound
= self.bound
535 var tpl
= new Template
536 if bound
== null then return tpl
538 tpl
.add bound
.html_signature
544 # Returns the signature of this type whithout bounds.
545 fun html_short_signature
: Template is abstract
547 # Returns the signature of this type.
548 fun html_signature
: Template is abstract
551 redef class MClassType
552 redef fun html_link
do return mclass
.html_link
553 redef fun html_short_signature
do return html_link
554 redef fun html_signature
do return html_link
557 redef class MNullableType
558 redef fun html_short_signature
do
559 var tpl
= new Template
561 tpl
.add mtype
.html_short_signature
565 redef fun html_signature
do
566 var tpl
= new Template
568 tpl
.add mtype
.html_signature
573 redef class MGenericType
574 redef fun html_short_signature
do
576 var tpl
= new Template
577 tpl
.add
new Link.with_title
(lnk
.href
, mclass
.name
.html_escape
, lnk
.title
)
579 for i
in [0..arguments
.length
[ do
580 tpl
.add arguments
[i
].html_short_signature
581 if i
< arguments
.length
- 1 then tpl
.add
", "
587 redef fun html_signature
do
589 var tpl
= new Template
590 tpl
.add
new Link.with_title
(lnk
.href
, mclass
.name
.html_escape
, lnk
.title
)
592 for i
in [0..arguments
.length
[ do
593 tpl
.add arguments
[i
].html_signature
594 if i
< arguments
.length
- 1 then tpl
.add
", "
601 redef class MParameterType
602 redef fun html_short_signature
do return html_link
603 redef fun html_signature
do return html_link
604 redef fun html_raw_namespace
do return html_name
607 redef class MVirtualType
608 redef fun html_signature
do return html_link
609 redef fun html_raw_namespace
do return html_name
612 redef class MSignature
613 redef fun html_short_signature
do
614 var tpl
= new Template
615 if not mparameters
.is_empty
then
617 for i
in [0..mparameters
.length
[ do
618 tpl
.add mparameters
[i
].html_short_signature
619 if i
< mparameters
.length
- 1 then tpl
.add
", "
626 redef fun html_signature
do
627 var tpl
= new Template
628 if not mparameters
.is_empty
then
630 for i
in [0..mparameters
.length
[ do
631 tpl
.add mparameters
[i
].html_signature
632 if i
< mparameters
.length
- 1 then tpl
.add
", "
636 var return_mtype
= self.return_mtype
637 if return_mtype
!= null then
639 tpl
.add return_mtype
.html_signature
645 redef class MParameter
647 # Returns `self` name and ellipsys if any.
648 fun html_short_signature
: Template do
649 var tpl
= new Template
651 if is_vararg
then tpl
.add
"..."
655 # Returns `self` name with it's static type and ellipsys if any.
656 fun html_signature
: Template do
657 var tpl
= new Template
659 tpl
.add mtype
.html_signature
660 if is_vararg
then tpl
.add
"..."
665 redef class MEntityTree
666 # Render `self` as a hierarchical UnorderedList.
667 fun html_list
: UnorderedList do
668 var lst
= new_unordered_list
670 var li
= new_mentity_item
(r
)
672 build_html_list
(r
, li
)
677 # Build the html list recursively.
678 private fun build_html_list
(e
: MEntity, li
: ListItem) do
679 if not sub
.has_key
(e
) then return
681 var lst
= new_unordered_list
683 if e2
isa MGroup and e2
.is_root
then
684 build_html_list
(e2
, li
)
686 var sli
= new_mentity_item
(e2
)
688 build_html_list
(e2
, sli
)
691 var text
= new Template
693 if not lst
.is_empty
then text
.add lst
697 # HTML unordered List used to compose the tree.
699 # Redefine this method to add custom CSS classes or other html attributes.
700 protected fun new_unordered_list
: UnorderedList do return new UnorderedList
702 # Return a li element for `mconcern` that can be displayed in a concern list
703 protected fun new_mentity_item
(mentity
: MEntity): ListItem do
704 var tpl
= new Template
705 tpl
.add mentity
.html_link
706 var comment
= mentity
.html_synopsis
707 if comment
!= null then
711 return new ListItem(tpl
)