Merge: Rosettacode grayscale implementation
[nit.git] / src / doc / html_templates / html_components.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 html_components
18
19 import doc_base
20 import html::bootstrap
21 import json::static
22
23 # A label with a text content.
24 class DocHTMLLabel
25 super BSLabel
26
27 redef init do
28 css_classes.clear
29 css_classes.add "label"
30 end
31
32 # Init this label from css classes.
33 init with_classes(classes: Array[String]) do
34 init("label", "")
35 css_classes.add_all classes
36 end
37 end
38
39 # A HTML tag attribute
40 # `<tag attr="value">`
41 #
42 # ~~~nit
43 # var attr: TagAttribute
44 #
45 # attr = new TagAttribute("foo", null)
46 # assert attr.write_to_string == " foo=\"\""
47 #
48 # attr = new TagAttribute("foo", "bar<>")
49 # assert attr.write_to_string == " foo=\"bar&lt;&gt;\""
50 # ~~~
51 class TagAttribute
52 super Template
53
54 var name: String
55 var value: nullable String
56
57 redef fun rendering do
58 var value = self.value
59 if value == null then
60 # SEE: http://www.w3.org/TR/html5/infrastructure.html#boolean-attributes
61 add " {name.html_escape}=\"\""
62 else
63 add " {name.html_escape}=\"{value.html_escape}\""
64 end
65 end
66 end
67
68 # Javacript template that can be added into a DocPage.
69 class TplScript
70 super Template
71
72 # HTML attributes to add in this tag.
73 var attrs = new Array[TagAttribute]
74
75 # Text content of this script tag.
76 var content: nullable Writable = null is writable
77
78 init do
79 attrs.add(new TagAttribute("type", "text/javascript"))
80 end
81
82 # Render the content of this script.
83 protected fun render_content do
84 if content != null then add content.as(not null)
85 end
86
87 redef fun rendering do
88 add "<script"
89 for attr in attrs do add attr
90 addn ">"
91 render_content
92 addn "</script>"
93 end
94 end
95
96 # JS script for Piwik Tracker
97 class TplPiwikScript
98 super TplScript
99
100 # Piwik URL to use for this tracker.
101 var tracker_url: String
102
103 # Site ID used on Piwik system.
104 var site_id: String
105
106 redef fun render_content do
107 var site_id = self.site_id.to_json
108 var tracker_url = self.tracker_url.trim
109 if tracker_url.chars.last != '/' then tracker_url += "/"
110 tracker_url = "://{tracker_url}".to_json
111
112 addn "<!-- Piwik -->"
113 addn "var _paq = _paq || [];"
114 addn " _paq.push([\"trackPageView\"]);"
115 addn " _paq.push([\"enableLinkTracking\"]);"
116 addn "(function() \{"
117 addn " var u=((\"https:\" == document.location.protocol) ? \"https\" : \"http\") + {tracker_url};"
118 addn " _paq.push([\"setTrackerUrl\", u+\"piwik.php\"]);"
119 addn " _paq.push([\"setSiteId\", {site_id}]);"
120 addn " var d=document, g=d.createElement(\"script\"), s=d.getElementsByTagName(\"script\")[0]; g.type=\"text/javascript\";"
121 addn " g.defer=true; g.async=true; g.src=u+\"piwik.js\"; s.parentNode.insertBefore(g,s);"
122 addn "\})();"
123 end
124 end