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.
19 import doc_model
# FIXME maintain compatibility with old templates.
21 # The model of a Nitdoc documentation.
23 # `DocModel` contains the list of the `DocPage` to be generated.
25 # The model is populated through `DocPhase` to be constructed.
26 # It is a placeholder to share data between each phase.
29 # `DocPage` composing the documentation.
31 # This is where `DocPhase` store and access pages to produce documentation.
32 var pages
= new Array[DocPage]
34 # Nit `Model` from which we extract the documentation.
35 var model
: Model is writable
37 # The entry point of the `model`.
38 var mainmodule
: MModule is writable
41 # A documentation page abstraction.
43 # The page contains a link to the `root` of the `DocComposite` that compose the
48 var title
: String is writable
50 # Root element of the page.
52 # `DocPhase` access the structure of the page from the `DocRoot`.
53 var root
= new DocRoot
55 redef fun to_s
do return title
58 # `DocPage` elements that can be nested in another.
60 # `DocComposite` is an abstraction for everything that go in a `DocPage` like
61 # sections, articles, images, lists, graphs...
63 # It provides base services for nesting mechanisms following the
64 # *Composite pattern*.
65 # The composed structure is a tree from a `DocRoot` that can be manipulated
67 abstract class DocComposite
70 var parent
: nullable DocComposite = null
72 # Does `self` have a `parent`?
73 fun is_root
: Bool do return parent
== null
75 # Children elements contained in `self`.
77 # Children are ordered, this order can be changed by the `DocPhase`.
78 var children
= new Array[DocComposite]
80 # Does `self` have `children`?
81 fun is_empty
: Bool do return children
.is_empty
83 # Add a `child` to `self`.
85 # Shortcut for `children.add`.
86 fun add
(child
: DocComposite) do children
.add child
89 # The `DocComposite` element that contains all the other.
91 # The root uses a specific subclass to provide different a different behavior
92 # than other `DocComposite` elements.
96 # No op for `RootSection`.
97 redef fun parent
=(p
) do end
100 # Base page elements.
102 # `DocSection` are used to break the documentation page into meaningfull parts.
104 # The content of the documentation summary is based on the section structure
105 # contained in the DocComposite tree.
110 # `DocArticle` are pieces of documentation.
112 # They maintains the content (text, list, image...) of a documentation page.
117 # A DocPhase is a step in the production of a Nitdoc documentation.
119 # Phases work from a `DocModel`.
120 # Specific phases are used to populate, organize, enhance and render the content
121 # of the documentation pages.
123 # See `doc_phases` for available DocPhase.
126 # Link to the ToolContext to access Nitdoc tool options.
129 # `DocModel` used by this phase to work.
132 # Starting point of a `DocPhase`.
134 # This is where the behavior of the phase is implemented.
135 # Phases can populate, edit or render the `doc` from here.
136 fun apply
is abstract
139 redef class ToolContext
141 # Directory where the Nitdoc is rendered.
142 var opt_dir
= new OptionString("output directory", "-d", "--dir")
144 # Shortcut for `opt_dir.value` with default "doc".
145 var output_dir
: String is lazy
do return opt_dir
.value
or else "doc"
149 option_context
.add_option
(opt_dir
)
153 # Catalog properties by kind.
154 class PropertiesByKind
156 var virtual_types
= new PropertyGroup[MVirtualTypeProp]("Virtual types")
159 var constructors
= new PropertyGroup[MMethod]("Contructors")
162 var attributes
= new PropertyGroup[MAttribute]("Attributes")
165 var methods
= new PropertyGroup[MMethod]("Methods")
168 var inner_classes
= new PropertyGroup[MInnerClass]("Inner classes")
172 # Sorted in the order they are displayed to the user.
173 var groups
: SequenceRead[PropertyGroup[MProperty]] = [
178 inner_classes
: PropertyGroup[MProperty]]
180 # Add each the specified property to the appropriate list.
181 init with_elements
(properties
: Collection[MProperty]) do add_all
(properties
)
183 # Add the specified property to the appropriate list.
184 fun add
(property
: MProperty) do
185 if property
isa MMethod then
186 if property
.is_init
then
187 constructors
.add property
191 else if property
isa MVirtualTypeProp then
192 virtual_types
.add property
193 else if property
isa MAttribute then
194 attributes
.add property
195 else if property
isa MInnerClass then
196 inner_classes
.add property
202 # Add each the specified property to the appropriate list.
203 fun add_all
(properties
: Collection[MProperty]) do
204 for p
in properties
do add
(p
)
207 # Sort each group with the specified comparator.
208 fun sort_groups
(comparator
: Comparator) do
209 for g
in groups
do comparator
.sort
(g
)
213 # An ordered list of properties of the same kind.
214 class PropertyGroup[E
: MProperty]
217 # The title of the group, as displayed to the user.