nitdoc: Escape attributes.
[nit.git] / src / doc / doc_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 # HTML templates used by Nitdoc to generate API documentation
16 # Pages are assembled using `Template`
17 module doc_templates
18
19 import template
20 import json::static
21
22 # A documentation page
23 class TplPage
24 super Template
25
26 # Page title in HTML header
27 var title: String is writable, noinit
28
29 # Page url
30 var url: String is writable, noinit
31
32 # Directory where css, js and other assets can be found
33 var shareurl: String is writable, noinit
34
35 # Attributes of the body tag element
36 var body_attrs = new Array[TagAttribute]
37
38 # Top menu template if any
39 var topmenu: TplTopMenu is writable, noinit
40
41 # Sidebar template if any
42 var sidebar: nullable TplSidebar = null is writable
43
44 # Content of the page in form a TplSection
45 var sections = new Array[TplSection]
46
47 # Footer content if any
48 var footer: nullable Streamable = null is writable
49
50 # JS scripts to append at the end of the body
51 var scripts = new Array[TplScript]
52
53 # Add a section to this page
54 fun add_section(section: TplSection) do
55 sections.add section
56 end
57
58 # Render the html header
59 private fun render_head do
60 var css = (self.shareurl / "css").html_escape
61 var vendors = (self.shareurl / "vendors").html_escape
62
63 add "<!DOCTYPE html>"
64 add "<head>"
65 add " <meta charset='utf-8'/>"
66 add " <!--link rel='stylesheet' href='{css}/Nitdoc.UI.css' type='text/css'/-->"
67 add " <link rel='stylesheet' href='{vendors}/bootstrap/css/bootstrap.min.css'/>"
68 add " <link rel='stylesheet' href='{css}/nitdoc.bootstrap.css'/>"
69 add " <link rel='stylesheet' href='{css}/nitdoc.css'/>"
70 add " <link rel='stylesheet' href='{css}/Nitdoc.QuickSearch.css'/>"
71 add " <link rel='stylesheet' href='{css}/Nitdoc.ModalBox.css'/>"
72 add " <link rel='stylesheet' href='{css}/Nitdoc.GitHub.css'/>"
73 add " <title>{title}</title>"
74 add "</head>"
75 add "<body"
76 for attr in body_attrs do add attr
77 add ">"
78 end
79
80 # Render the topmenu template
81 private fun render_topmenu do
82 add " <div class='row'>"
83 add topmenu
84 add " </div>"
85 end
86
87 # Render the sidebar
88 # Sidebar is automatically populated with a summary of all sections
89 fun render_sidebar do
90 if sidebar == null then return
91 var summary = new TplSummary.with_order(0)
92 for section in sections do
93 section.render_summary summary
94 end
95 sidebar.boxes.add summary
96 add sidebar.as(not null)
97 end
98 # Render the footer and content
99 private fun render_content do
100 for section in sections do add section
101 if footer != null then
102 add "<div class='well footer'>"
103 add footer.as(not null)
104 add "</div>"
105 end
106 end
107
108 # Render JS scripts
109 private fun render_footer do
110 var vendors = (self.shareurl / "vendors").html_escape
111 var js = (self.shareurl / "js").html_escape
112
113 add "<script src='{vendors}/jquery/jquery-1.11.1.min.js'></script>"
114 add "<script src='{vendors}/jquery/jquery-ui-1.10.4.custom.min.js'></script>"
115 add "<script src='{vendors}/bootstrap/js/bootstrap.min.js'></script>"
116 add "<script data-main='{js}/nitdoc' src='{js}/lib/require.js'></script>"
117 for script in scripts do add script
118 add """<script>
119 $(function () {
120 $("[data-toggle='tooltip']").tooltip();
121 $("[data-toggle='popover']").popover();
122 });
123 </script>"""
124 add "</body>"
125 add "</html>"
126 end
127
128 # Render the whole page
129 redef fun rendering do
130 render_head
131 add "<div class='container-fluid'>"
132 render_topmenu
133 add " <div class='row' id='content'>"
134 if sidebar != null then
135 add "<div class='col col-xs-3 col-lg-2'>"
136 render_sidebar
137 add "</div>"
138 add "<div class='col col-xs-9 col-lg-10' data-spy='scroll' data-target='.summary'>"
139 render_content
140 add "</div>"
141 else
142 add "<div class='col col-xs-12'>"
143 render_content
144 add "</div>"
145 end
146 add " </div>"
147 add "</div>"
148 render_footer
149 end
150 end
151
152 #########################
153 # general layout elements
154 #########################
155
156 # Top menu bar template
157 class TplTopMenu
158 super Template
159
160 # Brand link to display in first position of the top menu
161 private var brand: nullable Streamable = null is writable
162 # Elements of the topmenu
163 private var elts = new Array[Streamable]
164
165 # The page url where the top menu is displayed.
166 #
167 # Used to select the active link.
168 private var current_url: String
169
170 # Add a new link to the menu.
171 fun add_link(content: TplLink) do
172 var is_active = content.href == current_url
173 add_item(content, is_active)
174 end
175
176 # Add a content between `<li>` tags
177 fun add_item(content: Streamable, is_active: Bool) do
178 var tpl = new Template
179 tpl.add "<li"
180 if is_active then
181 tpl.add " class='active'"
182 end
183 tpl.add ">"
184 tpl.add content
185 tpl.add "</li>"
186 add_raw(tpl)
187 end
188
189 # Add a raw content to the menu
190 fun add_raw(content: Streamable) do
191 elts.add content
192 end
193
194 redef fun rendering do
195 if brand == null and elts.is_empty then return
196 add "<nav id='topmenu' class='navbar navbar-default navbar-fixed-top' role='navigation'>"
197 add " <div class='container-fluid'>"
198 add " <div class='navbar-header'>"
199 add " <button type='button' class='navbar-toggle' "
200 add " data-toggle='collapse' data-target='#topmenu-collapse'>"
201 add " <span class='sr-only'>Toggle menu</span>"
202 add " <span class='icon-bar'></span>"
203 add " <span class='icon-bar'></span>"
204 add " <span class='icon-bar'></span>"
205 add " </button>"
206 if brand != null then add brand.as(not null)
207 add " </div>"
208 add " <div class='collapse navbar-collapse' id='topmenu-collapse'>"
209 if not elts.is_empty then
210 add "<ul class='nav navbar-nav'>"
211 for elt in elts do add elt
212 add "</ul>"
213 end
214 add " </div>"
215 add " </div>"
216 add "</nav>"
217 end
218 end
219
220 # A sidebar template
221 class TplSidebar
222 super Template
223
224 # Sidebar contains sidebar element templates called boxes
225 var boxes = new Array[TplSidebarElt]
226
227 # Sort boxes by order priority
228 private fun order_boxes do
229 var sorter = new OrderComparator
230 sorter.sort(boxes)
231 end
232
233 redef fun rendering do
234 if boxes.is_empty then return
235 order_boxes
236 add "<div id='sidebar'>"
237 for box in boxes do add box
238 add "</div>"
239 end
240 end
241
242 # Comparator used to sort boxes by order
243 private class OrderComparator
244 super Comparator
245
246 redef type COMPARED: TplSidebarElt
247
248 redef fun compare(a, b) do
249 if a.order < b.order then return -1
250 if a.order > b.order then return 1
251 return 0
252 end
253 end
254
255 # Something that can be put in the sidebar
256 class TplSidebarElt
257 super Template
258
259 # Order of the box in the sidebar
260 var order: Int = 1
261
262 init with_order(order: Int) do self.order = order
263 end
264
265 # Agenericbox that can be added to sidebar
266 class TplSideBox
267 super TplSidebarElt
268
269 # Title of the box to display
270 # Title is also a placeholder for the collapse link
271 var title: String
272
273 # Box HTML id
274 # equals to `title.to_cmangle` by default
275 # Used for collapsing
276 var id: String is noinit
277
278 # Content to display in the box
279 # box will not be rendered if the content is null
280 var content: nullable Streamable = null is writable
281
282 # Is the box opened by default
283 # otherwise, the user will have to clic on the title to display the content
284 var is_open = false is writable
285
286 init do
287 self.id = title.to_cmangle
288 end
289
290 init with_content(title: String, content: Streamable) do
291 init(title)
292 self.content = content
293 end
294
295 redef fun rendering do
296 if content == null then return
297 var open = ""
298 if is_open then open = "in"
299 add "<div class='panel'>"
300 add " <div class='panel-heading'>"
301 add " <a data-toggle='collapse' data-parent='#sidebar' data-target='#box_{id}' href='#'>"
302 add title
303 add " </a>"
304 add " </div>"
305 add " <div id='box_{id}' class='panel-body collapse {open}'>"
306 add content.as(not null)
307 add " </div>"
308 add "</div>"
309 end
310 end
311
312 # Something that can go on a summary template
313 class TplSummaryElt
314 super Template
315
316 # Add an element to the summary
317 fun add_child(child: TplSummaryElt) is abstract
318 end
319
320 # A summary that can go on the sidebar
321 # If the page contains a sidebar, the summary is automatically placed
322 # on top of the sidebarauto-generated
323 # summary contains anchors to all sections displayed in the page
324 class TplSummary
325 super TplSidebarElt
326 super TplSummaryElt
327
328 # Summary elements to display
329 var children = new Array[TplSummaryElt]
330
331 redef fun add_child(child) do children.add child
332
333 redef fun rendering do
334 if children.is_empty then return
335 add "<div class='panel'>"
336 add " <div class='panel-heading'>"
337 add " <a data-toggle='collapse' data-parent='#sidebar' data-target='#box-sum' href='#'>"
338 add "Summary"
339 add " </a>"
340 add " </div>"
341 add " <div id='box-sum' class='summary collapse in'>"
342 add " <ul class='nav'>"
343 for entry in children do add entry
344 add " </ul>"
345 add " </div>"
346 add "</div>"
347 end
348 end
349
350 # A summary entry
351 class TplSummaryEntry
352 super TplSummaryElt
353
354 # Text to display
355 var text: Streamable
356
357 # Children of this entry
358 # Will be displayed as a tree
359 var children = new Array[TplSummaryElt]
360
361 redef fun add_child(child) do children.add child
362
363 redef fun rendering do
364 add "<li>"
365 add text
366 if not children.is_empty then
367 add "<ul class='nav'>"
368 for entry in children do add entry
369 add "</ul>"
370 end
371 add "</li>"
372 end
373 end
374
375 # Something that can go in a section
376 # Sections are automatically collected to populate the menu
377 class TplSectionElt
378 super Template
379
380 # HTML anchor id
381 var id: String
382
383 # Title to display if any
384 # if both `title` and `summary_title` are null then
385 # the section will not appear in the summary
386 var title: nullable Streamable = null is writable
387
388 # Subtitle to display if any
389 var subtitle: nullable Streamable = null is writable
390
391 # Title that appear in the summary
392 # if null use `title` instead
393 var summary_title: nullable String = null is writable
394
395 # CSS classes to apply on the section element
396 var css_classes = new Array[String]
397
398 # CSS classes to apply on the title heading element
399 var title_classes = new Array[String]
400
401 # Parent article/section if any
402 var parent: nullable TplSectionElt = null
403
404 init with_title(id: String, title: Streamable) do
405 init(id)
406 self.title = title
407 end
408
409 # Level <hX> for HTML heading
410 protected fun hlvl: Int do
411 if parent == null then return 1
412 return parent.hlvl + 1
413 end
414
415 # Elements contained by this section
416 var children = new Array[TplSectionElt]
417
418 # Add an element in this section
419 fun add_child(child: TplSectionElt) do
420 child.parent = self
421 children.add child
422 end
423
424 # Is the section empty (no content at all)
425 fun is_empty: Bool do return children.is_empty
426
427 # Render this section in the summary
428 fun render_summary(parent: TplSummaryElt) do
429 if is_empty then return
430 var title = summary_title
431 if title == null and self.title != null then title = self.title.write_to_string
432 if title == null then return
433 var lnk = new TplLink("#{id}", title)
434 var entry = new TplSummaryEntry(lnk)
435 for child in children do
436 child.render_summary(entry)
437 end
438 parent.add_child entry
439 end
440 end
441
442 # A HTML <section> element
443 class TplSection
444 super TplSectionElt
445
446 redef fun rendering do
447 add "<section id='{id}' class='{css_classes.join(" ")}'>"
448 if title != null then
449 var lvl = hlvl
450 if lvl == 2 then title_classes.add "well well-sm"
451 add "<h{lvl} class='{title_classes.join(" ")}'>"
452 add title.as(not null)
453 add "</h{lvl}>"
454 end
455 if subtitle != null then
456 add "<div class='info subtitle'>"
457 add subtitle.as(not null)
458 add "</div>"
459 end
460 for child in children do
461 add child
462 end
463 add "</section>"
464 end
465 end
466
467 # A page article that can go in a section
468 class TplArticle
469 super TplSectionElt
470
471 # Content for this article
472 var content: nullable Streamable = null is writable
473 var source_link: nullable Streamable = null is writable
474
475 init with_content(id: String, title: Streamable, content: Streamable) do
476 with_title(id, title)
477 self.content = content
478 end
479
480 redef fun render_summary(parent) do
481 if is_empty then return
482 var title = summary_title
483 if title == null and self.title != null then title = self.title.write_to_string
484 if title == null then return
485 var lnk = new TplLink("#{id}", title)
486 parent.add_child new TplSummaryEntry(lnk)
487 end
488
489 redef fun rendering do
490 if is_empty then return
491 add "<article id='{id}' class='{css_classes.join(" ")}'>"
492 if source_link != null then
493 add "<div class='source-link'>"
494 add source_link.as(not null)
495 add "</div>"
496 end
497 if title != null then
498 var lvl = hlvl
499 if lvl == 2 then title_classes.add "well well-sm"
500 add "<h{lvl} class='{title_classes.join(" ")}'>"
501 add title.as(not null)
502 add "</h{lvl}>"
503 end
504 if subtitle != null then
505 add "<div class='info subtitle'>"
506 add subtitle.as(not null)
507 add "</div>"
508 end
509 if content != null then
510 add content.as(not null)
511 end
512 for child in children do
513 add child
514 end
515 add """</article>"""
516 end
517
518 redef fun is_empty: Bool do
519 return title == null and subtitle == null and content == null and children.is_empty
520 end
521 end
522
523 # A module / class / prop definition
524 class TplDefinition
525 super Template
526
527 # Comment to display
528 var comment: nullable Streamable = null is writable
529
530 # Namespace for this definition
531 var namespace: nullable Streamable = null is writable
532
533 # Location link to display
534 var location: nullable Streamable = null is writable
535
536 private fun render_info do
537 add "<div class='info text-right'>"
538 if namespace != null then
539 if comment == null then
540 add "<span class=\"noComment\">no comment for </span>"
541 end
542 add namespace.as(not null)
543 end
544 if location != null then
545 add " "
546 add location.as(not null)
547 end
548 add "</div>"
549 end
550
551 private fun render_comment do
552 if comment != null then add comment.as(not null)
553 end
554
555 redef fun rendering do
556 add "<div class='definition'>"
557 render_comment
558 render_info
559 add "</div>"
560 end
561 end
562
563 # Class definition
564 class TplClassDefinition
565 super TplDefinition
566
567 var intros = new Array[TplListElt]
568 var redefs = new Array[TplListElt]
569
570 init do end
571
572 redef fun rendering do
573 add "<div class='definition'>"
574 render_comment
575 render_info
576 render_list("Introduces", intros)
577 render_list("Redefines", redefs)
578 add "</div>"
579 end
580
581 private fun render_list(name: String, elts: Array[TplListElt]) do
582 if elts.is_empty then return
583 add "<h5>{name.html_escape}</h5>"
584 add "<ul class='list-unstyled list-definition'>"
585 for elt in elts do add elt
586 add "</ul>"
587 end
588 end
589
590 # Layout for Search page
591 class TplSearchPage
592 super TplSectionElt
593
594 var modules = new Array[Streamable]
595 var classes = new Array[Streamable]
596 var props = new Array[Streamable]
597
598 redef fun rendering do
599 var title = self.title
600 if title != null then add "<h1>{title.to_s.html_escape}</h1>"
601 add "<div class='container-fluid'>"
602 add " <div class='row'>"
603 if not modules.is_empty then
604 add "<div class='col-xs-4'>"
605 add "<h3>Modules</h3>"
606 add "<ul>"
607 for m in modules do
608 add "<li>"
609 add m
610 add "</li>"
611 end
612 add "</ul>"
613 add "</div>"
614 end
615 if not classes.is_empty then
616 add "<div class='col-xs-4'>"
617 add "<h3>Classes</h3>"
618 add "<ul>"
619 for c in classes do
620 add "<li>"
621 add c
622 add "</li>"
623 end
624 add "</ul>"
625 add "</div>"
626 end
627 if not props.is_empty then
628 add "<div class='col-xs-4'>"
629 add "<h3>Properties</h3>"
630 add "<ul>"
631 for p in props do
632 add "<li>"
633 add p
634 add "</li>"
635 end
636 add "</ul>"
637 add "</div>"
638 end
639 add " </div>"
640 add "</div>"
641 end
642 end
643
644 #####################
645 # Basiv HTML elements
646 #####################
647
648 # A html link <a>
649 class TplLink
650 super Template
651
652 # Link href
653 var href: String is writable
654
655 # Text to display in the link
656 var text: Streamable is writable
657
658 # Optional title
659 var title: nullable String = null is writable
660
661 init with_title(href, text, title: String) do
662 init(href, text)
663 self.title = title
664 end
665
666 redef fun rendering do
667 add "<a href=\""
668 add href
669 add "\""
670 if title != null then
671 add " title=\""
672 add title.as(not null)
673 add "\""
674 end
675 add ">"
676 add text
677 add "</a>"
678 end
679 end
680
681 # A <ul> list
682 class TplList
683 super TplListElt
684
685 # Elements contained in this list
686 # can be <li> or <ul> elements
687 var elts = new Array[TplListElt]
688
689 # CSS classes of the <ul> element
690 var css_classes = new Array[String]
691
692 # Add content wrapped in a <li> element
693 fun add_li(item: TplListItem) do elts.add item
694
695 init do end
696
697 init with_classes(classes: Array[String]) do self.css_classes = classes
698
699 fun is_empty: Bool do return elts.is_empty
700
701 redef fun rendering do
702 if elts.is_empty then return
703 add "<ul class='{css_classes.join(" ")}'>"
704 for elt in elts do add elt
705 add "</ul>"
706 end
707 end
708
709 # Something that can be added to a TplList
710 class TplListElt
711 super Template
712 end
713
714 # A list item <li>
715 class TplListItem
716 super TplListElt
717
718 # Content of the list item
719 var content = new Template
720
721 # CSS classes of the <li> element
722 var css_classes = new Array[String]
723
724 init do end
725
726 init with_content(content: Streamable) do append(content)
727
728 init with_classes(content: Streamable, classes: Array[String]) do
729 with_content(content)
730 css_classes = classes
731 end
732
733 # Append `content` to the item
734 # similar to `self.content.add`
735 fun append(content: Streamable) do self.content.add content
736
737 redef fun rendering do
738 add "<li class='{css_classes.join(" ")}'>"
739 add content
740 add "</li>"
741 end
742 end
743
744 # A Bootstrap tab component that contains `TplTabPanel`.
745 class TplTab
746 super Template
747
748 # Panels contained in the tab.
749 var panels = new Array[TplTabPanel]
750
751 # Add a new panel.
752 fun add_panel(panel: TplTabPanel) do panels.add panel
753
754 # CSS classes of the tab component.
755 var css_classes = new Array[String]
756
757 redef fun rendering do
758 add "<div class='tab-content'>"
759 for panel in panels do add panel
760 add "</div>"
761 end
762 end
763
764 # A panel that goes in a `TplTab`.
765 class TplTabPanel
766 super Template
767
768 # CSS classes of the pane element.
769 var css_classes = new Array[String]
770
771 # The panel id.
772 #
773 # Used to show/hide panel.
774 var id: String is noinit
775
776 # The panel name.
777 #
778 # Displayed in the tab header or in the pointing link.
779 var name: Streamable
780
781 # Is the panel visible by default?
782 var is_active = false is writable
783
784 # Body of the panel
785 var content: nullable Streamable = null is writable
786
787 # Get a link pointing to this panel.
788 fun tpl_link_to: Streamable do
789 var lnk = new Template
790 lnk.add "<a data-target='#{id}' data-toggle='pill'>"
791 lnk.add name
792 lnk.add "</a>"
793 return lnk
794 end
795
796 redef fun rendering do
797 add "<div class='tab-pane {css_classes.join(" ")}"
798 if is_active then add "active"
799 add "' id='{id}'>"
800 if content != null then add content.as(not null)
801 add "</div>"
802 end
803 end
804
805 # A label with a text content
806 class TplLabel
807 super Template
808
809 # Content of the label if any
810 var content: nullable Streamable = null is writable
811
812 # CSS classes of the <span> element
813 var css_classes = new Array[String]
814
815 init with_content(content: Streamable) do self.content = content
816 init with_classes(classes: Array[String]) do self.css_classes = classes
817
818 redef fun rendering do
819 add "<span class='label {css_classes.join(" ")}'>"
820 if content != null then add content.as(not null)
821 add "</span>"
822 end
823 end
824
825 # A label with an icon
826 class TplIcon
827 super TplLabel
828
829 # Bootsrap icon name
830 # see: http://getbootstrap.com/components/#glyphicons
831 var icon: String
832
833 init with_icon(icon: String) do self.icon = icon
834
835 redef fun rendering do
836 add "<span class='glyphicon glyphicon-{icon} {css_classes.join(" ")}'>"
837 if content != null then add content.as(not null)
838 add "</span>"
839 end
840 end
841
842 # A HTML tag attribute
843 # `<tag attr="value">`
844 #
845 # ~~~nit
846 # var attr: TagAttribute
847 #
848 # attr = new TagAttribute("foo", null)
849 # assert attr.write_to_string == " foo=\"\""
850 #
851 # attr = new TagAttribute("foo", "bar<>")
852 # assert attr.write_to_string == " foo=\"bar&lt;&gt;\""
853 # ~~~
854 class TagAttribute
855 super Template
856
857 var name: String
858 var value: nullable String
859
860 redef fun rendering do
861 var value = self.value
862 if value == null then
863 # SEE: http://www.w3.org/TR/html5/infrastructure.html#boolean-attributes
864 add " {name.html_escape}=\"\""
865 else
866 add " {name.html_escape}=\"{value.html_escape}\""
867 end
868 end
869 end
870
871 # Javacript template
872 class TplScript
873 super Template
874
875 var attrs = new Array[TagAttribute]
876 var content: nullable Streamable = null is writable
877
878 init do
879 attrs.add(new TagAttribute("type", "text/javascript"))
880 end
881
882 protected fun render_content do
883 if content != null then add content.as(not null)
884 end
885
886 redef fun rendering do
887 add "<script"
888 for attr in attrs do add attr
889 add ">"
890 render_content
891 add "</script>"
892 end
893 end
894
895 # JS script for Piwik Tracker
896 class TplPiwikScript
897 super TplScript
898
899 var tracker_url: String
900 var site_id: String
901
902 redef fun render_content do
903 var site_id = self.site_id.to_json
904 var tracker_url = self.tracker_url.trim
905 if tracker_url.chars.last != '/' then tracker_url += "/"
906 tracker_url = "://{tracker_url}".to_json
907
908 add "<!-- Piwik -->"
909 add "var _paq = _paq || [];"
910 add " _paq.push([\"trackPageView\"]);"
911 add " _paq.push([\"enableLinkTracking\"]);"
912 add "(function() \{"
913 add " var u=((\"https:\" == document.location.protocol) ? \"https\" : \"http\") + {tracker_url};"
914 add " _paq.push([\"setTrackerUrl\", u+\"piwik.php\"]);"
915 add " _paq.push([\"setSiteId\", {site_id}]);"
916 add " var d=document, g=d.createElement(\"script\"), s=d.getElementsByTagName(\"script\")[0]; g.type=\"text/javascript\";"
917 add " g.defer=true; g.async=true; g.src=u+\"piwik.js\"; s.parentNode.insertBefore(g,s);"
918 add "\})();"
919 end
920 end
921