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 # Returns the MEntity full_name escaped for html.
36 var html_full_name
: String is lazy
do return full_name
.html_escape
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/{full_name}", 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 var html_modifiers
= ["package"]
111 redef fun html_namespace
do return html_link
112 redef var css_classes
= ["public"]
116 redef var html_modifiers
= ["group"]
118 # Depends if `self` is root or not.
120 # * If root `mpackage`.
121 # * Else `mpackage::self`.
122 redef fun html_namespace
do
123 var tpl
= new Template
124 tpl
.add mpackage
.html_namespace
125 if mpackage
.root
!= self then
132 redef var css_classes
= ["public"]
137 redef var html_modifiers
= ["module"]
139 # Depends if `self` belongs to a MGroup.
141 # * If mgroup `mgroup::self`.
143 redef fun html_namespace
do
144 var mgroup
= self.mgroup
145 var tpl
= new Template
146 if mgroup
!= null then
147 tpl
.add mgroup
.html_namespace
154 redef var css_classes
= ["public"]
158 redef fun mdoc_or_fallback
do return intro
.mdoc
161 redef var html_name
is lazy
do
162 var tpl
= new Template
163 tpl
.add name
.html_escape
166 var parameter_names
= new Array[String]
167 for p
in mparameters
do
168 parameter_names
.add
(p
.html_name
)
170 tpl
.add parameter_names
.join
(", ")
173 return tpl
.write_to_string
176 redef fun html_modifiers
do return intro
.html_modifiers
177 redef fun html_declaration
do return intro
.html_declaration
179 # Returns `mpackage::self`.
180 redef fun html_namespace
do
181 var mgroup
= intro_mmodule
.mgroup
182 var tpl
= new Template
183 if mgroup
!= null then
184 tpl
.add mgroup
.mpackage
.html_namespace
193 # Returns `intro.html_short_signature`.
194 fun html_short_signature
: Template do return intro
.html_short_signature
196 # Returns `intro.html_signature`.
197 fun html_signature
: Template do return intro
.html_signature
199 redef fun html_icon
do return intro
.html_icon
200 redef fun css_classes
do return intro
.css_classes
203 redef class MClassDef
204 redef fun mdoc_or_fallback
do return mdoc
or else mclass
.mdoc_or_fallback
206 # Depends if `self` is an intro or not.
208 # * If intro contains the visibility and kind.
209 # * If redef contains the `redef` keyword and kind.
210 redef fun html_modifiers
do
211 var res
= new Array[String]
215 if mclass
.visibility
!= public_visibility
then
216 res
.add mclass
.visibility
.to_s
219 res
.add mclass
.kind
.to_s
223 # Depends if `self` is an intro or not.
225 # For intro: `private abstract class Foo[E: Object]`
226 # For redef: `redef class Foo[E]`
227 redef fun html_declaration
do
228 var tpl
= new Template
230 tpl
.add html_modifiers
.join
(" ")
234 tpl
.add html_signature
236 tpl
.add html_short_signature
242 # Returns `mmodule::self`
243 redef fun html_namespace
do
244 var tpl
= new Template
245 tpl
.add mmodule
.html_namespace
247 tpl
.add mclass
.html_link
252 # Returns the MClassDef generic signature without static bounds.
253 fun html_short_signature
: Template do
254 var tpl
= new Template
255 var mparameters
= mclass
.mparameters
256 if not mparameters
.is_empty
then
258 for i
in [0..mparameters
.length
[ do
259 tpl
.add mparameters
[i
].html_name
260 if i
< mparameters
.length
- 1 then tpl
.add
", "
267 # Returns the MClassDef generic signature with static bounds.
268 fun html_signature
: Template do
269 var tpl
= new Template
270 var mparameters
= mclass
.mparameters
271 if not mparameters
.is_empty
then
273 for i
in [0..mparameters
.length
[ do
274 tpl
.add
"{mparameters[i].html_name}: "
275 tpl
.add bound_mtype
.arguments
[i
].html_signature
276 if i
< mparameters
.length
- 1 then tpl
.add
", "
283 redef fun css_classes
do
284 var set
= new HashSet[String]
285 if is_intro
then set
.add
"intro"
286 for m
in mclass
.intro
.modifiers
do set
.add m
.to_cmangle
287 for m
in modifiers
do set
.add m
.to_cmangle
292 # List of all modifiers like redef, private etc.
293 var modifiers
: Array[String] is lazy
do
294 var res
= new Array[String]
298 res
.add mclass
.visibility
.to_s
300 res
.add mclass
.kind
.to_s
305 redef class MProperty
306 redef fun mdoc_or_fallback
do return intro
.mdoc
307 redef fun html_modifiers
do return intro
.html_modifiers
308 redef fun html_declaration
do return intro
.html_declaration
310 # Returns `mclass::self`.
311 redef fun html_namespace
do
312 var tpl
= new Template
313 tpl
.add intro_mclassdef
.mclass
.html_namespace
315 tpl
.add intro
.html_link
320 # Returns `intro.html_short_signature`.
321 fun html_short_signature
: Template do return intro
.html_short_signature
323 # Returns `intro.html_signature`.
324 fun html_signature
: Template do return intro
.html_signature
326 redef fun css_classes
do return intro
.css_classes
330 redef fun mdoc_or_fallback
do return mdoc
or else mproperty
.mdoc_or_fallback
332 # Depends if `self` is an intro or not.
334 # * If intro contains the visibility and kind.
335 # * If redef contains the `redef` keyword and kind.
336 redef fun html_modifiers
do
337 var res
= new Array[String]
341 if mproperty
.visibility
!= public_visibility
then
342 res
.add mproperty
.visibility
.to_s
348 # Depends if `self` is an intro or not.
350 # For intro: `private fun foo(e: Object): Bar is abstract`
351 # For redef: `redef fun foo(e) is cached`
352 redef fun html_declaration
do
353 var tpl
= new Template
355 tpl
.add html_modifiers
.join
(" ")
359 tpl
.add html_signature
361 tpl
.add mproperty
.intro
.html_link
362 tpl
.add html_short_signature
368 # Returns `mclassdef::self`
369 redef fun html_namespace
do
370 var tpl
= new Template
371 tpl
.add mclassdef
.html_namespace
377 # Returns the MPropdDef signature without static types.
378 fun html_short_signature
: Template is abstract
380 # Returns the MPropDef signature with static types.
381 fun html_signature
: Template is abstract
383 redef fun css_classes
do
384 var set
= new HashSet[String]
385 if is_intro
then set
.add
"intro"
386 for m
in mproperty
.intro
.modifiers
do set
.add m
.to_cmangle
387 for m
in modifiers
do set
.add m
.to_cmangle
391 # List of all modifiers like redef, private, abstract, intern, fun etc.
392 var modifiers
: Array[String] is lazy
do
393 var res
= new Array[String]
397 res
.add mproperty
.visibility
.to_s
400 if mprop
isa MVirtualTypeDef then
402 else if mprop
isa MMethodDef then
403 if mprop
.is_abstract
then
405 else if mprop
.is_intern
then
408 if mprop
.mproperty
.is_init
then
418 redef class MAttributeDef
420 redef fun html_modifiers
do
426 redef fun html_short_signature
do return new Template
428 redef fun html_signature
do
429 var static_mtype
= self.static_mtype
430 var tpl
= new Template
431 if static_mtype
!= null then
433 tpl
.add static_mtype
.html_signature
439 redef class MMethodDef
441 # FIXME annotation should be handled in their own way
442 redef fun html_modifiers
do
443 if mproperty
.is_init
then
444 var res
= new Array[String]
445 if mproperty
.visibility
!= public_visibility
then
446 res
.add mproperty
.visibility
.to_s
453 else if is_intern
then
460 redef fun html_declaration
do
461 if mproperty
.is_init
then
462 var tpl
= new Template
464 tpl
.add html_modifiers
.join
(" ")
467 tpl
.add html_signature
474 redef fun html_short_signature
do
475 var new_msignature
= self.new_msignature
476 if mproperty
.is_root_init
and new_msignature
!= null then
477 return new_msignature
.html_short_signature
479 return msignature
.as(not null).html_short_signature
482 redef fun html_signature
do
483 var new_msignature
= self.new_msignature
484 if mproperty
.is_root_init
and new_msignature
!= null then
485 return new_msignature
.html_signature
487 return msignature
.as(not null).html_signature
491 redef class MVirtualTypeProp
492 redef fun html_link
do return mvirtualtype
.html_link
495 redef class MVirtualTypeDef
497 redef fun html_modifiers
do
503 redef fun html_short_signature
do return new Template
505 redef fun html_signature
do
506 var bound
= self.bound
507 var tpl
= new Template
508 if bound
== null then return tpl
510 tpl
.add bound
.html_signature
516 # Returns the signature of this type whithout bounds.
517 fun html_short_signature
: Template is abstract
519 # Returns the signature of this type.
520 fun html_signature
: Template is abstract
523 redef class MClassType
524 redef fun html_link
do return mclass
.html_link
525 redef fun html_short_signature
do return html_link
526 redef fun html_signature
do return html_link
529 redef class MNullableType
530 redef fun html_short_signature
do
531 var tpl
= new Template
533 tpl
.add mtype
.html_short_signature
537 redef fun html_signature
do
538 var tpl
= new Template
540 tpl
.add mtype
.html_signature
545 redef class MGenericType
546 redef fun html_short_signature
do
548 var tpl
= new Template
549 tpl
.add
new Link.with_title
(lnk
.href
, mclass
.name
.html_escape
, lnk
.title
)
551 for i
in [0..arguments
.length
[ do
552 tpl
.add arguments
[i
].html_short_signature
553 if i
< arguments
.length
- 1 then tpl
.add
", "
559 redef fun html_signature
do
561 var tpl
= new Template
562 tpl
.add
new Link.with_title
(lnk
.href
, mclass
.name
.html_escape
, lnk
.title
)
564 for i
in [0..arguments
.length
[ do
565 tpl
.add arguments
[i
].html_signature
566 if i
< arguments
.length
- 1 then tpl
.add
", "
573 redef class MParameterType
574 redef fun html_short_signature
do return html_link
575 redef fun html_signature
do return html_link
578 redef class MVirtualType
579 redef fun html_signature
do return html_link
582 redef class MSignature
583 redef fun html_short_signature
do
584 var tpl
= new Template
585 if not mparameters
.is_empty
then
587 for i
in [0..mparameters
.length
[ do
588 tpl
.add mparameters
[i
].html_short_signature
589 if i
< mparameters
.length
- 1 then tpl
.add
", "
596 redef fun html_signature
do
597 var tpl
= new Template
598 if not mparameters
.is_empty
then
600 for i
in [0..mparameters
.length
[ do
601 tpl
.add mparameters
[i
].html_signature
602 if i
< mparameters
.length
- 1 then tpl
.add
", "
606 var return_mtype
= self.return_mtype
607 if return_mtype
!= null then
609 tpl
.add return_mtype
.html_signature
615 redef class MParameter
617 # Returns `self` name and ellipsys if any.
618 fun html_short_signature
: Template do
619 var tpl
= new Template
621 if is_vararg
then tpl
.add
"..."
625 # Returns `self` name with it's static type and ellipsys if any.
626 fun html_signature
: Template do
627 var tpl
= new Template
629 tpl
.add mtype
.html_signature
630 if is_vararg
then tpl
.add
"..."
635 redef class MEntityTree
636 # Render `self` as a hierarchical UnorderedList.
637 fun html_list
: UnorderedList do
638 var lst
= new_unordered_list
640 var li
= new_mentity_item
(r
)
642 build_html_list
(r
, li
)
647 # Build the html list recursively.
648 private fun build_html_list
(e
: MEntity, li
: ListItem) do
649 if not sub
.has_key
(e
) then return
651 var lst
= new_unordered_list
653 if e2
isa MGroup and e2
.is_root
then
654 build_html_list
(e2
, li
)
656 var sli
= new_mentity_item
(e2
)
658 build_html_list
(e2
, sli
)
661 var text
= new Template
663 if not lst
.is_empty
then text
.add lst
667 # HTML unordered List used to compose the tree.
669 # Redefine this method to add custom CSS classes or other html attributes.
670 protected fun new_unordered_list
: UnorderedList do return new UnorderedList
672 # Return a li element for `mconcern` that can be displayed in a concern list
673 protected fun new_mentity_item
(mentity
: MEntity): ListItem do
674 var tpl
= new Template
675 tpl
.add mentity
.html_link
676 var comment
= mentity
.html_synopsis
677 if comment
!= null then
681 return new ListItem(tpl
)