stdlib/strings: Moved Buffer to FlatBuffer, Buffer is now abstract.
[nit.git] / src / metrics / model_hyperdoc.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Copyright 2012 Jean Privat <jean@pryen.org>
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16
17 # Dump of Nit model into hypertext human-readable format.
18 module model_hyperdoc
19
20 import model
21 import metrics_base
22 import phase
23 import frontend
24
25 redef class ToolContext
26 var model_hyperdoc_phase = new ModelHyperdocPhase(self, null)
27 end
28
29 private class ModelHyperdocPhase
30 super Phase
31 redef fun process_mainmodule(mainmodule)
32 do
33 if not toolcontext.opt_generate_hyperdoc.value and not toolcontext.opt_all.value then return
34 generate_model_hyperdoc(toolcontext, toolcontext.modelbuilder.model)
35 end
36 end
37
38 # Genetate a HTML file for the model.
39 # The generated file contains the description of each entity of the model
40 fun generate_model_hyperdoc(toolcontext: ToolContext, model: Model)
41 do
42 var buf = new FlatBuffer
43 buf.append("<html>\n<body>\n")
44 buf.append("<h1>Model</h1>\n")
45
46 buf.append("<h2>Projects</h2>\n")
47 for mproject in model.mprojects do
48 buf.append("<h3 id='project-{mproject}'>Project {mproject}</h3>\n")
49 buf.append("<dl>\n")
50 buf.append("<dt>groups</dt>\n")
51 for x in mproject.mgroups do
52 buf.append("<dd>{linkto(x)}</dd>\n")
53 end
54 buf.append("</dl>\n")
55 end
56
57 buf.append("<h2>Groups</h2>\n")
58 for mproject in model.mprojects do
59 for mgroup in mproject.mgroups do
60 buf.append("<h3 id='group-{mgroup}'>Group {mgroup}</h3>\n")
61 buf.append("<dl>\n")
62 buf.append("<dt>project</dt>\n")
63 buf.append("<dd>{linkto(mproject)}</dd>\n")
64 buf.append("<dt>filepath</dt>\n")
65 buf.append("<dd>{mgroup.filepath}</dd>\n")
66 var p = mgroup.parent
67 if p != null then
68 buf.append("<dt>parent group</dt>\n")
69 buf.append("<dd>{linkto(p)}</dd>\n")
70 end
71 buf.append("<dt>nested groups</dt>\n")
72 for x in mgroup.in_nesting.direct_smallers do
73 buf.append("<dd>{linkto(x)}</dd>\n")
74 end
75 buf.append("<dt>modules</dt>\n")
76 for x in mgroup.mmodules do
77 buf.append("<dd>{linkto(x)}</dd>\n")
78 end
79 end
80 buf.append("</dl>\n")
81 end
82
83 buf.append("<h2>Modules</h2>\n")
84 for mmodule in model.mmodules do
85 buf.append("<h3 id='module-{mmodule}'>{mmodule}</h3>\n")
86 buf.append("<dl>\n")
87 buf.append("<dt>group</dt>\n")
88 var grp = mmodule.mgroup
89 if grp != null then buf.append("<dd>{linkto(grp)}</dd>\n")
90 buf.append("<dt>direct import</dt>\n")
91 for x in mmodule.in_importation.direct_greaters do
92 buf.append("<dd>{linkto(x)}</dd>\n")
93 end
94 buf.append("<dt>direct clients</dt>\n")
95 for x in mmodule.in_importation.direct_smallers do
96 buf.append("<dd>{linkto(x)}</dd>\n")
97 end
98 buf.append("<dt>introduced classes</dt>\n")
99 for x in mmodule.mclassdefs do
100 if not x.is_intro then continue
101 buf.append("<dd>{linkto(x.mclass)} by {linkto(x)}</dd>\n")
102 end
103 buf.append("<dt>refined classes</dt>\n")
104 for x in mmodule.mclassdefs do
105 if x.is_intro then continue
106 buf.append("<dd>{linkto(x.mclass)} by {linkto(x)}</dd>\n")
107 end
108 buf.append("</dl>\n")
109 end
110 buf.append("<h2>Classes</h2>\n")
111 for mclass in model.mclasses do
112 buf.append("<h3 id='class-{mclass}'>{mclass}</h3>\n")
113 buf.append("<dl>\n")
114 buf.append("<dt>module of introduction</dt>\n")
115 buf.append("<dd>{linkto(mclass.intro_mmodule)}</dd>\n")
116 buf.append("<dt>class definitions</dt>\n")
117 for x in mclass.mclassdefs do
118 buf.append("<dd>{linkto(x)} in {linkto(x.mmodule)}</dd>\n")
119 end
120 buf.append("</dl>\n")
121 end
122 buf.append("<h2>Class Definitions</h2>\n")
123 for mclass in model.mclasses do
124 for mclassdef in mclass.mclassdefs do
125 buf.append("<h3 id='classdef-{mclassdef}'>{mclassdef}</h3>\n")
126 buf.append("<dl>\n")
127 buf.append("<dt>module</dt>\n")
128 buf.append("<dd>{linkto(mclassdef.mmodule)}</dd>\n")
129 buf.append("<dt>class</dt>\n")
130 buf.append("<dd>{linkto(mclassdef.mclass)}</dd>\n")
131 buf.append("<dt>direct refinements</dt>\n")
132 for x in mclassdef.in_hierarchy.direct_greaters do
133 if x.mclass != mclass then continue
134 buf.append("<dd>{linkto(x)} in {linkto(x.mmodule)}</dd>\n")
135 end
136 buf.append("<dt>direct refinemees</dt>\n")
137 for x in mclassdef.in_hierarchy.direct_smallers do
138 if x.mclass != mclass then continue
139 buf.append("<dd>{linkto(x)} in {linkto(x.mmodule)}</dd>\n")
140 end
141 buf.append("<dt>direct superclasses</dt>\n")
142 for x in mclassdef.supertypes do
143 buf.append("<dd>{linkto(x.mclass)} by {x}</dd>\n")
144 end
145 buf.append("<dt>introduced properties</dt>\n")
146 for x in mclassdef.mpropdefs do
147 if not x.is_intro then continue
148 buf.append("<dd>{linkto(x.mproperty)} by {linkto(x)}</dd>\n")
149 end
150 buf.append("<dt>redefined properties</dt>\n")
151 for x in mclassdef.mpropdefs do
152 if x.is_intro then continue
153 buf.append("<dd>{linkto(x.mproperty)} by {linkto(x)}</dd>\n")
154 end
155 buf.append("</dl>\n")
156 end
157 end
158 buf.append("<h2>Properties</h2>\n")
159 for mprop in model.mproperties do
160 buf.append("<h3 id='property-{mprop}'>{mprop}</h3>\n")
161 buf.append("<dl>\n")
162 buf.append("<dt>module of introdcution</dt>\n")
163 buf.append("<dd>{linkto(mprop.intro_mclassdef.mmodule)}</dd>\n")
164 buf.append("<dt>class of introduction</dt>\n")
165 buf.append("<dd>{linkto(mprop.intro_mclassdef.mclass)}</dd>\n")
166 buf.append("<dt>class definition of introduction</dt>\n")
167 buf.append("<dd>{linkto(mprop.intro_mclassdef)}</dd>\n")
168 buf.append("<dt>property definitions</dt>\n")
169 for x in mprop.mpropdefs do
170 buf.append("<dd>{linkto(x)} in {linkto(x.mclassdef)}</dd>\n")
171 end
172 buf.append("</dl>\n")
173 end
174 buf.append("<h2>Property Definitions</h2>\n")
175 for mprop in model.mproperties do
176 for mpropdef in mprop.mpropdefs do
177 buf.append("<h3 id='propdef-{mpropdef}'>{mpropdef}</h3>\n")
178 buf.append("<dl>\n")
179 buf.append("<dt>module</dt>\n")
180 buf.append("<dd>{linkto(mpropdef.mclassdef.mmodule)}</dd>\n")
181 buf.append("<dt>class</dt>\n")
182 buf.append("<dd>{linkto(mpropdef.mclassdef.mclass)}</dd>\n")
183 buf.append("<dt>class definition</dt>\n")
184 buf.append("<dd>{linkto(mpropdef.mclassdef)}</dd>\n")
185 buf.append("<dt>super definitions</dt>\n")
186 for x in mpropdef.mproperty.lookup_super_definitions(mpropdef.mclassdef.mmodule, mpropdef.mclassdef.bound_mtype) do
187 buf.append("<dd>{linkto(x)} in {linkto(x.mclassdef)}</dd>\n")
188 end
189 end
190 end
191 buf.append("</body></html>\n")
192 var f = new OFStream.open(toolcontext.output_dir.join_path("model.html"))
193 f.write(buf.to_s)
194 f.close
195 end
196
197 private fun linkto(o: Object): String
198 do
199 if o isa MProject then
200 return "<a href='#project-{o}'>{o}</a>"
201 else if o isa MGroup then
202 return "<a href='#group-{o}'>{o}</a>"
203 else if o isa MModule then
204 return "<a href='#module-{o}'>{o}</a>"
205 else if o isa MClass then
206 return "<a href='#class-{o}'>{o}</a>"
207 else if o isa MClassDef then
208 return "<a href='#classdef-{o}'>{o}</a>"
209 else if o isa MProperty then
210 return "<a href='#property-{o}'>{o}</a>"
211 else if o isa MPropDef then
212 return "<a href='#propdef-{o}'>{o}</a>"
213 else
214 print "cannot linkto {o.class_name}"
215 abort
216 end
217 end