src/doc/api: add links to renderer code
[nit.git] / src / doc / html_templates / html_model.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 for Nit model MEntities.
16 module html_model
17
18 import doc_base
19 import html_components
20 import ordered_tree
21 import model_html
22
23 redef class MEntity
24 # URL of this entity’s Nitdoc page.
25 fun nitdoc_url: String is abstract
26
27 # Returns a Link to the mentity `html_url`.
28 #
29 # Example: `<a href="html_url" title="mdoc.short_comment">html_short_name</a>
30 redef var html_link is lazy do
31 var tpl = new Link(nitdoc_url, html_name)
32 var mdoc = mdoc_or_fallback
33 if mdoc != null then
34 tpl.title = mdoc.synopsis
35 end
36 return tpl
37 end
38
39 # Returns a Link to the mentity `nitdoc_id`.
40 #
41 # Example: `<a href="#nitdoc_id" title="mdoc.short_comment">html_short_name</a>
42 fun html_link_to_anchor: Link do
43 var tpl = new Link("#{nitdoc_id}", html_name)
44 var mdoc = mdoc_or_fallback
45 if mdoc != null then
46 tpl.title = mdoc.synopsis
47 end
48 return tpl
49 end
50
51 # A li element that can go in a `HTMLList`.
52 fun html_list_item: ListItem do
53 var tpl = new Template
54 tpl.add new DocHTMLLabel.with_classes(css_classes)
55 tpl.add html_link
56 var comment = html_synopsis
57 if comment != null then
58 tpl.add ": "
59 tpl.add comment
60 end
61 return new ListItem(tpl)
62 end
63 end
64
65 redef class MPackage
66 redef var nitdoc_id = name.to_cmangle is lazy
67
68 redef fun nitdoc_url do
69 var root = self.root
70 if root == null then return super
71 return root.nitdoc_url
72 end
73 end
74
75 redef class MGroup
76 redef var nitdoc_id is lazy do
77 var parent = self.parent
78 if parent != null then
79 return "{parent.nitdoc_id}__{name.to_cmangle}"
80 end
81 return name.to_cmangle
82 end
83
84 redef fun nitdoc_url do return "group_{nitdoc_id}.html"
85 end
86
87 redef class MModule
88 redef var nitdoc_id is lazy do
89 var mgroup = self.mgroup
90 if mgroup != null then
91 if mgroup.mmodules.length == 1 then
92 return "{mgroup.nitdoc_id}-"
93 else
94 return "{mgroup.nitdoc_id}__{name.to_cmangle}"
95 end
96 end
97 return name.to_cmangle
98 end
99
100 redef fun nitdoc_url do return "module_{nitdoc_id}.html"
101 end
102
103 redef class MClass
104 redef var nitdoc_id = "{intro_mmodule.nitdoc_id}__{name.to_cmangle}" is lazy
105 redef fun nitdoc_url do return "class_{nitdoc_id}.html"
106 end
107
108 redef class MClassDef
109 redef var nitdoc_id = "{mmodule.nitdoc_id}__{name.to_cmangle}" is lazy
110 redef fun nitdoc_url do return "{mclass.nitdoc_url}#{nitdoc_id}"
111 end
112
113 redef class MProperty
114 redef var nitdoc_id = "{intro_mclassdef.mclass.nitdoc_id}__{name.to_cmangle}" is lazy
115 redef fun nitdoc_url do return "property_{nitdoc_id}.html"
116 end
117
118 redef class MPropDef
119 redef var nitdoc_id = "{mclassdef.nitdoc_id}__{name.to_cmangle}" is lazy
120 redef fun nitdoc_url do return "{mproperty.nitdoc_url}#{nitdoc_id}"
121 end
122
123 redef class MAttributeDef
124
125 redef fun html_modifiers do
126 var res = super
127 res.add "var"
128 return res
129 end
130
131 redef fun html_short_signature do return new Template
132
133 redef fun html_signature do
134 var static_mtype = self.static_mtype
135 var tpl = new Template
136 if static_mtype != null then
137 tpl.add ": "
138 tpl.add static_mtype.html_signature
139 end
140 return tpl
141 end
142 end
143
144 redef class MParameterType
145 redef fun html_link do
146 return new Link("{mclass.nitdoc_url}#FT_{name.to_cmangle}", name, "formal type")
147 end
148 end
149
150 redef class MVirtualType
151 redef fun html_link do return mproperty.intro.html_link
152 end
153
154 redef class ConcernsTree
155 # Render `self` as a hierarchical UnorderedList.
156 fun html_list: UnorderedList do
157 var lst = new UnorderedList
158 lst.css_classes.add "list-unstyled list-definition"
159 for r in roots do
160 var li = r.html_concern_item
161 lst.add_li li
162 build_html_list(r, li)
163 end
164 return lst
165 end
166
167 # Build the html list recursively.
168 private fun build_html_list(e: MConcern, li: ListItem) do
169 if not sub.has_key(e) then return
170 var subs = sub[e]
171 var lst = new UnorderedList
172 lst.css_classes.add "list-unstyled list-definition"
173 for e2 in subs do
174 if e2 isa MGroup and e2.is_root then
175 build_html_list(e2, li)
176 else
177 var sli = e2.html_concern_item
178 lst.add_li sli
179 build_html_list(e2, sli)
180 end
181 end
182 var text = new Template
183 text.add li.text
184 if not lst.is_empty then text.add lst
185 li.text = text
186 end
187 end
188
189 redef class MConcern
190 # Return a li element for `self` that can be displayed in a concern list
191 private fun html_concern_item: ListItem do
192 var lnk = html_link
193 var tpl = new Template
194 tpl.add new Link("#{nitdoc_id}.concern", lnk.text, lnk.title)
195 var comment = html_synopsis
196 if comment != null then
197 tpl.add ": "
198 tpl.add comment
199 end
200 return new ListItem(tpl)
201 end
202 end
203
204 ################################################################################
205 # Additions to `model_ext`.
206
207 redef class MRawType
208 redef fun html_signature do
209 var tpl = new Template
210
211 for part in parts do
212 if part.target != null then
213 tpl.add part.target.as(not null).html_link
214 else
215 tpl.add part.text.html_escape
216 end
217 end
218 return tpl
219 end
220 end
221
222 redef class MInnerClass
223 redef fun nitdoc_url do return inner.nitdoc_url
224 redef fun html_signature do return inner.html_signature
225 end
226
227 redef class MInnerClassDef
228 redef fun nitdoc_url do return inner.nitdoc_url
229
230 redef fun html_link_to_anchor do return inner.html_link_to_anchor
231 redef fun html_link do return inner.html_link
232 redef fun html_signature do return inner.html_signature
233 end