Merge: Kill `model_utils`
[nit.git] / src / doc / doc_base.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 # Base entities shared by all the nitdoc code.
16 module doc_base
17
18 import toolcontext
19 import model_ext
20
21 # The model of a Nitdoc documentation.
22 #
23 # `DocModel` contains the list of the `DocPage` to be generated.
24 #
25 # The model is populated through `DocPhase` to be constructed.
26 # It is a placeholder to share data between each phase.
27 class DocModel
28
29 # `DocPage` composing the documentation associated to their ids.
30 #
31 # This is where `DocPhase` store and access pages to produce documentation.
32 #
33 # See `add_page`.
34 var pages: Map[String, DocPage] = new HashMap[String, DocPage]
35
36 # Nit `Model` from which we extract the documentation.
37 var model: Model is writable
38
39 # The entry point of the `model`.
40 var mainmodule: MModule is writable
41
42 # Add a `page` to this documentation.
43 fun add_page(page: DocPage) do
44 if pages.has_key(page.id) then
45 print "Warning: multiple page with the same id `{page.id}`"
46 end
47 pages[page.id] = page
48 end
49 end
50
51 # A documentation page abstraction.
52 #
53 # The page contains a link to the `root` of the `DocComposite` that compose the
54 # the page.
55 class DocPage
56
57 # Page uniq id.
58 #
59 # The `id` is used as name for the generated file corresponding to the page
60 # (if any).
61 # Because multiple pages can be generated in the same directory it should be
62 # uniq.
63 #
64 # The `id` can also be used to establish links between pages (HTML links,
65 # HTML anchors, vim links, etc.).
66 var id: String is writable
67
68 # Title of this page.
69 var title: String is writable
70
71 # Root element of the page.
72 #
73 # `DocPhase` access the structure of the page from the `DocRoot`.
74 var root = new DocRoot
75
76 redef fun to_s do return title
77
78 # Pretty prints the content of this page.
79 fun pretty_print: Writable do
80 var res = new Template
81 res.addn "page: {title}"
82 res.addn ""
83 root.pretty_print_in(res)
84 return res
85 end
86 end
87
88 # `DocPage` elements that can be nested in another.
89 #
90 # `DocComposite` is an abstraction for everything that go in a `DocPage` like
91 # sections, articles, images, lists, graphs...
92 #
93 # It provides base services for nesting mechanisms following the
94 # *Composite pattern*.
95 # The composed structure is a tree from a `DocRoot` that can be manipulated
96 # recursively.
97 abstract class DocComposite
98
99 # Parent element.
100 var parent: nullable DocComposite = null is writable
101
102 # Element uniq id.
103 #
104 # The `id` is used as name for the generated element (if any).
105 # Because multiple elements can be generated in the same container
106 # it should be uniq.
107 #
108 # The `id` can also be used to establish links between elements
109 # (HTML links, HTML anchors, vim links, etc.).
110 var id: String is writable
111
112 # Item title if any.
113 var title: nullable String
114
115 # Does `self` have a `parent`?
116 fun is_root: Bool do return parent == null
117
118 # Children elements contained in `self`.
119 #
120 # Children are ordered, this order can be changed by the `DocPhase`.
121 var children = new Array[DocComposite]
122
123 # Is `self` not displayed in the page.
124 #
125 # By default, empty elements are hidden.
126 fun is_hidden: Bool do return children.is_empty
127
128 # Title used in table of content if any.
129 var toc_title: nullable String is writable, lazy do return title
130
131 # Is `self` hidden in the table of content?
132 var is_toc_hidden: Bool is writable, lazy do
133 return toc_title == null or is_hidden
134 end
135
136 # Add a `child` to `self`.
137 #
138 # Shortcut for `children.add`.
139 fun add_child(child: DocComposite) do
140 child.parent = self
141 children.add child
142 end
143
144 # Depth of `self` in the composite tree.
145 fun depth: Int do
146 if parent == null then return 0
147 return parent.depth + 1
148 end
149
150 # Pretty prints this composite recursively.
151 fun pretty_print: Writable do
152 var res = new Template
153 pretty_print_in(res)
154 return res
155 end
156
157 # Appends the Pretty print of this composite in `res`.
158 private fun pretty_print_in(res: Template) do
159 res.add "#" * depth
160 res.addn " {id}"
161 for child in children do child.pretty_print_in(res)
162 end
163 end
164
165 # The `DocComposite` element that contains all the other.
166 #
167 # The root uses a specific subclass to provide different a different behavior
168 # than other `DocComposite` elements.
169 class DocRoot
170 noautoinit
171 super DocComposite
172
173 redef var id = "<root>"
174 redef var title = "<root>"
175
176 # No op for `RootSection`.
177 redef fun parent=(p) do end
178 end
179
180 # Base page elements.
181
182 # `DocSection` are used to break the documentation page into meaningfull parts.
183 #
184 # The content of the documentation summary is based on the section structure
185 # contained in the DocComposite tree.
186 class DocSection
187 super DocComposite
188 end
189
190 # `DocArticle` are pieces of documentation.
191 #
192 # They maintains the content (text, list, image...) of a documentation page.
193 class DocArticle
194 super DocComposite
195 end
196
197 # A DocPhase is a step in the production of a Nitdoc documentation.
198 #
199 # Phases work from a `DocModel`.
200 # Specific phases are used to populate, organize, enhance and render the content
201 # of the documentation pages.
202 #
203 # See `doc_phases` for available DocPhase.
204 class DocPhase
205
206 # Link to the ToolContext to access Nitdoc tool options.
207 var ctx: ToolContext
208
209 # `DocModel` used by this phase to work.
210 var doc: DocModel
211
212 # Starting point of a `DocPhase`.
213 #
214 # This is where the behavior of the phase is implemented.
215 # Phases can populate, edit or render the `doc` from here.
216 fun apply is abstract
217 end
218
219 redef class ToolContext
220
221 # Directory where the Nitdoc is rendered.
222 var opt_dir = new OptionString("output directory", "-d", "--dir")
223
224 # Shortcut for `opt_dir.value` with default "doc".
225 var output_dir: String is lazy do return opt_dir.value or else "doc"
226
227 redef init do
228 super
229 option_context.add_option(opt_dir)
230 end
231 end
232
233 # Catalog properties by kind.
234 class PropertiesByKind
235 # The virtual types.
236 var virtual_types = new PropertyGroup[MVirtualTypeProp]("Virtual types")
237
238 # The constructors.
239 var constructors = new PropertyGroup[MMethod]("Contructors")
240
241 # The attributes.
242 var attributes = new PropertyGroup[MAttribute]("Attributes")
243
244 # The methods.
245 var methods = new PropertyGroup[MMethod]("Methods")
246
247 # The inner classes.
248 var inner_classes = new PropertyGroup[MInnerClass]("Inner classes")
249
250 # All the groups.
251 #
252 # Sorted in the order they are displayed to the user.
253 var groups: SequenceRead[PropertyGroup[MProperty]] = [
254 virtual_types,
255 constructors,
256 attributes,
257 methods,
258 inner_classes: PropertyGroup[MProperty]]
259
260 # Add each the specified property to the appropriate list.
261 init with_elements(properties: Collection[MProperty]) do add_all(properties)
262
263 # Add the specified property to the appropriate list.
264 fun add(property: MProperty) do
265 if property isa MMethod then
266 if property.is_init then
267 constructors.add property
268 else
269 methods.add property
270 end
271 else if property isa MVirtualTypeProp then
272 virtual_types.add property
273 else if property isa MAttribute then
274 attributes.add property
275 else if property isa MInnerClass then
276 inner_classes.add property
277 else
278 abort
279 end
280 end
281
282 # Add each the specified property to the appropriate list.
283 fun add_all(properties: Collection[MProperty]) do
284 for p in properties do add(p)
285 end
286
287 # Sort each group with the specified comparator.
288 fun sort_groups(comparator: Comparator) do
289 for g in groups do comparator.sort(g)
290 end
291 end
292
293 # An ordered list of properties of the same kind.
294 class PropertyGroup[E: MProperty]
295 super Array[E]
296
297 # The title of the group, as displayed to the user.
298 var title: String
299 end
300
301 redef class MEntity
302 # ID used as a unique ID and in file names.
303 #
304 # **Must** match the following (POSIX ERE) regular expression:
305 #
306 # ~~~POSIX ERE
307 # ^[A-Za-z_][A-Za-z0-9._-]*$
308 # ~~~
309 #
310 # That way, the ID is always a valid URI component and a valid XML name.
311 fun nitdoc_id: String do return full_name.to_cmangle
312
313 # Name displayed in console for debug and tests.
314 fun nitdoc_name: String do return name.html_escape
315 end
316
317 redef class MModule
318
319 # Avoid id conflict with group
320 redef fun nitdoc_id do
321 if mgroup == null then return super
322 return "{mgroup.full_name}::{full_name}".to_cmangle
323 end
324 end
325
326 redef class MClassDef
327 redef fun nitdoc_name do return mclass.nitdoc_name
328 end
329
330 redef class MPropDef
331 redef fun nitdoc_name do return mproperty.nitdoc_name
332 end