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