ac02f108ba059722599221c61eb08fcfaca186f8
[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_namespace do return html_link
108 redef fun html_icon do return new BSIcon("book", ["text-muted"])
109 redef var css_classes = ["public"]
110 end
111
112 redef class MGroup
113 redef fun html_icon do return new BSIcon("folder-close", ["text-muted"])
114
115 redef fun html_namespace do
116 var tpl = new Template
117 var parent = self.parent
118 if parent != null then
119 tpl.add parent.html_namespace
120 tpl.add " > "
121 end
122 tpl.add html_link
123 return tpl
124 end
125 end
126
127 redef class MModule
128 redef fun html_icon do return new BSIcon("file", ["text-muted"])
129
130 redef fun html_namespace do
131 var mpackage = self.mpackage
132 var tpl = new Template
133 if mpackage != null then
134 tpl.add mpackage.html_namespace
135 tpl.add " :: "
136 end
137 tpl.add html_link
138 return tpl
139 end
140 end
141
142 redef class MClass
143 redef fun html_icon do return new BSIcon("stop", css_classes)
144 redef fun html_signature(short) do return intro.html_signature(short)
145 redef fun css_classes do return super + [visibility.to_s]
146
147 redef fun html_namespace do
148 var mgroup = intro_mmodule.mgroup
149 var tpl = new Template
150 if mgroup != null then
151 tpl.add mgroup.mpackage.html_namespace
152 tpl.add " :: "
153 end
154 tpl.add "<span>"
155 tpl.add html_link
156 tpl.add "</span>"
157 return tpl
158 end
159 end
160
161 redef class MClassDef
162 redef fun css_classes do return super + mclass.css_classes
163
164 redef fun html_namespace do
165 var tpl = new Template
166 var mpackage = mmodule.mpackage
167 if mpackage != null and is_intro then
168 if is_intro then
169 tpl.add mpackage.html_namespace
170 tpl.add " $ "
171 else
172 tpl.add mmodule.html_namespace
173 tpl.add " $ "
174 var intro_mpackage = mclass.intro.mmodule.mpackage
175 if intro_mpackage != null and mpackage != intro_mpackage then
176 tpl.add intro_mpackage.html_namespace
177 tpl.add " :: "
178 end
179 end
180 else
181 tpl.add mmodule.html_namespace
182 tpl.add " $ "
183 end
184 tpl.add html_link
185 return tpl
186 end
187
188 redef fun html_icon do
189 if is_intro then
190 return new BSIcon("plus", css_classes)
191 end
192 return new BSIcon("asterisk", css_classes)
193 end
194
195 redef fun html_signature(short) do
196 var tpl = new Template
197 var mparameters = mclass.mparameters
198 if not mparameters.is_empty then
199 tpl.add "["
200 for i in [0..mparameters.length[ do
201 tpl.add mparameters[i].html_name
202 if short == null or not short then
203 tpl.add ": "
204 tpl.add bound_mtype.arguments[i].html_signature(short)
205 end
206 if i < mparameters.length - 1 then tpl.add ", "
207 end
208 tpl.add "]"
209 end
210 return tpl
211 end
212 end
213
214 redef class MProperty
215 redef fun html_declaration do return intro.html_declaration
216 redef fun html_signature(short) do return intro.html_signature(short)
217 redef fun html_icon do return new BSIcon("tag", css_classes)
218 redef fun css_classes do return super + [visibility.to_s]
219
220 redef fun html_namespace do
221 var tpl = new Template
222 tpl.add intro_mclassdef.mclass.html_namespace
223 tpl.add " :: "
224 tpl.add intro.html_link
225 return tpl
226 end
227 end
228
229 redef class MPropDef
230 redef fun css_classes do return super + mproperty.css_classes
231
232 redef fun html_namespace do
233 var tpl = new Template
234 tpl.add mclassdef.html_namespace
235 tpl.add " :: "
236 tpl.add html_link
237 return tpl
238 end
239
240 redef fun html_icon do
241 if is_intro then
242 return new BSIcon("plus", css_classes)
243 end
244 return new BSIcon("asterisk", css_classes)
245 end
246 end
247
248 redef class MAttributeDef
249 redef fun html_signature(short) do
250 var static_mtype = self.static_mtype
251 var tpl = new Template
252 if static_mtype != null then
253 tpl.add ": "
254 tpl.add static_mtype.html_signature(short)
255 end
256 return tpl
257 end
258 end
259
260 redef class MMethodDef
261 redef fun html_signature(short) do
262 var new_msignature = self.new_msignature
263 if mproperty.is_root_init and new_msignature != null then
264 return new_msignature.html_signature(short)
265 end
266 var msignature = self.msignature
267 if msignature == null then return new Template
268 return msignature.html_signature(short)
269 end
270 end
271
272 redef class MVirtualTypeProp
273 redef fun html_link(text, title) do return mvirtualtype.html_link(text, title)
274 end
275
276 redef class MVirtualTypeDef
277 redef fun html_signature(short) do
278 var bound = self.bound
279 var tpl = new Template
280 if bound == null then return tpl
281 tpl.add ": "
282 tpl.add bound.html_signature(short)
283 return tpl
284 end
285 end
286
287 redef class MType
288 redef fun html_signature(short) do return html_link
289 end
290
291 redef class MClassType
292 redef fun html_link(text, title) do return mclass.html_link(text, title)
293 end
294
295 redef class MNullableType
296 redef fun html_signature(short) do
297 var tpl = new Template
298 tpl.add "nullable "
299 tpl.add mtype.html_signature(short)
300 return tpl
301 end
302 end
303
304 redef class MGenericType
305 redef fun html_signature(short) do
306 var lnk = html_link
307 var tpl = new Template
308 tpl.add new Link(lnk.href, mclass.name.html_escape, lnk.title)
309 tpl.add "["
310 for i in [0..arguments.length[ do
311 tpl.add arguments[i].html_signature(short)
312 if i < arguments.length - 1 then tpl.add ", "
313 end
314 tpl.add "]"
315 return tpl
316 end
317 end
318
319 redef class MParameterType
320 redef fun html_link(text, title) do
321 if text == null then text = name
322 if title == null then title = "formal type"
323 return new Link("{mclass.html_url}#FT_{name.to_cmangle}", text, title)
324 end
325 end
326
327 redef class MVirtualType
328 redef fun html_link(text, title) do return mproperty.intro.html_link(text, title)
329 end
330
331 redef class MSignature
332 redef fun html_signature(short) do
333 var tpl = new Template
334 if not mparameters.is_empty then
335 tpl.add "("
336 for i in [0..mparameters.length[ do
337 tpl.add mparameters[i].html_signature(short)
338 if i < mparameters.length - 1 then tpl.add ", "
339 end
340 tpl.add ")"
341 end
342 if short == null or not short then
343 var return_mtype = self.return_mtype
344 if return_mtype != null then
345 tpl.add ": "
346 tpl.add return_mtype.html_signature(short)
347 end
348 end
349 return tpl
350 end
351 end
352
353 redef class MParameter
354 redef fun html_signature(short) do
355 var tpl = new Template
356 tpl.add name
357 if short == null or not short then
358 tpl.add ": "
359 tpl.add mtype.html_signature(short)
360 end
361 if is_vararg then tpl.add "..."
362 return tpl
363 end
364 end