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