nitdoc: Escape links’ attributes.
[nit.git] / src / doc / doc_pages.nit
index a8936fe..0d26ad6 100644 (file)
@@ -24,6 +24,8 @@ redef class ToolContext
        private var opt_source = new OptionString("link for source (%f for filename, %l for first line, %L for last line)", "--source")
        private var opt_sharedir = new OptionString("directory containing nitdoc assets", "--sharedir")
        private var opt_shareurl = new OptionString("use shareurl instead of copy shared files", "--shareurl")
+       private var opt_no_attributes = new OptionBool("ignore the attributes",
+                       "--no-attributes")
        private var opt_nodot = new OptionBool("do not generate graphes with graphviz", "--no-dot")
        private var opt_private = new OptionBool("also generate private API", "--private")
 
@@ -46,7 +48,8 @@ redef class ToolContext
                super
 
                var opts = option_context
-               opts.add_option(opt_dir, opt_source, opt_sharedir, opt_shareurl, opt_nodot, opt_private)
+               opts.add_option(opt_dir, opt_source, opt_sharedir, opt_shareurl,
+                               opt_no_attributes, opt_nodot, opt_private)
                opts.add_option(opt_custom_title, opt_custom_footer, opt_custom_intro, opt_custom_brand)
                opts.add_option(opt_github_upstream, opt_github_base_sha1, opt_github_gitdir)
                opts.add_option(opt_piwik_tracker, opt_piwik_site_id)
@@ -83,6 +86,23 @@ redef class ToolContext
                        end
                end
        end
+
+       # Filter the entity based on the options specified by the user.
+       #
+       # Return `true` if the specified entity has to be included in the generated
+       # documentation
+       private fun filter_mclass(mclass: MClass): Bool do
+               return mclass.visibility >= min_visibility
+       end
+
+       # Filter the entity based on the options specified by the user.
+       #
+       # Return `true` if the specified entity has to be included in the generated
+       # documentation
+       private fun filter_mproperty(mproperty: MProperty): Bool do
+               return mproperty.visibility >= min_visibility and
+                       not (opt_no_attributes.value and mproperty isa MAttribute)
+       end
 end
 
 # The Nitdoc class explores the model and generate pages for each mentities found
@@ -154,7 +174,7 @@ class Nitdoc
 
        private fun classes do
                for mclass in model.mclasses do
-                       if mclass.visibility <= ctx.min_visibility then continue
+                       if not ctx.filter_mclass(mclass) then continue
                        var page = new NitdocClass(ctx, model, mainmodule, mclass)
                        page.render.write_to_file("{ctx.output_dir.to_s}/{page.page_url}")
                end
@@ -162,9 +182,8 @@ class Nitdoc
 
        private fun properties do
                for mproperty in model.mproperties do
-                       if mproperty.visibility <= ctx.min_visibility then continue
+                       if not ctx.filter_mproperty(mproperty) then continue
                        if mproperty isa MInnerClass then continue
-                       if mproperty isa MAttribute then continue
                        var page = new NitdocProperty(ctx, model, mainmodule, mproperty)
                        page.render.write_to_file("{ctx.output_dir.to_s}/{page.page_url}")
                end
@@ -196,12 +215,11 @@ class QuickSearch
                        add_result_for(mmodule.name, mmodule.full_name, mmodule.nitdoc_url)
                end
                for mclass in model.mclasses do
-                       if mclass.visibility < ctx.min_visibility then continue
+                       if not ctx.filter_mclass(mclass) then continue
                        add_result_for(mclass.name, mclass.full_name, mclass.nitdoc_url)
                end
                for mproperty in model.mproperties do
-                       if mproperty.visibility < ctx.min_visibility then continue
-                       if mproperty isa MAttribute then continue
+                       if not ctx.filter_mproperty(mproperty) then continue
                        for mpropdef in mproperty.mpropdefs do
                                var full_name = mpropdef.mclassdef.mclass.full_name
                                var cls_url = mpropdef.mclassdef.mclass.nitdoc_url
@@ -368,7 +386,7 @@ abstract class NitdocPage
                var source = ctx.opt_source.value
                if source == null then
                        var url = location.file.filename.simplify_path
-                       return "<a target='_blank' title='Show source' href=\"{url}\">View Source</a>"
+                       return "<a target='_blank' title='Show source' href=\"{url.html_escape}\">View Source</a>"
                end
                # THIS IS JUST UGLY ! (but there is no replace yet)
                var x = source.split_with("%f")
@@ -378,7 +396,7 @@ abstract class NitdocPage
                x = source.split_with("%L")
                source = x.join(location.line_end.to_s)
                source = source.simplify_path
-               return "<a target='_blank' title='Show source' href=\"{source.to_s}\">View Source</a>"
+               return "<a target='_blank' title='Show source' href=\"{source.to_s.html_escape}\">View Source</a>"
        end
 
        # MProject description template
@@ -386,8 +404,9 @@ abstract class NitdocPage
                var article = mproject.tpl_article
                article.subtitle = mproject.tpl_declaration
                article.content = mproject.tpl_definition
-               if mproject.mdoc != null then
-                       article.content = mproject.mdoc.tpl_short_comment
+               var mdoc = mproject.mdoc_or_fallback
+               if mdoc != null then
+                       article.content = mdoc.tpl_short_comment
                end
                return article
        end
@@ -409,7 +428,7 @@ abstract class NitdocPage
                var intros = mmodule.intro_mclassdefs(ctx.min_visibility).to_a
                if not intros.is_empty then
                        mainmodule.linearize_mclassdefs(intros)
-                       var intros_art = new TplArticle.with_title("{mmodule.nitdoc_id}_intros", "Introduces")
+                       var intros_art = new TplArticle.with_title("{mmodule.nitdoc_id}.intros", "Introduces")
                        var intros_lst = new TplList.with_classes(["list-unstyled", "list-labeled"])
                        for mclassdef in intros do
                                intros_lst.add_li mclassdef.tpl_list_item
@@ -422,7 +441,7 @@ abstract class NitdocPage
                var redefs = mmodule.redef_mclassdefs(ctx.min_visibility).to_a
                if not redefs.is_empty then
                        mainmodule.linearize_mclassdefs(redefs)
-                       var redefs_art = new TplArticle.with_title("{mmodule.nitdoc_id}_redefs", "Redefines")
+                       var redefs_art = new TplArticle.with_title("{mmodule.nitdoc_id}.redefs", "Redefines")
                        var redefs_lst = new TplList.with_classes(["list-unstyled", "list-labeled"])
                        for mclassdef in redefs do
                                redefs_lst.add_li mclassdef.tpl_list_item
@@ -451,7 +470,7 @@ abstract class NitdocPage
                        redef_article.source_link = tpl_showsource(mclassdef.location)
                        article.add_child redef_article
                        # mpropdefs list
-                       var intros = new TplArticle.with_title("{mclassdef.nitdoc_id}_intros", "Introduces")
+                       var intros = new TplArticle.with_title("{mclassdef.nitdoc_id}.intros", "Introduces")
                        var intros_lst = new TplList.with_classes(["list-unstyled", "list-labeled"])
                        for mpropdef in mclassdef.collect_intro_mpropdefs(ctx.min_visibility) do
                                intros_lst.add_li mpropdef.tpl_list_item
@@ -460,7 +479,7 @@ abstract class NitdocPage
                                intros.content = intros_lst
                                redef_article.add_child intros
                        end
-                       var redefs = new TplArticle.with_title("{mclassdef.nitdoc_id}_redefs", "Redefines")
+                       var redefs = new TplArticle.with_title("{mclassdef.nitdoc_id}.redefs", "Redefines")
                        var redefs_lst = new TplList.with_classes(["list-unstyled", "list-labeled"])
                        for mpropdef in mclassdef.collect_redef_mpropdefs(ctx.min_visibility) do
                                redefs_lst.add_li mpropdef.tpl_list_item
@@ -507,10 +526,10 @@ abstract class NitdocPage
                article.title_classes.add "signature"
                article.summary_title = "{mprop.nitdoc_name}"
                article.subtitle = main_mpropdef.tpl_namespace
-               if main_mpropdef.mdoc != null then
-                       article.content = main_mpropdef.mdoc.tpl_comment
+               if main_mpropdef.mdoc_or_fallback != null then
+                       article.content = main_mpropdef.mdoc_or_fallback.tpl_comment
                end
-               var subarticle = new TplArticle("{main_mpropdef.nitdoc_id}_redefs")
+               var subarticle = new TplArticle("{main_mpropdef.nitdoc_id}.redefs")
                # Add redef in same `MClass`
                if local_mpropdefs.length > 1 then
                        for mpropdef in local_mpropdefs do
@@ -523,8 +542,8 @@ abstract class NitdocPage
                                redef_article.title_classes.add "signature info"
                                redef_article.css_classes.add "nospace"
                                var redef_content = new Template
-                               if mpropdef.mdoc != null then
-                                       redef_content.add mpropdef.mdoc.tpl_comment
+                               if mpropdef.mdoc_or_fallback != null then
+                                       redef_content.add mpropdef.mdoc_or_fallback.tpl_comment
                                end
                                redef_article.content = redef_content
                                subarticle.add_child redef_article
@@ -532,7 +551,7 @@ abstract class NitdocPage
                end
                # Add linearization
                if lin.length > 1 then
-                       var lin_article = new TplArticle("{main_mpropdef.nitdoc_id}_lin")
+                       var lin_article = new TplArticle("{main_mpropdef.nitdoc_id}.lin")
                        lin_article.title = "Inheritance"
                        var lst = new TplList.with_classes(["list-unstyled", "list-labeled"])
                        for mpropdef in lin do
@@ -658,7 +677,7 @@ class NitdocSearch
        private fun classes_list: Array[MClass] do
                var sorted = new Array[MClass]
                for mclass in model.mclasses do
-                       if mclass.visibility < ctx.min_visibility then continue
+                       if not ctx.filter_mclass(mclass) then continue
                        sorted.add mclass
                end
                name_sorter.sort(sorted)
@@ -669,9 +688,7 @@ class NitdocSearch
        private fun mprops_list: Array[MProperty] do
                var sorted = new Array[MProperty]
                for mproperty in model.mproperties do
-                       if mproperty.visibility < ctx.min_visibility then continue
-                       if mproperty isa MAttribute then continue
-                       sorted.add mproperty
+                       if ctx.filter_mproperty(mproperty) then sorted.add mproperty
                end
                name_sorter.sort(sorted)
                return sorted
@@ -905,7 +922,7 @@ class NitdocModule
 
                # Graph
                var mmodules = new HashSet[MModule]
-               mmodules.add_all mmodule.in_nesting.direct_greaters
+               mmodules.add_all mmodule.nested_mmodules
                mmodules.add_all imports
                if clients.length < 10 then mmodules.add_all clients
                mmodules.add mmodule
@@ -1084,22 +1101,17 @@ class NitdocClass
 
        # Property list to display in sidebar
        fun tpl_sidebar_properties do
-               var kind_map = sort_by_kind(mclass_inherited_mprops)
+               var by_kind = new PropertiesByKind.with_elements(mclass_inherited_mprops)
                var summary = new TplList.with_classes(["list-unstyled"])
 
-               tpl_sidebar_list("Virtual types", kind_map["type"].to_a, summary)
-               tpl_sidebar_list("Constructors", kind_map["init"].to_a, summary)
-               tpl_sidebar_list("Methods", kind_map["fun"].to_a, summary)
-               if not kind_map["inner"].is_empty then
-                       tpl_sidebar_list("Inner classes", kind_map["inner"].to_a, summary)
-               end
+               by_kind.sort_groups(name_sorter)
+               for g in by_kind.groups do tpl_sidebar_list(g, summary)
                tpl_sidebar.boxes.add new TplSideBox.with_content("All properties", summary)
        end
 
-       private fun tpl_sidebar_list(name: String, mprops: Array[MProperty], summary: TplList) do
+       private fun tpl_sidebar_list(mprops: PropertyGroup[MProperty], summary: TplList) do
                if mprops.is_empty then return
-               name_sorter.sort(mprops)
-               var entry = new TplListItem.with_content(name)
+               var entry = new TplListItem.with_content(mprops.title)
                var list = new TplList.with_classes(["list-unstyled", "list-labeled"])
                for mprop in mprops do
                        list.add_li tpl_sidebar_item(mprop)
@@ -1115,7 +1127,8 @@ class NitdocClass
                        var cls_url = mprop.intro.mclassdef.mclass.nitdoc_url
                        var def_url = "{cls_url}#{mprop.nitdoc_id}"
                        var lnk = new TplLink(def_url, mprop.name)
-                       if mprop.intro.mdoc != null then lnk.title = mprop.intro.mdoc.short_comment
+                       var mdoc = mprop.intro.mdoc_or_fallback
+                       if mdoc != null then lnk.title = mdoc.short_comment
                        var item = new Template
                        item.add new TplLabel.with_classes(classes)
                        item.add lnk
@@ -1137,8 +1150,9 @@ class NitdocClass
                var section = new TplSection.with_title("top", tpl_title)
                section.subtitle = mclass.intro.tpl_declaration
                var article = new TplArticle("comment")
-               if mclass.mdoc != null then
-                       article.content = mclass.mdoc.tpl_comment
+               var mdoc = mclass.mdoc_or_fallback
+               if mdoc != null then
+                       article.content = mdoc.tpl_comment
                end
                section.add_child article
                return section
@@ -1159,15 +1173,14 @@ class NitdocClass
                # parents
                var hparents = new HashSet[MClass]
                for c in mclass.in_hierarchy(mainmodule).direct_greaters do
-                       if c.visibility < ctx.min_visibility then continue
-                       hparents.add c
+                       if ctx.filter_mclass(c) then hparents.add c
                end
 
                # ancestors
                var hancestors = new HashSet[MClass]
                for c in mclass.in_hierarchy(mainmodule).greaters do
                        if c == mclass then continue
-                       if c.visibility < ctx.min_visibility then continue
+                       if not ctx.filter_mclass(c) then continue
                        if hparents.has(c) then continue
                        hancestors.add c
                end
@@ -1175,15 +1188,14 @@ class NitdocClass
                # children
                var hchildren = new HashSet[MClass]
                for c in mclass.in_hierarchy(mainmodule).direct_smallers do
-                       if c.visibility < ctx.min_visibility then continue
-                       hchildren.add c
+                       if ctx.filter_mclass(c) then hchildren.add c
                end
 
                # descendants
                var hdescendants = new HashSet[MClass]
                for c in mclass.in_hierarchy(mainmodule).smallers do
                        if c == mclass then continue
-                       if c.visibility < ctx.min_visibility then continue
+                       if not ctx.filter_mclass(c) then continue
                        if hchildren.has(c) then continue
                        hdescendants.add c
                end
@@ -1266,34 +1278,21 @@ class NitdocClass
 
                                # properties
                                var mprops = mmodules2mprops[mentity]
-                               var kind_map = sort_by_kind(mprops)
+                               var by_kind = new PropertiesByKind.with_elements(mprops)
 
-                               # virtual types
-                               for article in tpl_mproperty_articles(kind_map, "type") do
-                                       section.add_child article
-                               end
-                               # constructors
-                               for article in tpl_mproperty_articles(kind_map, "init") do
-                                       section.add_child article
-                               end
-                               # methods
-                               for article in tpl_mproperty_articles(kind_map, "fun") do
-                                       section.add_child article
-                               end
-                               # inner classes
-                               for article in tpl_mproperty_articles(kind_map, "inner") do
-                                       section.add_child article
+                               for g in by_kind.groups do
+                                       for article in tpl_mproperty_articles(g) do
+                                               section.add_child article
+                                       end
                                end
                                parent.add_child section
                        end
                end
        end
 
-       private fun tpl_mproperty_articles(kind_map: Map[String, Set[MProperty]],
-               kind_name: String): Sequence[TplArticle] do
+       private fun tpl_mproperty_articles(elts: Collection[MProperty]):
+                       Sequence[TplArticle] do
                var articles = new List[TplArticle]
-               var elts = kind_map[kind_name].to_a
-               name_sorter.sort(elts)
                for elt in elts do
                        var local_defs = mprops2mdefs[elt]
                        # var all_defs = elt.mpropdefs
@@ -1346,28 +1345,6 @@ class NitdocClass
                return map
        end
 
-       private fun sort_by_kind(mprops: Collection[MProperty]): Map[String, Set[MProperty]] do
-               var map = new HashMap[String, Set[MProperty]]
-               map["type"] = new HashSet[MProperty]
-               map["init"] = new HashSet[MProperty]
-               map["fun"] = new HashSet[MProperty]
-               map["inner"] = new HashSet[MProperty]
-               for mprop in mprops do
-                       if mprop isa MVirtualTypeProp then
-                               map["type"].add mprop
-                       else if mprop isa MMethod then
-                               if mprop.is_init then
-                                       map["init"].add mprop
-                               else
-                                       map["fun"].add mprop
-                               end
-                       else if mprop isa MInnerClass then
-                               map["inner"].add mprop
-                       end
-               end
-               return map
-       end
-
        private fun mclass_inherited_mprops: Set[MProperty] do
                var res = new HashSet[MProperty]
                var local = mclass.local_mproperties(ctx.min_visibility)
@@ -1440,6 +1417,74 @@ class NitdocClass
        end
 end
 
+# Groups properties by kind.
+private class PropertiesByKind
+       # The virtual types.
+       var virtual_types = new PropertyGroup[MVirtualTypeProp]("Virtual types")
+
+       # The constructors.
+       var constructors = new PropertyGroup[MMethod]("Contructors")
+
+       # The attributes.
+       var attributes = new PropertyGroup[MAttribute]("Attributes")
+
+       # The methods.
+       var methods = new PropertyGroup[MMethod]("Methods")
+
+       # The inner classes.
+       var inner_classes = new PropertyGroup[MInnerClass]("Inner classes")
+
+       # All the groups.
+       #
+       # Sorted in the order they are displayed to the user.
+       var groups: SequenceRead[PropertyGroup[MProperty]] = [
+                       virtual_types,
+                       constructors,
+                       attributes,
+                       methods,
+                       inner_classes: PropertyGroup[MProperty]]
+
+       # Add each the specified property to the appropriate list.
+       init with_elements(properties: Collection[MProperty]) do add_all(properties)
+
+       # Add the specified property to the appropriate list.
+       fun add(property: MProperty) do
+               if property isa MMethod then
+                       if property.is_init then
+                               constructors.add property
+                       else
+                               methods.add property
+                       end
+               else if property isa MVirtualTypeProp then
+                       virtual_types.add property
+               else if property isa MAttribute then
+                       attributes.add property
+               else if property isa MInnerClass then
+                       inner_classes.add property
+               else
+                       abort
+               end
+       end
+
+       # Add each the specified property to the appropriate list.
+       fun add_all(properties: Collection[MProperty]) do
+               for p in properties do add(p)
+       end
+
+       # Sort each group with the specified comparator.
+       fun sort_groups(comparator: Comparator) do
+               for g in groups do comparator.sort(g)
+       end
+end
+
+# A Group of properties of the same kind.
+private class PropertyGroup[E: MProperty]
+       super Array[E]
+
+       # The title of the group, as displayed to the user.
+       var title: String
+end
+
 # A MProperty page
 class NitdocProperty
        super NitdocPage