tests: Updated test for byte literals
[nit.git] / src / doc / html_templates / html_model.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
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
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
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.
14
15 # HTML templates for Nit model MEntities.
16 module html_model
17
18 import doc_base
19 import doc_down
20 import html_components
21 import html::bootstrap
22 import ordered_tree
23
24 redef class MEntity
25 # URL of this entity’s Nitdoc page.
26 fun nitdoc_url: String is abstract
27
28 # Returns the mentity name without short signature.
29 #
30 # * MProject: `foo`
31 # * MGroup: `foo`
32 # * MModule: `foo`
33 # * MClass: `Foo[E]`
34 # * MClassDef: `Foo[E]`
35 # * MProperty: `foo(e)`
36 # * MPropdef: `foo(e)`
37 var html_name: String is lazy do return name.html_escape
38
39 # Returns a Link to the mentity `html_url`.
40 #
41 # Example: `<a href="html_url" title="mdoc.short_comment">html_short_name</a>
42 var html_link: Link is lazy do
43 var tpl = new Link(nitdoc_url, html_name)
44 var mdoc = mdoc_or_fallback
45 if mdoc != null then
46 tpl.title = mdoc.short_comment
47 end
48 return tpl
49 end
50
51 # Returns a Link to the mentity `nitdoc_id`.
52 #
53 # Example: `<a href="#nitdoc_id" title="mdoc.short_comment">html_short_name</a>
54 fun html_link_to_anchor: Link do
55 var tpl = new Link("#{nitdoc_id}", html_name)
56 var mdoc = mdoc_or_fallback
57 if mdoc != null then
58 tpl.title = mdoc.short_comment
59 end
60 return tpl
61 end
62
63 # Returns the list of keyword used in `self` declaration.
64 fun html_modifiers: Array[String] is abstract
65
66 # Returns the complete MEntity declaration decorated with HTML.
67 #
68 # * MProject: `project foo`
69 # * MGroup: `group foo`
70 # * MModule: `module foo`
71 # * MClass: `private abstract class Foo[E: Object]`
72 # * MClassDef: `redef class Foo[E]`
73 # * MProperty: `private fun foo(e: Object): Int`
74 # * MPropdef: `redef fun foo(e)`
75 fun html_declaration: Template do
76 var tpl = new Template
77 tpl.add "<span>"
78 tpl.add html_modifiers.join(" ")
79 tpl.add " "
80 tpl.add html_link
81 tpl.add "</span>"
82 return tpl
83 end
84
85 # Returns `self` namespace decorated with HTML links.
86 #
87 # * MProject: `mproject`
88 # * MGroup: `mproject(::group)`
89 # * MModule: `mgroup::mmodule`
90 # * MClass: `mproject::mclass`
91 # * MClassDef: `mmodule::mclassdef`
92 # * MProperty: `mclass::mprop`
93 # * MPropdef: `mclassdef:mpropdef`
94 fun html_namespace: Template is abstract
95
96 # Returns the comment of this MEntity formatted as HTML.
97 var html_comment: nullable Writable is lazy do
98 var mdoc = mdoc_or_fallback
99 if mdoc == null then return null
100 return mdoc.tpl_comment
101 end
102
103 # Returns the comment of this MEntity formatted as HTML.
104 var html_short_comment: nullable Writable is lazy do
105 var mdoc = mdoc_or_fallback
106 if mdoc == null then return null
107 return mdoc.tpl_short_comment
108 end
109
110 # Icon that will be displayed before the title
111 fun html_icon: BSIcon do
112 var icon = new BSIcon("tag")
113 icon.css_classes.add_all(css_classes)
114 return icon
115 end
116
117 # CSS classes used to decorate `self`.
118 #
119 # Mainly used for icons.
120 var css_classes = new Array[String]
121
122 # A li element that can go in a `HTMLList`.
123 fun html_list_item: ListItem do
124 var tpl = new Template
125 tpl.add new DocHTMLLabel.with_classes(css_classes)
126 tpl.add html_link
127 var comment = html_short_comment
128 if comment != null then
129 tpl.add ": "
130 tpl.add comment
131 end
132 return new ListItem(tpl)
133 end
134 end
135
136 redef class MProject
137 redef var nitdoc_id = name.to_cmangle is lazy
138 redef fun nitdoc_url do return root.nitdoc_url
139 redef var html_modifiers = ["project"]
140 redef fun html_namespace do return html_link
141 redef var css_classes = ["public"]
142 end
143
144 redef class MGroup
145 redef var nitdoc_id is lazy do
146 if parent != null then
147 return "{parent.nitdoc_id}__{name.to_cmangle}"
148 end
149 return name.to_cmangle
150 end
151
152 redef fun nitdoc_url do return "group_{nitdoc_id}.html"
153 redef var html_modifiers = ["group"]
154
155 # Depends if `self` is root or not.
156 #
157 # * If root `mproject`.
158 # * Else `mproject::self`.
159 redef fun html_namespace do
160 var tpl = new Template
161 tpl.add mproject.html_namespace
162 if mproject.root != self then
163 tpl.add "::"
164 tpl.add html_link
165 end
166 return tpl
167 end
168
169 redef var css_classes = ["public"]
170 end
171
172 redef class MModule
173 redef var nitdoc_id is lazy do
174 if mgroup != null then
175 if mgroup.mmodules.length == 1 then
176 return "{mgroup.nitdoc_id}-"
177 else
178 return "{mgroup.nitdoc_id}__{name.to_cmangle}"
179 end
180 end
181 return name.to_cmangle
182 end
183
184 redef fun nitdoc_url do return "module_{nitdoc_id}.html"
185 redef var html_modifiers = ["module"]
186
187 # Depends if `self` belongs to a MGroup.
188 #
189 # * If mgroup `mgroup::self`.
190 # * Else `self`.
191 redef fun html_namespace do
192 var tpl = new Template
193 if mgroup != null then
194 tpl.add mgroup.html_namespace
195 tpl.add "::"
196 end
197 tpl.add html_link
198 return tpl
199 end
200
201 redef var css_classes = ["public"]
202 end
203
204 redef class MClass
205 redef var nitdoc_id = "{intro_mmodule.nitdoc_id}__{name.to_cmangle}" is lazy
206 redef fun nitdoc_url do return "class_{nitdoc_id}.html"
207 redef fun mdoc_or_fallback do return intro.mdoc
208
209 # Format: `Foo[E]`
210 redef var html_name is lazy do
211 var tpl = new Template
212 tpl.add name.html_escape
213 if arity > 0 then
214 tpl.add "["
215 var parameter_names = new Array[String]
216 for p in mparameters do
217 parameter_names.add(p.html_name)
218 end
219 tpl.add parameter_names.join(", ")
220 tpl.add "]"
221 end
222 return tpl.write_to_string
223 end
224
225 redef fun html_modifiers do return intro.html_modifiers
226 redef fun html_declaration do return intro.html_declaration
227
228 # Returns `mproject::self`.
229 redef fun html_namespace do
230 var tpl = new Template
231 tpl.add intro_mmodule.mgroup.mproject.html_namespace
232 tpl.add "::<span>"
233 tpl.add html_link
234 tpl.add "</span>"
235 return tpl
236 end
237
238 # Returns `intro.html_short_signature`.
239 fun html_short_signature: Template do return intro.html_short_signature
240
241 # Returns `intro.html_signature`.
242 fun html_signature: Template do return intro.html_signature
243
244 redef fun html_icon do return intro.html_icon
245 redef fun css_classes do return intro.css_classes
246 end
247
248 redef class MClassDef
249 redef var nitdoc_id = "{mmodule.nitdoc_id}__{name.to_cmangle}" is lazy
250 redef fun nitdoc_url do return "{mclass.nitdoc_url}#{nitdoc_id}"
251 redef fun mdoc_or_fallback do return mdoc or else mclass.mdoc_or_fallback
252
253 # Depends if `self` is an intro or not.
254 #
255 # * If intro contains the visibility and kind.
256 # * If redef contains the `redef` keyword and kind.
257 redef fun html_modifiers do
258 var res = new Array[String]
259 if not is_intro then
260 res.add "redef"
261 else
262 if mclass.visibility != public_visibility then
263 res.add mclass.visibility.to_s
264 end
265 end
266 res.add mclass.kind.to_s
267 return res
268 end
269
270 # Depends if `self` is an intro or not.
271 #
272 # For intro: `private abstract class Foo[E: Object]`
273 # For redef: `redef class Foo[E]`
274 redef fun html_declaration do
275 var tpl = new Template
276 tpl.add "<span>"
277 tpl.add html_modifiers.join(" ")
278 tpl.add " "
279 tpl.add html_link
280 if is_intro then
281 tpl.add html_signature
282 else
283 tpl.add html_short_signature
284 end
285 tpl.add "</span>"
286 return tpl
287 end
288
289 # Returns `mmodule::self`
290 redef fun html_namespace do
291 var tpl = new Template
292 tpl.add mmodule.html_namespace
293 tpl.add "::<span>"
294 tpl.add mclass.html_link
295 tpl.add "</span>"
296 return tpl
297 end
298
299 # Returns the MClassDef generic signature without static bounds.
300 fun html_short_signature: Template do
301 var tpl = new Template
302 var mparameters = mclass.mparameters
303 if not mparameters.is_empty then
304 tpl.add "["
305 for i in [0..mparameters.length[ do
306 tpl.add mparameters[i].html_name
307 if i < mparameters.length - 1 then tpl.add ", "
308 end
309 tpl.add "]"
310 end
311 return tpl
312 end
313
314 # Returns the MClassDef generic signature with static bounds.
315 fun html_signature: Template do
316 var tpl = new Template
317 var mparameters = mclass.mparameters
318 if not mparameters.is_empty then
319 tpl.add "["
320 for i in [0..mparameters.length[ do
321 tpl.add "{mparameters[i].html_name}: "
322 tpl.add bound_mtype.arguments[i].html_signature
323 if i < mparameters.length - 1 then tpl.add ", "
324 end
325 tpl.add "]"
326 end
327 return tpl
328 end
329
330 redef fun css_classes do
331 var set = new HashSet[String]
332 if is_intro then set.add "intro"
333 for m in mclass.intro.modifiers do set.add m.to_cmangle
334 for m in modifiers do set.add m.to_cmangle
335 return set.to_a
336 end
337 end
338
339 redef class MProperty
340 redef var nitdoc_id = "{intro_mclassdef.mclass.nitdoc_id}__{name.to_cmangle}" is lazy
341 redef fun nitdoc_url do return "property_{nitdoc_id}.html"
342 redef fun mdoc_or_fallback do return intro.mdoc
343 redef fun html_modifiers do return intro.html_modifiers
344 redef fun html_declaration do return intro.html_declaration
345
346 # Returns `mclass::self`.
347 redef fun html_namespace do
348 var tpl = new Template
349 tpl.add intro_mclassdef.mclass.html_namespace
350 tpl.add "::<span>"
351 tpl.add intro.html_link
352 tpl.add "</span>"
353 return tpl
354 end
355
356 # Returns `intro.html_short_signature`.
357 fun html_short_signature: Template do return intro.html_short_signature
358
359 # Returns `intro.html_signature`.
360 fun html_signature: Template do return intro.html_signature
361
362 redef fun css_classes do return intro.css_classes
363 end
364
365 redef class MPropDef
366 redef var nitdoc_id = "{mclassdef.nitdoc_id}__{name.to_cmangle}" is lazy
367 redef fun nitdoc_url do return "{mproperty.nitdoc_url}#{nitdoc_id}"
368 redef fun mdoc_or_fallback do return mdoc or else mproperty.mdoc_or_fallback
369
370 # Depends if `self` is an intro or not.
371 #
372 # * If intro contains the visibility and kind.
373 # * If redef contains the `redef` keyword and kind.
374 redef fun html_modifiers do
375 var res = new Array[String]
376 if not is_intro then
377 res.add "redef"
378 else
379 if mproperty.visibility != public_visibility then
380 res.add mproperty.visibility.to_s
381 end
382 end
383 return res
384 end
385
386 # Depends if `self` is an intro or not.
387 #
388 # For intro: `private fun foo(e: Object): Bar is abstract`
389 # For redef: `redef fun foo(e) is cached`
390 redef fun html_declaration do
391 var tpl = new Template
392 tpl.add "<span>"
393 tpl.add html_modifiers.join(" ")
394 tpl.add " "
395 if is_intro then
396 tpl.add html_link
397 tpl.add html_signature
398 else
399 tpl.add mproperty.intro.html_link
400 tpl.add html_short_signature
401 end
402 tpl.add "</span>"
403 return tpl
404 end
405
406 # Returns `mclassdef::self`
407 redef fun html_namespace do
408 var tpl = new Template
409 tpl.add mclassdef.html_namespace
410 tpl.add "::"
411 tpl.add html_link
412 return tpl
413 end
414
415 # Returns the MPropdDef signature without static types.
416 fun html_short_signature: Template is abstract
417
418 # Returns the MPropDef signature with static types.
419 fun html_signature: Template is abstract
420
421 redef fun css_classes do
422 var set = new HashSet[String]
423 if is_intro then set.add "intro"
424 for m in mproperty.intro.modifiers do set.add m.to_cmangle
425 for m in modifiers do set.add m.to_cmangle
426 return set.to_a
427 end
428 end
429
430 redef class MAttributeDef
431
432 redef fun html_modifiers do
433 var res = super
434 res.add "var"
435 return res
436 end
437
438 redef fun html_short_signature do return new Template
439
440 redef fun html_signature do
441 var tpl = new Template
442 if static_mtype != null then
443 tpl.add ": "
444 tpl.add static_mtype.html_signature
445 end
446 return tpl
447 end
448 end
449
450 redef class MMethodDef
451
452 # FIXME annotation should be handled in their own way
453 redef fun html_modifiers do
454 if mproperty.is_init then
455 var res = new Array[String]
456 if mproperty.visibility != public_visibility then
457 res.add mproperty.visibility.to_s
458 end
459 return res
460 end
461 var res = super
462 if is_abstract then
463 res.add "abstract"
464 else if is_intern then
465 res.add "intern"
466 end
467 res.add "fun"
468 return res
469 end
470
471 redef fun html_declaration do
472 if mproperty.is_init then
473 var tpl = new Template
474 tpl.add "<span>"
475 tpl.add html_modifiers.join(" ")
476 tpl.add " "
477 tpl.add html_link
478 tpl.add html_signature
479 tpl.add "</span>"
480 return tpl
481 end
482 return super
483 end
484
485 redef fun html_short_signature do
486 if mproperty.is_root_init and new_msignature != null then
487 return new_msignature.html_short_signature
488 end
489 return msignature.html_short_signature
490 end
491
492 redef fun html_signature do
493 if mproperty.is_root_init and new_msignature != null then
494 return new_msignature.html_signature
495 end
496 return msignature.html_signature
497 end
498 end
499
500 redef class MVirtualTypeProp
501 redef fun html_link do return mvirtualtype.html_link
502 end
503
504 redef class MVirtualTypeDef
505
506 redef fun html_modifiers do
507 var res = super
508 res.add "type"
509 return res
510 end
511
512 redef fun html_short_signature do return new Template
513
514 redef fun html_signature do
515 var tpl = new Template
516 if bound == null then return tpl
517 tpl.add ": "
518 tpl.add bound.html_signature
519 return tpl
520 end
521 end
522
523 redef class MType
524 # Returns the signature of this type whithout bounds.
525 fun html_short_signature: Template is abstract
526
527 # Returns the signature of this type.
528 fun html_signature: Template is abstract
529 end
530
531 redef class MClassType
532 redef fun html_link do return mclass.html_link
533 redef fun html_short_signature do return html_link
534 redef fun html_signature do return html_link
535 end
536
537 redef class MNullableType
538
539 redef fun html_short_signature do
540 var tpl = new Template
541 tpl.add "nullable "
542 tpl.add mtype.html_short_signature
543 return tpl
544 end
545
546 redef fun html_signature do
547 var tpl = new Template
548 tpl.add "nullable "
549 tpl.add mtype.html_signature
550 return tpl
551 end
552 end
553
554 redef class MGenericType
555 redef fun html_short_signature do
556 var lnk = html_link
557 var tpl = new Template
558 tpl.add new Link.with_title(lnk.href, mclass.name.html_escape, lnk.title)
559 tpl.add "["
560 for i in [0..arguments.length[ do
561 tpl.add arguments[i].html_short_signature
562 if i < arguments.length - 1 then tpl.add ", "
563 end
564 tpl.add "]"
565 return tpl
566 end
567
568 redef fun html_signature do
569 var lnk = html_link
570 var tpl = new Template
571 tpl.add new Link.with_title(lnk.href, mclass.name.html_escape, lnk.title)
572 tpl.add "["
573 for i in [0..arguments.length[ do
574 tpl.add arguments[i].html_signature
575 if i < arguments.length - 1 then tpl.add ", "
576 end
577 tpl.add "]"
578 return tpl
579 end
580 end
581
582 redef class MParameterType
583 redef fun html_link do
584 return new Link.with_title("{mclass.nitdoc_url}#FT_{name.to_cmangle}", name, "formal type")
585 end
586
587 redef fun html_short_signature do return html_link
588 redef fun html_signature do return html_link
589 end
590
591 redef class MVirtualType
592 redef fun html_link do return mproperty.intro.html_link
593 redef fun html_signature do return html_link
594 end
595
596 redef class MSignature
597
598 redef fun html_short_signature do
599 var tpl = new Template
600 if not mparameters.is_empty then
601 tpl.add "("
602 for i in [0..mparameters.length[ do
603 tpl.add mparameters[i].html_short_signature
604 if i < mparameters.length - 1 then tpl.add ", "
605 end
606 tpl.add ")"
607 end
608 return tpl
609 end
610
611 redef fun html_signature do
612 var tpl = new Template
613 if not mparameters.is_empty then
614 tpl.add "("
615 for i in [0..mparameters.length[ do
616 tpl.add mparameters[i].html_signature
617 if i < mparameters.length - 1 then tpl.add ", "
618 end
619 tpl.add ")"
620 end
621 if return_mtype != null then
622 tpl.add ": "
623 tpl.add return_mtype.html_signature
624 end
625 return tpl
626 end
627 end
628
629 redef class MParameter
630
631 # Returns `self` name and ellipsys if any.
632 fun html_short_signature: Template do
633 var tpl = new Template
634 tpl.add name
635 if is_vararg then tpl.add "..."
636 return tpl
637 end
638
639 # Returns `self` name with it's static type and ellipsys if any.
640 fun html_signature: Template do
641 var tpl = new Template
642 tpl.add "{name}: "
643 tpl.add mtype.html_signature
644 if is_vararg then tpl.add "..."
645 return tpl
646 end
647 end
648
649 redef class ConcernsTree
650 # Render `self` as a hierarchical UnorderedList.
651 fun html_list: UnorderedList do
652 var lst = new UnorderedList
653 lst.css_classes.add "list-unstyled list-definition"
654 for r in roots do
655 var li = r.html_concern_item
656 lst.add_li li
657 build_html_list(r, li)
658 end
659 return lst
660 end
661
662 # Build the html list recursively.
663 private fun build_html_list(e: MConcern, li: ListItem) do
664 if not sub.has_key(e) then return
665 var subs = sub[e]
666 var lst = new UnorderedList
667 lst.css_classes.add "list-unstyled list-definition"
668 for e2 in subs do
669 if e2 isa MGroup and e2.is_root then
670 build_html_list(e2, li)
671 else
672 var sli = e2.html_concern_item
673 lst.add_li sli
674 build_html_list(e2, sli)
675 end
676 end
677 var text = new Template
678 text.add li.text
679 if not lst.is_empty then text.add lst
680 li.text = text
681 end
682 end
683
684 redef class MConcern
685 # Return a li element for `self` that can be displayed in a concern list
686 private fun html_concern_item: ListItem do
687 var lnk = html_link
688 var tpl = new Template
689 tpl.add new Link.with_title("#concern:{nitdoc_id}", lnk.text, lnk.title)
690 var comment = html_short_comment
691 if comment != null then
692 tpl.add ": "
693 tpl.add comment
694 end
695 return new ListItem(tpl)
696 end
697 end
698
699 ################################################################################
700 # Additions to `model_ext`.
701
702 redef class MRawType
703 redef fun html_signature do
704 var tpl = new Template
705
706 for part in parts do
707 if part.target != null then
708 tpl.add part.target.as(not null).html_link
709 else
710 tpl.add part.text.html_escape
711 end
712 end
713 return tpl
714 end
715 end
716
717 redef class MInnerClass
718 redef fun nitdoc_url do return inner.nitdoc_url
719 redef fun html_signature do return inner.html_signature
720 end
721
722 redef class MInnerClassDef
723 redef fun nitdoc_url do return inner.nitdoc_url
724
725 redef fun html_link_to_anchor do return inner.html_link_to_anchor
726 redef fun html_link do return inner.html_link
727 redef fun html_signature do return inner.html_signature
728 end