doc: Fix the presentation of the inner classes.
[nit.git] / src / doc / doc_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 # Nitdoc model template parts generation
16 module doc_model
17
18 import model_utils
19 import docdown
20 import doc_templates
21 import ordered_tree
22 import model_ext
23
24
25 ################################################################################
26 # Additions to Nit entities.
27
28 redef class MDoc
29 # Comment synopsys HTML escaped
30 fun short_comment: String do return content.first.html_escape
31
32 # Full comment HTML escaped
33 fun full_comment: String do return content.join("\n").html_escape
34
35 # Synopsys in a template
36 fun tpl_short_comment: Streamable do return short_markdown
37
38 # Full comment in a template
39 fun tpl_comment: Streamable do return full_markdown
40 end
41
42 redef class Location
43 # Github url based on this location
44 fun github(gitdir: String): String do
45 var base_dir = getcwd.join_path(gitdir).simplify_path
46 var file_loc = getcwd.join_path(file.filename).simplify_path
47 var gith_loc = file_loc.substring(base_dir.length + 1, file_loc.length)
48 return "{gith_loc}:{line_start},{column_start}--{line_end},{column_end}"
49 end
50 end
51
52 redef class MEntity
53 # HTML Escaped name
54 fun nitdoc_name: String is abstract
55
56 # Used as HTML unique ids
57 fun nitdoc_id: String is abstract
58
59 # URL of this entity Nitdoc page
60 fun nitdoc_url: String is abstract
61
62 # A template link to the mentity `nitdoc_id`
63 fun tpl_anchor: TplLink do
64 var tpl = new TplLink("#{nitdoc_id}", nitdoc_name)
65 if mdoc != null then
66 tpl.title = mdoc.short_comment
67 end
68 return tpl
69 end
70
71 # A template link to the mentity `nitdoc_url`
72 fun tpl_link: TplLink do
73 var tpl = new TplLink(nitdoc_url, nitdoc_name)
74 if mdoc != null then
75 tpl.title = mdoc.short_comment
76 end
77 return tpl
78 end
79
80 # A template article that briefly describe the entity
81 fun tpl_short_article: TplArticle do
82 var tpl = tpl_article
83 if mdoc != null then
84 tpl.content = mdoc.tpl_short_comment
85 end
86 return tpl
87 end
88
89 # A template article that describe the entity
90 fun tpl_article: TplArticle do
91 var tpl = new TplArticle.with_title(nitdoc_id, tpl_title)
92 tpl.title_classes.add "signature"
93 tpl.subtitle = tpl_namespace
94 tpl.summary_title = nitdoc_name
95 return tpl
96 end
97
98 # A template signature that contains modifiers and parameters
99 fun tpl_declaration: Template is abstract
100
101 # A template namespace
102 fun tpl_namespace: Template is abstract
103
104 # A template definition of the mentity
105 # include name, sysnopsys, comment and namespace
106 fun tpl_definition: TplDefinition is abstract
107
108 # A li element that can go in a list
109 fun tpl_list_item: TplListItem do
110 var lnk = new Template
111 lnk.add new TplLabel.with_classes(tpl_css_classes)
112 lnk.add tpl_link
113 if mdoc != null then
114 lnk.add ": "
115 lnk.add mdoc.short_markdown
116 end
117 return new TplListItem.with_content(lnk)
118 end
119
120 fun tpl_css_classes: Array[String] is abstract
121
122 # Box title for this mentity
123 fun tpl_title: Template do
124 var title = new Template
125 title.add tpl_icon
126 title.add tpl_namespace
127 return title
128 end
129
130 # Icon that will be displayed before the title
131 fun tpl_icon: TplIcon do
132 var icon = new TplIcon.with_icon("tag")
133 icon.css_classes.add_all(tpl_css_classes)
134 return icon
135 end
136 end
137
138 redef class MConcern
139 # Return a li element for `self` that can be displayed in a concern list
140 private fun tpl_concern_item: TplListItem do
141 var lnk = new Template
142 lnk.add tpl_anchor
143 if mdoc != null then
144 lnk.add ": "
145 lnk.add mdoc.short_markdown
146 end
147 return new TplListItem.with_content(lnk)
148 end
149 end
150
151 redef class MProject
152 redef fun nitdoc_name do return name.html_escape
153 redef fun nitdoc_id do return nitdoc_name
154 redef fun nitdoc_url do return root.nitdoc_url
155
156 redef fun mdoc do
157 if root != null then
158 return root.mdoc
159 end
160 return super
161 end
162
163 redef fun tpl_declaration do
164 var tpl = new Template
165 tpl.add "<span>project "
166 tpl.add tpl_link
167 tpl.add "</span>"
168 return tpl
169 end
170
171 redef fun tpl_namespace do return tpl_link
172
173 redef fun tpl_definition do
174 var tpl = new TplDefinition
175 if mdoc != null then
176 tpl.comment = mdoc.tpl_comment
177 end
178 return tpl
179 end
180
181 redef fun tpl_css_classes do return ["public"]
182 end
183
184 redef class MGroup
185 redef fun nitdoc_name do return name.html_escape
186
187 redef fun nitdoc_id do
188 if parent != null then
189 return "{parent.nitdoc_id}__{nitdoc_name}"
190 end
191 return "{mproject.nitdoc_id}__{nitdoc_name}"
192 end
193
194 redef fun nitdoc_url do return "group_{nitdoc_id}.html"
195
196 redef fun tpl_namespace do
197 var tpl = new Template
198 tpl.add mproject.tpl_namespace
199 if mproject.root != self then
200 tpl.add "::"
201 tpl.add tpl_link
202 end
203 return tpl
204 end
205
206 redef fun tpl_declaration do
207 var tpl = new Template
208 tpl.add "<span>group "
209 tpl.add tpl_link
210 tpl.add "</span>"
211 return tpl
212 end
213
214 redef fun tpl_definition do
215 var tpl = new TplDefinition
216 if mdoc != null then
217 tpl.comment = mdoc.tpl_comment
218 end
219 return tpl
220 end
221 end
222
223 redef class MModule
224 redef fun nitdoc_name do return name.html_escape
225
226 redef fun nitdoc_id do
227 if mgroup != null then
228 return "{mgroup.nitdoc_id}__{nitdoc_name}"
229 end
230 return nitdoc_name
231 end
232
233 redef fun nitdoc_url do return "module_{nitdoc_id}.html"
234
235 redef fun tpl_declaration do
236 var tpl = new Template
237 tpl.add "<span>module "
238 tpl.add tpl_link
239 tpl.add "</span>"
240 return tpl
241 end
242
243 redef fun tpl_namespace do
244 var tpl = new Template
245 if mgroup != null then
246 tpl.add mgroup.tpl_namespace
247 tpl.add "::"
248 end
249 tpl.add tpl_link
250 return tpl
251 end
252
253 redef fun tpl_definition do
254 var tpl = new TplClassDefinition
255 if mdoc != null then
256 tpl.comment = mdoc.tpl_comment
257 end
258 return tpl
259 end
260
261 redef fun tpl_css_classes do return ["public"]
262 end
263
264 redef class MClass
265 redef fun nitdoc_name do return name.html_escape
266 redef fun nitdoc_id do return "{intro_mmodule.mgroup.mproject}__{name.to_cmangle}"
267 redef fun nitdoc_url do return "class_{nitdoc_id}.html"
268 redef fun mdoc do return intro.mdoc
269
270 redef fun tpl_declaration do return intro.tpl_declaration
271
272 redef fun tpl_namespace do
273 var tpl = new Template
274 tpl.add intro_mmodule.mgroup.mproject.tpl_namespace
275 tpl.add "::<span>"
276 tpl.add tpl_link
277 tpl.add "</span>"
278 return tpl
279 end
280
281 redef fun tpl_title do
282 var title = new Template
283 title.add tpl_icon
284 title.add tpl_link
285 title.add tpl_signature
286 return title
287 end
288
289 redef fun tpl_icon do return intro.tpl_icon
290
291 fun tpl_signature: Template do
292 var tpl = new Template
293 if arity > 0 then
294 tpl.add "["
295 var parameter_names = new Array[String]
296 for p in mparameters do
297 parameter_names.add(p.name)
298 end
299 tpl.add parameter_names.join(", ")
300 tpl.add "]"
301 end
302 return tpl
303 end
304
305 redef fun tpl_article do
306 var tpl = super
307 tpl.summary_title = "{nitdoc_name}{tpl_signature.write_to_string}"
308 return tpl
309 end
310
311 redef fun tpl_css_classes do return intro.tpl_css_classes
312 end
313
314 redef class MClassDef
315 redef fun nitdoc_name do return mclass.nitdoc_name
316 redef fun nitdoc_id do return "{mmodule.nitdoc_id}__{name.to_cmangle}"
317 redef fun nitdoc_url do return "{mclass.nitdoc_url}#{nitdoc_id}"
318
319 redef fun tpl_namespace do
320 var tpl = new Template
321 tpl.add mmodule.tpl_namespace
322 tpl.add "::<span>"
323 tpl.add mclass.tpl_link
324 tpl.add "</span>"
325 return tpl
326 end
327
328 redef fun tpl_article do
329 var tpl = new TplArticle(nitdoc_id)
330 tpl.summary_title = "in {mmodule.nitdoc_name}"
331 tpl.title = tpl_declaration
332 tpl.title_classes.add "signature"
333 var title = new Template
334 title.add "in "
335 title.add mmodule.tpl_namespace
336 tpl.subtitle = title
337 if mdoc != null then
338 tpl.content = mdoc.tpl_comment
339 end
340 return tpl
341 end
342
343 redef fun tpl_title do
344 var title = new Template
345 title.add tpl_icon
346 title.add tpl_link
347 title.add tpl_signature
348 return title
349 end
350
351 redef fun tpl_declaration do
352 var tpl = new Template
353 tpl.add tpl_modifiers
354 tpl.add tpl_link
355 tpl.add tpl_signature
356 return tpl
357 end
358
359 fun tpl_signature: Template do
360 var tpl = new Template
361 var mparameters = mclass.mparameters
362 if not mparameters.is_empty then
363 tpl.add "["
364 for i in [0..mparameters.length[ do
365 tpl.add "{mparameters[i].name}: "
366 tpl.add bound_mtype.arguments[i].tpl_signature
367 if i < mparameters.length - 1 then tpl.add ", "
368 end
369 tpl.add "]"
370 end
371 return tpl
372 end
373
374 redef fun tpl_definition do
375 var tpl = new TplClassDefinition
376 tpl.namespace = tpl_namespace
377 if mdoc != null then
378 tpl.comment = mdoc.tpl_comment
379 end
380 return tpl
381 end
382
383 redef fun tpl_css_classes do
384 var set = new HashSet[String]
385 if is_intro then set.add "intro"
386 set.add_all mclass.intro.modifiers
387 set.add_all modifiers
388 return set.to_a
389 end
390
391 fun tpl_modifiers: Template do
392 var tpl = new Template
393 for modifier in modifiers do
394 if modifier == "public" then continue
395 tpl.add "{modifier} "
396 end
397 return tpl
398 end
399
400 redef fun tpl_list_item do
401 var lnk = new Template
402 lnk.add new TplLabel.with_classes(tpl_css_classes)
403 lnk.add tpl_link
404 if mdoc != null then
405 lnk.add ": "
406 lnk.add mdoc.short_markdown
407 else if mclass.intro.mdoc != null then
408 lnk.add ": "
409 lnk.add mclass.intro.mdoc.short_markdown
410 end
411 return new TplListItem.with_content(lnk)
412 end
413
414 redef fun tpl_anchor: TplLink do
415 var tpl = new TplLink("#{nitdoc_id}", nitdoc_name)
416 if mdoc != null then
417 tpl.title = mdoc.short_comment
418 else if mclass.intro.mdoc != null then
419 tpl.title = mclass.intro.mdoc.short_comment
420 end
421 return tpl
422 end
423
424 redef fun tpl_link: TplLink do
425 var tpl = new TplLink(nitdoc_url, nitdoc_name)
426 if mdoc != null then
427 tpl.title = mdoc.short_comment
428 else if mclass.intro.mdoc != null then
429 tpl.title = mclass.intro.mdoc.short_comment
430 end
431 return tpl
432 end
433 end
434
435 redef class MProperty
436 redef fun nitdoc_name do return name.html_escape
437 redef fun nitdoc_id do return "{intro_mclassdef.mclass.nitdoc_id}__{name.to_cmangle}"
438 redef fun nitdoc_url do return "property_{nitdoc_id}.html"
439
440 redef fun mdoc do return intro.mdoc
441
442 redef fun tpl_namespace do
443 var tpl = new Template
444 tpl.add intro_mclassdef.mclass.tpl_namespace
445 tpl.add "::<span>"
446 tpl.add intro.tpl_link
447 tpl.add "</span>"
448 return tpl
449 end
450
451 redef fun tpl_declaration do return intro.tpl_declaration
452
453 fun tpl_signature: Template do return new Template
454
455 redef fun tpl_title do return intro.tpl_title
456
457 redef fun tpl_icon do return intro.tpl_icon
458
459 redef fun tpl_css_classes do return intro.tpl_css_classes
460 end
461
462 redef class MPropDef
463 redef fun nitdoc_name do return mproperty.nitdoc_name
464 redef fun nitdoc_id do return "{mclassdef.nitdoc_id}__{name.to_cmangle}"
465 redef fun nitdoc_url do return "{mproperty.nitdoc_url}#{nitdoc_id}"
466
467 redef fun tpl_anchor: TplLink do
468 var tpl = new TplLink("#{nitdoc_id}", nitdoc_name)
469 if mdoc != null then
470 tpl.title = mdoc.short_comment
471 else if mproperty.intro.mdoc != null then
472 tpl.title = mproperty.intro.mdoc.short_comment
473 end
474 return tpl
475 end
476
477 redef fun tpl_link: TplLink do
478 var tpl = new TplLink(nitdoc_url, nitdoc_name)
479 if mdoc != null then
480 tpl.title = mdoc.short_comment
481 else if mproperty.intro.mdoc != null then
482 tpl.title = mproperty.intro.mdoc.short_comment
483 end
484 return tpl
485 end
486
487 redef fun tpl_namespace do
488 var tpl = new Template
489 tpl.add mclassdef.tpl_namespace
490 tpl.add "::"
491 tpl.add tpl_link
492 return tpl
493 end
494
495 redef fun tpl_article do
496 var tpl = new TplArticle(nitdoc_id)
497 tpl.summary_title = "in {mclassdef.nitdoc_name}"
498 var title = new Template
499 title.add "in "
500 title.add mclassdef.tpl_link
501 tpl.title = title
502 tpl.subtitle = tpl_declaration
503 if mdoc != null then
504 tpl.content = mdoc.tpl_comment
505 end
506 return tpl
507 end
508
509 redef fun tpl_definition do
510 var tpl = new TplDefinition
511 tpl.namespace = mclassdef.tpl_namespace
512 if mdoc != null then
513 tpl.comment = mdoc.tpl_comment
514 end
515 return tpl
516 end
517
518 redef fun tpl_declaration do
519 var tpl = new Template
520 tpl.add tpl_modifiers
521 tpl.add tpl_link
522 tpl.add tpl_signature
523 return tpl
524 end
525
526 redef fun tpl_css_classes do
527 var set = new HashSet[String]
528 if is_intro then set.add "intro"
529 set.add_all mproperty.intro.modifiers
530 set.add_all modifiers
531 return set.to_a
532 end
533
534 fun tpl_modifiers: Template do
535 var tpl = new Template
536 for modifier in modifiers do
537 if modifier == "public" then continue
538 tpl.add "{modifier} "
539 end
540 return tpl
541 end
542
543 fun tpl_signature: Template do return new Template
544
545 redef fun tpl_list_item do
546 var lnk = new Template
547 lnk.add new TplLabel.with_classes(tpl_css_classes.to_a)
548 var anchor = tpl_link
549 anchor.href = "{mclassdef.mclass.nitdoc_url}#{mproperty.nitdoc_id}"
550 lnk.add anchor
551 if mdoc != null then
552 lnk.add ": "
553 lnk.add mdoc.short_markdown
554 else if mproperty.intro.mdoc != null then
555 lnk.add ": "
556 lnk.add mproperty.intro.mdoc.short_markdown
557 end
558 return new TplListItem.with_content(lnk)
559 end
560
561 fun tpl_inheritance_item: TplListItem do
562 var lnk = new Template
563 lnk.add new TplLabel.with_classes(tpl_css_classes.to_a)
564 lnk.add mclassdef.mmodule.tpl_namespace
565 lnk.add "::"
566 var anchor = mclassdef.tpl_link
567 anchor.href = "{mclassdef.mclass.nitdoc_url}#{mproperty.nitdoc_id}"
568 lnk.add anchor
569 if mdoc != null then
570 lnk.add ": "
571 lnk.add mdoc.short_markdown
572 end
573 var li = new TplListItem.with_content(lnk)
574 li.css_classes.add "signature"
575 return li
576 end
577 end
578
579 redef class MMethod
580 redef fun tpl_signature do
581 var tpl = new Template
582 var params = new Array[String]
583 for param in intro.msignature.mparameters do
584 params.add param.name
585 end
586 if not params.is_empty then
587 tpl.add "("
588 tpl.add params.join(", ")
589 tpl.add ")"
590 end
591 return tpl
592 end
593 end
594
595 redef class MMethodDef
596 redef fun tpl_signature do return msignature.tpl_signature
597 end
598
599 redef class MVirtualTypeProp
600 redef fun tpl_link do return mvirtualtype.tpl_link
601 redef fun tpl_signature do return tpl_link
602 end
603
604 redef class MVirtualTypeDef
605 redef fun tpl_signature do
606 var tpl = new Template
607 tpl.add ": "
608 tpl.add bound.tpl_signature
609 return tpl
610 end
611 end
612
613 redef class MInnerClass
614 redef fun nitdoc_url do return inner.nitdoc_url
615 redef fun tpl_signature do return inner.tpl_signature
616 end
617
618 redef class MInnerClassDef
619 redef fun nitdoc_url do return inner.nitdoc_url
620
621 redef fun tpl_anchor do return inner.tpl_anchor
622 redef fun tpl_link do return inner.tpl_link
623 redef fun tpl_signature do return inner.tpl_signature
624
625 redef fun tpl_definition do
626 var tpl = new TplClassDefinition
627 tpl.namespace = mclassdef.tpl_namespace
628 if mdoc != null then
629 tpl.comment = mdoc.tpl_comment
630 end
631 return tpl
632 end
633 end
634
635 redef class MType
636 fun tpl_signature: Template is abstract
637 end
638
639 redef class MClassType
640 redef fun tpl_link do return mclass.tpl_link
641 redef fun tpl_signature do return tpl_link
642 end
643
644 redef class MNullableType
645 redef fun tpl_signature do
646 var tpl = new Template
647 tpl.add "nullable "
648 tpl.add mtype.tpl_signature
649 return tpl
650 end
651 end
652
653 redef class MGenericType
654 redef fun tpl_signature do
655 var tpl = new Template
656 tpl.add tpl_link
657 tpl.add "["
658 for i in [0..arguments.length[ do
659 tpl.add arguments[i].tpl_signature
660 if i < arguments.length - 1 then tpl.add ", "
661 end
662 tpl.add "]"
663 return tpl
664 end
665 end
666
667 redef class MParameterType
668 redef fun tpl_link do
669 return new TplLink.with_title("{mclass.nitdoc_url}#FT_{name}", name, "formal type")
670 end
671 redef fun tpl_signature do return tpl_link
672 end
673
674 redef class MVirtualType
675 redef fun tpl_link do return mproperty.intro.tpl_link
676 redef fun tpl_signature do return tpl_link
677 end
678
679 redef class MSignature
680 redef fun tpl_signature do
681 var tpl = new Template
682 if not mparameters.is_empty then
683 tpl.add "("
684 for i in [0..mparameters.length[ do
685 tpl.add mparameters[i].tpl_signature
686 if i < mparameters.length - 1 then tpl.add ", "
687 end
688 tpl.add ")"
689 end
690 if return_mtype != null then
691 tpl.add ": "
692 tpl.add return_mtype.tpl_signature
693 end
694 return tpl
695 end
696 end
697
698 redef class MParameter
699 fun tpl_signature: Template do
700 var tpl = new Template
701 tpl.add "{name}: "
702 tpl.add mtype.tpl_signature
703 if is_vararg then tpl.add "..."
704 return tpl
705 end
706 end
707
708 redef class ConcernsTree
709
710 private var seen = new HashSet[MConcern]
711
712 redef fun add(p, e) do
713 if seen.has(e) then return
714 seen.add e
715 super(p, e)
716 end
717
718 fun to_tpl: TplList do
719 var lst = new TplList.with_classes(["list-unstyled", "list-definition"])
720 for r in roots do
721 var li = r.tpl_concern_item
722 lst.add_li li
723 build_list(r, li)
724 end
725 return lst
726 end
727
728 private fun build_list(e: MConcern, li: TplListItem) do
729 if not sub.has_key(e) then return
730 var subs = sub[e]
731 var lst = new TplList.with_classes(["list-unstyled", "list-definition"])
732 for e2 in subs do
733 if e2 isa MGroup and e2.is_root then
734 build_list(e2, li)
735 else
736 var sli = e2.tpl_concern_item
737 lst.add_li sli
738 build_list(e2, sli)
739 end
740 end
741 li.append lst
742 end
743 end
744
745
746 ################################################################################
747 # Additions to `model_ext`.
748
749 redef class MRawType
750 redef fun tpl_signature do
751 var tpl = new Template
752
753 for part in parts do
754 if part.target != null then
755 tpl.add part.target.as(not null).tpl_link
756 else
757 tpl.add part.text.html_escape
758 end
759 end
760 return tpl
761 end
762 end