6d67e2dbc760defee3b03954817a2029f70c333b
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]
108 # HTML Tree containing all the nested element of `self`.
110 # The nested elements depend on the type of `self`:
111 # `MPackage`: root mgroup
112 # `MGroup`: directly nested mgroups, mmodules
113 # `MModule`: mclassdefs
114 # `MClassDef`: mpropdefs
115 fun html_tree
: UnorderedList do
116 var list
= new UnorderedList
117 list
.add_li html_tree_li
121 # HTML Tree list item used by `html_tree`.
122 private fun html_tree_li
: ListItem do return new ListItem(html_link
)
127 redef fun html_raw_namespace
do return html_name
129 redef fun html_tree_li
do
130 var tpl
= new Template
132 var list
= new UnorderedList
135 list
.add_li root
.html_tree_li
138 return new ListItem(tpl
)
141 redef var html_modifiers
= ["package"]
142 redef fun html_namespace
do return html_link
143 redef var css_classes
= ["public"]
147 redef fun html_raw_namespace
do
148 var parent
= self.parent
149 if parent
!= null then
150 return "{parent.html_raw_namespace}::{html_name}"
152 return "{mpackage.html_raw_namespace}::{html_name}"
155 redef var html_modifiers
= ["group"]
157 # Depends if `self` is root or not.
159 # * If root `mpackage`.
160 # * Else `mpackage::self`.
161 redef fun html_namespace
do
162 var tpl
= new Template
163 tpl
.add mpackage
.html_namespace
164 if mpackage
.root
!= self then
171 redef var css_classes
= ["public"]
173 redef fun html_tree_li
do
174 var tpl
= new Template
176 var list
= new UnorderedList
177 for mgroup
in in_nesting
.direct_smallers
do
178 list
.add_li mgroup
.html_tree_li
180 for mmodule
in mmodules
do
181 list
.add_li mmodule
.html_tree_li
184 return new ListItem(tpl
)
190 redef var html_modifiers
= ["module"]
192 # Depends if `self` belongs to a MGroup.
194 # * If mgroup `mgroup::self`.
196 redef fun html_namespace
do
197 var mgroup
= self.mgroup
198 var tpl
= new Template
199 if mgroup
!= null then
200 tpl
.add mgroup
.html_namespace
207 redef fun html_raw_namespace
do
208 var mpackage
= self.mpackage
209 var mgroup
= self.mgroup
210 if mgroup
!= null then
211 return "{mgroup.html_raw_namespace}::{html_name}"
212 else if mpackage
!= null then
213 return "{mpackage.html_raw_namespace}::{html_name}"
218 redef var css_classes
= ["public"]
220 redef fun html_tree_li
do
221 var tpl
= new Template
223 var list
= new UnorderedList
224 for mclassdef
in mclassdefs
do
225 list
.add_li mclassdef
.html_tree_li
228 return new ListItem(tpl
)
233 redef fun mdoc_or_fallback
do return intro
.mdoc
236 redef var html_name
is lazy
do
237 var tpl
= new Template
238 tpl
.add name
.html_escape
241 var parameter_names
= new Array[String]
242 for p
in mparameters
do
243 parameter_names
.add
(p
.html_name
)
245 tpl
.add parameter_names
.join
(", ")
248 return tpl
.write_to_string
251 redef fun html_modifiers
do return intro
.html_modifiers
252 redef fun html_declaration
do return intro
.html_declaration
254 # Returns `mpackage::self`.
255 redef fun html_namespace
do
256 var mgroup
= intro_mmodule
.mgroup
257 var tpl
= new Template
258 if mgroup
!= null then
259 tpl
.add mgroup
.mpackage
.html_namespace
268 redef fun html_raw_namespace
do return intro
.html_raw_namespace
270 # Returns `intro.html_short_signature`.
271 fun html_short_signature
: Template do return intro
.html_short_signature
273 # Returns `intro.html_signature`.
274 fun html_signature
: Template do return intro
.html_signature
276 redef fun html_icon
do return intro
.html_icon
277 redef fun css_classes
do return intro
.css_classes
280 redef class MClassDef
281 redef fun html_raw_namespace
do return "{mmodule.html_raw_namespace}::{html_name}"
283 redef fun html_tree_li
do
284 var tpl
= new Template
286 var list
= new UnorderedList
287 for mpropdef
in mpropdefs
do
288 list
.add_li mpropdef
.html_tree_li
291 return new ListItem(tpl
)
294 redef fun mdoc_or_fallback
do return mdoc
or else mclass
.mdoc_or_fallback
296 # Depends if `self` is an intro or not.
298 # * If intro contains the visibility and kind.
299 # * If redef contains the `redef` keyword and kind.
300 redef fun html_modifiers
do
301 var res
= new Array[String]
305 if mclass
.visibility
!= public_visibility
then
306 res
.add mclass
.visibility
.to_s
309 res
.add mclass
.kind
.to_s
313 # Depends if `self` is an intro or not.
315 # For intro: `private abstract class Foo[E: Object]`
316 # For redef: `redef class Foo[E]`
317 redef fun html_declaration
do
318 var tpl
= new Template
320 tpl
.add html_modifiers
.join
(" ")
324 tpl
.add html_signature
326 tpl
.add html_short_signature
332 # Returns `mmodule::self`
333 redef fun html_namespace
do
334 var tpl
= new Template
335 tpl
.add mmodule
.html_namespace
337 tpl
.add mclass
.html_link
342 # Returns the MClassDef generic signature without static bounds.
343 fun html_short_signature
: Template do
344 var tpl
= new Template
345 var mparameters
= mclass
.mparameters
346 if not mparameters
.is_empty
then
348 for i
in [0..mparameters
.length
[ do
349 tpl
.add mparameters
[i
].html_name
350 if i
< mparameters
.length
- 1 then tpl
.add
", "
357 # Returns the MClassDef generic signature with static bounds.
358 fun html_signature
: Template do
359 var tpl
= new Template
360 var mparameters
= mclass
.mparameters
361 if not mparameters
.is_empty
then
363 for i
in [0..mparameters
.length
[ do
364 tpl
.add
"{mparameters[i].html_name}: "
365 tpl
.add bound_mtype
.arguments
[i
].html_signature
366 if i
< mparameters
.length
- 1 then tpl
.add
", "
373 redef fun css_classes
do
374 var set
= new HashSet[String]
375 if is_intro
then set
.add
"intro"
376 for m
in mclass
.intro
.modifiers
do set
.add m
.to_cmangle
377 for m
in modifiers
do set
.add m
.to_cmangle
382 # List of all modifiers like redef, private etc.
383 var modifiers
: Array[String] is lazy
do
384 var res
= new Array[String]
388 res
.add mclass
.visibility
.to_s
390 res
.add mclass
.kind
.to_s
395 redef class MProperty
396 redef fun mdoc_or_fallback
do return intro
.mdoc
397 redef fun html_modifiers
do return intro
.html_modifiers
398 redef fun html_declaration
do return intro
.html_declaration
400 # Returns `mclass::self`.
401 redef fun html_namespace
do
402 var tpl
= new Template
403 tpl
.add intro_mclassdef
.mclass
.html_namespace
405 tpl
.add intro
.html_link
410 redef fun html_raw_namespace
do return intro
.html_raw_namespace
412 # Returns `intro.html_short_signature`.
413 fun html_short_signature
: Template do return intro
.html_short_signature
415 # Returns `intro.html_signature`.
416 fun html_signature
: Template do return intro
.html_signature
418 redef fun css_classes
do return intro
.css_classes
422 redef fun html_raw_namespace
do return "{mclassdef.html_raw_namespace}::{html_name}"
423 redef fun mdoc_or_fallback
do return mdoc
or else mproperty
.mdoc_or_fallback
425 # Depends if `self` is an intro or not.
427 # * If intro contains the visibility and kind.
428 # * If redef contains the `redef` keyword and kind.
429 redef fun html_modifiers
do
430 var res
= new Array[String]
434 if mproperty
.visibility
!= public_visibility
then
435 res
.add mproperty
.visibility
.to_s
441 # Depends if `self` is an intro or not.
443 # For intro: `private fun foo(e: Object): Bar is abstract`
444 # For redef: `redef fun foo(e) is cached`
445 redef fun html_declaration
do
446 var tpl
= new Template
448 tpl
.add html_modifiers
.join
(" ")
452 tpl
.add html_signature
454 tpl
.add mproperty
.intro
.html_link
455 tpl
.add html_short_signature
461 # Returns `mclassdef::self`
462 redef fun html_namespace
do
463 var tpl
= new Template
464 tpl
.add mclassdef
.html_namespace
470 # Returns the MPropdDef signature without static types.
471 fun html_short_signature
: Template is abstract
473 # Returns the MPropDef signature with static types.
474 fun html_signature
: Template is abstract
476 redef fun css_classes
do
477 var set
= new HashSet[String]
478 if is_intro
then set
.add
"intro"
479 for m
in mproperty
.intro
.modifiers
do set
.add m
.to_cmangle
480 for m
in modifiers
do set
.add m
.to_cmangle
484 # List of all modifiers like redef, private, abstract, intern, fun etc.
485 var modifiers
: Array[String] is lazy
do
486 var res
= new Array[String]
490 res
.add mproperty
.visibility
.to_s
493 if mprop
isa MVirtualTypeDef then
495 else if mprop
isa MMethodDef then
496 if mprop
.is_abstract
then
498 else if mprop
.is_intern
then
501 if mprop
.mproperty
.is_init
then
511 redef class MAttributeDef
513 redef fun html_modifiers
do
519 redef fun html_short_signature
do return new Template
521 redef fun html_signature
do
522 var static_mtype
= self.static_mtype
523 var tpl
= new Template
524 if static_mtype
!= null then
526 tpl
.add static_mtype
.html_signature
532 redef class MMethodDef
534 # FIXME annotation should be handled in their own way
535 redef fun html_modifiers
do
536 if mproperty
.is_init
then
537 var res
= new Array[String]
538 if mproperty
.visibility
!= public_visibility
then
539 res
.add mproperty
.visibility
.to_s
546 else if is_intern
then
553 redef fun html_declaration
do
554 if mproperty
.is_init
then
555 var tpl
= new Template
557 tpl
.add html_modifiers
.join
(" ")
560 tpl
.add html_signature
567 redef fun html_short_signature
do
568 var new_msignature
= self.new_msignature
569 if mproperty
.is_root_init
and new_msignature
!= null then
570 return new_msignature
.html_short_signature
572 return msignature
.as(not null).html_short_signature
575 redef fun html_signature
do
576 var new_msignature
= self.new_msignature
577 if mproperty
.is_root_init
and new_msignature
!= null then
578 return new_msignature
.html_signature
580 return msignature
.as(not null).html_signature
584 redef class MVirtualTypeProp
585 redef fun html_link
do return mvirtualtype
.html_link
588 redef class MVirtualTypeDef
590 redef fun html_modifiers
do
596 redef fun html_short_signature
do return new Template
598 redef fun html_signature
do
599 var bound
= self.bound
600 var tpl
= new Template
601 if bound
== null then return tpl
603 tpl
.add bound
.html_signature
609 # Returns the signature of this type whithout bounds.
610 fun html_short_signature
: Template is abstract
612 # Returns the signature of this type.
613 fun html_signature
: Template is abstract
616 redef class MClassType
617 redef fun html_link
do return mclass
.html_link
618 redef fun html_short_signature
do return html_link
619 redef fun html_signature
do return html_link
622 redef class MNullableType
623 redef fun html_short_signature
do
624 var tpl
= new Template
626 tpl
.add mtype
.html_short_signature
630 redef fun html_signature
do
631 var tpl
= new Template
633 tpl
.add mtype
.html_signature
638 redef class MGenericType
639 redef fun html_short_signature
do
641 var tpl
= new Template
642 tpl
.add
new Link.with_title
(lnk
.href
, mclass
.name
.html_escape
, lnk
.title
)
644 for i
in [0..arguments
.length
[ do
645 tpl
.add arguments
[i
].html_short_signature
646 if i
< arguments
.length
- 1 then tpl
.add
", "
652 redef fun html_signature
do
654 var tpl
= new Template
655 tpl
.add
new Link.with_title
(lnk
.href
, mclass
.name
.html_escape
, lnk
.title
)
657 for i
in [0..arguments
.length
[ do
658 tpl
.add arguments
[i
].html_signature
659 if i
< arguments
.length
- 1 then tpl
.add
", "
666 redef class MParameterType
667 redef fun html_short_signature
do return html_link
668 redef fun html_signature
do return html_link
669 redef fun html_raw_namespace
do return html_name
672 redef class MVirtualType
673 redef fun html_signature
do return html_link
674 redef fun html_raw_namespace
do return html_name
677 redef class MSignature
678 redef fun html_short_signature
do
679 var tpl
= new Template
680 if not mparameters
.is_empty
then
682 for i
in [0..mparameters
.length
[ do
683 tpl
.add mparameters
[i
].html_short_signature
684 if i
< mparameters
.length
- 1 then tpl
.add
", "
691 redef fun html_signature
do
692 var tpl
= new Template
693 if not mparameters
.is_empty
then
695 for i
in [0..mparameters
.length
[ do
696 tpl
.add mparameters
[i
].html_signature
697 if i
< mparameters
.length
- 1 then tpl
.add
", "
701 var return_mtype
= self.return_mtype
702 if return_mtype
!= null then
704 tpl
.add return_mtype
.html_signature
710 redef class MParameter
712 # Returns `self` name and ellipsys if any.
713 fun html_short_signature
: Template do
714 var tpl
= new Template
716 if is_vararg
then tpl
.add
"..."
720 # Returns `self` name with it's static type and ellipsys if any.
721 fun html_signature
: Template do
722 var tpl
= new Template
724 tpl
.add mtype
.html_signature
725 if is_vararg
then tpl
.add
"..."