ebc6eff254cf5621414bb41002b2924527724fcb
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 # Github url based on this location
26 fun github
(gitdir
: String): String do
27 var base_dir
= getcwd
.join_path
(gitdir
).simplify_path
28 var file_loc
= getcwd
.join_path
(file
.filename
).simplify_path
29 var gith_loc
= file_loc
.substring
(base_dir
.length
+ 1, file_loc
.length
)
30 return "{gith_loc}:{line_start},{column_start}--{line_end},{column_end}"
35 # ID used as a HTML unique ID and in file names.
37 # **Must** match the following (POSIX ERE) regular expression:
40 # ^[A-Za-z_][A-Za-z0-9._-]*$
43 # That way, the ID is always a valid URI component and a valid XML name.
44 fun nitdoc_id
: String is abstract
46 # URL of this entity’s Nitdoc page.
47 fun nitdoc_url
: String is abstract
49 # Returns the mentity name without short signature.
55 # * MClassDef: `Foo[E]`
56 # * MProperty: `foo(e)`
57 # * MPropdef: `foo(e)`
58 var html_name
: String is lazy
do return name
.html_escape
60 # Returns a Link to the mentity `html_url`.
62 # Example: `<a href="html_url" title="mdoc.short_comment">html_short_name</a>
63 var html_link
: Link is lazy
do
64 var tpl
= new Link(nitdoc_url
, html_name
)
65 var mdoc
= mdoc_or_fallback
67 tpl
.title
= mdoc
.short_comment
72 # Returns a Link to the mentity `nitdoc_id`.
74 # Example: `<a href="#nitdoc_id" title="mdoc.short_comment">html_short_name</a>
75 fun html_link_to_anchor
: Link do
76 var tpl
= new Link("#{nitdoc_id}", html_name
)
77 var mdoc
= mdoc_or_fallback
79 tpl
.title
= mdoc
.short_comment
84 # Returns the list of keyword used in `self` declaration.
85 fun html_modifiers
: Array[String] is abstract
87 # Returns the complete MEntity declaration decorated with HTML.
89 # * MProject: `project foo`
90 # * MGroup: `group foo`
91 # * MModule: `module foo`
92 # * MClass: `private abstract class Foo[E: Object]`
93 # * MClassDef: `redef class Foo[E]`
94 # * MProperty: `private fun foo(e: Object): Int`
95 # * MPropdef: `redef fun foo(e)`
96 fun html_declaration
: Template do
97 var tpl
= new Template
99 tpl
.add html_modifiers
.join
(" ")
106 # A template article that briefly describe the entity
107 fun tpl_short_article
: TplArticle do
108 var tpl
= tpl_article
109 var mdoc
= mdoc_or_fallback
111 tpl
.content
= mdoc
.tpl_short_comment
116 # A template article that describe the entity
117 fun tpl_article
: TplArticle do
118 var tpl
= new TplArticle.with_title
(nitdoc_id
, tpl_title
)
119 tpl
.title_classes
.add
"signature"
120 tpl
.subtitle
= tpl_namespace
121 tpl
.summary_title
= html_name
125 # A template namespace
126 fun tpl_namespace
: Template is abstract
128 # A template definition of the mentity
129 # include name, sysnopsys, comment and namespace
130 fun tpl_definition
: TplDefinition is abstract
132 # A li element that can go in a list
133 fun tpl_list_item
: TplListItem do
134 var lnk
= new Template
135 lnk
.add
new TplLabel.with_classes
(tpl_css_classes
)
137 var mdoc
= mdoc_or_fallback
140 lnk
.add mdoc
.tpl_short_comment
142 return new TplListItem.with_content
(lnk
)
145 var tpl_css_classes
= new Array[String]
147 # Box title for this mentity
148 fun tpl_title
: Template do
149 var title
= new Template
151 title
.add tpl_namespace
155 # Icon that will be displayed before the title
156 fun tpl_icon
: TplIcon do
157 var icon
= new TplIcon.with_icon
("tag")
158 icon
.css_classes
.add_all
(tpl_css_classes
)
164 # Return a li element for `self` that can be displayed in a concern list
165 private fun tpl_concern_item
: TplListItem do
166 var lnk
= new Template
167 lnk
.add html_link_to_anchor
168 var mdoc
= mdoc_or_fallback
171 lnk
.add mdoc
.tpl_short_comment
173 return new TplListItem.with_content
(lnk
)
178 redef var nitdoc_id
= name
.to_cmangle
is lazy
179 redef fun nitdoc_url
do return root
.nitdoc_url
180 redef var html_modifiers
= ["project"]
182 redef fun tpl_namespace
do return html_link
184 redef fun tpl_definition
do
185 var tpl
= new TplDefinition
186 var mdoc
= mdoc_or_fallback
188 tpl
.comment
= mdoc
.tpl_comment
193 redef fun tpl_css_classes
do return ["public"]
197 redef var nitdoc_id
is lazy
do
198 if parent
!= null then
199 return "{parent.nitdoc_id}__{name.to_cmangle}"
201 return name
.to_cmangle
204 redef fun nitdoc_url
do return "group_{nitdoc_id}.html"
205 redef var html_modifiers
= ["group"]
207 redef fun tpl_namespace
do
208 var tpl
= new Template
209 tpl
.add mproject
.tpl_namespace
210 if mproject
.root
!= self then
217 redef fun tpl_definition
do
218 var tpl
= new TplDefinition
219 var mdoc
= mdoc_or_fallback
221 tpl
.comment
= mdoc
.tpl_comment
228 redef var nitdoc_id
is lazy
do
229 if mgroup
!= null then
230 if mgroup
.mmodules
.length
== 1 then
231 return "{mgroup.nitdoc_id}-"
233 return "{mgroup.nitdoc_id}__{name.to_cmangle}"
236 return name
.to_cmangle
239 redef fun nitdoc_url
do return "module_{nitdoc_id}.html"
240 redef var html_modifiers
= ["module"]
242 redef fun tpl_namespace
do
243 var tpl
= new Template
244 if mgroup
!= null then
245 tpl
.add mgroup
.tpl_namespace
252 redef fun tpl_definition
do
253 var tpl
= new TplClassDefinition
254 var mdoc
= mdoc_or_fallback
256 tpl
.comment
= mdoc
.tpl_comment
261 redef fun tpl_css_classes
do return ["public"]
265 redef var nitdoc_id
= "{intro_mmodule.nitdoc_id}__{name.to_cmangle}" is lazy
266 redef fun nitdoc_url
do return "class_{nitdoc_id}.html"
267 redef fun mdoc_or_fallback
do return intro
.mdoc
270 redef var html_name
is lazy
do
271 var tpl
= new Template
272 tpl
.add name
.html_escape
275 var parameter_names
= new Array[String]
276 for p
in mparameters
do
277 parameter_names
.add
(p
.html_name
)
279 tpl
.add parameter_names
.join
(", ")
282 return tpl
.write_to_string
285 redef fun html_modifiers
do return intro
.html_modifiers
286 redef fun html_declaration
do return intro
.html_declaration
288 redef fun tpl_definition
do return intro
.tpl_definition
290 redef fun tpl_namespace
do
291 var tpl
= new Template
292 tpl
.add intro_mmodule
.mgroup
.mproject
.tpl_namespace
299 redef fun tpl_title
do
300 var title
= new Template
306 redef fun tpl_icon
do return intro
.tpl_icon
308 fun tpl_signature
: Template do
309 var tpl
= new Template
312 var parameter_names
= new Array[String]
313 for p
in mparameters
do
314 parameter_names
.add
(p
.html_name
)
316 tpl
.add parameter_names
.join
(", ")
322 redef fun tpl_css_classes
do return intro
.tpl_css_classes
325 redef class MClassDef
326 redef var nitdoc_id
= "{mmodule.nitdoc_id}__{name.to_cmangle}" is lazy
327 redef fun nitdoc_url
do return "{mclass.nitdoc_url}#{nitdoc_id}"
328 redef fun mdoc_or_fallback
do return mdoc
or else mclass
.mdoc_or_fallback
330 # Depends if `self` is an intro or not.
332 # * If intro contains the visibility and kind.
333 # * If redef contains the `redef` keyword and kind.
334 redef fun html_modifiers
do
335 var res
= new Array[String]
339 if mclass
.visibility
!= public_visibility
then
340 res
.add mclass
.visibility
.to_s
343 res
.add mclass
.kind
.to_s
347 # Depends if `self` is an intro or not.
349 # For intro: `private abstract class Foo[E: Object]`
350 # For redef: `redef class Foo[E]`
351 # TODO change the implementation to correspond to the comment.
352 redef fun html_declaration
do
353 var tpl
= new Template
355 tpl
.add html_modifiers
.join
(" ")
358 tpl
.add tpl_signature
363 redef fun tpl_namespace
do
364 var tpl
= new Template
365 tpl
.add mmodule
.tpl_namespace
367 tpl
.add mclass
.html_link
372 redef fun tpl_article
do
373 var tpl
= new TplArticle(nitdoc_id
)
374 tpl
.summary_title
= "in {mmodule.html_name}"
375 tpl
.title
= html_declaration
376 tpl
.title_classes
.add
"signature"
377 var title
= new Template
379 title
.add mmodule
.tpl_namespace
381 var mdoc
= mdoc_or_fallback
383 tpl
.content
= mdoc
.tpl_comment
388 redef fun tpl_title
do
389 var title
= new Template
395 fun tpl_signature
: Template do
396 var tpl
= new Template
397 var mparameters
= mclass
.mparameters
398 if not mparameters
.is_empty
then
400 for i
in [0..mparameters
.length
[ do
401 tpl
.add
"{mparameters[i].html_name}: "
402 tpl
.add bound_mtype
.arguments
[i
].tpl_signature
403 if i
< mparameters
.length
- 1 then tpl
.add
", "
410 redef fun tpl_definition
do
411 var tpl
= new TplClassDefinition
412 var mdoc
= mdoc_or_fallback
414 tpl
.comment
= mdoc
.tpl_comment
419 redef fun tpl_css_classes
do
420 var set
= new HashSet[String]
421 if is_intro
then set
.add
"intro"
422 for m
in mclass
.intro
.modifiers
do set
.add m
.to_cmangle
423 for m
in modifiers
do set
.add m
.to_cmangle
427 fun tpl_modifiers
: Template do
428 var tpl
= new Template
429 for modifier
in modifiers
do
430 if modifier
== "public" then continue
431 tpl
.add
"{modifier.html_escape} "
437 redef class MProperty
438 redef var nitdoc_id
= "{intro_mclassdef.mclass.nitdoc_id}__{name.to_cmangle}" is lazy
439 redef fun nitdoc_url
do return "property_{nitdoc_id}.html"
440 redef fun mdoc_or_fallback
do return intro
.mdoc
441 redef fun html_modifiers
do return intro
.html_modifiers
442 redef fun html_declaration
do return intro
.html_declaration
444 redef fun tpl_namespace
do
445 var tpl
= new Template
446 tpl
.add intro_mclassdef
.mclass
.tpl_namespace
448 tpl
.add intro
.html_link
453 fun tpl_signature
: Template do return new Template
455 redef fun tpl_title
do return intro
.tpl_title
457 redef fun tpl_icon
do return intro
.tpl_icon
459 redef fun tpl_css_classes
do return intro
.tpl_css_classes
463 redef var nitdoc_id
= "{mclassdef.nitdoc_id}__{name.to_cmangle}" is lazy
464 redef fun nitdoc_url
do return "{mproperty.nitdoc_url}#{nitdoc_id}"
465 redef fun mdoc_or_fallback
do return mdoc
or else mproperty
.mdoc_or_fallback
467 # Depends if `self` is an intro or not.
469 # * If intro contains the visibility and kind.
470 # * If redef contains the `redef` keyword and kind.
471 redef fun html_modifiers
do
472 var res
= new Array[String]
476 if mproperty
.visibility
!= public_visibility
then
477 res
.add mproperty
.visibility
.to_s
483 # Depends if `self` is an intro or not.
485 # For intro: `private fun foo(e: Object): Bar is abstract`
486 # For redef: `redef fun foo(e) is cached`
487 # TODO change the implementation to correspond to the comment.
488 redef fun html_declaration
do
489 var tpl
= new Template
491 tpl
.add html_modifiers
.join
(" ")
494 tpl
.add tpl_signature
499 redef fun tpl_namespace
do
500 var tpl
= new Template
501 tpl
.add mclassdef
.tpl_namespace
507 redef fun tpl_article
do
508 var tpl
= new TplArticle(nitdoc_id
)
509 tpl
.summary_title
= "in {mclassdef.html_name}"
510 var title
= new Template
512 title
.add mclassdef
.html_link
514 tpl
.subtitle
= html_declaration
515 var mdoc
= mdoc_or_fallback
517 tpl
.content
= mdoc
.tpl_comment
522 redef fun tpl_definition
do
523 var tpl
= new TplDefinition
524 var mdoc
= mdoc_or_fallback
526 tpl
.comment
= mdoc
.tpl_comment
531 redef fun tpl_css_classes
do
532 var set
= new HashSet[String]
533 if is_intro
then set
.add
"intro"
534 for m
in mproperty
.intro
.modifiers
do set
.add m
.to_cmangle
535 for m
in modifiers
do set
.add m
.to_cmangle
539 fun tpl_modifiers
: Template do
540 var tpl
= new Template
541 for modifier
in modifiers
do
542 if modifier
== "public" then continue
543 tpl
.add
"{modifier.html_escape} "
548 fun tpl_signature
: Template do return new Template
550 redef fun tpl_list_item
do
551 var lnk
= new Template
552 lnk
.add
new TplLabel.with_classes
(tpl_css_classes
.to_a
)
553 var atext
= html_link
.text
554 var ahref
= "{mclassdef.mclass.nitdoc_url}#{mproperty.nitdoc_id}"
555 var atitle
= html_link
.title
556 var anchor
= new Link.with_title
(ahref
, atext
, atitle
)
558 var mdoc
= mdoc_or_fallback
561 lnk
.add mdoc
.tpl_short_comment
563 return new TplListItem.with_content
(lnk
)
566 fun tpl_inheritance_item
: TplListItem do
567 var lnk
= new Template
568 lnk
.add
new TplLabel.with_classes
(tpl_css_classes
.to_a
)
569 lnk
.add mclassdef
.mmodule
.tpl_namespace
571 var atext
= mclassdef
.html_link
.text
572 var ahref
= "{mclassdef.mclass.nitdoc_url}#{mproperty.nitdoc_id}"
573 var atitle
= mclassdef
.html_link
.title
574 var anchor
= new Link.with_title
(ahref
, atext
, atitle
)
576 var mdoc
= mdoc_or_fallback
579 lnk
.add mdoc
.tpl_short_comment
581 var li
= new TplListItem.with_content
(lnk
)
582 li
.css_classes
.add
"signature"
587 redef class MAttributeDef
589 redef fun html_modifiers
do
595 redef fun tpl_signature
do
596 var tpl
= new Template
597 if static_mtype
!= null then
599 tpl
.add static_mtype
.tpl_signature
606 redef fun tpl_signature
do
607 var tpl
= new Template
608 var params
= new Array[String]
609 for param
in intro
.msignature
.mparameters
do
610 params
.add param
.name
.html_escape
612 if not params
.is_empty
then
614 tpl
.add params
.join
(", ")
621 redef class MMethodDef
623 # FIXME annotation should be handled in their own way
624 redef fun html_modifiers
do
628 else if is_intern
then
631 if mproperty
.is_init
then
639 redef fun tpl_signature
do return msignature
.tpl_signature
642 redef class MVirtualTypeProp
643 redef fun html_link
do return mvirtualtype
.html_link
644 redef fun tpl_signature
do return html_link
647 redef class MVirtualTypeDef
649 redef fun html_modifiers
do
655 redef fun tpl_signature
do
656 var tpl
= new Template
657 if bound
== null then return tpl
659 tpl
.add bound
.tpl_signature
665 fun tpl_signature
: Template is abstract
668 redef class MClassType
669 redef fun html_link
do return mclass
.html_link
670 redef fun tpl_signature
do return html_link
673 redef class MNullableType
674 redef fun tpl_signature
do
675 var tpl
= new Template
677 tpl
.add mtype
.tpl_signature
682 redef class MGenericType
683 redef fun tpl_signature
do
685 var tpl
= new Template
686 tpl
.add
new Link.with_title
(lnk
.href
, mclass
.name
.html_escape
, lnk
.title
)
688 for i
in [0..arguments
.length
[ do
689 tpl
.add arguments
[i
].tpl_signature
690 if i
< arguments
.length
- 1 then tpl
.add
", "
697 redef class MParameterType
698 redef fun html_link
do
699 return new Link.with_title
("{mclass.nitdoc_url}#FT_{name.to_cmangle}", name
, "formal type")
701 redef fun tpl_signature
do return html_link
704 redef class MVirtualType
705 redef fun html_link
do return mproperty
.intro
.html_link
706 redef fun tpl_signature
do return html_link
709 redef class MSignature
710 redef fun tpl_signature
do
711 var tpl
= new Template
712 if not mparameters
.is_empty
then
714 for i
in [0..mparameters
.length
[ do
715 tpl
.add mparameters
[i
].tpl_signature
716 if i
< mparameters
.length
- 1 then tpl
.add
", "
720 if return_mtype
!= null then
722 tpl
.add return_mtype
.tpl_signature
728 redef class MParameter
729 fun tpl_signature
: Template do
730 var tpl
= new Template
732 tpl
.add mtype
.tpl_signature
733 if is_vararg
then tpl
.add
"..."
738 redef class ConcernsTree
740 private var seen
= new HashSet[MConcern]
742 redef fun add
(p
, e
) do
743 if seen
.has
(e
) then return
748 fun to_tpl
: TplList do
749 var lst
= new TplList.with_classes
(["list-unstyled", "list-definition"])
751 var li
= r
.tpl_concern_item
758 private fun build_list
(e
: MConcern, li
: TplListItem) do
759 if not sub
.has_key
(e
) then return
761 var lst
= new TplList.with_classes
(["list-unstyled", "list-definition"])
763 if e2
isa MGroup and e2
.is_root
then
766 var sli
= e2
.tpl_concern_item
776 ################################################################################
777 # Additions to `model_ext`.
780 redef fun tpl_signature
do
781 var tpl
= new Template
784 if part
.target
!= null then
785 tpl
.add part
.target
.as(not null).html_link
787 tpl
.add part
.text
.html_escape
794 redef class MInnerClass
795 redef fun nitdoc_url
do return inner
.nitdoc_url
796 redef fun tpl_signature
do return inner
.tpl_signature
799 redef class MInnerClassDef
800 redef fun nitdoc_url
do return inner
.nitdoc_url
802 redef fun html_link_to_anchor
do return inner
.html_link_to_anchor
803 redef fun html_link
do return inner
.html_link
804 redef fun tpl_signature
do return inner
.tpl_signature
806 redef fun tpl_definition
do
807 var tpl
= new TplClassDefinition
808 var mdoc
= mdoc_or_fallback
810 tpl
.comment
= mdoc
.tpl_comment