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 associated to their ids.
32 # This is where `DocPhase` store and access pages to produce documentation.
35 var pages
: Map[String, DocPage] = new HashMap[String, DocPage]
37 # Nit `Model` from which we extract the documentation.
38 var model
: Model is writable
40 # The entry point of the `model`.
41 var mainmodule
: MModule is writable
43 # Add a `page` to this documentation.
44 fun add_page
(page
: DocPage) do
45 if pages
.has_key
(page
.id
) then
46 print
"Warning: multiple page with the same id `{page.id}`"
52 # A documentation page abstraction.
54 # The page contains a link to the `root` of the `DocComposite` that compose the
60 # The `id` is used as name for the generated file corresponding to the page
62 # Because multiple pages can be generated in the same directory it should be
65 # The `id` can also be used to establish links between pages (HTML links,
66 # HTML anchors, vim links, etc.).
67 var id
: String is writable
70 var title
: String is writable
72 # Root element of the page.
74 # `DocPhase` access the structure of the page from the `DocRoot`.
75 var root
= new DocRoot
77 redef fun to_s
do return title
80 # `DocPage` elements that can be nested in another.
82 # `DocComposite` is an abstraction for everything that go in a `DocPage` like
83 # sections, articles, images, lists, graphs...
85 # It provides base services for nesting mechanisms following the
86 # *Composite pattern*.
87 # The composed structure is a tree from a `DocRoot` that can be manipulated
89 abstract class DocComposite
92 var parent
: nullable DocComposite = null is writable
94 # Does `self` have a `parent`?
95 fun is_root
: Bool do return parent
== null
97 # Children elements contained in `self`.
99 # Children are ordered, this order can be changed by the `DocPhase`.
100 var children
= new Array[DocComposite]
102 # Does `self` have `children`?
103 fun is_empty
: Bool do return children
.is_empty
105 # Add a `child` to `self`.
107 # Shortcut for `children.add`.
108 fun add_child
(child
: DocComposite) do
113 # Depth of `self` in the composite tree.
115 if parent
== null then return 0
116 return parent
.depth
+ 1
120 # The `DocComposite` element that contains all the other.
122 # The root uses a specific subclass to provide different a different behavior
123 # than other `DocComposite` elements.
127 # No op for `RootSection`.
128 redef fun parent
=(p
) do end
131 # Base page elements.
133 # `DocSection` are used to break the documentation page into meaningfull parts.
135 # The content of the documentation summary is based on the section structure
136 # contained in the DocComposite tree.
141 # `DocArticle` are pieces of documentation.
143 # They maintains the content (text, list, image...) of a documentation page.
148 # A DocPhase is a step in the production of a Nitdoc documentation.
150 # Phases work from a `DocModel`.
151 # Specific phases are used to populate, organize, enhance and render the content
152 # of the documentation pages.
154 # See `doc_phases` for available DocPhase.
157 # Link to the ToolContext to access Nitdoc tool options.
160 # `DocModel` used by this phase to work.
163 # Starting point of a `DocPhase`.
165 # This is where the behavior of the phase is implemented.
166 # Phases can populate, edit or render the `doc` from here.
167 fun apply
is abstract
170 redef class ToolContext
172 # Directory where the Nitdoc is rendered.
173 var opt_dir
= new OptionString("output directory", "-d", "--dir")
175 # Shortcut for `opt_dir.value` with default "doc".
176 var output_dir
: String is lazy
do return opt_dir
.value
or else "doc"
180 option_context
.add_option
(opt_dir
)
184 # Catalog properties by kind.
185 class PropertiesByKind
187 var virtual_types
= new PropertyGroup[MVirtualTypeProp]("Virtual types")
190 var constructors
= new PropertyGroup[MMethod]("Contructors")
193 var attributes
= new PropertyGroup[MAttribute]("Attributes")
196 var methods
= new PropertyGroup[MMethod]("Methods")
199 var inner_classes
= new PropertyGroup[MInnerClass]("Inner classes")
203 # Sorted in the order they are displayed to the user.
204 var groups
: SequenceRead[PropertyGroup[MProperty]] = [
209 inner_classes
: PropertyGroup[MProperty]]
211 # Add each the specified property to the appropriate list.
212 init with_elements
(properties
: Collection[MProperty]) do add_all
(properties
)
214 # Add the specified property to the appropriate list.
215 fun add
(property
: MProperty) do
216 if property
isa MMethod then
217 if property
.is_init
then
218 constructors
.add property
222 else if property
isa MVirtualTypeProp then
223 virtual_types
.add property
224 else if property
isa MAttribute then
225 attributes
.add property
226 else if property
isa MInnerClass then
227 inner_classes
.add property
233 # Add each the specified property to the appropriate list.
234 fun add_all
(properties
: Collection[MProperty]) do
235 for p
in properties
do add
(p
)
238 # Sort each group with the specified comparator.
239 fun sort_groups
(comparator
: Comparator) do
240 for g
in groups
do comparator
.sort
(g
)
244 # An ordered list of properties of the same kind.
245 class PropertyGroup[E
: MProperty]
248 # The title of the group, as displayed to the user.
253 # ID used as a unique ID and in file names.
255 # **Must** match the following (POSIX ERE) regular expression:
258 # ^[A-Za-z_][A-Za-z0-9._-]*$
261 # That way, the ID is always a valid URI component and a valid XML name.
262 fun nitdoc_id
: String do return full_name
.to_cmangle
264 # Name displayed in console for debug and tests.
265 fun nitdoc_name
: String do return name
.html_escape
270 # Avoid id conflict with group
271 redef fun nitdoc_id
do
272 if mgroup
== null then return super
273 return "{mgroup.full_name}::{full_name}".to_cmangle
277 redef class MClassDef
278 redef fun nitdoc_name
do return mclass
.nitdoc_name
282 redef fun nitdoc_name
do return mproperty
.nitdoc_name