bda79e72558e053c0a116131b2e5b58f8e967abe
[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),
55 new ReadmePhase(toolcontext, doc): DocPhase]
56
57 if not toolcontext.opt_test.value then
58 phases.add new RenderHTMLPhase(toolcontext, doc)
59 end
60
61 for phase in phases do
62 toolcontext.info("# {phase.class_name}", 1)
63 phase.apply
64 end
65
66 if toolcontext.opt_test.value then
67 # Pages metrics
68 var page_counter = new Counter[String]
69 var pages = doc.pages.keys.to_a
70 default_comparator.sort(pages)
71 for title in pages do
72 var page = doc.pages[title]
73 page_counter.inc page.class_name
74 print page.pretty_print.write_to_string
75 end
76 print "Generated {doc.pages.length} pages"
77 page_counter.print_elements(100)
78 # Model metrics
79 var model_counter = new Counter[String]
80 for mentity in doc.mentities do
81 model_counter.inc mentity.class_name
82 end
83 print "Found {doc.mentities.length} mentities"
84 model_counter.print_elements(100)
85 end
86 end
87 end
88
89 # build toolcontext
90 var toolcontext = new ToolContext
91 var tpl = new Template
92 tpl.add "Usage: nitdoc [OPTION]... <file.nit>...\n"
93 tpl.add "Generates HTML pages of API documentation from Nit source files."
94 toolcontext.tooldescription = tpl.write_to_string
95
96 # process options
97 toolcontext.process_options(args)
98 var arguments = toolcontext.option_context.rest
99
100 # build model
101 var model = new Model
102 var mbuilder = new ModelBuilder(model, toolcontext)
103 var mmodules = mbuilder.parse_full(arguments)
104
105 # process
106 if mmodules.is_empty then return
107 mbuilder.run_phases
108 toolcontext.run_global_phases(mmodules)