7a51f0692b1083e33b789ab3cc3e46c172c9b93c
[nit.git] / src / doc / templates / templates_html.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 # Translate mentities to html blocks.
16 module templates_html
17
18 import model::model_collect
19 import doc::doc_down
20 import html::bootstrap
21
22 redef class MEntity
23
24 # The MEntity unique ID in the HTML output
25 var html_id: String is lazy do return full_name.to_cmangle
26
27 # The MEntity URL in the HTML output
28 #
29 # You MUST redefine this method.
30 # Depending on your implementation, this URL can be a page URL or an anchor.
31 var html_url: String is lazy do return html_id
32
33 # The MEntity name escaped for HTML
34 var html_name: String is lazy do return name.html_escape
35
36 # The MEntity `full_name` escaped for HTML
37 var html_full_name: String is lazy do return full_name.html_escape
38
39 # Link to the MEntity in the HTML output
40 #
41 # You should redefine this method depending on the organization or your
42 # output.
43 fun html_link(text, title: nullable String): Link do
44 if text == null then
45 text = html_name
46 end
47 var mdoc = self.mdoc_or_fallback
48 if title == null and mdoc != null then
49 title = mdoc.synopsis.html_escape
50 end
51 return new Link(html_url, text, title)
52 end
53
54 # Returns the complete MEntity declaration decorated with HTML
55 #
56 # Examples:
57 # * MPackage: `package foo`
58 # * MGroup: `group foo`
59 # * MModule: `module foo`
60 # * MClass: `private abstract class Foo[E: Object]`
61 # * MClassDef: `redef class Foo[E]`
62 # * MProperty: `private fun foo(e: Object): Int`
63 # * MPropdef: `redef fun foo(e)`
64 fun html_declaration: Template do
65 var tpl = new Template
66 tpl.add "<span class='signature'>"
67 for modifier in collect_modifiers do
68 tpl.add "<span class='modifier'>{modifier}</span>&nbsp;"
69 end
70 tpl.add "<span class='name'>{html_link.write_to_string}</span>"
71 tpl.add html_signature(false)
72 tpl.add "</span>"
73 return tpl
74 end
75
76 # Returns the MEntity signature decorated with HTML
77 #
78 # This function only returns the parenthesis and return types.
79 # See `html_declaration` for the full declaration including modifiers and name.
80 fun html_signature(short: nullable Bool): Template do return new Template
81
82 # Returns `full_name` decorated with HTML links
83 fun html_namespace: Template is abstract
84
85 # An icon representative of the mentity
86 fun html_icon: BSIcon do return new BSIcon("tag", ["text-muted"])
87
88 # A li element that can go in a `HTMLList`
89 fun html_list_item: ListItem do
90 var tpl = new Template
91 tpl.add html_namespace
92 var comment = mdoc_or_fallback
93 if comment != null then
94 tpl.add ": "
95 tpl.add comment.html_synopsis
96 end
97 return new ListItem(tpl)
98 end
99
100 # CSS classes used to decorate `self`
101 #
102 # Mainly used for icons.
103 var css_classes: Array[String] = collect_modifiers is lazy
104 end
105
106 redef class MPackage
107 redef fun html_url do return "package_{html_id}.html"
108 redef fun html_namespace do return html_link
109 redef fun html_icon do return new BSIcon("book", ["text-muted"])
110 redef var css_classes = ["public"]
111 end
112
113 redef class MGroup
114 redef fun html_url do return "group_{html_id}.html"
115 redef fun html_icon do return new BSIcon("folder-close", ["text-muted"])
116
117 redef fun html_namespace do
118 var tpl = new Template
119 var parent = self.parent
120 if parent != null then
121 tpl.add parent.html_namespace
122 tpl.add " > "
123 end
124 tpl.add html_link
125 return tpl
126 end
127 end
128
129 redef class MModule
130 redef fun html_url do return "module_{html_id}.html"
131 redef fun html_icon do return new BSIcon("file", ["text-muted"])
132
133 redef fun html_namespace do
134 var mpackage = self.mpackage
135 var tpl = new Template
136 if mpackage != null then
137 tpl.add mpackage.html_namespace
138 tpl.add " :: "
139 end
140 tpl.add html_link
141 return tpl
142 end
143 end
144
145 redef class MClass
146 redef fun html_url do return "class_{html_id}.html"
147 redef fun html_icon do return new BSIcon("stop", css_classes)
148 redef fun html_signature(short) do return intro.html_signature(short)
149 redef fun css_classes do return super + [visibility.to_s]
150
151 redef fun html_namespace do
152 var mgroup = intro_mmodule.mgroup
153 var tpl = new Template
154 if mgroup != null then
155 tpl.add mgroup.mpackage.html_namespace
156 tpl.add " :: "
157 end
158 tpl.add "<span>"
159 tpl.add html_link
160 tpl.add "</span>"
161 return tpl
162 end
163 end
164
165 redef class MClassDef
166 redef fun html_url do return "{mclass.html_url}#{html_id}"
167 redef fun css_classes do return super + mclass.css_classes
168
169 redef fun html_namespace do
170 var tpl = new Template
171 var mpackage = mmodule.mpackage
172 if mpackage != null and is_intro then
173 if is_intro then
174 tpl.add mpackage.html_namespace
175 tpl.add " $ "
176 else
177 tpl.add mmodule.html_namespace
178 tpl.add " $ "
179 var intro_mpackage = mclass.intro.mmodule.mpackage
180 if intro_mpackage != null and mpackage != intro_mpackage then
181 tpl.add intro_mpackage.html_namespace
182 tpl.add " :: "
183 end
184 end
185 else
186 tpl.add mmodule.html_namespace
187 tpl.add " $ "
188 end
189 tpl.add html_link
190 return tpl
191 end
192
193 redef fun html_icon do
194 if is_intro then
195 return new BSIcon("plus", css_classes)
196 end
197 return new BSIcon("asterisk", css_classes)
198 end
199
200 redef fun html_signature(short) do
201 var tpl = new Template
202 var mparameters = mclass.mparameters
203 if not mparameters.is_empty then
204 tpl.add "["
205 for i in [0..mparameters.length[ do
206 tpl.add mparameters[i].html_name
207 if short == null or not short then
208 tpl.add ": "
209 tpl.add bound_mtype.arguments[i].html_signature(short)
210 end
211 if i < mparameters.length - 1 then tpl.add ", "
212 end
213 tpl.add "]"
214 end
215 return tpl
216 end
217 end
218
219 redef class MProperty
220 redef fun html_url do return "property_{html_id}.html"
221 redef fun html_declaration do return intro.html_declaration
222 redef fun html_signature(short) do return intro.html_signature(short)
223 redef fun html_icon do return new BSIcon("tag", css_classes)
224 redef fun css_classes do return super + [visibility.to_s]
225
226 redef fun html_namespace do
227 var tpl = new Template
228 tpl.add intro_mclassdef.mclass.html_namespace
229 tpl.add " :: "
230 tpl.add intro.html_link
231 return tpl
232 end
233 end
234
235 redef class MPropDef
236 redef fun html_url do return "{mproperty.html_url}#{html_id}"
237 redef fun css_classes do return super + mproperty.css_classes
238
239 redef fun html_namespace do
240 var tpl = new Template
241 tpl.add mclassdef.html_namespace
242 tpl.add " :: "
243 tpl.add html_link
244 return tpl
245 end
246
247 redef fun html_icon do
248 if is_intro then
249 return new BSIcon("plus", css_classes)
250 end
251 return new BSIcon("asterisk", css_classes)
252 end
253 end
254
255 redef class MAttributeDef
256 redef fun html_signature(short) do
257 var static_mtype = self.static_mtype
258 var tpl = new Template
259 if static_mtype != null then
260 tpl.add ": "
261 tpl.add static_mtype.html_signature(short)
262 end
263 return tpl
264 end
265 end
266
267 redef class MMethodDef
268 redef fun html_signature(short) do
269 var new_msignature = self.new_msignature
270 if mproperty.is_root_init and new_msignature != null then
271 return new_msignature.html_signature(short)
272 end
273 return msignature.as(not null).html_signature(short)
274 end
275 end
276
277 redef class MVirtualTypeProp
278 redef fun html_link(text, title) do return mvirtualtype.html_link(text, title)
279 end
280
281 redef class MVirtualTypeDef
282 redef fun html_signature(short) do
283 var bound = self.bound
284 var tpl = new Template
285 if bound == null then return tpl
286 tpl.add ": "
287 tpl.add bound.html_signature(short)
288 return tpl
289 end
290 end
291
292 redef class MType
293 redef fun html_signature(short) do return html_link
294 end
295
296 redef class MClassType
297 redef fun html_link(text, title) do return mclass.html_link(text, title)
298 end
299
300 redef class MNullableType
301 redef fun html_signature(short) do
302 var tpl = new Template
303 tpl.add "nullable "
304 tpl.add mtype.html_signature(short)
305 return tpl
306 end
307 end
308
309 redef class MGenericType
310 redef fun html_signature(short) do
311 var lnk = html_link
312 var tpl = new Template
313 tpl.add new Link(lnk.href, mclass.name.html_escape, lnk.title)
314 tpl.add "["
315 for i in [0..arguments.length[ do
316 tpl.add arguments[i].html_signature(short)
317 if i < arguments.length - 1 then tpl.add ", "
318 end
319 tpl.add "]"
320 return tpl
321 end
322 end
323
324 redef class MParameterType
325 redef fun html_link(text, title) do
326 if text == null then text = name
327 if title == null then title = "formal type"
328 return new Link("{mclass.html_url}#FT_{name.to_cmangle}", text, title)
329 end
330 end
331
332 redef class MVirtualType
333 redef fun html_link(text, title) do return mproperty.intro.html_link(text, title)
334 end
335
336 redef class MSignature
337 redef fun html_signature(short) do
338 var tpl = new Template
339 if not mparameters.is_empty then
340 tpl.add "("
341 for i in [0..mparameters.length[ do
342 tpl.add mparameters[i].html_signature(short)
343 if i < mparameters.length - 1 then tpl.add ", "
344 end
345 tpl.add ")"
346 end
347 if short == null or not short then
348 var return_mtype = self.return_mtype
349 if return_mtype != null then
350 tpl.add ": "
351 tpl.add return_mtype.html_signature(short)
352 end
353 end
354 return tpl
355 end
356 end
357
358 redef class MParameter
359 redef fun html_signature(short) do
360 var tpl = new Template
361 tpl.add name
362 if short == null or not short then
363 tpl.add ": "
364 tpl.add mtype.html_signature(short)
365 end
366 if is_vararg then tpl.add "..."
367 return tpl
368 end
369 end