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