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 # Nitdoc page generation
20 # The NitdocContext contains all the knowledge used for doc generation
22 private var opt_dir
= new OptionString("output directory", "-d", "--dir")
23 private var opt_source
= new OptionString("link for source (%f for filename, %l for first line, %L for last line)", "--source")
24 private var opt_sharedir
= new OptionString("directory containing nitdoc assets", "--sharedir")
25 private var opt_shareurl
= new OptionString("use shareurl instead of copy shared files", "--shareurl")
26 private var opt_nodot
= new OptionBool("do not generate graphes with graphviz", "--no-dot")
27 private var opt_private
= new OptionBool("also generate private API", "--private")
29 private var opt_custom_title
= new OptionString("custom title for homepage", "--custom-title")
30 private var opt_custom_brand
= new OptionString("custom link to external site", "--custom-brand")
31 private var opt_custom_intro
= new OptionString("custom intro text for homepage", "--custom-overview-text")
32 private var opt_custom_footer
= new OptionString("custom footer text", "--custom-footer-text")
34 private var opt_github_upstream
= new OptionString("Git branch where edited commits will be pulled into (ex: user:repo:branch)", "--github-upstream")
35 private var opt_github_base_sha1
= new OptionString("Git sha1 of base commit used to create pull request", "--github-base-sha1")
36 private var opt_github_gitdir
= new OptionString("Git working directory used to resolve path name (ex: /home/me/myproject/)", "--github-gitdir")
38 private var opt_piwik_tracker
= new OptionString("Piwik tracker URL (ex: nitlanguage.org/piwik/)", "--piwik-tracker")
39 private var opt_piwik_site_id
= new OptionString("Piwik site ID", "--piwik-site-id")
41 private var toolcontext
= new ToolContext
42 private var mbuilder
: ModelBuilder
43 private var mainmodule
: MModule
44 private var output_dir
: String
45 private var min_visibility
: MVisibility
48 var opts
= toolcontext
.option_context
49 opts
.add_option
(opt_dir
, opt_source
, opt_sharedir
, opt_shareurl
, opt_nodot
, opt_private
)
50 opts
.add_option
(opt_custom_title
, opt_custom_footer
, opt_custom_intro
, opt_custom_brand
)
51 opts
.add_option
(opt_github_upstream
, opt_github_base_sha1
, opt_github_gitdir
)
52 opts
.add_option
(opt_piwik_tracker
, opt_piwik_site_id
)
54 var tpl
= new Template
55 tpl
.add
"Usage: nitdoc [OPTION]... <file.nit>...\n"
56 tpl
.add
"Generates HTML pages of API documentation from Nit source files."
57 toolcontext
.tooldescription
= tpl
.write_to_string
58 toolcontext
.process_options
(args
)
61 self.parse
(toolcontext
.option_context
.rest
)
64 private fun process_options
do
65 if opt_private
.value
then
66 min_visibility
= none_visibility
68 min_visibility
= protected_visibility
70 var gh_upstream
= opt_github_upstream
.value
71 var gh_base_sha
= opt_github_base_sha1
.value
72 var gh_gitdir
= opt_github_gitdir
.value
73 if not gh_upstream
== null or not gh_base_sha
== null or not gh_gitdir
== null then
74 if gh_upstream
== null or gh_base_sha
== null or gh_gitdir
== null then
75 print
"Error: Options {opt_github_upstream.names.first}, {opt_github_base_sha1.names.first} and {opt_github_gitdir.names.first} are required to enable the GitHub plugin"
81 private fun parse
(arguments
: Array[String]) do
83 mbuilder
= new ModelBuilder(model
, toolcontext
)
84 var mmodules
= mbuilder
.parse
(arguments
)
85 if mmodules
.is_empty
then return
87 if mmodules
.length
== 1 then
88 mainmodule
= mmodules
.first
90 mainmodule
= new MModule(model
, null, "<main>", new Location(null, 0, 0, 0, 0))
91 mainmodule
.is_fictive
= true
92 mainmodule
.set_imported_mmodules
(mmodules
)
96 fun generate_nitdoc
do
105 private fun init_output_dir
do
106 # location output dir
107 var output_dir
= opt_dir
.value
108 if output_dir
== null then
111 self.output_dir
= output_dir
112 # create destination dir if it's necessary
113 if not output_dir
.file_exists
then output_dir
.mkdir
115 var sharedir
= opt_sharedir
.value
116 if sharedir
== null then
117 var dir
= toolcontext
.nit_dir
119 print
"Error: Cannot locate nitdoc share files. Uses --sharedir or envvar NIT_DIR"
122 sharedir
= "{dir}/share/nitdoc"
123 if not sharedir
.file_exists
then
124 print
"Error: Cannot locate nitdoc share files. Uses --sharedir or envvar NIT_DIR"
129 if opt_shareurl
.value
== null then
130 sys
.system
("cp -r {sharedir.to_s}/* {output_dir.to_s}/")
132 sys
.system
("cp -r {sharedir.to_s}/resources/ {output_dir.to_s}/resources/")
137 private fun overview
do
138 var overviewpage
= new NitdocOverview(self)
139 overviewpage
.render
.write_to_file
("{output_dir.to_s}/index.html")
142 private fun search
do
143 var searchpage
= new NitdocSearch(self)
144 searchpage
.render
.write_to_file
("{output_dir.to_s}/search.html")
147 private fun modules
do
148 for mmodule
in mbuilder
.model
.mmodules
do
149 if mmodule
.name
== "<main>" then continue
150 var modulepage
= new NitdocModule(mmodule
, self)
151 modulepage
.render
.write_to_file
("{output_dir.to_s}/{mmodule.nitdoc_url}")
155 private fun classes
do
156 for mclass
in mbuilder
.model
.mclasses
do
157 var classpage
= new NitdocClass(mclass
, self)
158 classpage
.render
.write_to_file
("{output_dir.to_s}/{mclass.nitdoc_url}")
162 private fun quicksearch_list
do
163 var quicksearch
= new QuickSearch(self)
164 quicksearch
.render
.write_to_file
("{output_dir.to_s}/quicksearch-list.js")
168 # Nitdoc QuickSearch list generator
170 # Create a JSON object containing links to:
174 # All entities are grouped by name to make the research easier.
177 private var mmodules
= new HashSet[MModule]
178 private var mclasses
= new HashSet[MClass]
179 private var mpropdefs
= new HashMap[String, Set[MPropDef]]
181 init(ctx
: NitdocContext) do
182 for mmodule
in ctx
.mbuilder
.model
.mmodules
do
183 if mmodule
.name
== "<main>" then continue
186 for mclass
in ctx
.mbuilder
.model
.mclasses
do
187 if mclass
.visibility
< ctx
.min_visibility
then continue
190 for mproperty
in ctx
.mbuilder
.model
.mproperties
do
191 if mproperty
.visibility
< ctx
.min_visibility
then continue
192 if mproperty
isa MAttribute then continue
193 if not mpropdefs
.has_key
(mproperty
.name
) then
194 mpropdefs
[mproperty
.name
] = new HashSet[MPropDef]
196 mpropdefs
[mproperty
.name
].add_all
(mproperty
.mpropdefs
)
200 fun render
: Template do
201 var tpl
= new Template
202 tpl
.add
"var nitdocQuickSearchRawList=\{ "
203 for mmodule
in mmodules
do
204 tpl
.add
"\"{mmodule.name}\
":["
205 tpl
.add
"\{txt:\"{mmodule.full_name}\",url
:\
"{mmodule.nitdoc_url}\"\
},"
208 for mclass in mclasses do
209 var full_name = mclass.intro.mmodule.full_name
210 tpl.add "\
"{mclass.name}\":["
211 tpl.add "\
{txt:\"{full_name}\
",url:\"{mclass.nitdoc_url}\
"\},"
214 for mproperty
, mprops
in mpropdefs
do
215 tpl
.add
"\"{mproperty}\
":["
216 for mpropdef
in mprops
do
217 var full_name
= mpropdef
.mclassdef
.mclass
.full_name
218 tpl
.add
"\{txt:\"{full_name}\",url
:\
"{mpropdef.nitdoc_url}\"\
},"
228 # Define page structure and properties
229 abstract class NitdocPage
231 private var ctx: NitdocContext
232 private var model: Model
233 private var name_sorter = new MEntityNameSorter
235 init(ctx: NitdocContext) do
237 self.model = ctx.mbuilder.model
240 # Render the page as a html template
241 fun render: Template do
243 if ctx.opt_shareurl.value != null then
244 shareurl = ctx.opt_shareurl.value.as(not null)
249 tpl.title = tpl_title
250 tpl.shareurl = shareurl
251 tpl.topmenu = tpl_topmenu
253 tpl.footer = ctx.opt_custom_footer.value
254 tpl.body_attrs.add(new TagAttribute("data-bootstrap-share
", shareurl))
255 tpl.sidebar = tpl_sidebar
258 var tracker_url = ctx.opt_piwik_tracker.value
259 var site_id = ctx.opt_piwik_site_id.value
260 if tracker_url != null and site_id != null then
261 tpl.scripts.add new TplPiwikScript(tracker_url, site_id)
266 # Build page template
267 fun tpl_page: TplPage is abstract
269 # Build page sidebar if any
270 fun tpl_sidebar: nullable TplSidebar do return null
272 # Build page title string
273 fun tpl_title: String do
274 if ctx.opt_custom_title.value != null then
275 return ctx.opt_custom_title.value.to_s
280 # Build top menu template
281 fun tpl_topmenu: TplTopMenu do
282 var topmenu = new TplTopMenu
283 var brand = ctx.opt_custom_brand.value
284 if brand != null then
285 var tpl = new Template
286 tpl.add "<span
class='navbar-brand'>"
294 # Build page content template
295 fun tpl_content is abstract
297 # Clickable graphviz image using dot format
298 # return null if no graph for this page
299 fun tpl_graph(dot: FlatBuffer, name: String, title: String): nullable TplArticle do
300 if ctx.opt_nodot.value then return null
301 var output_dir = ctx.output_dir
302 var file = new OFStream.open("{output_dir}/{name}.dot
")
305 sys.system("\
{ test -f {output_dir}/{name}.png
&& test
-f
{output_dir}/{name}.s
.dot
&& diff
{output_dir}/{name}.dot
{output_dir}/{name}.s
.dot
>/dev
/null 2>&1 ; \
} || \
{ cp {output_dir}/{name}.dot
{output_dir}/{name}.s
.dot
&& dot
-Tpng -o
{output_dir}/{name}.png
-Tcmapx -o
{output_dir}/{name}.map
{output_dir}/{name}.s
.dot
; \
}")
306 var fmap = new IFStream.open("{output_dir}/{name}.map
")
307 var map = fmap.read_all
310 var article = new TplArticle.with_title("graph
", title)
311 var content = new Template
312 content.add "<img src
='{name}.png' usemap
='#{name}' style
='margin:auto' alt
='{title}'/>"
314 article.content = content
318 # A (source) link template for a given location
319 fun tpl_showsource(location: nullable Location): nullable String
321 if location == null then return null
322 var source = ctx.opt_source.value
323 if source == null then return "({location.file.filename.simplify_path})"
324 # THIS IS JUST UGLY ! (but there is no replace yet)
325 var x = source.split_with("%f
")
326 source = x.join(location.file.filename.simplify_path)
327 x = source.split_with("%l
")
328 source = x.join(location.line_start.to_s)
329 x = source.split_with("%L
")
330 source = x.join(location.line_end.to_s)
331 source = source.simplify_path
332 return " (<a target
='_blank' title
='Show source' href
=\
"{source.to_s}\">source
</a
>)"
335 # MClassDef description template
336 fun tpl_mclass_article(mclass: MClass, mclassdefs: Array[MClassDef]): TplArticle do
337 var article = new TplArticle(mclass.nitdoc_anchor)
338 var title = new Template
339 var icon = new TplIcon.with_icon("tag
")
340 icon.css_classes.add_all(mclass.intro.tpl_css_classes)
342 title.add mclass.tpl_link
343 title.add mclass.intro.tpl_signature
344 article.title = title
345 article.title_classes.add "signature
"
346 article.subtitle = mclass.tpl_declaration
347 article.summary_title = "{mclass.nitdoc_name}{mclass.tpl_signature.write_to_string}"
348 #article.subtitle = new Template
349 #article.subtitle.add mprop.intro.tpl_modifiers
350 #article.subtitle.add mprop.intro.tpl_namespace
351 var content = new Template
353 if not mclassdefs.has(mclass.intro) then
355 var intro = mclass.intro
356 var location = intro.location
357 var sourcelink = tpl_showsource(location)
358 var intro_def = intro.tpl_definition
359 intro_def.location = sourcelink
360 content.add intro_def
362 ctx.mainmodule.linearize_mclassdefs(mclassdefs)
363 for mclassdef in mclassdefs do
364 # add mclassdef full description
365 var location = mclassdef.location
366 var sourcelink = tpl_showsource(location)
367 var prop_def = mclassdef.tpl_definition.as(TplClassDefinition)
368 prop_def.location = sourcelink
369 for mpropdef in mclassdef.mpropdefs do
370 var intro = mpropdef.mproperty.intro
371 if mpropdef isa MAttributeDef then continue
372 if mpropdef.mproperty.visibility < ctx.min_visibility then continue
374 var lnk = new Template
375 lnk.add new TplLabel.with_classes(mpropdef.tpl_css_classes.to_a)
376 lnk.add mpropdef.tpl_link
377 if intro.mdoc != null then
379 lnk.add intro.mdoc.short_comment
381 if mpropdef.is_intro then
382 prop_def.intros.add new TplListItem.with_content(lnk)
384 prop_def.redefs.add new TplListItem.with_content(lnk)
389 article.content = content
393 # MProp description template
394 fun tpl_mprop_article(mprop: MProperty, mpropdefs: Array[MPropDef]): TplArticle do
395 var article = new TplArticle(mprop.intro.nitdoc_anchor)
396 var icon = new TplIcon.with_icon("tag
")
397 icon.css_classes.add_all(mprop.intro.tpl_css_classes)
398 var title = new Template
400 title.add mprop.nitdoc_name
401 title.add mprop.intro.tpl_signature
402 article.title = title
403 article.title_classes.add "signature
"
404 article.subtitle = mprop.tpl_declaration
405 article.summary_title = mprop.nitdoc_name
406 #article.subtitle = new Template
407 #article.subtitle.add mprop.intro.tpl_modifiers
408 #article.subtitle.add mprop.intro.tpl_namespace
409 var content = new Template
411 if not mpropdefs.has(mprop.intro) then
413 var intro = mprop.intro
414 var location = intro.location
415 var sourcelink = tpl_showsource(location)
416 var intro_def = intro.tpl_definition
417 intro_def.location = sourcelink
418 content.add intro_def
421 ctx.mainmodule.linearize_mpropdefs(mpropdefs)
422 for mpropdef in mpropdefs do
423 # add mpropdef description
424 var location = mpropdef.location
425 var sourcelink = tpl_showsource(location)
426 var prop_def = mpropdef.tpl_definition
427 prop_def.location = sourcelink
430 article.content = content
436 # Display a list of modules contained in program
440 init(ctx: NitdocContext) do super(ctx)
442 private var page = new TplPage
443 redef fun tpl_page do return page
445 private var sidebar = new TplSidebar
446 redef fun tpl_sidebar do return sidebar
448 redef fun tpl_title do
449 if ctx.opt_custom_title.value != null then
450 return ctx.opt_custom_title.value.to_s
456 redef fun tpl_topmenu do
458 topmenu.add_item(new TplLink("#", "Overview"), true)
459 topmenu
.add_item
(new TplLink("search.html", "Index"), false)
464 private fun tpl_intro
: TplSection do
465 var section
= new TplSection.with_title
("overview", tpl_title
)
466 var article
= new TplArticle("intro")
467 if ctx
.opt_custom_intro
.value
!= null then
468 article
.content
= ctx
.opt_custom_intro
.value
.to_s
470 section
.add_child article
475 private fun tpl_projects
(section
: TplSection) do
477 var ssection
= new TplSection.with_title
("projects", "Projects")
478 for mproject
in model
.mprojects
do
479 ssection
.add_child mproject
.tpl_article
481 section
.add_child ssection
484 redef fun tpl_content
do
487 tpl_page
.add_section top
492 # Display a list of modules, classes and properties
496 init(ctx
: NitdocContext) do super(ctx
)
498 private var page
= new TplPage
499 redef fun tpl_page
do return page
501 redef fun tpl_title
do return "Index"
503 redef fun tpl_topmenu
do
505 topmenu
.add_item
(new TplLink("index.html", "Overview"), false)
506 topmenu
.add_item
(new TplLink("#", "Index"), true)
510 redef fun tpl_content
do
511 var tpl
= new TplSearchPage("search_all")
512 var section
= new TplSection("search")
516 for mmodule
in modules_list
do
517 tpl
.modules
.add mmodule
.tpl_link
520 for mclass
in classes_list
do
521 tpl
.classes
.add mclass
.tpl_link
524 for mproperty
in mprops_list
do
526 m
.add mproperty
.intro
.tpl_link
528 m
.add mproperty
.intro
.mclassdef
.mclass
.tpl_link
532 section
.add_child tpl
533 tpl_page
.add_section section
536 # Extract mmodule list to display (sorted by name)
537 private fun modules_list
: Array[MModule] do
538 var sorted
= new Array[MModule]
539 for mmodule
in ctx
.mbuilder
.model
.mmodule_importation_hierarchy
do
540 if mmodule
.name
== "<main>" then continue
543 name_sorter
.sort
(sorted
)
547 # Extract mclass list to display (sorted by name)
548 private fun classes_list
: Array[MClass] do
549 var sorted
= new Array[MClass]
550 for mclass
in ctx
.mbuilder
.model
.mclasses
do
551 if mclass
.visibility
< ctx
.min_visibility
then continue
554 name_sorter
.sort
(sorted
)
558 # Extract mproperty list to display (sorted by name)
559 private fun mprops_list
: Array[MProperty] do
560 var sorted
= new Array[MProperty]
561 for mproperty
in ctx
.mbuilder
.model
.mproperties
do
562 if mproperty
.visibility
< ctx
.min_visibility
then continue
563 if mproperty
isa MAttribute then continue
566 name_sorter
.sort
(sorted
)
572 # Display the list of introduced and redefined classes in module
576 private var mmodule
: MModule
578 init(mmodule
: MModule, ctx
: NitdocContext) do
579 self.mmodule
= mmodule
583 private var page
= new TplPage
584 redef fun tpl_page
do return page
586 private var sidebar
= new TplSidebar
587 redef fun tpl_sidebar
do return sidebar
589 redef fun tpl_title
do return "{mmodule.nitdoc_name}"
591 redef fun tpl_topmenu
do
593 topmenu
.add_item
(new TplLink("index.html", "Overview"), false)
594 topmenu
.add_item
(new TplLink("#", "{mmodule.nitdoc_name}"), true)
595 topmenu
.add_item
(new TplLink("search.html", "Index"), false)
600 private fun tpl_intro
: TplSection do
601 var section
= new TplSection.with_title
(mmodule
.nitdoc_anchor
, tpl_title
)
602 section
.subtitle
= mmodule
.tpl_declaration
604 var article
= new TplArticle("intro")
605 var def
= mmodule
.tpl_definition
606 var location
= mmodule
.location
607 def
.location
= tpl_showsource
(location
)
608 article
.content
= def
609 section
.add_child article
613 # inheritance section
614 private fun tpl_inheritance
(parent
: TplSection) do
615 # Extract relevent modules
616 var nested
= mmodule
.in_nesting
.direct_greaters
.to_a
617 var imports
= mmodule
.in_importation
.greaters
618 if imports
.length
> 10 then imports
= mmodule
.in_importation
.direct_greaters
619 var clients
= mmodule
.in_importation
.smallers
620 if clients
.length
> 10 then clients
= mmodule
.in_importation
.direct_smallers
623 var section
= new TplSection.with_title
("inheritance", "Inheritance")
626 var mmodules
= new HashSet[MModule]
627 mmodules
.add_all nested
628 mmodules
.add_all imports
629 if clients
.length
< 10 then mmodules
.add_all clients
631 var graph
= tpl_dot
(mmodules
)
632 if graph
!= null then section
.add_child graph
635 if not nested
.is_empty
then
636 var lst
= nested
.to_a
638 section
.add_child tpl_list
("nesting", "Nested modules", lst
)
642 var lst
= new Array[MModule]
643 for dep
in imports
do
644 if dep
.is_fictive
then continue
645 if dep
== mmodule
then continue
648 if not lst
.is_empty
then
650 section
.add_child tpl_list
("imports", "Imports", lst
)
654 lst
= new Array[MModule]
655 for dep
in clients
do
656 if dep
.is_fictive
then continue
657 if dep
== mmodule
then continue
660 if not lst
.is_empty
then
662 section
.add_child tpl_list
("clients", "Clients", lst
)
665 parent
.add_child section
668 private fun tpl_list
(id
: String, title
: String, mmodules
: Array[MModule]): TplArticle do
669 var article
= new TplArticle.with_title
(id
, title
)
670 var list
= new TplList.with_classes
(["list-unstyled", "list-definition"])
671 for mmodule
in mmodules
do list
.elts
.add mmodule
.tpl_list_item
672 article
.content
= list
676 private fun tpl_mclasses
(parent
: TplSection) do
677 var mclassdefs
= new HashSet[MClassDef]
678 mclassdefs
.add_all mmodule
.in_nesting_intro_mclassdefs
(ctx
.min_visibility
)
679 mclassdefs
.add_all mmodule
.in_nesting_redef_mclassdefs
(ctx
.min_visibility
)
680 var mclasses2mdefs
= sort_by_mclass
(mclassdefs
)
681 var sorted_mclasses
= mclasses2mdefs
.keys
.to_a
682 name_sorter
.sort sorted_mclasses
685 var section
= new TplSection.with_title
("intros", "Introductions")
686 var intros
= mmodule
.in_nesting_intro_mclasses
(ctx
.min_visibility
)
687 var sorted_intros
= intros
.to_a
688 name_sorter
.sort
(sorted_intros
)
689 for mclass
in sorted_intros
do
690 if not mclasses2mdefs
.has_key
(mclass
) then continue
691 section
.add_child tpl_mclass_article
(mclass
, mclasses2mdefs
[mclass
].to_a
)
693 parent
.add_child section
696 section
= new TplSection.with_title
("redefs", "Refinements")
697 var redefs
= mmodule
.in_nesting_redef_mclasses
(ctx
.min_visibility
).to_a
698 name_sorter
.sort
(redefs
)
699 for mclass
in redefs
do
700 if intros
.has
(mclass
) then continue
701 if not mclasses2mdefs
.has_key
(mclass
) then continue
702 section
.add_child tpl_mclass_article
(mclass
, mclasses2mdefs
[mclass
].to_a
)
704 parent
.add_child section
707 redef fun tpl_content
do
711 tpl_page
.add_section top
714 # Genrate dot hierarchy for class inheritance
715 fun tpl_dot
(mmodules
: Collection[MModule]): nullable TplArticle do
716 var poset
= new POSet[MModule]
717 for mmodule
in mmodules
do
718 if mmodule
.is_fictive
then continue
719 poset
.add_node mmodule
720 for omodule
in mmodules
do
721 if mmodule
.is_fictive
then continue
722 poset
.add_node mmodule
723 if mmodule
.in_importation
< omodule
then
724 poset
.add_edge
(mmodule
, omodule
)
729 var op
= new FlatBuffer
730 var name
= "dep_{mmodule.name}"
731 op
.append
("digraph {name} \{ 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")
732 for mmodule
in poset
do
733 if mmodule
== self.mmodule
then
734 op
.append
("\"{mmodule.name}\
"[shape=box,margin=0.03];\n")
736 op
.append
("\"{mmodule.name}\
"[URL=\"{mmodule.nitdoc_url}\
"];\n")
738 for omodule
in poset
[mmodule
].direct_greaters
do
739 op
.append
("\"{mmodule.name}\
"->\"{omodule.name}\
";\n")
743 return tpl_graph
(op
, name
, "Dependency graph")
746 private fun sort_by_mclass
(mclassdefs
: Collection[MClassDef]): Map[MClass, Set[MClassDef]] do
747 var map
= new HashMap[MClass, Set[MClassDef]]
748 for mclassdef
in mclassdefs
do
749 var mclass
= mclassdef
.mclass
750 if not map
.has_key
(mclass
) then map
[mclass
] = new HashSet[MClassDef]
751 map
[mclass
].add mclassdef
758 # Display a list properties defined or redefined for this class
762 private var mclass
: MClass
763 private var mprops2mdefs
: Map[MProperty, Set[MPropDef]]
765 init(mclass
: MClass, ctx
: NitdocContext) do
768 var mpropdefs
= new HashSet[MPropDef]
769 mpropdefs
.add_all mclass
.intro_mpropdefs
(ctx
.min_visibility
)
770 mpropdefs
.add_all mclass
.redef_mpropdefs
(ctx
.min_visibility
)
771 mprops2mdefs
= sort_by_mproperty
(mpropdefs
)
774 private var page
= new TplPage
775 redef fun tpl_page
do return page
777 private var sidebar
= new TplSidebar
778 redef fun tpl_sidebar
do return sidebar
780 redef fun tpl_title
do return "{mclass.nitdoc_name}{mclass.tpl_signature.write_to_string}"
782 redef fun tpl_topmenu
do
785 if mclass
.public_owner
== null then
786 mmodule
= mclass
.intro_mmodule
788 mmodule
= mclass
.public_owner
.as(not null)
790 topmenu
.add_item
(new TplLink("index.html", "Overview"), false)
791 topmenu
.add_item
(new TplLink("{mmodule.nitdoc_url}", "{mmodule.nitdoc_name}"), false)
792 topmenu
.add_item
(new TplLink("#", "{mclass.nitdoc_name}"), true)
793 topmenu
.add_item
(new TplLink("search.html", "Index"), false)
797 # Property list to display in sidebar
798 fun tpl_sidebar_properties
do
799 var kind_map
= sort_by_kind
(mclass_inherited_mprops
)
800 var summary
= new TplList.with_classes
(["list-unstyled"])
802 tpl_sidebar_list
("Virtual types", kind_map
["type"].to_a
, summary
)
803 tpl_sidebar_list
("Constructors", kind_map
["init"].to_a
, summary
)
804 tpl_sidebar_list
("Methods", kind_map
["fun"].to_a
, summary
)
805 tpl_sidebar
.boxes
.add
new TplSideBox.with_content
("All properties", summary
)
808 private fun tpl_sidebar_list
(name
: String, mprops
: Array[MProperty], summary
: TplList) do
809 if mprops
.is_empty
then return
810 name_sorter
.sort
(mprops
)
811 var entry
= new TplListItem.with_content
(name
)
812 var list
= new TplList.with_classes
(["list-unstyled", "list-labeled"])
813 for mprop
in mprops
do
814 list
.add_li tpl_sidebar_item
(mprop
)
817 summary
.elts
.add entry
820 private fun tpl_sidebar_item
(mprop
: MProperty): Template do
821 var classes
= mprop
.intro
.tpl_css_classes
.to_a
822 if not mprops2mdefs
.has_key
(mprop
) then
823 classes
.add
"inherit"
824 var lnk
= new Template
825 lnk
.add
new TplLabel.with_classes
(classes
)
826 lnk
.add mprop
.intro
.tpl_link
829 var defs
= mprops2mdefs
[mprop
]
830 if defs
.has
(mprop
.intro
) then
835 var lnk
= new Template
836 lnk
.add
new TplLabel.with_classes
(classes
)
837 lnk
.add mprop
.intro
.tpl_anchor
841 private fun tpl_intro
: TplSection do
842 var section
= new TplSection.with_title
(mclass
.nitdoc_anchor
, tpl_title
)
843 section
.subtitle
= mclass
.tpl_declaration
844 var article
= new TplArticle("intro")
845 var intro
= mclass
.intro
846 var def
= intro
.tpl_definition
847 var location
= intro
.location
848 def
.location
= tpl_showsource
(location
)
849 article
.content
= def
850 section
.add_child article
854 private fun tpl_concerns
(section
: TplSection) do
855 var mmodules
= collect_mmodules
(mprops2mdefs
.keys
)
856 var owner_map
= sort_by_public_owner
(mmodules
)
857 var owners
= owner_map
.keys
.to_a
859 if not owners
.is_empty
then
860 var article
= new TplArticle.with_title
("concerns", "Concerns")
861 name_sorter
.sort owners
862 var list
= new TplList.with_classes
(["list-unstyled", "list-definition"])
863 for owner
in owners
do
864 var li
= new Template
865 li
.add owner
.tpl_anchor
866 if owner
.mdoc
!= null then
868 li
.add owner
.mdoc
.short_comment
870 var smmodules
= owner_map
[owner
].to_a
871 #if not smmodules.length >= 1 then
872 var slist
= new TplList.with_classes
(["list-unstyled", "list-definition"])
873 name_sorter
.sort smmodules
874 for mmodule
in smmodules
do
875 if mmodule
== owner
then continue
876 var sli
= new Template
877 sli
.add mmodule
.tpl_anchor
878 if mmodule
.mdoc
!= null then
880 sli
.add mmodule
.mdoc
.short_comment
888 article
.content
= list
889 section
.add_child article
893 private fun tpl_inheritance
(parent
: TplSection) do
895 var hparents
= new HashSet[MClass]
896 for c
in mclass
.in_hierarchy
(ctx
.mainmodule
).direct_greaters
do
897 if c
.visibility
< ctx
.min_visibility
then continue
902 var hancestors
= new HashSet[MClass]
903 for c
in mclass
.in_hierarchy
(ctx
.mainmodule
).greaters
do
904 if c
== mclass
then continue
905 if c
.visibility
< ctx
.min_visibility
then continue
906 if hparents
.has
(c
) then continue
911 var hchildren
= new HashSet[MClass]
912 for c
in mclass
.in_hierarchy
(ctx
.mainmodule
).direct_smallers
do
913 if c
.visibility
< ctx
.min_visibility
then continue
918 var hdescendants
= new HashSet[MClass]
919 for c
in mclass
.in_hierarchy
(ctx
.mainmodule
).smallers
do
920 if c
== mclass
then continue
921 if c
.visibility
< ctx
.min_visibility
then continue
922 if hchildren
.has
(c
) then continue
927 var section
= new TplSection.with_title
("inheritance", "Inheritance")
930 var mclasses
= new HashSet[MClass]
931 mclasses
.add_all hancestors
932 mclasses
.add_all hparents
933 if hchildren
.length
< 10 then mclasses
.add_all hchildren
934 if hdescendants
.length
< 10 then mclasses
.add_all hdescendants
936 var graph
= tpl_dot
(mclasses
)
937 if graph
!= null then section
.add_child graph
940 if not hparents
.is_empty
then
941 var lst
= hparents
.to_a
943 section
.add_child tpl_list
("parents", "Parents", lst
)
947 if not hancestors
.is_empty
then
948 var lst
= hancestors
.to_a
950 section
.add_child tpl_list
("ancestors", "Ancestors", lst
)
954 if not hchildren
.is_empty
and hchildren
.length
< 15 then
955 var lst
= hchildren
.to_a
957 section
.add_child tpl_list
("children", "Children", lst
)
961 if not hdescendants
.is_empty
and hchildren
.length
< 15 then
962 var lst
= hdescendants
.to_a
964 section
.add_child tpl_list
("descendants", "Descendants", lst
)
967 parent
.add_child section
970 private fun tpl_list
(id
: String, title
: String, elts
: Array[MClass]): TplArticle do
971 var article
= new TplArticle.with_title
(id
, title
)
972 var list
= new TplList.with_classes
(["list-unstyled", "list-definition"])
973 for elt
in elts
do list
.elts
.add elt
.tpl_list_item
974 article
.content
= list
978 private fun tpl_properties
(parent
: TplSection) do
979 var mod_map
= sort_by_mmodule
(mprops2mdefs
.keys
)
980 var owner_map
= sort_by_public_owner
(mod_map
.keys
)
981 var owners
= owner_map
.keys
.to_a
983 for owner
in owners
do
984 var section
= new TplSection(owner
.nitdoc_anchor
)
985 var title
= new Template
986 title
.add
"Introductions in "
987 title
.add owner
.tpl_link
988 section
.title
= title
989 section
.summary_title
= "In {owner.nitdoc_name}"
990 for mmodule
in owner_map
[owner
] do
992 var mprops
= mod_map
[mmodule
]
993 var kind_map
= sort_by_kind
(mprops
)
996 var elts
= kind_map
["type"].to_a
997 name_sorter
.sort
(elts
)
999 var defs
= mprops2mdefs
[elt
].to_a
1000 section
.add_child tpl_mprop_article
(elt
, defs
)
1004 elts
= kind_map
["init"].to_a
1005 name_sorter
.sort
(elts
)
1007 var defs
= mprops2mdefs
[elt
].to_a
1008 section
.add_child tpl_mprop_article
(elt
, defs
)
1012 elts
= kind_map
["fun"].to_a
1013 name_sorter
.sort
(elts
)
1015 var defs
= mprops2mdefs
[elt
].to_a
1016 section
.add_child tpl_mprop_article
(elt
, defs
)
1019 parent
.add_child section
1023 redef fun tpl_content
do
1024 tpl_sidebar_properties
1027 tpl_inheritance
(top
)
1029 tpl_page
.add_section top
1032 private fun sort_by_mproperty
(mpropdefs
: Collection[MPropDef]): Map[MProperty, Set[MPropDef]] do
1033 var map
= new HashMap[MProperty, Set[MPropDef]]
1034 for mpropdef
in mpropdefs
do
1035 var mproperty
= mpropdef
.mproperty
1036 if not map
.has_key
(mproperty
) then map
[mproperty
] = new HashSet[MPropDef]
1037 map
[mproperty
].add mpropdef
1042 private fun sort_by_mmodule
(mprops
: Collection[MProperty]): Map[MModule, Set[MProperty]] do
1043 var map
= new HashMap[MModule, Set[MProperty]]
1044 for mprop
in mprops
do
1045 var mpropdefs
= mprops2mdefs
[mprop
].to_a
1046 ctx
.mainmodule
.linearize_mpropdefs
(mpropdefs
)
1047 var mmodule
= mpropdefs
.first
.mclassdef
.mmodule
1048 if not map
.has_key
(mmodule
) then map
[mmodule
] = new HashSet[MProperty]
1049 map
[mmodule
].add mprop
1054 private fun sort_by_kind
(mprops
: Collection[MProperty]): Map[String, Set[MProperty]] do
1055 var map
= new HashMap[String, Set[MProperty]]
1056 map
["type"] = new HashSet[MProperty]
1057 map
["init"] = new HashSet[MProperty]
1058 map
["fun"] = new HashSet[MProperty]
1059 for mprop
in mprops
do
1060 if mprop
isa MVirtualTypeProp then
1061 map
["type"].add mprop
1062 else if mprop
isa MMethod then
1063 if mprop
.is_init
then
1064 map
["init"].add mprop
1066 map
["fun"].add mprop
1073 private fun mclass_inherited_mprops
: Set[MProperty] do
1074 var res
= new HashSet[MProperty]
1075 var local
= mclass
.local_mproperties
(ctx
.min_visibility
)
1076 for mprop
in mclass
.inherited_mproperties
(ctx
.mainmodule
, ctx
.min_visibility
) do
1077 if local
.has
(mprop
) then continue
1078 #if mprop isa MMethod and mprop.is_init then continue
1079 if mprop
.intro
.mclassdef
.mclass
.name
== "Object" and
1080 (mprop
.visibility
== protected_visibility
or
1081 mprop
.intro
.mclassdef
.mmodule
.name
!= "kernel") then continue
1088 private fun collect_mmodules
(mprops
: Collection[MProperty]): Set[MModule] do
1089 var res
= new HashSet[MModule]
1090 for mprop
in mprops
do
1091 if mprops2mdefs
.has_key
(mprop
) then
1092 for mpropdef
in mprops2mdefs
[mprop
] do res
.add mpropdef
.mclassdef
.mmodule
1098 private fun sort_by_public_owner
(mmodules
: Collection[MModule]): Map[MModule, Set[MModule]] do
1099 var map
= new HashMap[MModule, Set[MModule]]
1100 for mmodule
in mmodules
do
1102 if mmodule
.public_owner
!= null then owner
= mmodule
.public_owner
.as(not null)
1103 if not map
.has_key
(owner
) then map
[owner
] = new HashSet[MModule]
1104 map
[owner
].add mmodule
1109 # Generate dot hierarchy for classes
1110 fun tpl_dot
(mclasses
: Collection[MClass]): nullable TplArticle do
1111 var poset
= new POSet[MClass]
1113 for mclass
in mclasses
do
1114 poset
.add_node mclass
1115 for oclass
in mclasses
do
1116 poset
.add_node oclass
1117 if mclass
.in_hierarchy
(ctx
.mainmodule
) < oclass
then
1118 poset
.add_edge
(mclass
, oclass
)
1123 var op
= new FlatBuffer
1124 var name
= "dep_{mclass.name}"
1125 op
.append
("digraph {name} \{ 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")
1128 op
.append
("\"{c.name}\
"[shape=box,margin=0.03];\n")
1130 op
.append
("\"{c.name}\
"[URL=\"{c.nitdoc_url}\
"];\n")
1132 for c2
in poset
[c
].direct_greaters
do
1133 op
.append
("\"{c.name}\
"->\"{c2.name}\
";\n")
1137 return tpl_graph
(op
, name
, "Inheritance graph")