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.
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.
30 # `DocPage` composing the documentation.
32 # This is where `DocPhase` store and access pages to produce documentation.
33 var pages
= new Array[DocPage]
35 # Nit `Model` from which we extract the documentation.
36 var model
: Model is writable
38 # The entry point of the `model`.
39 var mainmodule
: MModule is writable
42 # A documentation page abstraction.
44 # The page contains a link to the `root` of the `DocComposite` that compose the
49 var title
: String is writable
51 # Root element of the page.
53 # `DocPhase` access the structure of the page from the `DocRoot`.
54 var root
= new DocRoot
56 redef fun to_s
do return title
59 # `DocPage` elements that can be nested in another.
61 # `DocComposite` is an abstraction for everything that go in a `DocPage` like
62 # sections, articles, images, lists, graphs...
64 # It provides base services for nesting mechanisms following the
65 # *Composite pattern*.
66 # The composed structure is a tree from a `DocRoot` that can be manipulated
68 abstract class DocComposite
71 var parent
: nullable DocComposite = null is writable
73 # Does `self` have a `parent`?
74 fun is_root
: Bool do return parent
== null
76 # Children elements contained in `self`.
78 # Children are ordered, this order can be changed by the `DocPhase`.
79 var children
= new Array[DocComposite]
81 # Does `self` have `children`?
82 fun is_empty
: Bool do return children
.is_empty
84 # Add a `child` to `self`.
86 # Shortcut for `children.add`.
87 fun add_child
(child
: DocComposite) do
93 # The `DocComposite` element that contains all the other.
95 # The root uses a specific subclass to provide different a different behavior
96 # than other `DocComposite` elements.
100 # No op for `RootSection`.
101 redef fun parent
=(p
) do end
104 # Base page elements.
106 # `DocSection` are used to break the documentation page into meaningfull parts.
108 # The content of the documentation summary is based on the section structure
109 # contained in the DocComposite tree.
114 # `DocArticle` are pieces of documentation.
116 # They maintains the content (text, list, image...) of a documentation page.
121 # A DocPhase is a step in the production of a Nitdoc documentation.
123 # Phases work from a `DocModel`.
124 # Specific phases are used to populate, organize, enhance and render the content
125 # of the documentation pages.
127 # See `doc_phases` for available DocPhase.
130 # Link to the ToolContext to access Nitdoc tool options.
133 # `DocModel` used by this phase to work.
136 # Starting point of a `DocPhase`.
138 # This is where the behavior of the phase is implemented.
139 # Phases can populate, edit or render the `doc` from here.
140 fun apply
is abstract
143 redef class ToolContext
145 # Directory where the Nitdoc is rendered.
146 var opt_dir
= new OptionString("output directory", "-d", "--dir")
148 # Shortcut for `opt_dir.value` with default "doc".
149 var output_dir
: String is lazy
do return opt_dir
.value
or else "doc"
153 option_context
.add_option
(opt_dir
)
157 # Catalog properties by kind.
158 class PropertiesByKind
160 var virtual_types
= new PropertyGroup[MVirtualTypeProp]("Virtual types")
163 var constructors
= new PropertyGroup[MMethod]("Contructors")
166 var attributes
= new PropertyGroup[MAttribute]("Attributes")
169 var methods
= new PropertyGroup[MMethod]("Methods")
172 var inner_classes
= new PropertyGroup[MInnerClass]("Inner classes")
176 # Sorted in the order they are displayed to the user.
177 var groups
: SequenceRead[PropertyGroup[MProperty]] = [
182 inner_classes
: PropertyGroup[MProperty]]
184 # Add each the specified property to the appropriate list.
185 init with_elements
(properties
: Collection[MProperty]) do add_all
(properties
)
187 # Add the specified property to the appropriate list.
188 fun add
(property
: MProperty) do
189 if property
isa MMethod then
190 if property
.is_init
then
191 constructors
.add property
195 else if property
isa MVirtualTypeProp then
196 virtual_types
.add property
197 else if property
isa MAttribute then
198 attributes
.add property
199 else if property
isa MInnerClass then
200 inner_classes
.add property
206 # Add each the specified property to the appropriate list.
207 fun add_all
(properties
: Collection[MProperty]) do
208 for p
in properties
do add
(p
)
211 # Sort each group with the specified comparator.
212 fun sort_groups
(comparator
: Comparator) do
213 for g
in groups
do comparator
.sort
(g
)
217 # An ordered list of properties of the same kind.
218 class PropertyGroup[E
: MProperty]
221 # The title of the group, as displayed to the user.
226 # Name displayed in console for debug and tests.
227 fun nitdoc_name
: String do return name
.html_escape
230 redef class MClassDef
231 redef fun nitdoc_name
do return mclass
.nitdoc_name
235 redef fun nitdoc_name
do return mproperty
.nitdoc_name