1 # This file is part of NIT ( http://www.nitlanguage.org ).
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
7 # http://www.apache.org/licenses/LICENSE-2.0
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.
15 # Adds importation and class hierarchy graphs.
20 import html_templates
::html_model
# FIXME maybe this phase should depend on `html_render`
22 redef class ToolContext
24 # Do not generate `graphviz` diagrams.
25 var opt_nodot
= new OptionBool("do not generate graphes with graphviz", "--no-dot")
29 option_context
.add_option
(opt_nodot
)
33 # This phase insert importation and inheritance graphs into pages.
38 if ctx
.opt_nodot
.value
then return
39 for page
in doc
.pages
do
40 var article
= page
.build_graph
(self, doc
)
41 if article
== null then continue
43 # page.root.add article
44 page
.root
.children
[1].children
.insert
(article
, 0)
50 # Build dot graph articles from `mmodules` list.
52 # Since only `MEntity pages` contain a graph, this method returns null in all
54 private fun build_graph
(v
: GraphPhase, doc
: DocModel): nullable GraphArticle do return null
57 # TODO graph generation can be factorized in POSet.
59 redef class MModulePage
60 redef fun build_graph
(v
, doc
) do
61 var op
= new FlatBuffer
62 var name
= "dep_module_{mentity.nitdoc_id}"
63 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")
64 for mmodule
in poset
do
65 if mmodule
== self.mentity
then
66 op
.append
("\"{mmodule.name.escape_to_dot}\
"[shape=box,margin=0.03];\n")
68 op
.append
("\"{mmodule.name.escape_to_dot}\
"[URL=\"{mmodule.nitdoc_url.escape_to_dot}\
"];\n")
70 for omodule
in poset
[mmodule
].direct_greaters
do
71 op
.append
("\"{mmodule.name.escape_to_dot}\
"->\"{omodule.name.escape_to_dot}\
";\n")
75 return new GraphArticle(mentity
, name
, op
)
79 redef class MClassPage
80 redef fun build_graph
(v
, doc
) do
81 var op
= new FlatBuffer
82 var name
= "dep_class_{mentity.nitdoc_id}"
83 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")
84 var classes
= poset
.to_a
85 var todo
= new Array[MClass]
86 var done
= new HashSet[MClass]
87 doc
.mainmodule
.linearize_mclasses
(classes
)
88 if not classes
.is_empty
then todo
.add classes
.first
89 while not todo
.is_empty
do
91 if done
.has
(c
) then continue
93 if c
== self.mentity
then
94 op
.append
("\"{c.name.escape_to_dot}\
"[shape=box,margin=0.03];\n")
96 op
.append
("\"{c.name.escape_to_dot}\
"[URL=\"{c.nitdoc_url.escape_to_dot}\
"];\n")
98 var smallers
= poset
[c
].direct_smallers
99 if smallers
.length
< 10 then
100 for c2
in smallers
do
101 op
.append
("\"{c2.name.escape_to_dot}\
"->\"{c.name.escape_to_dot}\
";\n")
103 todo
.add_all smallers
105 op
.append
("\"...\
"->\"{c.name.escape_to_dot}\
";\n")
109 return new GraphArticle(mentity
, name
, op
)
113 # An article that display an importation or inheritance graph.
115 # The graph is stored in dot format.
116 # The final output is delayed untill rendering.
118 super MEntityComposite
120 # Graph ID (used for outputing file with names).
123 # Dot script of the graph.