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 # Documentation generator for the nit language.
16 # Generate API documentation in HTML format from nit source code.
20 import modelize_property
22 # The NitdocContext contains all the knowledge used for doc generation
25 private var toolcontext
= new ToolContext
26 private var model
: Model
27 private var mbuilder
: ModelBuilder
28 private var mainmodule
: MModule
29 private var class_hierarchy
: POSet[MClass]
30 private var arguments
: Array[String]
31 private var output_dir
: nullable String
32 private var dot_dir
: nullable String
33 private var share_dir
: nullable String
34 private var source
: nullable String
35 private var min_visibility
: MVisibility
37 private var opt_dir
= new OptionString("Directory where doc is generated", "-d", "--dir")
38 private var opt_source
= new OptionString("What link for source (%f for filename, %l for first line, %L for last line)", "--source")
39 private var opt_sharedir
= new OptionString("Directory containing the nitdoc files", "--sharedir")
40 private var opt_nodot
= new OptionBool("Do not generate graphes with graphiviz", "--no-dot")
41 private var opt_private
: OptionBool = new OptionBool("Generate the private API", "--private")
43 private var opt_custom_title
: OptionString = new OptionString("Title displayed in the top of the Overview page and as suffix of all page names", "--custom-title")
44 private var opt_custom_menu_items
: OptionString = new OptionString("Items displayed in menu before the 'Overview' item (Each item must be enclosed in 'li' tags)", "--custom-menu-items")
45 private var opt_custom_overview_text
: OptionString = new OptionString("Text displayed as introduction of Overview page before the modules list", "--custom-overview-text")
46 private var opt_custom_footer_text
: OptionString = new OptionString("Text displayed as footer of all pages", "--custom-footer-text")
49 self.arguments
= toolcontext
.option_context
.rest
50 toolcontext
.option_context
.options
.clear
51 toolcontext
.option_context
.add_option
(opt_dir
)
52 toolcontext
.option_context
.add_option
(opt_source
)
53 toolcontext
.option_context
.add_option
(opt_sharedir
)
54 toolcontext
.option_context
.add_option
(opt_nodot
)
55 toolcontext
.option_context
.add_option
(opt_private
)
56 toolcontext
.option_context
.add_option
(opt_custom_title
)
57 toolcontext
.option_context
.add_option
(opt_custom_footer_text
)
58 toolcontext
.option_context
.add_option
(opt_custom_overview_text
)
59 toolcontext
.option_context
.add_option
(opt_custom_menu_items
)
60 toolcontext
.process_options
62 if arguments
.length
< 1 then
63 toolcontext
.option_context
.usage
68 mbuilder
= new ModelBuilder(model
, toolcontext
)
69 # Here we load an process all modules passed on the command line
70 var mmodules
= mbuilder
.parse
(arguments
)
71 if mmodules
.is_empty
then return
74 if mmodules
.length
== 1 then
75 mainmodule
= mmodules
.first
77 # We need a main module, so we build it by importing all modules
78 mainmodule
= new MModule(model
, null, "<main>", new Location(null, 0, 0, 0, 0))
79 mainmodule
.set_imported_mmodules
(mmodules
)
81 self.class_hierarchy
= mainmodule
.flatten_mclass_hierarchy
85 private fun process_options
do
86 if not opt_dir
.value
is null then
87 output_dir
= opt_dir
.value
91 if not opt_sharedir
.value
is null then
92 share_dir
= opt_sharedir
.value
94 var dir
= "NIT_DIR".environ
96 dir
= "{sys.program_name.dirname}/../share/nitdoc"
98 dir
= "{dir}/share/nitdoc"
101 if share_dir
is null then
102 print
"Error: Cannot locate nitdoc share files. Uses --sharedir or envvar NIT_DIR"
105 dir
= "{share_dir.to_s}/scripts/js-facilities.js"
106 if share_dir
is null then
107 print
"Error: Invalid nitdoc share files. Check --sharedir or envvar NIT_DIR"
111 if opt_private
.value
then
112 min_visibility
= none_visibility
114 min_visibility
= protected_visibility
117 source
= opt_source
.value
120 fun generate_nitdoc
do
121 # Create destination dir if it's necessary
122 if not output_dir
.file_exists
then output_dir
.mkdir
123 sys
.system
("cp -r {share_dir.to_s}/* {output_dir.to_s}/")
125 if not opt_nodot
.value
then self.dot_dir
= output_dir
.to_s
133 private fun overview
do
134 var overviewpage
= new NitdocOverview(self, dot_dir
)
135 overviewpage
.save
("{output_dir.to_s}/index.html")
138 private fun fullindex
do
139 var fullindex
= new NitdocFullindex(self)
140 fullindex
.save
("{output_dir.to_s}/full-index.html")
143 private fun modules
do
144 for mmodule
in model
.mmodules
do
145 if mmodule
.name
== "<main>" then continue
146 var modulepage
= new NitdocModule(mmodule
, self, dot_dir
)
147 modulepage
.save
("{output_dir.to_s}/{mmodule.url}")
151 private fun classes
do
152 for mclass
in mbuilder
.model
.mclasses
do
153 var classpage
= new NitdocClass(mclass
, self, dot_dir
, source
)
154 classpage
.save
("{output_dir.to_s}/{mclass.url}")
158 private fun quicksearch_list
do
159 var file
= new OFStream.open
("{output_dir.to_s}/quicksearch-list.js")
160 file
.write
("var entries = \{ ")
161 for mmodule
in model
.mmodules
do
162 file
.write
("\"{mmodule.name}\
": [")
163 file
.write
("\{txt: \"{mmodule.name}\", url
:\
"{mmodule.url}\" \
},")
166 for mclass in model.mclasses do
167 if mclass.visibility < min_visibility then continue
168 file.write("\
"{mclass.name}\": [")
169 file.write("\
{txt: \"{mclass.name}\
", url:\"{mclass.url}\
" \},")
172 var name2mprops
= new HashMap[String, Set[MPropDef]]
173 for mproperty
in model
.mproperties
do
174 if mproperty
.visibility
< min_visibility
then continue
175 if mproperty
isa MAttribute then continue
176 if not name2mprops
.has_key
(mproperty
.name
) then name2mprops
[mproperty
.name
] = new HashSet[MPropDef]
177 name2mprops
[mproperty
.name
].add_all
(mproperty
.mpropdefs
)
179 for mproperty
, mpropdefs
in name2mprops
do
180 file
.write
("\"{mproperty}\
": [")
181 for mpropdef
in mpropdefs
do
182 file
.write
("\{txt: \"{mpropdef.full_name}\", url
:\
"{mpropdef.url}\" \
},")
193 abstract class NitdocPage
195 var dot_dir: nullable String
196 var source: nullable String
197 var ctx: NitdocContext
199 init(ctx: NitdocContext) do
203 protected fun head do
204 append("<meta charset
='utf-8'/>")
205 append("<script
type='text/javascript' src
='scripts/jquery-1.7.1.min.js'></script
>")
206 append("<script
type='text/javascript' src
='quicksearch-list.js'></script
>")
207 append("<script
type='text/javascript' src
='scripts/base64.js'></script
>")
208 append("<script
type='text/javascript' src
='scripts/github.js'></script
>")
209 append("<script
type='text/javascript' src
='scripts/js-facilities.js'></script
>")
210 append("<link rel
='stylesheet' href
='styles/main.css' type='text/css' media
='screen'/>")
212 if ctx.opt_custom_title.value != null then
213 title = " | {ctx.opt_custom_title.value.to_s}"
215 append("<title
>{self.title}{title}</title
>")
218 protected fun menu do
219 if ctx.opt_custom_menu_items.value != null then
220 append(ctx.opt_custom_menu_items.value.to_s)
224 protected fun title: String is abstract
226 protected fun header do
228 append("<nav
class='main'>")
236 protected fun content is abstract
238 protected fun footer do
239 if ctx.opt_custom_footer_text.value != null then
240 append("<footer
>{ctx.opt_custom_footer_text.value.to_s}</footer
>")
244 # Generate a clickable graphviz image using a dot content
245 protected fun generate_dot(dot: String, name: String, alt: String) do
246 var output_dir = dot_dir
247 if output_dir == null then return
248 var file = new OFStream.open("{output_dir}/{name}.dot
")
251 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
; \
}")
252 append("<article
class='graph'>")
253 append("<img src
='{name}.png' usemap
='#{name}' style
='margin:auto' alt
='{alt}'/>")
255 var fmap = new IFStream.open("{output_dir}/{name}.map
")
256 append(fmap.read_all)
260 # Add a (source) link for a given location
261 protected fun show_source(l: Location): String
263 if source == null then
264 return "({l.file.filename.simplify_path})"
266 # THIS IS JUST UGLY ! (but there is no replace yet)
267 var x = source.split_with("%f
")
268 source = x.join(l.file.filename.simplify_path)
269 x = source.split_with("%l
")
270 source = x.join(l.line_start.to_s)
271 x = source.split_with("%L
")
272 source = x.join(l.line_end.to_s)
273 return " (<a href
=\
"{source.to_s}\">source
</a
>)"
277 # Render the page as a html string
278 protected fun render do
279 append("<!DOCTYPE html
>")
285 append("<div
class='page'>")
292 # Append a string to the page
293 fun append(s: String) do out.write(s)
295 # Save html page in the specified file
296 fun save(file: String) do
297 self.out = new OFStream.open(file)
301 private var out: nullable OFStream
307 private var mbuilder: ModelBuilder
308 private var mmodules = new Array[MModule]
310 init(ctx: NitdocContext, dot_dir: nullable String) do
312 self.mbuilder = ctx.mbuilder
313 self.dot_dir = dot_dir
315 var mmodules = new HashSet[MModule]
316 for mmodule in mbuilder.model.mmodules do
317 if mmodule.name == "<main
>" then continue
318 var owner = mmodule.public_owner
319 if owner != null then
322 mmodules.add(mmodule)
326 var sorter = new MModuleNameSorter
327 self.mmodules.add_all(mmodules)
328 sorter.sort(self.mmodules)
331 redef fun title do return "Overview"
335 append("<li
class='current'>Overview</li
>")
336 append("<li
><a href
='full-index.html'>Full Index</a
></li
>")
340 append("<div
class='content fullpage'>")
341 var title = "Overview"
342 if ctx.opt_custom_title.value != null then
343 title = ctx.opt_custom_title.value.to_s
345 append("<h1
>{title}</h1
>")
347 if ctx.opt_custom_overview_text.value != null then
348 text = ctx.opt_custom_overview_text.value.to_s
350 append("<article
class='overview'>{text}</article
>")
351 append("<article
class='overview'>")
353 append("<h2
>Modules</h2
>")
355 for mmodule in mmodules do
356 if mbuilder.mmodule2nmodule.has_key(mmodule) then
357 var amodule = mbuilder.mmodule2nmodule[mmodule]
359 mmodule.html_link(self)
360 append(" 
;{amodule.short_comment}</li
>")
370 private fun process_generate_dot do
371 # build poset with public owners
372 var poset = new POSet[MModule]
373 for mmodule in mmodules do
374 poset.add_node(mmodule)
375 for omodule in mmodules do
376 if mmodule == omodule then continue
377 if mmodule.in_importation < omodule then
378 poset.add_node(omodule)
379 poset.add_edge(mmodule, omodule)
385 op.append("digraph dep \
{ 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
")
386 for mmodule in poset do
387 op.append("\
"{mmodule.name}\"[URL=\
"{mmodule.url}\"];\n
")
388 for omodule in poset[mmodule].direct_greaters do
389 op.append("\
"{mmodule.name}\"->\
"{omodule.name}\";\n
")
393 generate_dot(op.to_s, "dep
", "Modules hierarchy
")
397 # The full index page
398 class NitdocFullindex
401 init(ctx: NitdocContext) do
406 redef fun title do return "Full Index"
410 append("<li
><a href
='index.html'>Overview</a
></li
>")
411 append("<li
class='current'>Full Index</li
>")
415 append("<div
class='content fullpage'>")
416 append("<h1
>Full Index</h1
>")
423 # Add to content modules column
424 private fun module_column do
425 var sorted = ctx.mbuilder.model.mmodule_importation_hierarchy.to_a
426 var sorter = new MModuleNameSorter
428 append("<article
class='modules filterable'>")
429 append("<h2
>Modules</h2
>")
431 for mmodule in sorted do
433 mmodule.html_link(self)
440 # Add to content classes modules
441 private fun classes_column do
442 var sorted = ctx.mbuilder.model.mclasses
443 var sorter = new MClassNameSorter
445 append("<article
class='modules filterable'>")
446 append("<h2
>Classes</h2
>")
448 for mclass in sorted do
449 if mclass.visibility < ctx.min_visibility then continue
451 mclass.html_link(self)
458 # Insert the properties column of fullindex page
459 private fun properties_column do
460 var sorted = ctx.mbuilder.model.mproperties
461 var sorter = new MPropertyNameSorter
463 append("<article
class='modules filterable'>")
464 append("<h2
>Properties</h2
>")
466 for mproperty in sorted do
467 if mproperty.visibility < ctx.min_visibility then continue
468 if mproperty isa MAttribute then continue
470 mproperty.intro.html_link(self)
472 mproperty.intro.mclassdef.mclass.html_link(self)
485 private var mmodule: MModule
486 private var mbuilder: ModelBuilder
488 init(mmodule: MModule, ctx: NitdocContext, dot_dir: nullable String) do
490 self.mmodule = mmodule
491 self.mbuilder = ctx.mbuilder
492 self.dot_dir = dot_dir
496 if mbuilder.mmodule2nmodule.has_key(mmodule) then
497 var nmodule = mbuilder.mmodule2nmodule[mmodule]
498 return "{mmodule.name} module | {nmodule.short_comment}"
500 return "{mmodule.name} module"
506 append("<li
><a href
='index.html'>Overview</a
></li
>")
507 append("<li
class='current'>{mmodule.name}</li
>")
508 append("<li
><a href
='full-index.html'>Full Index</a
></li
>")
513 append("<div
class='content'>")
514 append("<h1
>{mmodule.name}</h1
>")
515 append("<div
class='subtitle info'>")
516 mmodule.html_signature(self)
518 mmodule.html_full_comment(self)
525 private fun process_generate_dot do
526 # build poset with public owners
527 var poset = new POSet[MModule]
528 for mmodule in self.mmodule.in_importation.poset do
529 if mmodule.name == "<main
>" then continue
530 if mmodule.public_owner != null then continue
531 if not mmodule.in_importation < self.mmodule and not self.mmodule.in_importation < mmodule and mmodule != self.mmodule then continue
532 poset.add_node(mmodule)
533 for omodule in mmodule.in_importation.poset do
534 if mmodule == omodule then continue
535 if omodule.name == "<main
>" then continue
536 if omodule.public_owner != null then continue
537 if mmodule.in_importation < omodule then
538 poset.add_node(omodule)
539 poset.add_edge(mmodule, omodule)
545 var name = "dep_
{mmodule.name}"
546 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
")
547 for mmodule in poset do
548 if mmodule == self.mmodule then
549 op.append("\
"{mmodule.name}\"[shape
=box
,margin
=0.03];\n
")
551 op.append("\
"{mmodule.name}\"[URL=\
"{mmodule.url}\"];\n
")
553 for omodule in poset[mmodule].direct_greaters do
554 op.append("\
"{mmodule.name}\"->\
"{omodule.name}\";\n
")
558 generate_dot(op.to_s, name, "Dependency graph
for module {mmodule.name}")
561 private fun sidebar do
562 append("<div
class='menu'>")
564 append("<h3
>Module Hierarchy</h3
>")
565 var dependencies = new Array[MModule]
566 for dep in mmodule.in_importation.greaters do
567 if dep == mmodule or dep.public_owner != null then continue
568 dependencies.add(dep)
570 if dependencies.length > 0 then
571 append("<h4
>All dependencies
</h4
>")
572 display_module_list(dependencies)
574 var clients = new Array[MModule]
575 for dep in mmodule.in_importation.smallers do
576 if dep.name == "<main
>" then continue
577 if dep == mmodule or dep.public_owner != null then continue
580 if clients.length > 0 then
581 append("<h4
>All clients
</h4
>")
582 display_module_list(clients)
585 if ctx.min_visibility < protected_visibility then
586 if mmodule.in_nesting.direct_greaters.length > 0 then
588 append("<h3
>Nested Modules</h3
>")
589 display_module_list(mmodule.in_nesting.direct_greaters.to_a)
596 private fun display_module_list(list: Array[MModule]) do
598 var sorter = new MModuleNameSorter
608 # display the class column
609 private fun classes do
610 var intro_mclasses = mmodule.intro_mclasses
611 var redef_mclasses = mmodule.redef_mclasses
612 var all_mclasses = new HashSet[MClass]
613 for m in mmodule.in_nesting.greaters do
614 all_mclasses.add_all(m.intro_mclasses)
615 all_mclasses.add_all(m.redef_mclasses)
617 all_mclasses.add_all(intro_mclasses)
618 all_mclasses.add_all(redef_mclasses)
620 var sorted = new Array[MClass]
621 sorted.add_all(all_mclasses)
622 var sorter = new MClassNameSorter
624 append("<div
class='module'>")
625 append("<article
class='classes filterable'>")
626 append("<h2
>Classes</h2
>")
629 if c.visibility < ctx.min_visibility then continue
630 if redef_mclasses.has(c) and c.intro_mmodule.public_owner != mmodule then
631 append("<li
class='redef'>")
632 append("<span title
='refined in this module'>R
</span
>")
634 append("<li
class='intro'>")
635 append("<span title
='introduced in this module'>I
</span
>")
645 # display the property column
646 private fun properties do
648 var mpropdefs = new HashSet[MPropDef]
649 for m in mmodule.in_nesting.greaters do
650 for c in m.mclassdefs do mpropdefs.add_all(c.mpropdefs)
652 for c in mmodule.mclassdefs do mpropdefs.add_all(c.mpropdefs)
653 var sorted = mpropdefs.to_a
654 var sorter = new MPropDefNameSorter
656 # display properties in one column
657 append("<article
class='properties filterable'>")
658 append("<h2
>Properties</h2
>")
660 for mprop in sorted do
661 if mprop isa MAttributeDef then continue
662 if mprop.mproperty.visibility < ctx.min_visibility then continue
663 mprop.html_list_item(self)
674 private var mclass: MClass
675 private var vtypes = new HashSet[MVirtualTypeDef]
676 private var consts = new HashSet[MMethodDef]
677 private var meths = new HashSet[MMethodDef]
678 private var inherited = new HashSet[MPropDef]
680 init(mclass: MClass, ctx: NitdocContext, dot_dir: nullable String, source: nullable String) do
683 self.dot_dir = dot_dir
686 for mclassdef in mclass.mclassdefs do
687 for mpropdef in mclassdef.mpropdefs do
688 if mpropdef.mproperty.visibility < ctx.min_visibility then continue
689 if mpropdef isa MVirtualTypeDef then vtypes.add(mpropdef)
690 if mpropdef isa MMethodDef then
691 if mpropdef.mproperty.is_init then
699 # get inherited properties
700 for pclass in mclass.in_hierarchy(ctx.mainmodule).greaters do
701 if pclass == mclass then continue
702 for pclassdef in pclass.mclassdefs do
703 for mprop in pclassdef.intro_mproperties do
704 var mpropdef = mprop.intro
705 if mprop.visibility < ctx.min_visibility then continue
706 if mpropdef isa MVirtualTypeDef then vtypes.add(mpropdef)
707 if mpropdef isa MMethodDef then
708 if mpropdef.mproperty.is_init then
714 inherited.add(mpropdef)
721 var nclass = ctx.mbuilder.mclassdef2nclassdef[mclass.intro]
722 if nclass isa AStdClassdef then
723 return "{mclass.name} class | {nclass.short_comment}"
725 return "{mclass.name} class"
731 append("<li
><a href
='index.html'>Overview</a
></li
>")
732 var public_owner = mclass.public_owner
733 if public_owner is null then
735 mclass.intro_mmodule.html_link(self)
739 public_owner.html_link(self)
742 append("<li
class='current'>{mclass.name}</li
>")
743 append("<li
><a href
='full-index.html'>Full Index</a
></li
>")
747 append("<div
class='menu'>")
751 append("<div
class='content'>")
756 private fun properties_column do
757 var sorter = new MPropDefNameSorter
758 append("<nav
class='properties filterable'>")
759 append("<h3
>Properties</h3
>")
761 if vtypes.length > 0 then
762 var vts = new Array[MVirtualTypeDef]
765 append("<h4
>Virtual Types</h4
>")
768 mprop.html_sidebar_item(self)
773 if consts.length > 0 then
774 var cts = new Array[MMethodDef]
777 append("<h4
>Constructors</h4
>")
780 if mprop.mproperty.name == "init" and mprop.mclassdef.mclass != mclass then continue
781 mprop.html_sidebar_item(self)
786 if meths.length > 0 then
787 var mts = new Array[MMethodDef]
790 append("<h4
>Methods</h4
>")
793 if mclass.name != "Object" and mprop.mproperty.intro_mclassdef.mclass.name == "Object" and mprop.mproperty.visibility <= protected_visibility then continue
794 mprop.html_sidebar_item(self)
801 private fun inheritance_column do
802 var sorted = new Array[MClass]
803 var sorterp = new MClassNameSorter
805 append("<h3
>Inheritance</h3
>")
806 var greaters = mclass.in_hierarchy(ctx.mainmodule).greaters.to_a
807 if greaters.length > 1 then
808 ctx.mainmodule.linearize_mclasses(greaters)
809 append("<h4
>Superclasses</h4
>")
811 for sup in greaters do
812 if sup == mclass then continue
819 var smallers = mclass.in_hierarchy(ctx.mainmodule).smallers.to_a
820 var direct_smallers = mclass.in_hierarchy(ctx.mainmodule).direct_smallers.to_a
821 if smallers.length <= 1 then
822 append("<h4
>No Known Subclasses</h4
>")
823 else if smallers.length <= 100 then
824 ctx.mainmodule.linearize_mclasses(smallers)
825 append("<h4
>Subclasses</h4
>")
827 for sub in smallers do
828 if sub == mclass then continue
834 else if direct_smallers.length <= 100 then
835 ctx.mainmodule.linearize_mclasses(direct_smallers)
836 append("<h4
>Direct Subclasses Only</h4
>")
838 for sub in direct_smallers do
839 if sub == mclass then continue
846 append("<h4
>Too much
Subclasses to list
</h4
>")
851 private fun class_doc do
853 append("<h1
>{mclass.signature}</h1
>")
854 append("<div
class='subtitle info'>")
855 mclass.html_full_signature(self)
858 var nclass = ctx.mbuilder.mclassdef2nclassdef[mclass.intro]
859 append("<section
class='description'>")
863 var concern2meths = new ArrayMap[MModule, Array[MMethodDef]]
864 var sorted_meths = new Array[MMethodDef]
865 var sorted = new Array[MModule]
866 sorted_meths.add_all(meths)
867 ctx.mainmodule.linearize_mpropdefs(sorted_meths)
869 if inherited.has(meth) then continue
870 var mmodule = meth.mclassdef.mmodule
871 if not concern2meths.has_key(mmodule) then
873 concern2meths[mmodule] = new Array[MMethodDef]
875 concern2meths[mmodule].add(meth)
877 var sections = new ArrayMap[MModule, Array[MModule]]
878 for mmodule in concern2meths.keys do
879 var owner = mmodule.public_owner
880 if owner == null then owner = mmodule
881 if not sections.has_key(owner) then sections[owner] = new Array[MModule]
882 if owner != mmodule then sections[owner].add(mmodule)
884 append("<section
class='concerns'>")
885 append("<h2
class='section-header'>Concerns</h2
>")
887 for owner, mmodules in sections do
888 var nowner = ctx.mbuilder.mmodule2nmodule[owner]
890 if nowner.short_comment.is_empty then
891 append("<a href
=\
"#{owner.anchor}\">{owner.name}</a
>")
893 append("<a href
=\
"#{owner.anchor}\">{owner.name}</a
>: {nowner.short_comment}")
895 if not mmodules.is_empty then
897 for mmodule in mmodules do
898 var nmodule = ctx.mbuilder.mmodule2nmodule[mmodule]
899 if nmodule.short_comment.is_empty then
900 append("<li
><a href
=\
"#{mmodule.anchor}\">{mmodule.name}</a
></li
>")
902 append("<li
><a href
=\
"#{mmodule.anchor}\">{mmodule.name}</a
>: {nmodule.short_comment}</li
>")
912 var prop_sorter = new MPropDefNameSorter
913 var lmmodule = new List[MModule]
914 # virtual and formal types
915 var local_vtypes = new Array[MVirtualTypeDef]
916 for vt in vtypes do if not inherited.has(vt) then local_vtypes.add(vt)
917 if local_vtypes.length > 0 or mclass.arity > 0 then
918 append("<section
class='types'>")
919 append("<h2
>Formal and Virtual Types</h2
>")
921 if mclass.arity > 0 and nclass isa AStdClassdef then
922 for ft, bound in mclass.parameter_types do
923 append("<article id
='FT_{ft}'>")
924 append("<h3
class='signature'>{ft}: ")
925 bound.html_link(self)
927 append("<div
class=\
"info\">formal generic
type</div
>")
932 prop_sorter.sort(local_vtypes)
933 for prop in local_vtypes do prop.html_full_desc(self)
937 var local_consts = new Array[MMethodDef]
938 for const in consts do if not inherited.has(const) then local_consts.add(const)
939 prop_sorter.sort(local_consts)
940 if local_consts.length > 0 then
941 append("<section
class='constructors'>")
942 append("<h2
class='section-header'>Constructors</h2
>")
943 for prop in local_consts do prop.html_full_desc(self)
947 if not concern2meths.is_empty then
948 append("<section
class='methods'>")
949 append("<h2
class='section-header'>Methods</h2
>")
950 for owner, mmodules in sections do
951 append("<a id
=\
"{owner.anchor}\"></a
>")
952 if owner != mclass.intro_mmodule and owner != mclass.public_owner then
953 var nowner = ctx.mbuilder.mmodule2nmodule[owner]
954 append("<h3
class=\
"concern-toplevel\">Methods refined
in ")
955 owner.html_link(self)
957 append("<p
class=\
"concern-doc\">")
958 owner.html_link(self)
959 if not nowner.short_comment.is_empty then
960 append(": {nowner.short_comment}")
964 if concern2meths.has_key(owner) then
965 var mmethods = concern2meths[owner]
966 prop_sorter.sort(mmethods)
967 for prop in mmethods do prop.html_full_desc(self)
969 for mmodule in mmodules do
970 append("<a id
=\
"{mmodule.anchor}\"></a
>")
971 var nmodule = ctx.mbuilder.mmodule2nmodule[mmodule]
972 if mmodule != mclass.intro_mmodule and mmodule != mclass.public_owner then
973 append("<p
class=\
"concern-doc\">")
974 mmodule.html_link(self)
975 if not nmodule.short_comment.is_empty then
976 append(": {nmodule.short_comment}")
980 var mmethods = concern2meths[mmodule]
981 prop_sorter.sort(mmethods)
982 for prop in mmethods do prop.html_full_desc(self)
986 # inherited properties
987 if inherited.length > 0 then
988 var sorted_inherited = new Array[MPropDef]
989 sorted_inherited.add_all(inherited)
990 ctx.mainmodule.linearize_mpropdefs(sorted_inherited)
991 var classes = new ArrayMap[MClass, Array[MPropDef]]
992 for mmethod in sorted_inherited.reversed do
993 var mclass = mmethod.mclassdef.mclass
994 if not classes.has_key(mclass) then classes[mclass] = new Array[MPropDef]
995 classes[mclass].add(mmethod)
997 append("<h3
>Inherited Properties</h3
>")
998 for c, mmethods in classes do
999 prop_sorter.sort(mmethods)
1000 append("<p
>Defined in ")
1003 for i in [0..mmethods.length[ do
1004 var mmethod = mmethods[i]
1005 mmethod.html_link(self)
1006 if i <= mmethods.length - 1 then append(", ")
1011 append("</section
>")
1014 private fun process_generate_dot do
1015 var pe = ctx.class_hierarchy[mclass]
1016 var cla = new HashSet[MClass]
1017 var sm = new HashSet[MClass]
1018 var sm2 = new HashSet[MClass]
1020 while cla.length + sm.length < 10 and sm.length > 0 do
1024 sm2.add_all(pe.poset[x].direct_smallers)
1030 cla.add_all(pe.greaters)
1033 var name = "dep_
{mclass.name}"
1034 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
")
1037 op.append("\
"{c.name}\"[shape
=box
,margin
=0.03];\n
")
1039 op.append("\
"{c.name}\"[URL=\
"{c.url}\"];\n
")
1041 for c2 in pe.poset[c].direct_greaters do
1042 if not cla.has(c2) then continue
1043 op.append("\
"{c.name}\"->\
"{c2.name}\";\n
")
1045 if not pe.poset[c].direct_smallers.is_empty then
1047 for c2 in pe.poset[c].direct_smallers do
1048 if cla.has(c2) then others = false
1051 op.append("\
"{c.name}...\"[label=\
"\"];\n
")
1052 op.append("\
"{c.name}...\"->\
"{c.name}\"[style
=dotted
];\n
")
1057 generate_dot(op.to_s, name, "Dependency graph
for class {mclass.name}")
1066 # URL to nitdoc page
1068 if url_cache == null then
1069 var res = new Buffer
1070 res.append("module_
")
1071 var mowner = public_owner
1072 if mowner != null then
1073 res.append("{public_owner.name}_
")
1075 res.append("{self.name}.html
")
1076 url_cache = res.to_s
1078 return url_cache.as(not null)
1080 private var url_cache: nullable String
1082 # html anchor id to the module in a nitdoc page
1083 fun anchor: String do
1084 if anchor_cache == null then
1085 var res = new Buffer
1087 var mowner = public_owner
1088 if mowner != null then
1089 res.append("{public_owner.name}_
")
1091 res.append(self.name)
1092 anchor_cache = res.to_s
1094 return anchor_cache.as(not null)
1096 private var anchor_cache: nullable String
1098 # Return a link (html a tag) to the nitdoc module page
1099 fun html_link(page: NitdocPage) do
1100 if html_link_cache == null then
1101 var res = new Buffer
1102 if page.ctx.mbuilder.mmodule2nmodule.has_key(self) then
1103 res.append("<a href
='{url}' title
='{page.ctx.mbuilder.mmodule2nmodule[self].short_comment}'>{name}</a
>")
1105 res.append("<a href
='{url}'>{name}</a
>")
1107 html_link_cache = res.to_s
1109 page.append(html_link_cache.as(not null))
1111 private var html_link_cache: nullable String
1113 # Return the module signature decorated with html
1114 fun html_signature(page: NitdocPage) do
1115 page.append("<span
>module ")
1116 html_full_namespace(page)
1117 page.append("</span
>")
1120 # Return the module full namespace decorated with html
1121 fun html_full_namespace(page: NitdocPage) do
1122 page.append("<span
>")
1123 var mowner = public_owner
1124 if mowner != null then
1125 public_owner.html_namespace(page)
1129 page.append("</span
>")
1132 # Return the module full namespace decorated with html
1133 fun html_namespace(page: NitdocPage) do
1134 page.append("<span
>")
1135 var mowner = public_owner
1136 if mowner != null then
1137 public_owner.html_namespace(page)
1141 page.append("</span
>")
1144 # Return the full comment of the module decorated with html
1145 fun html_full_comment(page: NitdocPage) do
1146 if page.ctx.mbuilder.mmodule2nmodule.has_key(self) then
1147 page.append("<div id
='description'>")
1148 page.append("<pre
class='text_label'>{page.ctx.mbuilder.mmodule2nmodule[self].full_comment}</pre
>")
1149 page.append("</div
>")
1155 # Return the module signature decorated with html
1156 fun html_full_signature(page: NitdocPage) do
1157 if visibility < public_visibility then page.append("{visibility.to_s} ")
1158 page.append("{kind} ")
1159 html_namespace(page)
1162 # name with formal parameter
1164 private fun signature: String do
1166 return "{name}[{intro.parameter_names.join(", ")}]"
1172 # Return a link (html a tag) to the nitdoc class page
1173 fun html_link(page: NitdocPage) do
1174 if html_link_cache == null then
1175 var res = new Buffer
1176 res.append("<a href
='{url}'")
1177 if page.ctx.mbuilder.mclassdef2nclassdef.has_key(intro) then
1178 var nclass = page.ctx.mbuilder.mclassdef2nclassdef[intro]
1179 if nclass isa AStdClassdef then
1180 res.append(" title
=\
"{nclass.short_comment}\"")
1183 res.append(">{signature}</a
>")
1184 html_link_cache = res.to_s
1186 page.append(html_link_cache.as(not null))
1188 private var html_link_cache: nullable String
1190 # Return the class namespace decorated with html
1191 fun html_namespace(page: NitdocPage) do
1192 intro_mmodule.html_namespace(page)
1193 page.append("::<span
>")
1195 page.append("</span
>")
1199 return "class_
{public_owner}_
{name}.html
"
1202 # Escape name for html output
1203 redef fun name do return super.html_escape
1206 redef class MProperty
1207 # Return the property namespace decorated with html
1208 fun html_namespace(page: NitdocPage) do
1209 intro_mclassdef.mclass.html_namespace(page)
1210 page.append("::<span
>")
1211 intro.html_link(page)
1212 page.append("</span
>")
1215 # Escape name for html output
1216 redef fun name do return super.html_escape
1220 fun html_link(page: NitdocPage) is abstract
1223 redef class MClassType
1224 redef fun html_link(page) do mclass.html_link(page)
1227 redef class MNullableType
1228 redef fun html_link(page) do
1229 page.append("nullable ")
1230 mtype.html_link(page)
1234 redef class MGenericType
1235 redef fun html_link(page) do
1236 page.append("<a href
='{mclass.url}'>{mclass.name}</a
>[")
1237 for i in [0..arguments.length[ do
1238 arguments[i].html_link(page)
1239 if i < arguments.length - 1 then page.append(", ")
1245 redef class MParameterType
1246 redef fun html_link(page) do
1247 var name = mclass.intro.parameter_names[rank]
1248 page.append("<a href
='{mclass.url}#FT_{name}' title
='formal type'>{name}</a
>")
1252 redef class MVirtualType
1253 redef fun html_link(page) do mproperty.intro.html_link(page)
1256 redef class MClassDef
1257 # Return the classdef namespace decorated with html
1258 fun html_namespace(page: NitdocPage) do
1259 mmodule.html_full_namespace(page)
1260 page.append("::<span
>")
1261 mclass.html_link(page)
1262 page.append("</span
>")
1266 redef class MPropDef
1268 if url_cache == null then
1269 url_cache = "{mclassdef.mclass.url}#{anchor}"
1271 return url_cache
.as(not null)
1273 private var url_cache
: nullable String
1275 fun anchor
: String do
1276 if anchor_cache
== null then
1277 anchor_cache
= "PROP_{mclassdef.mclass.public_owner.name}_{mproperty.name}"
1279 return anchor_cache
.as(not null)
1281 private var anchor_cache
: nullable String
1283 # Return a link (html a tag) to the nitdoc class page
1284 fun html_link
(page
: NitdocPage) do
1285 if html_link_cache
== null then
1286 var res
= new Buffer
1287 if page
.ctx
.mbuilder
.mpropdef2npropdef
.has_key
(self) then
1288 var nprop
= page
.ctx
.mbuilder
.mpropdef2npropdef
[self]
1289 res
.append
("<a href=\"{url}\
" title=\"{nprop.short_comment}\
">{mproperty.name}</a>")
1291 res
.append
("<a href=\"{url}\
">{mproperty.name}</a>")
1293 html_link_cache
= res
.to_s
1295 page
.append
(html_link_cache
.as(not null))
1297 private var html_link_cache
: nullable String
1299 # Return a list item for the mpropdef
1300 private fun html_list_item
(page
: NitdocPage) do
1302 page
.append
("<li class='intro'>")
1303 page
.append
("<span title='introduction'>I</span> ")
1305 page
.append
("<li class='redef'>")
1306 page
.append
("<span title='redefinition'>R</span> ")
1310 mclassdef
.mclass
.html_link
(page
)
1312 page
.append
("</li>")
1315 # Return a list item for the mpropdef
1316 private fun html_sidebar_item
(page
: NitdocClass) do
1317 if is_intro
and mclassdef
.mclass
== page
.mclass
then
1318 page
.append
("<li class='intro'>")
1319 page
.append
("<span title='Introduced'>I</span>")
1320 else if is_intro
and mclassdef
.mclass
!= page
.mclass
then
1321 page
.append
("<li class='inherit'>")
1322 page
.append
("<span title='Inherited'>H</span>")
1324 page
.append
("<li class='redef'>")
1325 page
.append
("<span title='Redefined'>R</span>")
1328 page
.append
("</li>")
1331 private fun html_full_desc
(page
: NitdocClass) is abstract
1332 private fun html_info
(page
: NitdocClass) is abstract
1334 fun full_name
: String do
1335 return "{mclassdef.mclass.public_owner.name}::{mclassdef.mclass.name}::{mproperty.name}"
1338 private fun html_inheritance
(page
: NitdocClass) do
1340 page
.append
("<p class='info'>")
1341 page
.ctx
.mainmodule
.linearize_mpropdefs
(mproperty
.mpropdefs
)
1342 var previous_defs
= new Array[MPropDef]
1343 var next_defs
= new Array[MPropDef]
1344 var self_passed
= false
1345 for def
in mproperty
.mpropdefs
do
1350 if not self_passed
then
1351 if def
.mclassdef
.mclass
.in_hierarchy
(page
.ctx
.mainmodule
) < page
.mclass
then continue
1352 if def
.is_intro
then continue
1353 previous_defs
.add
(def
)
1355 if page
.mclass
.in_hierarchy
(page
.ctx
.mainmodule
) < def
.mclassdef
.mclass
then continue
1359 page
.append
("defined by ")
1360 mclassdef
.mmodule
.html_full_namespace
(page
)
1361 if page
.ctx
.mbuilder
.mpropdef2npropdef
.has_key
(self) then
1362 page
.append
(" {page.show_source(page.ctx.mbuilder.mpropdef2npropdef[self].location)}")
1364 if not is_intro
then
1365 page
.append
(", introduced by ")
1366 mproperty
.intro
.mclassdef
.mclass
.html_link
(page
)
1367 if page
.ctx
.mbuilder
.mpropdef2npropdef
.has_key
(self) then
1368 page
.append
(" {page.show_source(page.ctx.mbuilder.mpropdef2npropdef[self].location)}")
1371 if not previous_defs
.is_empty
then
1372 page
.append
(", inherited from ")
1373 for i
in [0..previous_defs
.length
[ do
1374 var def
= previous_defs
[i
]
1375 def
.mclassdef
.mclass
.html_link
(page
)
1376 if page
.ctx
.mbuilder
.mpropdef2npropdef
.has_key
(def
) then
1377 page
.append
(" {page.show_source(page.ctx.mbuilder.mpropdef2npropdef[def].location)}")
1380 if i
< previous_defs
.length
- 1 then page
.append
(", ")
1383 if not next_defs
.is_empty
then
1384 page
.append
(", redefined by ")
1385 for i
in [0..next_defs
.length
[ do
1386 var def
= next_defs
[i
]
1387 def
.mclassdef
.mclass
.html_link
(page
)
1388 if page
.ctx
.mbuilder
.mpropdef2npropdef
.has_key
(def
) then
1389 page
.append
(" {page.show_source(page.ctx.mbuilder.mpropdef2npropdef[def].location)}")
1391 if i
< next_defs
.length
- 1 then page
.append
(", ")
1394 page
.append
(".</p>")
1397 private fun html_comment
(page
: NitdocClass) do
1398 if not page
.ctx
.mbuilder
.mpropdef2npropdef
.has_key
(self) then return
1399 var nprop
= page
.ctx
.mbuilder
.mpropdef2npropdef
[self]
1400 page
.append
("<div class='description'>")
1401 if not is_intro
and page
.ctx
.mbuilder
.mpropdef2npropdef
.has_key
(mproperty
.intro
) then
1402 var intro_nprop
= page
.ctx
.mbuilder
.mpropdef2npropdef
[mproperty
.intro
]
1403 page
.append
("<p class='info'>from ")
1404 mproperty
.html_namespace
(page
)
1406 if intro_nprop
.full_comment
== "" then
1407 page
.append
("<span class=\"noComment\
">No comment</span>")
1409 page
.append
("<pre>{intro_nprop.full_comment}</pre>")
1411 page
.append
("<p class='info'>from ")
1412 mclassdef
.html_namespace
(page
)
1415 if nprop
.full_comment
== "" then
1416 page
.append
("<span class=\"noComment\
">No comment</span>")
1418 page
.append
("<pre>{nprop.full_comment}</pre>")
1420 html_inheritance
(page
)
1421 page
.append
("</div>")
1425 redef class MMethodDef
1426 redef fun html_full_desc
(page
) do
1427 var classes
= new Array[String]
1428 var is_redef
= mproperty
.intro_mclassdef
.mclass
!= page
.mclass
1429 if mproperty
.is_init
then
1434 if is_redef
then classes
.add
("redef")
1435 classes
.add
(mproperty
.visibility
.to_s
)
1436 page
.append
("<article class='{classes.join(" ")}' id='{anchor}'>")
1437 if page
.ctx
.mbuilder
.mpropdef2npropdef
.has_key
(self) then
1438 page
.append
("<h3 class='signature'>{mproperty.name}")
1439 msignature
.html_signature
(page
)
1440 page
.append
("</h3>")
1442 page
.append
("<h3 class='signature'>init")
1443 msignature
.html_signature
(page
)
1444 page
.append
("</h3>")
1448 page
.append
("</article>")
1451 redef fun html_info
(page
) do
1452 page
.append
("<div class='info'>")
1453 if mproperty
.visibility
< public_visibility
then page
.append
("{mproperty.visibility.to_s} ")
1454 if mproperty
.intro_mclassdef
.mclass
!= page
.mclass
then page
.append
("redef ")
1455 if mproperty
.is_init
then
1456 page
.append
("init ")
1460 mproperty
.html_namespace
(page
)
1461 page
.append
("</div>")
1465 redef class MVirtualTypeDef
1466 redef fun html_full_desc
(page
) do
1467 var is_redef
= mproperty
.intro_mclassdef
.mclass
!= page
.mclass
1468 var classes
= new Array[String]
1470 if is_redef
then classes
.add
("redef")
1471 classes
.add
(mproperty
.visibility
.to_s
)
1472 page
.append
("<article class='{classes.join(" ")}' id='{anchor}'>")
1473 page
.append
("<h3 class='signature'>{mproperty.name}: ")
1474 bound
.html_link
(page
)
1475 page
.append
("</h3>")
1478 page
.append
("</article>")
1481 redef fun html_info
(page
) do
1482 page
.append
("<div class='info'>")
1483 if mproperty
.intro_mclassdef
.mclass
!= page
.mclass
then page
.append
("redef ")
1484 page
.append
("type ")
1485 mproperty
.html_namespace
(page
)
1486 page
.append
("</div>")
1490 redef class MSignature
1491 private fun html_signature
(page
: NitdocPage) do
1492 if not mparameters
.is_empty
then
1494 for i
in [0..mparameters
.length
[ do
1495 mparameters
[i
].html_link
(page
)
1496 if i
< mparameters
.length
- 1 then page
.append
(", ")
1500 if return_mtype
!= null then
1502 return_mtype
.html_link
(page
)
1507 redef class MParameter
1508 private fun html_link
(page
: NitdocPage) do
1509 page
.append
("{name}: ")
1510 mtype
.html_link
(page
)
1511 if is_vararg
then page
.append
("...")
1520 private fun short_comment
: String do
1521 if n_moduledecl
!= null and n_moduledecl
.n_doc
!= null then
1522 return n_moduledecl
.n_doc
.n_comment
.first
.text
.substring_from
(2).replace
("\n", "").html_escape
1527 private fun full_comment
: String do
1528 var res
= new Buffer
1529 if n_moduledecl
!= null and n_moduledecl
.n_doc
!= null then
1530 for t
in n_moduledecl
.n_doc
.n_comment
do
1531 res
.append
(t
.text
.substring_from
(1).html_escape
)
1538 redef class AStdClassdef
1539 private fun short_comment
: String do
1540 if n_doc
!= null then return n_doc
.n_comment
.first
.text
.substring_from
(2).replace
("\n", "").html_escape
1544 private fun full_comment
: String do
1545 var res
= new Buffer
1546 if n_doc
!= null then
1547 for t
in n_doc
.n_comment
do res
.append
(t
.text
.substring_from
(1).html_escape
)
1553 redef class APropdef
1554 private fun short_comment
: String do
1555 if n_doc
!= null then return n_doc
.n_comment
.first
.text
.substring_from
(2).replace
("\n", "").html_escape
1559 private fun full_comment
: String do
1560 var res
= new Buffer
1561 if n_doc
!= null then
1562 for t
in n_doc
.n_comment
do res
.append
(t
.text
.substring_from
(1).html_escape
)
1568 var nitdoc
= new NitdocContext
1569 nitdoc
.generate_nitdoc