45bc33ed128e75e87567e07046ee129c199a9ec9
[nit.git] / src / doc / console_templates / console_templates.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 # Introduces templates that compose the documentation in console rendering.
16 module console_templates
17
18 import console_model
19 import doc_phases::doc_structure
20
21 # Renders the page displayable as ASCII.
22 redef class DocPage
23 super Template
24
25 # Renders the footer and content.
26 private fun render_content do add root
27
28 # Renders the whole page
29 redef fun rendering do render_content
30 end
31
32 redef class DocComposite
33 super Template
34
35 # Title that can be decorated for console display.
36 #
37 # Set as `null` if you don't want to display a title.
38 var cs_title: nullable String is noinit, writable
39
40 # Subtitle that can be decorated for console display.
41 #
42 # Set as `null` if you don't want to display a subtitle.
43 var cs_subtitle: nullable String is noinit, writable
44
45 # Renders the element `cs_title` and `cs_subtitle` as any.
46 fun render_title do
47 if cs_title != null then
48 add "{"#" * depth} ".blue.bold
49 addn cs_title.blue.bold
50 end
51 if cs_subtitle != null then
52 addn cs_subtitle.gray.bold
53 end
54 if cs_title != null or cs_subtitle != null then addn ""
55 end
56
57 # Renders the element body.
58 fun render_body do for child in children do add child.write_to_string
59
60 redef fun rendering do
61 render_title
62 render_body
63 end
64 end
65
66 redef class DocRoot
67 redef fun rendering do render_body
68 end
69
70 redef class ConcernSection
71 redef var cs_title is lazy do return "in {mentity.cs_namespace}"
72 redef var cs_subtitle is lazy do return mentity.cs_declaration
73
74 redef fun rendering do
75 var mentity = self.mentity
76 if mentity isa MGroup and mentity.mproject.root == mentity then
77 render_body
78 else
79 super
80 end
81 end
82 end
83
84 redef class ConstructorsSection
85 redef var cs_title = "Constructors"
86 redef var cs_subtitle = null
87 end
88
89 redef class MEntityComposite
90 redef var cs_title is lazy do return mentity.cs_name
91 redef var cs_subtitle is lazy do return mentity.cs_namespace
92 end
93
94 redef class IntroArticle
95 redef var cs_title = null
96 redef var cs_subtitle = null
97
98 redef fun render_body do
99 addn " {mentity.cs_declaration.bold}"
100 addn " {mentity.cs_location.gray.bold}"
101 addn ""
102 var comment = mentity.cs_comment
103 if comment != null then
104 add comment
105 end
106 addn ""
107 super
108 end
109 end
110
111 redef class ConcernsArticle
112 redef var cs_title = "Concerns"
113 redef var cs_subtitle = null
114
115 redef fun render_body do
116 var w = new StringWriter
117 concerns.write_to(w)
118 addn w.to_s
119 end
120 end
121
122 redef class DefinitionArticle
123 # If short, displays only synopsyses.
124 var is_short = false is writable
125
126 redef fun render_body do
127 addn " {mentity.cs_visibility_color(mentity.cs_declaration).bold}"
128 addn " {mentity.cs_location.gray.bold}"
129 addn ""
130 var comment
131 if is_short then
132 comment = mentity.cs_short_comment
133 else
134 comment = mentity.cs_comment
135 end
136 if comment != null then
137 add comment
138 end
139 addn ""
140 super
141 end
142 end