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