nitdoc: move `DocComposite::title` to `doc_base`
[nit.git] / src / doc / html_templates / html_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 HTML rendering.
16 module html_templates
17
18 import html_model
19 import html::bootstrap
20 import doc_phases::doc_structure
21 import doc_phases::doc_hierarchies
22 import doc_phases::doc_graphs
23 import doc_phases::doc_intros_redefs
24 import doc_phases::doc_lin
25
26 # Renders the page as HTML.
27 redef class DocPage
28 super Template
29
30 # Page url.
31 var html_url: String is writable, noinit
32
33 # Directory where css, js and other assets can be found.
34 var shareurl: String is writable, noinit
35
36 # Attributes of the body tag element.
37 var body_attrs = new Array[TagAttribute]
38
39 # Top menu template if any.
40 var topmenu: DocTopMenu is writable, noinit
41
42 # Sidebar template if any.
43 var sidebar: nullable DocSideBar = null is writable
44
45 # Footer content if any.
46 var footer: nullable Writable = null is writable
47
48 # JS scripts to append at the end of the body
49 var scripts = new Array[TplScript]
50
51 # Renders the html `<head>`.
52 private fun render_head do
53 var css = (self.shareurl / "css").html_escape
54 var vendors = (self.shareurl / "vendors").html_escape
55
56 addn "<!DOCTYPE html>"
57 addn "<head>"
58 addn " <meta charset='utf-8'/>"
59 addn " <!--link rel='stylesheet' href='{css}/Nitdoc.UI.css' type='text/css'/-->"
60 addn " <link rel='stylesheet' href='{vendors}/bootstrap/css/bootstrap.min.css'/>"
61 addn " <link rel='stylesheet' href='{css}/nitdoc.bootstrap.css'/>"
62 addn " <link rel='stylesheet' href='{css}/nitdoc.css'/>"
63 addn " <link rel='stylesheet' href='{css}/Nitdoc.QuickSearch.css'/>"
64 addn " <link rel='stylesheet' href='{css}/Nitdoc.ModalBox.css'/>"
65 addn " <link rel='stylesheet' href='{css}/Nitdoc.GitHub.css'/>"
66 addn " <title>{title.html_escape}</title>"
67 addn "</head>"
68 add "<body"
69 for attr in body_attrs do add attr
70 addn ">"
71 end
72
73 # Renders the footer and content.
74 private fun render_content do
75 add root
76 if footer != null then
77 addn "<div class='well footer'>"
78 add footer.as(not null)
79 addn "</div>"
80 end
81 end
82
83 # Render JS scripts
84 private fun render_footer do
85 var vendors = (self.shareurl / "vendors").html_escape
86 var js = (self.shareurl / "js").html_escape
87
88 addn "<script src='{vendors}/jquery/jquery-1.11.1.min.js'></script>"
89 addn "<script src='{vendors}/jquery/jquery-ui-1.10.4.custom.min.js'></script>"
90 addn "<script src='{vendors}/bootstrap/js/bootstrap.min.js'></script>"
91 addn "<script data-main='{js}/nitdoc' src='{js}/lib/require.js'></script>"
92 for script in scripts do add script
93 addn """<script>
94 $(function () {
95 $("[data-toggle='tooltip']").tooltip();
96 $("[data-toggle='popover']").popover();
97 });
98 </script>"""
99 addn "</body>"
100 addn "</html>"
101 end
102
103 # Render the whole page
104 redef fun rendering do
105 render_head
106 addn "<div class='container-fluid'>"
107 addn " <div class='row'>"
108 add topmenu
109 addn " </div>"
110 addn " <div class='row' id='content'>"
111 var sidebar = self.sidebar
112 if sidebar != null then
113 addn "<div class='col col-xs-3 col-lg-2'>"
114 add sidebar
115 addn "</div>"
116 addn "<div class='col col-xs-9 col-lg-10' data-spy='scroll' data-target='.summary'>"
117 render_content
118 addn "</div>"
119 else
120 addn "<div class='col col-xs-12'>"
121 render_content
122 addn "</div>"
123 end
124 addn " </div>"
125 addn "</div>"
126 render_footer
127 end
128
129 # Render table of content for this page.
130 fun html_toc: UnorderedList do
131 var lst = new UnorderedList
132 lst.css_classes.add "nav"
133 for child in root.children do
134 child.render_toc_item(lst)
135 end
136 return lst
137 end
138 end
139
140 # Top menu bar template.
141 #
142 # FIXME should be a Bootstrap component template
143 # At this moment, the topmenu structure stills to specific to Nitdoc to use the
144 # generic component.
145 class DocTopMenu
146 super UnorderedList
147
148 # Brand link to display in first position of the top menu.
149 #
150 # This is where you want to put your logo.
151 var brand: nullable Writable is noinit, writable
152
153 # Active menu item.
154 #
155 # Depends on the current page, this allows to hilighted the current item.
156 #
157 # FIXME should be using Boostrap breadcrumbs component.
158 # This will still like this to avoid diff and be changed in further fixes
159 # when we will modify the output.
160 var active_item: nullable ListItem is noinit, writable
161
162 redef fun rendering do
163 addn "<nav id='topmenu' class='navbar navbar-default navbar-fixed-top' role='navigation'>"
164 addn " <div class='container-fluid'>"
165 addn " <div class='navbar-header'>"
166 add " <button type='button' class='navbar-toggle' "
167 addn " data-toggle='collapse' data-target='#topmenu-collapse'>"
168 addn " <span class='sr-only'>Toggle menu</span>"
169 addn " <span class='icon-bar'></span>"
170 addn " <span class='icon-bar'></span>"
171 addn " <span class='icon-bar'></span>"
172 addn " </button>"
173 if brand != null then
174 add "<span class='navbar-brand'>"
175 add brand.write_to_string
176 add "</span>"
177 end
178 addn " </div>"
179 addn " <div class='collapse navbar-collapse' id='topmenu-collapse'>"
180 addn " <ul class='nav navbar-nav'>"
181 for item in items do
182 if item == active_item then item.css_classes.add "active"
183 add item.write_to_string
184 end
185 addn " </ul>"
186 addn " </div>"
187 addn " </div>"
188 addn "</nav>"
189 end
190 end
191
192 # Nitdoc sidebar template.
193 class DocSideBar
194 super Template
195
196 # Sidebar contains `DocSideBox`.
197 var boxes = new Array[DocSideBox]
198
199 redef fun rendering do
200 if boxes.is_empty then return
201 addn "<div id='sidebar'>"
202 for box in boxes do add box
203 addn "</div>"
204 end
205 end
206
207 # Something that can be put in a DocSideBar.
208 class DocSideBox
209 super Template
210
211 # Box HTML id, used for Bootstrap collapsing feature.
212 #
213 # Use `html_title.to_cmangle` by default.
214 var id: String is lazy do return title.write_to_string.to_cmangle
215
216 # Title of the box to display.
217 var title: Writable
218
219 # Content to display in the box.
220 var content: Writable
221
222 # Is the box opened by default?
223 #
224 # Otherwise, the user will have to clic on the title to display the content.
225 #
226 # Default is `true`.
227 var is_open = true is writable
228
229 redef fun rendering do
230 var open = ""
231 if is_open then open = "in"
232 addn "<div class='panel'>"
233 addn " <div class='panel-heading'>"
234 add " <a data-toggle='collapse' data-parent='#sidebar'"
235 add " data-target='#box_{id}' href='#'>"
236 add title
237 addn " </a>"
238 addn " </div>"
239 addn " <div id='box_{id}' class='summary panel-body collapse {open}'>"
240 add content
241 addn " </div>"
242 addn "</div>"
243 end
244 end
245
246 redef class DocComposite
247 super Template
248
249 # HTML anchor id
250 var html_id: String is writable, lazy do return id
251
252 # Title to display if any.
253 #
254 # This title can be decorated with HTML.
255 var html_title: nullable Writable is writable, lazy do return title
256
257 # Subtitle to display if any.
258 var html_subtitle: nullable Writable is noinit, writable
259
260 # Render the element title and subtitle.
261 private fun render_title do
262 if html_title != null then
263 var header = new Header(hlvl, html_title.write_to_string)
264 header.css_classes.add "signature"
265 if hlvl == 2 then header.css_classes.add "well well-sm"
266 addn header
267 end
268 if html_subtitle != null then
269 addn "<div class='info subtitle'>"
270 addn html_subtitle.write_to_string
271 addn "</div>"
272 end
273 end
274
275 # Render the element body.
276 private fun render_body do
277 for child in children do addn child.write_to_string
278 end
279
280 redef fun rendering do
281 if is_hidden then return
282 render_title
283 render_body
284 end
285
286 # Level <hX> for HTML heading.
287 private fun hlvl: Int do return depth
288
289 # Is `self` not displayed in the page.
290 #
291 # By default, empty elements are hidden.
292 fun is_hidden: Bool do return is_empty
293
294 # A short, undecorated title that goes in the table of contents.
295 #
296 # By default, returns `html_title.to_s`, subclasses should redefine it.
297 var toc_title: String is lazy, writable do return html_title.to_s
298
299 # Is `self` hidden in the table of content?
300 var is_toc_hidden = false is writable
301
302 # Render this element in a table of contents.
303 private fun render_toc_item(lst: UnorderedList) do
304 if is_toc_hidden then return
305
306 var content = new Template
307 content.add new Link("#{html_id}", toc_title)
308
309 if not children.is_empty then
310 var sublst = new UnorderedList
311 sublst.css_classes.add "nav"
312 for child in children do
313 child.render_toc_item(sublst)
314 end
315 content.add sublst
316 end
317 lst.add_li new ListItem(content)
318 end
319
320 # ID used in HTML tab labels.
321 #
322 # We sanitize it for Boostrap JS panels that do not like ":" and "." in ids.
323 var html_tab_id: String is lazy do
324 var id = html_id.replace(":", "")
325 id = id.replace(".", "")
326 return "{id}-tab"
327 end
328 end
329
330 redef class DocRoot
331 redef fun rendering do
332 for child in children do addn child.write_to_string
333 end
334 end
335
336 redef class DocSection
337 super BSComponent
338
339 redef fun rendering do
340 if is_hidden then
341 addn "<a id=\"{html_id}\"></a>"
342 return
343 end
344 addn "<section{render_css_classes} id=\"{html_id}\">"
345 render_title
346 render_body
347 addn "</section>"
348 end
349 end
350
351 redef class DocArticle
352 super BSComponent
353
354 redef fun rendering do
355 if is_hidden then return
356 addn "<article{render_css_classes} id=\"{html_id}\">"
357 render_title
358 render_body
359 addn "</article>"
360 end
361 end
362
363 redef class TabbedGroup
364 redef fun render_body do
365 var tabs = new DocTabs("{html_id}.tabs", "")
366 for child in children do
367 if child.is_hidden then continue
368 tabs.add_panel new DocTabPanel(child.html_tab_id, child.toc_title, child)
369 end
370 addn tabs
371 end
372 end
373
374 redef class PanelGroup
375 redef var html_title = null
376 redef var toc_title is lazy do return title or else ""
377 redef var is_toc_hidden = true
378 end
379
380 redef class HomeArticle
381 redef var html_title = "Overview"
382
383 # HTML content to display on the home page.
384 #
385 # This attribute is set by the `doc_render` phase who knows the context.
386 var content: nullable String is noinit, writable
387
388 redef fun render_body do
389 var content = self.content
390 if content != null then add content
391 super
392 end
393 end
394
395 redef class IndexArticle
396 redef var html_title = "Index"
397 redef fun is_empty do
398 return mmodules.is_empty and mclasses.is_empty and mprops.is_empty
399 end
400
401 redef fun render_body do
402 addn "<div class='container-fluid'>"
403 addn " <div class='row'>"
404 render_list("Modules", mmodules)
405 render_list("Classes", mclasses)
406 render_list("Properties", mprops)
407 addn "</div>"
408 addn "</div>"
409 end
410
411 # Displays a list from the content of `mentities`.
412 private fun render_list(title: String, mentities: Array[MEntity]) do
413 if mentities.is_empty then return
414 addn "<div class='col-xs-4'>"
415 addn new Header(3, title)
416 var lst = new UnorderedList
417 for mentity in mentities do
418 if mentity isa MProperty then
419 var tpl = new Template
420 tpl.add mentity.intro.html_link
421 tpl.add " ("
422 tpl.add mentity.intro.mclassdef.mclass.html_link
423 tpl.add ")"
424 lst.add_li new ListItem(tpl)
425 else
426 lst.add_li new ListItem(mentity.html_link)
427 end
428 end
429 addn lst
430 addn "</div>"
431 end
432 end
433
434 redef class ProjectsSection
435 redef var html_title = "Projects"
436 end
437
438 redef class MEntityComposite
439 redef var html_title is lazy do return mentity.nitdoc_name
440 end
441
442 redef class MEntitySection
443 redef var html_title is lazy do return mentity.html_name
444 redef var html_subtitle is lazy do return mentity.html_declaration
445 end
446
447 redef class ConstructorsSection
448 redef var html_title = "Constructors"
449 redef var html_subtitle = null
450 redef fun is_toc_hidden do return is_empty
451 end
452
453 redef class ConcernSection
454 redef var html_title is lazy do return "in {mentity.nitdoc_name}"
455 redef fun is_toc_hidden do return is_empty
456 end
457
458 redef class ImportationListSection
459 redef var html_title is lazy do return "Dependencies"
460 end
461
462 redef class InheritanceListSection
463 redef var html_title is lazy do return "Inheritance"
464 end
465
466 redef class IntroArticle
467 redef var html_title = null
468 redef var is_hidden = false
469 redef var is_toc_hidden = true
470
471 # Link to source to display if any.
472 var html_source_link: nullable Writable is noinit, writable
473
474 redef fun render_body do
475 var tabs = new DocTabs("{html_id}.tabs", "")
476 var comment = mentity.html_comment
477 if comment != null then
478 tabs.add_panel new DocTabPanel("{html_tab_id}-comment", "Comment", comment)
479 end
480 for child in children do
481 if child.is_hidden then continue
482 tabs.add_panel new DocTabPanel(child.html_tab_id, child.toc_title, child)
483 end
484 var lnk = html_source_link
485 if lnk != null then
486 tabs.drop_list.items.add new ListItem(lnk)
487 end
488 addn tabs
489 end
490 end
491
492 redef class ConcernsArticle
493 redef var html_title = "Concerns"
494 redef fun is_hidden do return concerns.is_empty
495 redef fun render_body do add concerns.html_list
496 end
497
498 redef class DefinitionListArticle
499 redef var html_title is lazy do
500 var title = new Template
501 title.add mentity.html_icon
502 title.add mentity.html_link
503 return title
504 end
505
506 redef var html_subtitle is lazy do return mentity.html_namespace
507 redef var toc_title is lazy do return mentity.html_name
508 end
509
510 redef class DefinitionArticle
511 redef var html_title is lazy do return mentity.html_name
512 redef var html_subtitle is lazy do return mentity.html_declaration
513 redef var is_hidden = false
514
515 # Does `self` display only it's title and no body?
516 #
517 # FIXME diff hack
518 var is_no_body: Bool = false is writable
519
520 # Does `self` display only the short content as definition?
521 #
522 # FIXME diff hack
523 var is_short_comment: Bool = false is writable
524
525 # Link to source to display if any.
526 var html_source_link: nullable Writable is noinit, writable
527
528 redef fun render_body do
529 var tabs = new DocTabs("{html_id}.tabs", "")
530 if not is_no_body then
531 var comment
532 if is_short_comment then
533 comment = mentity.html_short_comment
534 else
535 comment = mentity.html_comment
536 end
537 if comment != null then
538 tabs.add_panel new DocTabPanel("{html_tab_id}-comment", "Comment", comment)
539 end
540 end
541 for child in children do
542 if child.is_hidden then continue
543 tabs.add_panel new DocTabPanel(child.html_tab_id, child.toc_title, child)
544 end
545 var lnk = html_source_link
546 if lnk != null then
547 tabs.drop_list.items.add new ListItem(lnk)
548 end
549 addn tabs
550 end
551 end
552
553 redef class HierarchyListArticle
554 redef fun is_empty do return mentities.is_empty
555 redef var is_toc_hidden = true
556
557 redef fun render_body do
558 var lst = new UnorderedList
559 lst.css_classes.add "list-unstyled list-definition"
560 for mentity in mentities do
561 lst.add_li mentity.html_list_item
562 end
563 addn lst
564 end
565 end
566
567 redef class IntrosRedefsSection
568 redef var toc_title do return "Intros / Redefs"
569 redef var html_title = null
570 redef var html_subtitle = null
571 redef var is_toc_hidden = true
572 end
573
574 redef class IntrosRedefsListArticle
575 redef fun is_hidden do return mentities.is_empty
576 redef var is_toc_hidden = true
577
578 redef fun render_body do
579 var lst = new UnorderedList
580 lst.css_classes.add "list-unstyled list-labeled"
581 for mentity in mentities do
582 lst.add_li mentity.html_list_item
583 end
584 add lst
585 end
586 end
587
588 redef class DefinitionLinArticle
589 redef var html_title is lazy do return "Linearization"
590 redef fun is_hidden do return mentities.is_empty
591 redef var is_toc_hidden = true
592
593 redef fun render_body do
594 var lst = new UnorderedList
595 lst.css_classes.add "list-unstyled list-labeled"
596 for mentity in mentities do
597 if not mentity isa MPropDef then continue # TODO handle all mentities
598 var tpl = new Template
599 tpl.add mentity.mclassdef.html_namespace
600 var comment = mentity.mclassdef.html_short_comment
601 if comment != null then
602 tpl.add ": "
603 tpl.add comment
604 end
605 var li = new ListItem(tpl)
606 li.css_classes.add "signature"
607 lst.add_li li
608 end
609 add lst
610 end
611 end
612
613 redef class GraphArticle
614 redef var html_title = null
615 redef var toc_title do return "Graph"
616 redef var is_hidden = false
617 redef var is_toc_hidden = true
618
619 # HTML map used to display link.
620 #
621 # This attribute is set by the `doc_render` phase who knows the context.
622 var map: String is noinit, writable
623
624 redef fun render_body do
625 addn "<div class=\"text-center\">"
626 addn " <img src='{graph_id}.png' usemap='#{graph_id}' style='margin:auto'"
627 addn " alt='{title or else ""}'/>"
628 add map
629 addn "</div>"
630 end
631 end