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