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