tests: update because changes in kernel and array
[nit.git] / src / doc / doc_phases / doc_graphs.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 # Adds importation and class hierarchy graphs.
16 module doc_graphs
17
18 import doc_poset
19
20 redef class ToolContext
21
22 # Do not generate `graphviz` diagrams.
23 var opt_nodot = new OptionBool("do not generate graphes with graphviz", "--no-dot")
24
25 redef init do
26 super
27 option_context.add_option(opt_nodot)
28 end
29 end
30
31 # This phase insert importation and inheritance graphs into pages.
32 class GraphPhase
33 super DocPhase
34
35 redef fun apply do
36 if ctx.opt_nodot.value then return
37 for page in doc.pages do
38 var article = page.build_graph(self, doc)
39 if article == null then continue
40 # FIXME avoid diff
41 # page.root.add article
42 page.root.children[1].children.insert(article, 0)
43 end
44 end
45 end
46
47 redef class DocPage
48 # Build dot graph articles from `mmodules` list.
49 #
50 # Since only `MEntity pages` contain a graph, this method returns null in all
51 # other cases.
52 private fun build_graph(v: GraphPhase, doc: DocModel): nullable GraphArticle do return null
53 end
54
55 # TODO graph generation can be factorized in POSet.
56
57 redef class MModulePage
58 redef fun build_graph(v, doc) do
59 var op = new FlatBuffer
60 var name = "dep_module_{mentity.nitdoc_id}"
61 op.append("digraph \"{name.escape_to_dot}\" \{ rankdir=BT; node[shape=none,margin=0,width=0,height=0,fontsize=10]; edge[dir=none,color=gray]; ranksep=0.2; nodesep=0.1;\n")
62 for mmodule in poset do
63 if mmodule == self.mentity then
64 op.append("\"{mmodule.name.escape_to_dot}\"[shape=box,margin=0.03];\n")
65 else
66 op.append("\"{mmodule.name.escape_to_dot}\"[URL=\"{mmodule.nitdoc_url.escape_to_dot}\"];\n")
67 end
68 for omodule in poset[mmodule].direct_greaters do
69 op.append("\"{mmodule.name.escape_to_dot}\"->\"{omodule.name.escape_to_dot}\";\n")
70 end
71 end
72 op.append("\}\n")
73 return new GraphArticle(name, op)
74 end
75 end
76
77 redef class MClassPage
78 redef fun build_graph(v, doc) do
79 var op = new FlatBuffer
80 var name = "dep_class_{mentity.nitdoc_id}"
81 op.append("digraph \"{name.escape_to_dot}\" \{ rankdir=BT; node[shape=none,margin=0,width=0,height=0,fontsize=10]; edge[dir=none,color=gray]; ranksep=0.2; nodesep=0.1;\n")
82 var classes = poset.to_a
83 var todo = new Array[MClass]
84 var done = new HashSet[MClass]
85 doc.mainmodule.linearize_mclasses(classes)
86 if not classes.is_empty then todo.add classes.first
87 while not todo.is_empty do
88 var c = todo.shift
89 if done.has(c) then continue
90 done.add c
91 if c == self.mentity then
92 op.append("\"{c.name.escape_to_dot}\"[shape=box,margin=0.03];\n")
93 else
94 op.append("\"{c.name.escape_to_dot}\"[URL=\"{c.nitdoc_url.escape_to_dot}\"];\n")
95 end
96 var smallers = poset[c].direct_smallers
97 if smallers.length < 10 then
98 for c2 in smallers do
99 op.append("\"{c2.name.escape_to_dot}\"->\"{c.name.escape_to_dot}\";\n")
100 end
101 todo.add_all smallers
102 else
103 op.append("\"...\"->\"{c.name.escape_to_dot}\";\n")
104 end
105 end
106 op.append("\}\n")
107 return new GraphArticle(name, op)
108 end
109 end
110
111 # An article that display an importation or inheritance graph.
112 #
113 # The graph is stored in dot format.
114 # The final output is delayed untill rendering.
115 class GraphArticle
116 super DocComposite
117
118 # Graph ID (used for outputing file with names).
119 var id: String
120
121 # Dot script of the graph.
122 var dot: Text
123 end