ni_nitdoc: renamed NItdoc into NitdocContext
[nit.git] / src / ni_nitdoc.nit
index c5bd3a6..e58f273 100644 (file)
@@ -19,7 +19,7 @@ module ni_nitdoc
 import model_utils
 import abstract_compiler
 
-class Nitdoc
+class NitdocContext
        private var toolcontext: ToolContext
        private var model: Model
        private var modelbuilder: ModelBuilder
@@ -30,16 +30,19 @@ class Nitdoc
        private var dot_dir: nullable String
        private var share_dir: nullable String
        private var source: nullable String
+       private var min_visibility: MVisibility
 
        private var opt_dir = new OptionString("Directory where doc is generated", "-d", "--dir")
        private var opt_source = new OptionString("What link for source (%f for filename, %l for first line, %L for last line)", "--source")
        private var opt_sharedir = new OptionString("Directory containing the nitdoc files", "--sharedir")
        private var opt_nodot = new OptionBool("Do not generate graphes with graphiviz", "--no-dot")
+       private var opt_private: OptionBool = new OptionBool("Generate the private API", "--private")
 
        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")
        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")
        private var opt_custom_overview_text: OptionString = new OptionString("Text displayed as introduction of Overview page before the modules list", "--custom-overview-text")
        private var opt_custom_footer_text: OptionString = new OptionString("Text displayed as footer of all pages", "--custom-footer-text")
+
        init(toolcontext: ToolContext) do
                # We need a model to collect stufs
                self.toolcontext = toolcontext
@@ -49,6 +52,7 @@ class Nitdoc
                toolcontext.option_context.add_option(opt_source)
                toolcontext.option_context.add_option(opt_sharedir)
                toolcontext.option_context.add_option(opt_nodot)
+               toolcontext.option_context.add_option(opt_private)
                toolcontext.option_context.add_option(opt_custom_title)
                toolcontext.option_context.add_option(opt_custom_footer_text)
                toolcontext.option_context.add_option(opt_custom_overview_text)
@@ -103,12 +107,14 @@ class Nitdoc
                                print "Error: Invalid nitdoc share files. Check --sharedir or envvar NIT_DIR"
                                abort
                        end
+
+                       if opt_private.value then
+                               min_visibility = none_visibility
+                       else
+                               min_visibility = protected_visibility
+                       end
                end
-               if not opt_source.value is null then
-                       source = ""
-               else
-                       source = opt_source.value
-               end
+               source = opt_source.value
        end
 
        fun start do
@@ -161,14 +167,14 @@ class Nitdoc
                        content.append("],")
                end
                for mclass in model.mclasses do
-                       if mclass.visibility <= none_visibility then continue
+                       if mclass.visibility < min_visibility then continue
                        content.append("\"{mclass.name}\": [")
                        content.append("\{txt: \"{mclass.name}\", url:\"{mclass.url}\" \},")
                        content.append("],")
                end
                var name2mprops = new HashMap[String, Set[MPropDef]]
                for mproperty in model.mproperties do
-                       if mproperty.visibility <= none_visibility then continue
+                       if mproperty.visibility < min_visibility then continue
                        if mproperty isa MAttribute then continue
                        if not name2mprops.has_key(mproperty.name) then name2mprops[mproperty.name] = new HashSet[MPropDef]
                        name2mprops[mproperty.name].add_all(mproperty.mpropdefs)
@@ -193,10 +199,10 @@ abstract class NitdocPage
 
        var dot_dir: nullable String
        var source: nullable String
-       var nitdoc: Nitdoc
+       var ctx: NitdocContext
 
-       init(nitdoc: Nitdoc) do
-               self.nitdoc = nitdoc
+       init(ctx: NitdocContext) do
+               self.ctx = ctx
        end
 
        fun append(str: String) do html.append(str)
@@ -209,15 +215,15 @@ abstract class NitdocPage
                append("<script type='text/javascript' src='scripts/js-facilities.js'></script>")
                append("<link rel='stylesheet' href='styles/main.css' type='text/css' media='screen'/>")
                var title = ""
-               if nitdoc.opt_custom_title.value != null then
-                       title = " | {nitdoc.opt_custom_title.value.to_s}"
+               if ctx.opt_custom_title.value != null then
+                       title = " | {ctx.opt_custom_title.value.to_s}"
                end
                append("<title>{self.title}{title}</title>")
        end
 
        fun menu do
-               if nitdoc.opt_custom_menu_items.value != null then
-                       append(nitdoc.opt_custom_menu_items.value.to_s)
+               if ctx.opt_custom_menu_items.value != null then
+                       append(ctx.opt_custom_menu_items.value.to_s)
                end
        end
 
@@ -273,8 +279,8 @@ abstract class NitdocPage
        fun content is abstract
 
        fun footer do
-               if nitdoc.opt_custom_footer_text.value != null then
-                       append("<footer>{nitdoc.opt_custom_footer_text.value.to_s}</footer>")
+               if ctx.opt_custom_footer_text.value != null then
+                       append("<footer>{ctx.opt_custom_footer_text.value.to_s}</footer>")
                end
        end
 
@@ -307,7 +313,7 @@ abstract class NitdocPage
                        source = x.join(l.line_start.to_s)
                        x = source.split_with("%L")
                        source = x.join(l.line_end.to_s)
-                       return " (<a href=\"{source.to_s}\">show code</a>)"
+                       return " (<a href=\"{source.to_s}\">source</a>)"
                end
        end
 
@@ -341,9 +347,9 @@ class NitdocOverview
        private var mbuilder: ModelBuilder
        private var mmodules = new Array[MModule]
 
-       init(nitdoc: Nitdoc, dot_dir: nullable String) do
-               super(nitdoc)
-               self.mbuilder = nitdoc.modelbuilder
+       init(ctx: NitdocContext, dot_dir: nullable String) do
+               super(ctx)
+               self.mbuilder = ctx.modelbuilder
                self.dot_dir = dot_dir
                # get modules
                var mmodules = new HashSet[MModule]
@@ -373,13 +379,13 @@ class NitdocOverview
        redef fun content do
                append("<div class='content fullpage'>")
                var title = "Overview"
-               if nitdoc.opt_custom_title.value != null then
-                       title = nitdoc.opt_custom_title.value.to_s
+               if ctx.opt_custom_title.value != null then
+                       title = ctx.opt_custom_title.value.to_s
                end
                append("<h1>{title}</h1>")
                var text = ""
-               if nitdoc.opt_custom_overview_text.value != null then
-                       text = nitdoc.opt_custom_overview_text.value.to_s
+               if ctx.opt_custom_overview_text.value != null then
+                       text = ctx.opt_custom_overview_text.value.to_s
                end
                append("<article class='overview'>{text}</article>")
                append("<article class='overview'>")
@@ -419,8 +425,8 @@ end
 class NitdocFullindex
        super NitdocPage
 
-       init(nitdoc: Nitdoc) do
-               super(nitdoc)
+       init(ctx: NitdocContext) do
+               super(ctx)
                self.dot_dir = null
        end
 
@@ -445,7 +451,7 @@ class NitdocFullindex
        fun module_column do
                var sorter = new ComparableSorter[MModule]
                var sorted = new Array[MModule]
-               for mmodule in nitdoc.modelbuilder.model.mmodule_importation_hierarchy do
+               for mmodule in ctx.modelbuilder.model.mmodule_importation_hierarchy do
                        sorted.add(mmodule)
                end
                sorter.sort(sorted)
@@ -453,7 +459,7 @@ class NitdocFullindex
                append("<h2>Modules</h2>")
                append("<ul>")
                for mmodule in sorted do
-                       append("<li>{mmodule.link(nitdoc.modelbuilder)}</li>")
+                       append("<li>{mmodule.link(ctx.modelbuilder)}</li>")
                end
                append("</ul>")
                append("</article>")
@@ -461,14 +467,15 @@ class NitdocFullindex
 
        # Add to content classes modules
        fun classes_column do
-               var sorted = nitdoc.modelbuilder.model.mclasses
+               var sorted = ctx.modelbuilder.model.mclasses
                var sorter = new ComparableSorter[MClass]
                sorter.sort(sorted)
                append("<article class='modules filterable'>")
                append("<h2>Classes</h2>")
                append("<ul>")
                for mclass in sorted do
-                       append("<li>{mclass.link(nitdoc.modelbuilder)}</li>")
+                       if mclass.visibility < ctx.min_visibility then continue
+                       append("<li>{mclass.link(ctx.modelbuilder)}</li>")
                end
                append("</ul>")
                append("</article>")
@@ -476,15 +483,16 @@ class NitdocFullindex
 
        # Insert the properties column of fullindex page
        fun properties_column do
-               var sorted = nitdoc.modelbuilder.model.mproperties
+               var sorted = ctx.modelbuilder.model.mproperties
                var sorter = new ComparableSorter[MProperty]
                sorter.sort(sorted)
                append("<article class='modules filterable'>")
                append("<h2>Properties</h2>")
                append("<ul>")
                for mproperty in sorted do
+                       if mproperty.visibility < ctx.min_visibility then continue
                        if mproperty isa MAttribute then continue
-                       append("<li>{mproperty.intro.link(nitdoc.modelbuilder)} ({mproperty.intro.mclassdef.mclass.link(nitdoc.modelbuilder)})</li>")
+                       append("<li>{mproperty.intro.link(ctx.modelbuilder)} ({mproperty.intro.mclassdef.mclass.link(ctx.modelbuilder)})</li>")
                end
                append("</ul>")
                append("</article>")
@@ -499,10 +507,10 @@ class NitdocModule
        private var mmodule: MModule
        private var mbuilder: ModelBuilder
 
-       init(mmodule: MModule, nitdoc: Nitdoc, dot_dir: nullable String) do
-               super(nitdoc)
+       init(mmodule: MModule, ctx: NitdocContext, dot_dir: nullable String) do
+               super(ctx)
                self.mmodule = mmodule
-               self.mbuilder = nitdoc.modelbuilder
+               self.mbuilder = ctx.modelbuilder
                self.dot_dir = dot_dir
        end
 
@@ -621,6 +629,7 @@ class NitdocModule
                append("<h2>Classes</h2>")
                append("<ul>")
                for c in sorted do
+                       if c.visibility < ctx.min_visibility then continue
                        if redef_mclasses.has(c) and c.intro_mmodule.public_owner != mmodule then
                                append("<li class='redef'>")
                                append("<span title='refined in this module'>R </span>")
@@ -653,7 +662,7 @@ class NitdocModule
                append("<ul>")
                for mprop in sorted do
                        if mprop isa MAttributeDef then continue
-                       if mprop.mproperty.visibility <= none_visibility then continue
+                       if mprop.mproperty.visibility < ctx.min_visibility then continue
                        append(mprop.html_list_item(mbuilder))
                end
                append("</ul>")
@@ -672,16 +681,16 @@ class NitdocClass
        private var meths = new HashSet[MMethodDef]
        private var inherited = new HashSet[MPropDef]
 
-       init(mclass: MClass, nitdoc: Nitdoc, dot_dir: nullable String, source: nullable String) do
-               super(nitdoc)
+       init(mclass: MClass, ctx: NitdocContext, dot_dir: nullable String, source: nullable String) do
+               super(ctx)
                self.mclass = mclass
-               self.mbuilder = nitdoc.modelbuilder
+               self.mbuilder = ctx.modelbuilder
                self.dot_dir = dot_dir
                self.source = source
                # load properties
                for mclassdef in mclass.mclassdefs do
                        for mpropdef in mclassdef.mpropdefs do
-                               if mpropdef.mproperty.visibility <= none_visibility then continue
+                               if mpropdef.mproperty.visibility < ctx.min_visibility then continue
                                if mpropdef isa MVirtualTypeDef then vtypes.add(mpropdef)
                                if mpropdef isa MMethodDef then
                                        if mpropdef.mproperty.is_init then
@@ -695,7 +704,7 @@ class NitdocClass
                # get inherited properties
                for mprop in mclass.inherited_mproperties do
                        var mpropdef = mprop.intro
-                       if mprop.visibility <= none_visibility then continue
+                       if mprop.visibility < ctx.min_visibility then continue
                        if mpropdef isa MVirtualTypeDef then vtypes.add(mpropdef)
                        if mpropdef isa MMethodDef then
                                if mpropdef.mproperty.is_init then
@@ -847,7 +856,7 @@ class NitdocClass
                var sorted_meths = new Array[MMethodDef]
                var sorted = new Array[MModule]
                sorted_meths.add_all(meths)
-               nitdoc.mainmodule.linearize_mpropdefs(sorted_meths)
+               ctx.mainmodule.linearize_mpropdefs(sorted_meths)
                for meth in meths do
                        if inherited.has(meth) then continue
                        var mmodule = meth.mclassdef.mmodule
@@ -966,7 +975,7 @@ class NitdocClass
                if inherited.length > 0 then
                        var sorted_inherited = new Array[MPropDef]
                        sorted_inherited.add_all(inherited)
-                       nitdoc.mainmodule.linearize_mpropdefs(sorted_inherited)
+                       ctx.mainmodule.linearize_mpropdefs(sorted_inherited)
                        var classes = new ArrayMap[MClass, Array[MPropDef]]
                        for mmethod in sorted_inherited.reversed do
                                var mclass = mmethod.mclassdef.mclass
@@ -989,7 +998,7 @@ class NitdocClass
        end
 
        fun process_generate_dot do
-               var pe = nitdoc.class_hierarchy[mclass]
+               var pe = ctx.class_hierarchy[mclass]
                var cla = new HashSet[MClass]
                var sm = new HashSet[MClass]
                var sm2 = new HashSet[MClass]
@@ -1156,11 +1165,7 @@ redef class MClass
        # Return the module signature decorated with html
        fun html_full_signature(mbuilder: ModelBuilder): String do
                var res = new Buffer
-               if visibility <= none_visibility then
-                       res.append("private ")
-               else if visibility == protected_visibility then
-                       res.append("protected ")
-               end
+               if visibility < public_visibility then res.append("{visibility.to_s} ")
                res.append("{kind} {html_namespace(mbuilder)}")
                return res.to_s
        end
@@ -1321,6 +1326,68 @@ redef class MPropDef
        fun full_name: String do
                return "{mclassdef.mclass.public_owner.name}::{mclassdef.mclass.name}::{mproperty.name}"
        end
+
+       fun html_inheritance(page: NitdocClass): String do
+               var res = new Buffer
+               # definitions block
+               res.append("<p class='info'>")
+               page.ctx.mainmodule.linearize_mpropdefs(mproperty.mpropdefs)
+               var previous_defs = new Array[MPropDef]
+               var next_defs = new Array[MPropDef]
+               var self_passed = false
+               for def in mproperty.mpropdefs do
+                       if def == self then
+                               self_passed = true
+                               continue
+                       end
+                       if not self_passed then
+                               if not page.mclass.ancestors.has(def.mclassdef.mclass) then continue
+                               if def.is_intro then continue
+                               previous_defs.add(def)
+                       else
+                               if not page.mclass.descendants.has(def.mclassdef.mclass) then continue
+                               next_defs.add(def)
+                       end
+               end
+               var source = ""
+               if page.mbuilder.mpropdef2npropdef.has_key(self) then
+                       source = " {page.show_source(page.mbuilder.mpropdef2npropdef[self].location)}"
+               end
+               res.append("defined by {mclassdef.mmodule.html_full_namespace(page.mbuilder)}{source}")
+               if not is_intro then
+                       source = ""
+                       if page.mbuilder.mpropdef2npropdef.has_key(mproperty.intro) then 
+                               source = " {page.show_source(page.mbuilder.mpropdef2npropdef[mproperty.intro].location)}"
+                       end
+                       res.append(", introduced by {mproperty.intro.mclassdef.mclass.link(page.mbuilder)}{source}")
+               end
+               if not previous_defs.is_empty then
+                       res.append(", inherited from ")
+                       for i in [0..previous_defs.length[ do
+                               var def = previous_defs[i]
+                               source = ""
+                               if page.mbuilder.mpropdef2npropdef.has_key(def) then 
+                                       source = " {page.show_source(page.mbuilder.mpropdef2npropdef[def].location)}"
+                               end
+                               res.append("{def.mclassdef.mclass.link(page.mbuilder)}{source}")
+                               if i < previous_defs.length - 1 then res.append(", ")
+                       end
+               end
+               if not next_defs.is_empty then
+                       res.append(", redefined by ")
+                       for i in [0..next_defs.length[ do
+                               var def = next_defs[i]
+                               source = ""
+                               if page.mbuilder.mpropdef2npropdef.has_key(def) then 
+                                       source = " {page.show_source(page.mbuilder.mpropdef2npropdef[def].location)}"
+                               end
+                                res.append("{def.mclassdef.mclass.link(page.mbuilder)}{source}")
+                                if i < next_defs.length - 1 then res.append(", ")
+                       end
+               end
+               res.append(".</p>")
+               return res.to_s
+       end
 end
 
 redef class MMethodDef
@@ -1336,13 +1403,7 @@ redef class MMethodDef
                classes.add("fun")
                if mprop.is_init then classes.add("init")
                if is_redef then classes.add("redef")
-               if mprop.visibility == none_visibility then
-                       classes.add("private")
-               else if mprop.visibility == protected_visibility then
-                       classes.add("protected")
-               else
-                       classes.add("public")
-               end
+               classes.add(mproperty.visibility.to_s)
                res.append("<article class='{classes.join(" ")}' id='{anchor}'>")
                if nprop isa AAttrPropdef then
                        if nprop.mreadpropdef == self then
@@ -1362,45 +1423,7 @@ redef class MMethodDef
                        res.append("<pre class=\"text_label\" title=\"\" name=\"\" tag=\"\" type=\"1\">{nprop.comment}</pre>")
                end
                res.append("<textarea id=\"fileContent\" class=\"edit\" cols=\"76\" rows=\"1\" style=\"display: none;\"></textarea><a id=\"cancelBtn\" style=\"display: none;\">Cancel</a><a id=\"commitBtn\" style=\"display: none;\">Commit</a><pre id=\"preSave\" class=\"text_label\" type=\"2\"></pre>")
-               # definitions block
-               res.append("<p class='info'>")
-               page.nitdoc.mainmodule.linearize_mpropdefs(mprop.mpropdefs)
-               var previous_defs = new Array[MMethodDef]
-               var next_defs = new Array[MMethodDef]
-               var self_passed = false
-               for def in mprop.mpropdefs do
-                       if def == self then
-                               self_passed = true
-                               continue
-                       end
-                       if not self_passed then
-                               if not page.mclass.ancestors.has(def.mclassdef.mclass) then continue
-                               if def.is_intro then continue
-                               previous_defs.add(def)
-                       else
-                               if not page.mclass.descendants.has(def.mclassdef.mclass) then continue
-                               next_defs.add(def)
-                       end
-               end
-               res.append("defined by {mclassdef.mmodule.html_full_namespace(page.mbuilder)}")
-               if not is_intro then
-                       res.append(", introduced by {mprop.intro.mclassdef.mclass.link(page.mbuilder)}")
-               end
-               if not previous_defs.is_empty then
-                       res.append(", inherited from ")
-                       for i in [0..previous_defs.length[ do
-                                res.append(previous_defs[i].mclassdef.mclass.link(page.mbuilder))
-                                if i < previous_defs.length - 1 then res.append(", ")
-                       end
-               end
-               if not next_defs.is_empty then
-                       res.append(", redefined by ")
-                       for i in [0..next_defs.length[ do
-                                res.append(next_defs[i].mclassdef.mclass.link(page.mbuilder))
-                                if i < next_defs.length - 1 then res.append(", ")
-                       end
-               end
-               res.append(".</p>")
+               res.append(html_inheritance(page))
                res.append("</div>")
                res.append("</article>")
                return res.to_s
@@ -1409,11 +1432,7 @@ redef class MMethodDef
        redef fun html_info(page) do
                var res = new Buffer
                res.append("<div class='info'>")
-               if mproperty.visibility <= none_visibility then
-                       res.append("private ")
-               else if mproperty.visibility <= protected_visibility then
-                       res.append("protected ")
-               end
+               if mproperty.visibility < public_visibility then res.append("{mproperty.visibility.to_s} ")
                if mproperty.intro_mclassdef.mclass != page.mclass then res.append("redef ")
                res.append("fun {mproperty.html_namespace(page.mbuilder)}")
                res.append("</div>")
@@ -1430,13 +1449,7 @@ redef class MVirtualTypeDef
                var classes = new Array[String]
                classes.add("type")
                if is_redef then classes.add("redef")
-               if mprop.visibility == none_visibility then
-                       classes.add("private")
-               else if mprop.visibility == protected_visibility then
-                       classes.add("protected")
-               else
-                       classes.add("public")
-               end
+               classes.add(mproperty.visibility.to_s)
                res.append("<article class='{classes.join(" ")}' id='{anchor}'>")
                res.append("<h3 class='signature'>{mprop.name}: {bound.link(page.mbuilder)}</h3>")
                res.append(html_info(page))
@@ -1449,45 +1462,7 @@ redef class MVirtualTypeDef
                        res.append("<a class=\"newComment\" title=\"32\" tag=\"\">New Comment</a>")
                end
                res.append("<textarea id=\"fileContent\" class=\"edit\" cols=\"76\" rows=\"1\" style=\"display: none;\"></textarea><a id=\"cancelBtn\" style=\"display: none;\">Cancel</a><a id=\"commitBtn\" style=\"display: none;\">Commit</a><pre id=\"preSave\" class=\"text_label\" type=\"2\"></pre>")
-               # definitions block
-               res.append("<p class='info'>")
-               page.nitdoc.mainmodule.linearize_mpropdefs(mprop.mpropdefs)
-               var previous_defs = new Array[MVirtualTypeDef]
-               var next_defs = new Array[MVirtualTypeDef]
-               var self_passed = false
-               for def in mprop.mpropdefs do
-                       if def == self then
-                               self_passed = true
-                               continue
-                       end
-                       if not self_passed then
-                               if not page.mclass.ancestors.has(def.mclassdef.mclass) then continue
-                               if def.is_intro then continue
-                               previous_defs.add(def)
-                       else
-                               if not page.mclass.descendants.has(def.mclassdef.mclass) then continue
-                               next_defs.add(def)
-                       end
-               end
-               res.append("defined by {mclassdef.mmodule.html_full_namespace(page.mbuilder)}")
-               if not is_intro then
-                       res.append(", introduced by {mprop.intro.mclassdef.mclass.link(page.mbuilder)}")
-               end
-               if not previous_defs.is_empty then
-                       res.append(", inherited from ")
-                       for i in [0..previous_defs.length[ do
-                                res.append(previous_defs[i].mclassdef.mclass.link(page.mbuilder))
-                                if i < previous_defs.length - 1 then res.append(", ")
-                       end
-               end
-               if not next_defs.is_empty then
-                       res.append(", redefined by ")
-                       for i in [0..next_defs.length[ do
-                                res.append(next_defs[i].mclassdef.mclass.link(page.mbuilder))
-                                if i < next_defs.length - 1 then res.append(", ")
-                       end
-               end
-               res.append(".</p>")
+               res.append(html_inheritance(page))
                res.append("</div>")
                res.append("</article>")
                return res.to_s
@@ -1643,5 +1618,5 @@ end
 var toolcontext = new ToolContext
 
 # Here we launch the nit index
-var nitdoc = new Nitdoc(toolcontext)
+var nitdoc = new NitdocContext(toolcontext)
 nitdoc.start