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 # ID used as a HTML unique ID and in file names.
27 # **Must** match the following (POSIX ERE) regular expression:
30 # ^[A-Za-z_][A-Za-z0-9._-]*$
33 # That way, the ID is always a valid URI component and a valid XML name.
34 fun nitdoc_id
: String is abstract
36 # URL of this entity’s Nitdoc page.
37 fun nitdoc_url
: String is abstract
39 # Returns the mentity name without short signature.
45 # * MClassDef: `Foo[E]`
46 # * MProperty: `foo(e)`
47 # * MPropdef: `foo(e)`
48 var html_name
: String is lazy
do return name
.html_escape
50 # Returns a Link to the mentity `html_url`.
52 # Example: `<a href="html_url" title="mdoc.short_comment">html_short_name</a>
53 var html_link
: Link is lazy
do
54 var tpl
= new Link(nitdoc_url
, html_name
)
55 var mdoc
= mdoc_or_fallback
57 tpl
.title
= mdoc
.short_comment
62 # Returns a Link to the mentity `nitdoc_id`.
64 # Example: `<a href="#nitdoc_id" title="mdoc.short_comment">html_short_name</a>
65 fun html_link_to_anchor
: Link do
66 var tpl
= new Link("#{nitdoc_id}", html_name
)
67 var mdoc
= mdoc_or_fallback
69 tpl
.title
= mdoc
.short_comment
74 # Returns the list of keyword used in `self` declaration.
75 fun html_modifiers
: Array[String] is abstract
77 # Returns the complete MEntity declaration decorated with HTML.
79 # * MProject: `project foo`
80 # * MGroup: `group foo`
81 # * MModule: `module foo`
82 # * MClass: `private abstract class Foo[E: Object]`
83 # * MClassDef: `redef class Foo[E]`
84 # * MProperty: `private fun foo(e: Object): Int`
85 # * MPropdef: `redef fun foo(e)`
86 fun html_declaration
: Template do
87 var tpl
= new Template
89 tpl
.add html_modifiers
.join
(" ")
96 # Returns `self` namespace decorated with HTML links.
98 # * MProject: `mproject`
99 # * MGroup: `mproject(::group)`
100 # * MModule: `mgroup::mmodule`
101 # * MClass: `mproject::mclass`
102 # * MClassDef: `mmodule::mclassdef`
103 # * MProperty: `mclass::mprop`
104 # * MPropdef: `mclassdef:mpropdef`
105 fun html_namespace
: Template is abstract
107 # Returns the comment of this MEntity formatted as HTML.
108 var html_comment
: nullable Writable is lazy
do
109 var mdoc
= mdoc_or_fallback
110 if mdoc
== null then return null
111 return mdoc
.tpl_comment
114 # Returns the comment of this MEntity formatted as HTML.
115 var html_short_comment
: nullable Writable is lazy
do
116 var mdoc
= mdoc_or_fallback
117 if mdoc
== null then return null
118 return mdoc
.tpl_short_comment
123 redef var nitdoc_id
= name
.to_cmangle
is lazy
124 redef fun nitdoc_url
do return root
.nitdoc_url
125 redef var html_modifiers
= ["project"]
126 redef fun html_namespace
do return html_link
130 redef var nitdoc_id
is lazy
do
131 if parent
!= null then
132 return "{parent.nitdoc_id}__{name.to_cmangle}"
134 return name
.to_cmangle
137 redef fun nitdoc_url
do return "group_{nitdoc_id}.html"
138 redef var html_modifiers
= ["group"]
140 # Depends if `self` is root or not.
142 # * If root `mproject`.
143 # * Else `mproject::self`.
144 redef fun html_namespace
do
145 var tpl
= new Template
146 tpl
.add mproject
.html_namespace
147 if mproject
.root
!= self then
156 redef var nitdoc_id
is lazy
do
157 if mgroup
!= null then
158 if mgroup
.mmodules
.length
== 1 then
159 return "{mgroup.nitdoc_id}-"
161 return "{mgroup.nitdoc_id}__{name.to_cmangle}"
164 return name
.to_cmangle
167 redef fun nitdoc_url
do return "module_{nitdoc_id}.html"
168 redef var html_modifiers
= ["module"]
170 # Depends if `self` belongs to a MGroup.
172 # * If mgroup `mgroup::self`.
174 redef fun html_namespace
do
175 var tpl
= new Template
176 if mgroup
!= null then
177 tpl
.add mgroup
.html_namespace
186 redef var nitdoc_id
= "{intro_mmodule.nitdoc_id}__{name.to_cmangle}" is lazy
187 redef fun nitdoc_url
do return "class_{nitdoc_id}.html"
188 redef fun mdoc_or_fallback
do return intro
.mdoc
191 redef var html_name
is lazy
do
192 var tpl
= new Template
193 tpl
.add name
.html_escape
196 var parameter_names
= new Array[String]
197 for p
in mparameters
do
198 parameter_names
.add
(p
.html_name
)
200 tpl
.add parameter_names
.join
(", ")
203 return tpl
.write_to_string
206 redef fun html_modifiers
do return intro
.html_modifiers
207 redef fun html_declaration
do return intro
.html_declaration
209 # Returns `mproject::self`.
210 redef fun html_namespace
do
211 var tpl
= new Template
212 tpl
.add intro_mmodule
.mgroup
.mproject
.html_namespace
219 # Returns `intro.html_short_signature`.
220 fun html_short_signature
: Template do return intro
.html_short_signature
222 # Returns `intro.html_signature`.
223 fun html_signature
: Template do return intro
.html_signature
226 redef class MClassDef
227 redef var nitdoc_id
= "{mmodule.nitdoc_id}__{name.to_cmangle}" is lazy
228 redef fun nitdoc_url
do return "{mclass.nitdoc_url}#{nitdoc_id}"
229 redef fun mdoc_or_fallback
do return mdoc
or else mclass
.mdoc_or_fallback
231 # Depends if `self` is an intro or not.
233 # * If intro contains the visibility and kind.
234 # * If redef contains the `redef` keyword and kind.
235 redef fun html_modifiers
do
236 var res
= new Array[String]
240 if mclass
.visibility
!= public_visibility
then
241 res
.add mclass
.visibility
.to_s
244 res
.add mclass
.kind
.to_s
248 # Depends if `self` is an intro or not.
250 # For intro: `private abstract class Foo[E: Object]`
251 # For redef: `redef class Foo[E]`
252 # TODO change the implementation to correspond to the comment.
253 redef fun html_declaration
do
254 var tpl
= new Template
256 tpl
.add html_modifiers
.join
(" ")
259 tpl
.add html_signature
264 # Returns `mmodule::self`
265 redef fun html_namespace
do
266 var tpl
= new Template
267 tpl
.add mmodule
.html_namespace
269 tpl
.add mclass
.html_link
274 # Returns the MClassDef generic signature without static bounds.
275 fun html_short_signature
: Template do
276 var tpl
= new Template
277 var mparameters
= mclass
.mparameters
278 if not mparameters
.is_empty
then
280 for i
in [0..mparameters
.length
[ do
281 tpl
.add mparameters
[i
].html_name
282 if i
< mparameters
.length
- 1 then tpl
.add
", "
289 # Returns the MClassDef generic signature with static bounds.
290 fun html_signature
: Template do
291 var tpl
= new Template
292 var mparameters
= mclass
.mparameters
293 if not mparameters
.is_empty
then
295 for i
in [0..mparameters
.length
[ do
296 tpl
.add
"{mparameters[i].html_name}: "
297 tpl
.add bound_mtype
.arguments
[i
].html_signature
298 if i
< mparameters
.length
- 1 then tpl
.add
", "
306 redef class MProperty
307 redef var nitdoc_id
= "{intro_mclassdef.mclass.nitdoc_id}__{name.to_cmangle}" is lazy
308 redef fun nitdoc_url
do return "property_{nitdoc_id}.html"
309 redef fun mdoc_or_fallback
do return intro
.mdoc
310 redef fun html_modifiers
do return intro
.html_modifiers
311 redef fun html_declaration
do return intro
.html_declaration
313 # Returns `mclass::self`.
314 redef fun html_namespace
do
315 var tpl
= new Template
316 tpl
.add intro_mclassdef
.mclass
.html_namespace
318 tpl
.add intro
.html_link
323 # Returns `intro.html_short_signature`.
324 fun html_short_signature
: Template do return intro
.html_short_signature
326 # Returns `intro.html_signature`.
327 fun html_signature
: Template do return intro
.html_signature
331 redef var nitdoc_id
= "{mclassdef.nitdoc_id}__{name.to_cmangle}" is lazy
332 redef fun nitdoc_url
do return "{mproperty.nitdoc_url}#{nitdoc_id}"
333 redef fun mdoc_or_fallback
do return mdoc
or else mproperty
.mdoc_or_fallback
335 # Depends if `self` is an intro or not.
337 # * If intro contains the visibility and kind.
338 # * If redef contains the `redef` keyword and kind.
339 redef fun html_modifiers
do
340 var res
= new Array[String]
344 if mproperty
.visibility
!= public_visibility
then
345 res
.add mproperty
.visibility
.to_s
351 # Depends if `self` is an intro or not.
353 # For intro: `private fun foo(e: Object): Bar is abstract`
354 # For redef: `redef fun foo(e) is cached`
355 # TODO change the implementation to correspond to the comment.
356 redef fun html_declaration
do
357 var tpl
= new Template
359 tpl
.add html_modifiers
.join
(" ")
362 tpl
.add html_signature
367 # Returns `mclassdef::self`
368 redef fun html_namespace
do
369 var tpl
= new Template
370 tpl
.add mclassdef
.html_namespace
376 # Returns the MPropdDef signature without static types.
377 fun html_short_signature
: Template is abstract
379 # Returns the MPropDef signature with static types.
380 fun html_signature
: Template is abstract
383 redef class MAttributeDef
385 redef fun html_modifiers
do
391 redef fun html_short_signature
do return new Template
393 redef fun html_signature
do
394 var tpl
= new Template
395 if static_mtype
!= null then
397 tpl
.add static_mtype
.html_signature
403 redef class MMethodDef
405 # FIXME annotation should be handled in their own way
406 redef fun html_modifiers
do
410 else if is_intern
then
413 if mproperty
.is_init
then
421 redef fun html_short_signature
do return msignature
.html_short_signature
422 redef fun html_signature
do return msignature
.html_signature
425 redef class MVirtualTypeProp
426 redef fun html_link
do return mvirtualtype
.html_link
429 redef class MVirtualTypeDef
431 redef fun html_modifiers
do
437 redef fun html_short_signature
do return new Template
439 redef fun html_signature
do
440 var tpl
= new Template
441 if bound
== null then return tpl
443 tpl
.add bound
.html_signature
449 # Returns the signature of this type whithout bounds.
450 fun html_short_signature
: Template is abstract
452 # Returns the signature of this type.
453 fun html_signature
: Template is abstract
456 redef class MClassType
457 redef fun html_link
do return mclass
.html_link
458 redef fun html_short_signature
do return html_link
459 redef fun html_signature
do return html_link
462 redef class MNullableType
464 redef fun html_short_signature
do
465 var tpl
= new Template
467 tpl
.add mtype
.html_short_signature
471 redef fun html_signature
do
472 var tpl
= new Template
474 tpl
.add mtype
.html_signature
479 redef class MGenericType
480 redef fun html_short_signature
do
482 var tpl
= new Template
483 tpl
.add
new Link.with_title
(lnk
.href
, mclass
.name
.html_escape
, lnk
.title
)
485 for i
in [0..arguments
.length
[ do
486 tpl
.add arguments
[i
].html_short_signature
487 if i
< arguments
.length
- 1 then tpl
.add
", "
493 redef fun html_signature
do
495 var tpl
= new Template
496 tpl
.add
new Link.with_title
(lnk
.href
, mclass
.name
.html_escape
, lnk
.title
)
498 for i
in [0..arguments
.length
[ do
499 tpl
.add arguments
[i
].html_signature
500 if i
< arguments
.length
- 1 then tpl
.add
", "
507 redef class MParameterType
508 redef fun html_link
do
509 return new Link.with_title
("{mclass.nitdoc_url}#FT_{name.to_cmangle}", name
, "formal type")
512 redef fun html_short_signature
do return html_link
513 redef fun html_signature
do return html_link
516 redef class MVirtualType
517 redef fun html_link
do return mproperty
.intro
.html_link
518 redef fun html_signature
do return html_link
521 redef class MSignature
523 redef fun html_short_signature
do
524 var tpl
= new Template
525 if not mparameters
.is_empty
then
527 for i
in [0..mparameters
.length
[ do
528 tpl
.add mparameters
[i
].html_short_signature
529 if i
< mparameters
.length
- 1 then tpl
.add
", "
536 redef fun html_signature
do
537 var tpl
= new Template
538 if not mparameters
.is_empty
then
540 for i
in [0..mparameters
.length
[ do
541 tpl
.add mparameters
[i
].html_signature
542 if i
< mparameters
.length
- 1 then tpl
.add
", "
546 if return_mtype
!= null then
548 tpl
.add return_mtype
.html_signature
554 redef class MParameter
556 # Returns `self` name and ellipsys if any.
557 fun html_short_signature
: Template do
558 var tpl
= new Template
560 if is_vararg
then tpl
.add
"..."
564 # Returns `self` name with it's static type and ellipsys if any.
565 fun html_signature
: Template do
566 var tpl
= new Template
568 tpl
.add mtype
.html_signature
569 if is_vararg
then tpl
.add
"..."
574 ################################################################################
575 # Additions to `model_ext`.
578 redef fun html_signature
do
579 var tpl
= new Template
582 if part
.target
!= null then
583 tpl
.add part
.target
.as(not null).html_link
585 tpl
.add part
.text
.html_escape
592 redef class MInnerClass
593 redef fun nitdoc_url
do return inner
.nitdoc_url
594 redef fun html_signature
do return inner
.html_signature
597 redef class MInnerClassDef
598 redef fun nitdoc_url
do return inner
.nitdoc_url
600 redef fun html_link_to_anchor
do return inner
.html_link_to_anchor
601 redef fun html_link
do return inner
.html_link
602 redef fun html_signature
do return inner
.html_signature