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 # Returns `self` namespace decorated with HTML links.
108 # * MProject: `mproject`
109 # * MGroup: `mproject(::group)`
110 # * MModule: `mgroup::mmodule`
111 # * MClass: `mproject::mclass`
112 # * MClassDef: `mmodule::mclassdef`
113 # * MProperty: `mclass::mprop`
114 # * MPropdef: `mclassdef:mpropdef`
115 fun html_namespace
: Template is abstract
117 # A template article that briefly describe the entity
118 fun tpl_short_article
: TplArticle do
119 var tpl
= tpl_article
120 var mdoc
= mdoc_or_fallback
122 tpl
.content
= mdoc
.tpl_short_comment
127 # A template article that describe the entity
128 fun tpl_article
: TplArticle do
129 var tpl
= new TplArticle.with_title
(nitdoc_id
, tpl_title
)
130 tpl
.title_classes
.add
"signature"
131 tpl
.subtitle
= html_namespace
132 tpl
.summary_title
= html_name
136 # A template definition of the mentity
137 # include name, sysnopsys, comment and namespace
138 fun tpl_definition
: TplDefinition is abstract
140 # A li element that can go in a list
141 fun tpl_list_item
: TplListItem do
142 var lnk
= new Template
143 lnk
.add
new TplLabel.with_classes
(tpl_css_classes
)
145 var mdoc
= mdoc_or_fallback
148 lnk
.add mdoc
.tpl_short_comment
150 return new TplListItem.with_content
(lnk
)
153 var tpl_css_classes
= new Array[String]
155 # Box title for this mentity
156 fun tpl_title
: Template do
157 var title
= new Template
159 title
.add html_namespace
163 # Icon that will be displayed before the title
164 fun tpl_icon
: TplIcon do
165 var icon
= new TplIcon.with_icon
("tag")
166 icon
.css_classes
.add_all
(tpl_css_classes
)
172 # Return a li element for `self` that can be displayed in a concern list
173 private fun tpl_concern_item
: TplListItem do
174 var lnk
= new Template
175 lnk
.add html_link_to_anchor
176 var mdoc
= mdoc_or_fallback
179 lnk
.add mdoc
.tpl_short_comment
181 return new TplListItem.with_content
(lnk
)
186 redef var nitdoc_id
= name
.to_cmangle
is lazy
187 redef fun nitdoc_url
do return root
.nitdoc_url
188 redef var html_modifiers
= ["project"]
189 redef fun html_namespace
do return html_link
191 redef fun tpl_definition
do
192 var tpl
= new TplDefinition
193 var mdoc
= mdoc_or_fallback
195 tpl
.comment
= mdoc
.tpl_comment
200 redef fun tpl_css_classes
do return ["public"]
204 redef var nitdoc_id
is lazy
do
205 if parent
!= null then
206 return "{parent.nitdoc_id}__{name.to_cmangle}"
208 return name
.to_cmangle
211 redef fun nitdoc_url
do return "group_{nitdoc_id}.html"
212 redef var html_modifiers
= ["group"]
214 # Depends if `self` is root or not.
216 # * If root `mproject`.
217 # * Else `mproject::self`.
218 redef fun html_namespace
do
219 var tpl
= new Template
220 tpl
.add mproject
.html_namespace
221 if mproject
.root
!= self then
228 redef fun tpl_definition
do
229 var tpl
= new TplDefinition
230 var mdoc
= mdoc_or_fallback
232 tpl
.comment
= mdoc
.tpl_comment
239 redef var nitdoc_id
is lazy
do
240 if mgroup
!= null then
241 if mgroup
.mmodules
.length
== 1 then
242 return "{mgroup.nitdoc_id}-"
244 return "{mgroup.nitdoc_id}__{name.to_cmangle}"
247 return name
.to_cmangle
250 redef fun nitdoc_url
do return "module_{nitdoc_id}.html"
251 redef var html_modifiers
= ["module"]
253 # Depends if `self` belongs to a MGroup.
255 # * If mgroup `mgroup::self`.
257 redef fun html_namespace
do
258 var tpl
= new Template
259 if mgroup
!= null then
260 tpl
.add mgroup
.html_namespace
267 redef fun tpl_definition
do
268 var tpl
= new TplClassDefinition
269 var mdoc
= mdoc_or_fallback
271 tpl
.comment
= mdoc
.tpl_comment
276 redef fun tpl_css_classes
do return ["public"]
280 redef var nitdoc_id
= "{intro_mmodule.nitdoc_id}__{name.to_cmangle}" is lazy
281 redef fun nitdoc_url
do return "class_{nitdoc_id}.html"
282 redef fun mdoc_or_fallback
do return intro
.mdoc
285 redef var html_name
is lazy
do
286 var tpl
= new Template
287 tpl
.add name
.html_escape
290 var parameter_names
= new Array[String]
291 for p
in mparameters
do
292 parameter_names
.add
(p
.html_name
)
294 tpl
.add parameter_names
.join
(", ")
297 return tpl
.write_to_string
300 redef fun html_modifiers
do return intro
.html_modifiers
301 redef fun html_declaration
do return intro
.html_declaration
303 # Returns `mproject::self`.
304 redef fun html_namespace
do
305 var tpl
= new Template
306 tpl
.add intro_mmodule
.mgroup
.mproject
.html_namespace
313 # Returns `intro.html_short_signature`.
314 fun html_short_signature
: Template do return intro
.html_short_signature
316 # Returns `intro.html_signature`.
317 fun html_signature
: Template do return intro
.html_signature
319 redef fun tpl_definition
do return intro
.tpl_definition
321 redef fun tpl_title
do
322 var title
= new Template
328 redef fun tpl_icon
do return intro
.tpl_icon
329 redef fun tpl_css_classes
do return intro
.tpl_css_classes
332 redef class MClassDef
333 redef var nitdoc_id
= "{mmodule.nitdoc_id}__{name.to_cmangle}" is lazy
334 redef fun nitdoc_url
do return "{mclass.nitdoc_url}#{nitdoc_id}"
335 redef fun mdoc_or_fallback
do return mdoc
or else mclass
.mdoc_or_fallback
337 # Depends if `self` is an intro or not.
339 # * If intro contains the visibility and kind.
340 # * If redef contains the `redef` keyword and kind.
341 redef fun html_modifiers
do
342 var res
= new Array[String]
346 if mclass
.visibility
!= public_visibility
then
347 res
.add mclass
.visibility
.to_s
350 res
.add mclass
.kind
.to_s
354 # Depends if `self` is an intro or not.
356 # For intro: `private abstract class Foo[E: Object]`
357 # For redef: `redef class Foo[E]`
358 # TODO change the implementation to correspond to the comment.
359 redef fun html_declaration
do
360 var tpl
= new Template
362 tpl
.add html_modifiers
.join
(" ")
365 tpl
.add html_signature
370 # Returns `mmodule::self`
371 redef fun html_namespace
do
372 var tpl
= new Template
373 tpl
.add mmodule
.html_namespace
375 tpl
.add mclass
.html_link
380 # Returns the MClassDef generic signature without static bounds.
381 fun html_short_signature
: Template do
382 var tpl
= new Template
383 var mparameters
= mclass
.mparameters
384 if not mparameters
.is_empty
then
386 for i
in [0..mparameters
.length
[ do
387 tpl
.add mparameters
[i
].html_name
388 if i
< mparameters
.length
- 1 then tpl
.add
", "
395 # Returns the MClassDef generic signature with static bounds.
396 fun html_signature
: Template do
397 var tpl
= new Template
398 var mparameters
= mclass
.mparameters
399 if not mparameters
.is_empty
then
401 for i
in [0..mparameters
.length
[ do
402 tpl
.add
"{mparameters[i].html_name}: "
403 tpl
.add bound_mtype
.arguments
[i
].html_signature
404 if i
< mparameters
.length
- 1 then tpl
.add
", "
411 redef fun tpl_article
do
412 var tpl
= new TplArticle(nitdoc_id
)
413 tpl
.summary_title
= "in {mmodule.html_name}"
414 tpl
.title
= html_declaration
415 tpl
.title_classes
.add
"signature"
416 var title
= new Template
418 title
.add mmodule
.html_namespace
420 var mdoc
= mdoc_or_fallback
422 tpl
.content
= mdoc
.tpl_comment
427 redef fun tpl_title
do
428 var title
= new Template
434 redef fun tpl_definition
do
435 var tpl
= new TplClassDefinition
436 var mdoc
= mdoc_or_fallback
438 tpl
.comment
= mdoc
.tpl_comment
443 redef fun tpl_css_classes
do
444 var set
= new HashSet[String]
445 if is_intro
then set
.add
"intro"
446 for m
in mclass
.intro
.modifiers
do set
.add m
.to_cmangle
447 for m
in modifiers
do set
.add m
.to_cmangle
451 fun tpl_modifiers
: Template do
452 var tpl
= new Template
453 for modifier
in modifiers
do
454 if modifier
== "public" then continue
455 tpl
.add
"{modifier.html_escape} "
461 redef class MProperty
462 redef var nitdoc_id
= "{intro_mclassdef.mclass.nitdoc_id}__{name.to_cmangle}" is lazy
463 redef fun nitdoc_url
do return "property_{nitdoc_id}.html"
464 redef fun mdoc_or_fallback
do return intro
.mdoc
465 redef fun html_modifiers
do return intro
.html_modifiers
466 redef fun html_declaration
do return intro
.html_declaration
468 # Returns `mclass::self`.
469 redef fun html_namespace
do
470 var tpl
= new Template
471 tpl
.add intro_mclassdef
.mclass
.html_namespace
473 tpl
.add intro
.html_link
478 # Returns `intro.html_short_signature`.
479 fun html_short_signature
: Template do return intro
.html_short_signature
481 # Returns `intro.html_signature`.
482 fun html_signature
: Template do return intro
.html_signature
484 redef fun tpl_title
do return intro
.tpl_title
486 redef fun tpl_icon
do return intro
.tpl_icon
488 redef fun tpl_css_classes
do return intro
.tpl_css_classes
492 redef var nitdoc_id
= "{mclassdef.nitdoc_id}__{name.to_cmangle}" is lazy
493 redef fun nitdoc_url
do return "{mproperty.nitdoc_url}#{nitdoc_id}"
494 redef fun mdoc_or_fallback
do return mdoc
or else mproperty
.mdoc_or_fallback
496 # Depends if `self` is an intro or not.
498 # * If intro contains the visibility and kind.
499 # * If redef contains the `redef` keyword and kind.
500 redef fun html_modifiers
do
501 var res
= new Array[String]
505 if mproperty
.visibility
!= public_visibility
then
506 res
.add mproperty
.visibility
.to_s
512 # Depends if `self` is an intro or not.
514 # For intro: `private fun foo(e: Object): Bar is abstract`
515 # For redef: `redef fun foo(e) is cached`
516 # TODO change the implementation to correspond to the comment.
517 redef fun html_declaration
do
518 var tpl
= new Template
520 tpl
.add html_modifiers
.join
(" ")
523 tpl
.add html_signature
528 # Returns `mclassdef::self`
529 redef fun html_namespace
do
530 var tpl
= new Template
531 tpl
.add mclassdef
.html_namespace
537 # Returns the MPropdDef signature without static types.
538 fun html_short_signature
: Template is abstract
540 # Returns the MPropDef signature with static types.
541 fun html_signature
: Template is abstract
543 redef fun tpl_article
do
544 var tpl
= new TplArticle(nitdoc_id
)
545 tpl
.summary_title
= "in {mclassdef.html_name}"
546 var title
= new Template
548 title
.add mclassdef
.html_link
550 tpl
.subtitle
= html_declaration
551 var mdoc
= mdoc_or_fallback
553 tpl
.content
= mdoc
.tpl_comment
558 redef fun tpl_definition
do
559 var tpl
= new TplDefinition
560 var mdoc
= mdoc_or_fallback
562 tpl
.comment
= mdoc
.tpl_comment
567 redef fun tpl_css_classes
do
568 var set
= new HashSet[String]
569 if is_intro
then set
.add
"intro"
570 for m
in mproperty
.intro
.modifiers
do set
.add m
.to_cmangle
571 for m
in modifiers
do set
.add m
.to_cmangle
575 fun tpl_modifiers
: Template do
576 var tpl
= new Template
577 for modifier
in modifiers
do
578 if modifier
== "public" then continue
579 tpl
.add
"{modifier.html_escape} "
584 redef fun tpl_list_item
do
585 var lnk
= new Template
586 lnk
.add
new TplLabel.with_classes
(tpl_css_classes
.to_a
)
587 var atext
= html_link
.text
588 var ahref
= "{mclassdef.mclass.nitdoc_url}#{mproperty.nitdoc_id}"
589 var atitle
= html_link
.title
590 var anchor
= new Link.with_title
(ahref
, atext
, atitle
)
592 var mdoc
= mdoc_or_fallback
595 lnk
.add mdoc
.tpl_short_comment
597 return new TplListItem.with_content
(lnk
)
600 fun tpl_inheritance_item
: TplListItem do
601 var lnk
= new Template
602 lnk
.add
new TplLabel.with_classes
(tpl_css_classes
.to_a
)
603 lnk
.add mclassdef
.mmodule
.html_namespace
605 var atext
= mclassdef
.html_link
.text
606 var ahref
= "{mclassdef.mclass.nitdoc_url}#{mproperty.nitdoc_id}"
607 var atitle
= mclassdef
.html_link
.title
608 var anchor
= new Link.with_title
(ahref
, atext
, atitle
)
610 var mdoc
= mdoc_or_fallback
613 lnk
.add mdoc
.tpl_short_comment
615 var li
= new TplListItem.with_content
(lnk
)
616 li
.css_classes
.add
"signature"
621 redef class MAttributeDef
623 redef fun html_modifiers
do
629 redef fun html_short_signature
do return new Template
631 redef fun html_signature
do
632 var tpl
= new Template
633 if static_mtype
!= null then
635 tpl
.add static_mtype
.html_signature
641 redef class MMethodDef
643 # FIXME annotation should be handled in their own way
644 redef fun html_modifiers
do
648 else if is_intern
then
651 if mproperty
.is_init
then
659 redef fun html_short_signature
do return msignature
.html_short_signature
660 redef fun html_signature
do return msignature
.html_signature
663 redef class MVirtualTypeProp
664 redef fun html_link
do return mvirtualtype
.html_link
667 redef class MVirtualTypeDef
669 redef fun html_modifiers
do
675 redef fun html_short_signature
do return new Template
677 redef fun html_signature
do
678 var tpl
= new Template
679 if bound
== null then return tpl
681 tpl
.add bound
.html_signature
687 # Returns the signature of this type whithout bounds.
688 fun html_short_signature
: Template is abstract
690 # Returns the signature of this type.
691 fun html_signature
: Template is abstract
694 redef class MClassType
695 redef fun html_link
do return mclass
.html_link
696 redef fun html_short_signature
do return html_link
697 redef fun html_signature
do return html_link
700 redef class MNullableType
702 redef fun html_short_signature
do
703 var tpl
= new Template
705 tpl
.add mtype
.html_short_signature
709 redef fun html_signature
do
710 var tpl
= new Template
712 tpl
.add mtype
.html_signature
717 redef class MGenericType
718 redef fun html_short_signature
do
720 var tpl
= new Template
721 tpl
.add
new Link.with_title
(lnk
.href
, mclass
.name
.html_escape
, lnk
.title
)
723 for i
in [0..arguments
.length
[ do
724 tpl
.add arguments
[i
].html_short_signature
725 if i
< arguments
.length
- 1 then tpl
.add
", "
731 redef fun html_signature
do
733 var tpl
= new Template
734 tpl
.add
new Link.with_title
(lnk
.href
, mclass
.name
.html_escape
, lnk
.title
)
736 for i
in [0..arguments
.length
[ do
737 tpl
.add arguments
[i
].html_signature
738 if i
< arguments
.length
- 1 then tpl
.add
", "
745 redef class MParameterType
746 redef fun html_link
do
747 return new Link.with_title
("{mclass.nitdoc_url}#FT_{name.to_cmangle}", name
, "formal type")
750 redef fun html_short_signature
do return html_link
751 redef fun html_signature
do return html_link
754 redef class MVirtualType
755 redef fun html_link
do return mproperty
.intro
.html_link
756 redef fun html_signature
do return html_link
759 redef class MSignature
761 redef fun html_short_signature
do
762 var tpl
= new Template
763 if not mparameters
.is_empty
then
765 for i
in [0..mparameters
.length
[ do
766 tpl
.add mparameters
[i
].html_short_signature
767 if i
< mparameters
.length
- 1 then tpl
.add
", "
774 redef fun html_signature
do
775 var tpl
= new Template
776 if not mparameters
.is_empty
then
778 for i
in [0..mparameters
.length
[ do
779 tpl
.add mparameters
[i
].html_signature
780 if i
< mparameters
.length
- 1 then tpl
.add
", "
784 if return_mtype
!= null then
786 tpl
.add return_mtype
.html_signature
792 redef class MParameter
794 # Returns `self` name and ellipsys if any.
795 fun html_short_signature
: Template do
796 var tpl
= new Template
798 if is_vararg
then tpl
.add
"..."
802 # Returns `self` name with it's static type and ellipsys if any.
803 fun html_signature
: Template do
804 var tpl
= new Template
806 tpl
.add mtype
.html_signature
807 if is_vararg
then tpl
.add
"..."
812 redef class ConcernsTree
814 private var seen
= new HashSet[MConcern]
816 redef fun add
(p
, e
) do
817 if seen
.has
(e
) then return
822 fun to_tpl
: TplList do
823 var lst
= new TplList.with_classes
(["list-unstyled", "list-definition"])
825 var li
= r
.tpl_concern_item
832 private fun build_list
(e
: MConcern, li
: TplListItem) do
833 if not sub
.has_key
(e
) then return
835 var lst
= new TplList.with_classes
(["list-unstyled", "list-definition"])
837 if e2
isa MGroup and e2
.is_root
then
840 var sli
= e2
.tpl_concern_item
850 ################################################################################
851 # Additions to `model_ext`.
854 redef fun html_signature
do
855 var tpl
= new Template
858 if part
.target
!= null then
859 tpl
.add part
.target
.as(not null).html_link
861 tpl
.add part
.text
.html_escape
868 redef class MInnerClass
869 redef fun nitdoc_url
do return inner
.nitdoc_url
870 redef fun html_signature
do return inner
.html_signature
873 redef class MInnerClassDef
874 redef fun nitdoc_url
do return inner
.nitdoc_url
876 redef fun html_link_to_anchor
do return inner
.html_link_to_anchor
877 redef fun html_link
do return inner
.html_link
878 redef fun html_signature
do return inner
.html_signature
880 redef fun tpl_definition
do
881 var tpl
= new TplClassDefinition
882 var mdoc
= mdoc_or_fallback
884 tpl
.comment
= mdoc
.tpl_comment