1 # This file is part of NIT ( http://www.nitlanguage.org ).
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
7 # http://www.apache.org/licenses/LICENSE-2.0
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.
15 # Base entities shared by all the nitdoc code.
20 import model
::model_views
22 # The model of a Nitdoc documentation.
24 # `DocModel` contains the list of the `DocPage` to be generated.
26 # The model is populated through `DocPhase` to be constructed.
27 # It is a placeholder to share data between each phase.
31 # `DocPage` composing the documentation associated to their ids.
33 # This is where `DocPhase` store and access pages to produce documentation.
36 var pages
: Map[String, DocPage] = new HashMap[String, DocPage]
38 # The entry point of the `model`.
39 var mainmodule
: MModule is writable
41 # Add a `page` to this documentation.
42 fun add_page
(page
: DocPage) do
43 if pages
.has_key
(page
.id
) then
44 print
"Warning: multiple page with the same id `{page.id}`"
50 # A documentation page abstraction.
52 # The page contains a link to the `root` of the `DocComposite` that compose the
58 # The `id` is used as name for the generated file corresponding to the page
60 # Because multiple pages can be generated in the same directory it should be
63 # The `id` can also be used to establish links between pages (HTML links,
64 # HTML anchors, vim links, etc.).
65 var id
: String is writable
68 var title
: String is writable
70 # Root element of the page.
72 # `DocPhase` access the structure of the page from the `DocRoot`.
73 var root
= new DocRoot
75 redef fun to_s
do return title
77 # Pretty prints the content of this page.
78 fun pretty_print
: Writable do
79 var res
= new Template
80 res
.addn
"{class_name} {title}"
81 for child
in root
.children
do
82 child
.pretty_print_in
(res
)
88 # `DocPage` elements that can be nested in another.
90 # `DocComposite` is an abstraction for everything that go in a `DocPage` like
91 # sections, articles, images, lists, graphs...
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
97 abstract class DocComposite
100 var parent
: nullable DocComposite = null is writable
104 # The `id` is used as name for the generated element (if any).
105 # Because multiple elements can be generated in the same container
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
113 var title
: nullable String is writable
115 # Does `self` have a `parent`?
116 fun is_root
: Bool do return parent
== null
118 # Children elements contained in `self`.
120 # Children are ordered, this order can be changed by the `DocPhase`.
121 var children
= new Array[DocComposite]
123 # Is `self` not displayed in the page.
125 # By default, empty elements are hidden.
126 fun is_hidden
: Bool do return children
.is_empty
128 # Title used in table of content if any.
129 var toc_title
: nullable String is writable, lazy
do return title
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
136 # Add a `child` to `self`.
138 # Shortcut for `children.add`.
139 fun add_child
(child
: DocComposite) do
144 # Depth of `self` in the composite tree.
146 var parent
= self.parent
147 if parent
== null then return 0
148 return parent
.depth
+ 1
151 # Pretty prints this composite recursively.
152 fun pretty_print
: Writable do
153 var res
= new Template
158 # Appends the Pretty print of this composite in `res`.
159 private fun pretty_print_in
(res
: Template) do
163 for child
in children
do child
.pretty_print_in
(res
)
167 # The `DocComposite` element that contains all the other.
169 # The root uses a specific subclass to provide different a different behavior
170 # than other `DocComposite` elements.
175 redef var id
= "<root>"
176 redef var title
= "<root>"
178 # No op for `RootSection`.
179 redef fun parent
=(p
) do end
182 # Base page elements.
184 # `DocSection` are used to break the documentation page into meaningfull parts.
186 # The content of the documentation summary is based on the section structure
187 # contained in the DocComposite tree.
192 # `DocArticle` are pieces of documentation.
194 # They maintains the content (text, list, image...) of a documentation page.
199 # A DocPhase is a step in the production of a Nitdoc documentation.
201 # Phases work from a `DocModel`.
202 # Specific phases are used to populate, organize, enhance and render the content
203 # of the documentation pages.
205 # See `doc_phases` for available DocPhase.
208 # Link to the ToolContext to access Nitdoc tool options.
211 # `DocModel` used by this phase to work.
214 # Starting point of a `DocPhase`.
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
221 redef class ToolContext
223 # Directory where the Nitdoc is rendered.
224 var opt_dir
= new OptionString("Output directory", "-d", "--dir")
226 # Shortcut for `opt_dir.value` with default "doc".
227 var output_dir
: String is lazy
do return opt_dir
.value
or else "doc"
231 option_context
.add_option
(opt_dir
)
235 # Catalog properties by kind.
236 class PropertiesByKind
238 var virtual_types
= new PropertyGroup[MVirtualTypeProp]("Virtual types")
241 var constructors
= new PropertyGroup[MMethod]("Contructors")
244 var attributes
= new PropertyGroup[MAttribute]("Attributes")
247 var methods
= new PropertyGroup[MMethod]("Methods")
250 var inner_classes
= new PropertyGroup[MInnerClass]("Inner classes")
254 # Sorted in the order they are displayed to the user.
255 var groups
: SequenceRead[PropertyGroup[MProperty]] = [
260 inner_classes
: PropertyGroup[MProperty]]
262 # Add each the specified property to the appropriate list.
263 init with_elements
(properties
: Collection[MProperty]) do add_all
(properties
)
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
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
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
)
289 # Sort each group with the specified comparator.
290 fun sort_groups
(comparator
: Comparator) do
291 for g
in groups
do comparator
.sort
(g
)
295 # An ordered list of properties of the same kind.
296 class PropertyGroup[E
: MProperty]
299 # The title of the group, as displayed to the user.
304 # ID used as a unique ID and in file names.
306 # **Must** match the following (POSIX ERE) regular expression:
309 # ^[A-Za-z_][A-Za-z0-9._-]*$
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
315 # Name displayed in console for debug and tests.
316 fun nitdoc_name
: String do return name
.html_escape
321 # Avoid id conflict with group
322 redef fun nitdoc_id
do
323 var mgroup
= self.mgroup
324 if mgroup
== null then return super
325 return "{mgroup.full_name}::{full_name}".to_cmangle
329 redef class MClassDef
330 redef fun nitdoc_name
do return mclass
.nitdoc_name
334 redef fun nitdoc_name
do return mproperty
.nitdoc_name