nitdoc: test pages structure
[nit.git] / src / nitdoc.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 # Documentation generator for the nit language.
16 #
17 # Generate API documentation in HTML format from nit source code.
18 module nitdoc
19
20 import modelbuilder
21 import doc
22 import counter
23
24 redef class ToolContext
25 # Nitdoc generation phase.
26 var docphase: Phase = new Nitdoc(self, null)
27
28 # File pattern used to link documentation to source code.
29 var opt_test = new OptionBool("do not render anything, only print test data", "--test")
30
31 redef init do
32 super
33 option_context.add_option(opt_test)
34 end
35 end
36
37 # Nitdoc phase explores the model and generate pages for each mentities found
38 private class Nitdoc
39 super Phase
40 redef fun process_mainmodule(mainmodule, mmodules)
41 do
42 var doc = new DocModel(mainmodule.model, mainmodule)
43
44 var phases = [
45 new ExtractionPhase(toolcontext, doc),
46 new IndexingPhase(toolcontext, doc),
47 new MakePagePhase(toolcontext, doc),
48 new POSetPhase(toolcontext, doc),
49 new ConcernsPhase(toolcontext, doc),
50 new StructurePhase(toolcontext, doc),
51 new InheritanceListsPhase(toolcontext, doc),
52 new IntroRedefListPhase(toolcontext, doc),
53 new LinListPhase(toolcontext, doc),
54 new GraphPhase(toolcontext, doc): DocPhase]
55
56 if not toolcontext.opt_test.value then
57 phases.add new RenderHTMLPhase(toolcontext, doc)
58 end
59
60 for phase in phases do
61 toolcontext.info("# {phase.class_name}", 1)
62 phase.apply
63 end
64
65 if toolcontext.opt_test.value then
66 # Pages metrics
67 var page_counter = new Counter[String]
68 var pages = doc.pages.keys.to_a
69 default_comparator.sort(pages)
70 for title in pages do
71 var page = doc.pages[title]
72 page_counter.inc page.class_name
73 print page.pretty_print.write_to_string
74 end
75 print "Generated {doc.pages.length} pages"
76 page_counter.print_elements(100)
77 # Model metrics
78 var model_counter = new Counter[String]
79 for mentity in doc.mentities do
80 model_counter.inc mentity.class_name
81 end
82 print "Found {doc.mentities.length} mentities"
83 model_counter.print_elements(100)
84 end
85 end
86 end
87
88 # build toolcontext
89 var toolcontext = new ToolContext
90 var tpl = new Template
91 tpl.add "Usage: nitdoc [OPTION]... <file.nit>...\n"
92 tpl.add "Generates HTML pages of API documentation from Nit source files."
93 toolcontext.tooldescription = tpl.write_to_string
94
95 # process options
96 toolcontext.process_options(args)
97 var arguments = toolcontext.option_context.rest
98
99 # build model
100 var model = new Model
101 var mbuilder = new ModelBuilder(model, toolcontext)
102 var mmodules = mbuilder.parse_full(arguments)
103
104 # process
105 if mmodules.is_empty then return
106 mbuilder.run_phases
107 toolcontext.run_global_phases(mmodules)