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 redef fun tpl_definition
do return intro
.tpl_definition
315 redef fun tpl_title
do
316 var title
= new Template
322 redef fun tpl_icon
do return intro
.tpl_icon
324 fun tpl_signature
: Template do
325 var tpl
= new Template
328 var parameter_names
= new Array[String]
329 for p
in mparameters
do
330 parameter_names
.add
(p
.html_name
)
332 tpl
.add parameter_names
.join
(", ")
338 redef fun tpl_css_classes
do return intro
.tpl_css_classes
341 redef class MClassDef
342 redef var nitdoc_id
= "{mmodule.nitdoc_id}__{name.to_cmangle}" is lazy
343 redef fun nitdoc_url
do return "{mclass.nitdoc_url}#{nitdoc_id}"
344 redef fun mdoc_or_fallback
do return mdoc
or else mclass
.mdoc_or_fallback
346 # Depends if `self` is an intro or not.
348 # * If intro contains the visibility and kind.
349 # * If redef contains the `redef` keyword and kind.
350 redef fun html_modifiers
do
351 var res
= new Array[String]
355 if mclass
.visibility
!= public_visibility
then
356 res
.add mclass
.visibility
.to_s
359 res
.add mclass
.kind
.to_s
363 # Depends if `self` is an intro or not.
365 # For intro: `private abstract class Foo[E: Object]`
366 # For redef: `redef class Foo[E]`
367 # TODO change the implementation to correspond to the comment.
368 redef fun html_declaration
do
369 var tpl
= new Template
371 tpl
.add html_modifiers
.join
(" ")
374 tpl
.add tpl_signature
379 # Returns `mmodule::self`
380 redef fun html_namespace
do
381 var tpl
= new Template
382 tpl
.add mmodule
.html_namespace
384 tpl
.add mclass
.html_link
389 redef fun tpl_article
do
390 var tpl
= new TplArticle(nitdoc_id
)
391 tpl
.summary_title
= "in {mmodule.html_name}"
392 tpl
.title
= html_declaration
393 tpl
.title_classes
.add
"signature"
394 var title
= new Template
396 title
.add mmodule
.html_namespace
398 var mdoc
= mdoc_or_fallback
400 tpl
.content
= mdoc
.tpl_comment
405 redef fun tpl_title
do
406 var title
= new Template
412 fun tpl_signature
: Template do
413 var tpl
= new Template
414 var mparameters
= mclass
.mparameters
415 if not mparameters
.is_empty
then
417 for i
in [0..mparameters
.length
[ do
418 tpl
.add
"{mparameters[i].html_name}: "
419 tpl
.add bound_mtype
.arguments
[i
].tpl_signature
420 if i
< mparameters
.length
- 1 then tpl
.add
", "
427 redef fun tpl_definition
do
428 var tpl
= new TplClassDefinition
429 var mdoc
= mdoc_or_fallback
431 tpl
.comment
= mdoc
.tpl_comment
436 redef fun tpl_css_classes
do
437 var set
= new HashSet[String]
438 if is_intro
then set
.add
"intro"
439 for m
in mclass
.intro
.modifiers
do set
.add m
.to_cmangle
440 for m
in modifiers
do set
.add m
.to_cmangle
444 fun tpl_modifiers
: Template do
445 var tpl
= new Template
446 for modifier
in modifiers
do
447 if modifier
== "public" then continue
448 tpl
.add
"{modifier.html_escape} "
454 redef class MProperty
455 redef var nitdoc_id
= "{intro_mclassdef.mclass.nitdoc_id}__{name.to_cmangle}" is lazy
456 redef fun nitdoc_url
do return "property_{nitdoc_id}.html"
457 redef fun mdoc_or_fallback
do return intro
.mdoc
458 redef fun html_modifiers
do return intro
.html_modifiers
459 redef fun html_declaration
do return intro
.html_declaration
461 # Returns `mclass::self`.
462 redef fun html_namespace
do
463 var tpl
= new Template
464 tpl
.add intro_mclassdef
.mclass
.html_namespace
466 tpl
.add intro
.html_link
471 fun tpl_signature
: Template do return new Template
473 redef fun tpl_title
do return intro
.tpl_title
475 redef fun tpl_icon
do return intro
.tpl_icon
477 redef fun tpl_css_classes
do return intro
.tpl_css_classes
481 redef var nitdoc_id
= "{mclassdef.nitdoc_id}__{name.to_cmangle}" is lazy
482 redef fun nitdoc_url
do return "{mproperty.nitdoc_url}#{nitdoc_id}"
483 redef fun mdoc_or_fallback
do return mdoc
or else mproperty
.mdoc_or_fallback
485 # Depends if `self` is an intro or not.
487 # * If intro contains the visibility and kind.
488 # * If redef contains the `redef` keyword and kind.
489 redef fun html_modifiers
do
490 var res
= new Array[String]
494 if mproperty
.visibility
!= public_visibility
then
495 res
.add mproperty
.visibility
.to_s
501 # Depends if `self` is an intro or not.
503 # For intro: `private fun foo(e: Object): Bar is abstract`
504 # For redef: `redef fun foo(e) is cached`
505 # TODO change the implementation to correspond to the comment.
506 redef fun html_declaration
do
507 var tpl
= new Template
509 tpl
.add html_modifiers
.join
(" ")
512 tpl
.add tpl_signature
517 # Returns `mclassdef::self`
518 redef fun html_namespace
do
519 var tpl
= new Template
520 tpl
.add mclassdef
.html_namespace
526 redef fun tpl_article
do
527 var tpl
= new TplArticle(nitdoc_id
)
528 tpl
.summary_title
= "in {mclassdef.html_name}"
529 var title
= new Template
531 title
.add mclassdef
.html_link
533 tpl
.subtitle
= html_declaration
534 var mdoc
= mdoc_or_fallback
536 tpl
.content
= mdoc
.tpl_comment
541 redef fun tpl_definition
do
542 var tpl
= new TplDefinition
543 var mdoc
= mdoc_or_fallback
545 tpl
.comment
= mdoc
.tpl_comment
550 redef fun tpl_css_classes
do
551 var set
= new HashSet[String]
552 if is_intro
then set
.add
"intro"
553 for m
in mproperty
.intro
.modifiers
do set
.add m
.to_cmangle
554 for m
in modifiers
do set
.add m
.to_cmangle
558 fun tpl_modifiers
: Template do
559 var tpl
= new Template
560 for modifier
in modifiers
do
561 if modifier
== "public" then continue
562 tpl
.add
"{modifier.html_escape} "
567 fun tpl_signature
: Template do return new Template
569 redef fun tpl_list_item
do
570 var lnk
= new Template
571 lnk
.add
new TplLabel.with_classes
(tpl_css_classes
.to_a
)
572 var atext
= html_link
.text
573 var ahref
= "{mclassdef.mclass.nitdoc_url}#{mproperty.nitdoc_id}"
574 var atitle
= html_link
.title
575 var anchor
= new Link.with_title
(ahref
, atext
, atitle
)
577 var mdoc
= mdoc_or_fallback
580 lnk
.add mdoc
.tpl_short_comment
582 return new TplListItem.with_content
(lnk
)
585 fun tpl_inheritance_item
: TplListItem do
586 var lnk
= new Template
587 lnk
.add
new TplLabel.with_classes
(tpl_css_classes
.to_a
)
588 lnk
.add mclassdef
.mmodule
.html_namespace
590 var atext
= mclassdef
.html_link
.text
591 var ahref
= "{mclassdef.mclass.nitdoc_url}#{mproperty.nitdoc_id}"
592 var atitle
= mclassdef
.html_link
.title
593 var anchor
= new Link.with_title
(ahref
, atext
, atitle
)
595 var mdoc
= mdoc_or_fallback
598 lnk
.add mdoc
.tpl_short_comment
600 var li
= new TplListItem.with_content
(lnk
)
601 li
.css_classes
.add
"signature"
606 redef class MAttributeDef
608 redef fun html_modifiers
do
614 redef fun tpl_signature
do
615 var tpl
= new Template
616 if static_mtype
!= null then
618 tpl
.add static_mtype
.tpl_signature
625 redef fun tpl_signature
do
626 var tpl
= new Template
627 var params
= new Array[String]
628 for param
in intro
.msignature
.mparameters
do
629 params
.add param
.name
.html_escape
631 if not params
.is_empty
then
633 tpl
.add params
.join
(", ")
640 redef class MMethodDef
642 # FIXME annotation should be handled in their own way
643 redef fun html_modifiers
do
647 else if is_intern
then
650 if mproperty
.is_init
then
658 redef fun tpl_signature
do return msignature
.tpl_signature
661 redef class MVirtualTypeProp
662 redef fun html_link
do return mvirtualtype
.html_link
663 redef fun tpl_signature
do return html_link
666 redef class MVirtualTypeDef
668 redef fun html_modifiers
do
674 redef fun tpl_signature
do
675 var tpl
= new Template
676 if bound
== null then return tpl
678 tpl
.add bound
.tpl_signature
684 fun tpl_signature
: Template is abstract
687 redef class MClassType
688 redef fun html_link
do return mclass
.html_link
689 redef fun tpl_signature
do return html_link
692 redef class MNullableType
693 redef fun tpl_signature
do
694 var tpl
= new Template
696 tpl
.add mtype
.tpl_signature
701 redef class MGenericType
702 redef fun tpl_signature
do
704 var tpl
= new Template
705 tpl
.add
new Link.with_title
(lnk
.href
, mclass
.name
.html_escape
, lnk
.title
)
707 for i
in [0..arguments
.length
[ do
708 tpl
.add arguments
[i
].tpl_signature
709 if i
< arguments
.length
- 1 then tpl
.add
", "
716 redef class MParameterType
717 redef fun html_link
do
718 return new Link.with_title
("{mclass.nitdoc_url}#FT_{name.to_cmangle}", name
, "formal type")
720 redef fun tpl_signature
do return html_link
723 redef class MVirtualType
724 redef fun html_link
do return mproperty
.intro
.html_link
725 redef fun tpl_signature
do return html_link
728 redef class MSignature
729 redef fun tpl_signature
do
730 var tpl
= new Template
731 if not mparameters
.is_empty
then
733 for i
in [0..mparameters
.length
[ do
734 tpl
.add mparameters
[i
].tpl_signature
735 if i
< mparameters
.length
- 1 then tpl
.add
", "
739 if return_mtype
!= null then
741 tpl
.add return_mtype
.tpl_signature
747 redef class MParameter
748 fun tpl_signature
: Template do
749 var tpl
= new Template
751 tpl
.add mtype
.tpl_signature
752 if is_vararg
then tpl
.add
"..."
757 redef class ConcernsTree
759 private var seen
= new HashSet[MConcern]
761 redef fun add
(p
, e
) do
762 if seen
.has
(e
) then return
767 fun to_tpl
: TplList do
768 var lst
= new TplList.with_classes
(["list-unstyled", "list-definition"])
770 var li
= r
.tpl_concern_item
777 private fun build_list
(e
: MConcern, li
: TplListItem) do
778 if not sub
.has_key
(e
) then return
780 var lst
= new TplList.with_classes
(["list-unstyled", "list-definition"])
782 if e2
isa MGroup and e2
.is_root
then
785 var sli
= e2
.tpl_concern_item
795 ################################################################################
796 # Additions to `model_ext`.
799 redef fun tpl_signature
do
800 var tpl
= new Template
803 if part
.target
!= null then
804 tpl
.add part
.target
.as(not null).html_link
806 tpl
.add part
.text
.html_escape
813 redef class MInnerClass
814 redef fun nitdoc_url
do return inner
.nitdoc_url
815 redef fun tpl_signature
do return inner
.tpl_signature
818 redef class MInnerClassDef
819 redef fun nitdoc_url
do return inner
.nitdoc_url
821 redef fun html_link_to_anchor
do return inner
.html_link_to_anchor
822 redef fun html_link
do return inner
.html_link
823 redef fun tpl_signature
do return inner
.tpl_signature
825 redef fun tpl_definition
do
826 var tpl
= new TplClassDefinition
827 var mdoc
= mdoc_or_fallback
829 tpl
.comment
= mdoc
.tpl_comment