nitdoc: Factorize filtering logic.
[nit.git] / src / doc / doc_pages.nit
index db5ba9b..a095845 100644 (file)
@@ -17,6 +17,7 @@ module doc_pages
 
 import toolcontext
 import doc_model
+private import json::static
 
 redef class ToolContext
        private var opt_dir = new OptionString("output directory", "-d", "--dir")
@@ -82,6 +83,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 mproperty isa MAttribute
+       end
 end
 
 # The Nitdoc class explores the model and generate pages for each mentities found
@@ -153,7 +171,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
@@ -161,7 +179,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
                        var page = new NitdocProperty(ctx, model, mainmodule, mproperty)
                        page.render.write_to_file("{ctx.output_dir.to_s}/{page.page_url}")
                end
@@ -182,9 +201,7 @@ end
 # All entities are grouped by name to make the research easier.
 class QuickSearch
 
-       private var mmodules = new HashSet[MModule]
-       private var mclasses = new HashSet[MClass]
-       private var mpropdefs = new HashMap[String, Set[MPropDef]]
+       private var table = new QuickSearchTable
 
        var ctx: ToolContext
        var model: Model
@@ -192,51 +209,71 @@ class QuickSearch
        init do
                for mmodule in model.mmodules do
                        if mmodule.is_fictive then continue
-                       mmodules.add mmodule
+                       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
-                       mclasses.add mclass
+                       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 mpropdefs.has_key(mproperty.name) then
-                               mpropdefs[mproperty.name] = new HashSet[MPropDef]
+                       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
+                               var def_url = "{cls_url}#{mpropdef.mproperty.nitdoc_id}"
+                               add_result_for(mproperty.name, full_name, def_url)
                        end
-                       mpropdefs[mproperty.name].add_all(mproperty.mpropdefs)
                end
        end
 
+       private fun add_result_for(query: String, txt: String, url: String) do
+               table[query].add new QuickSearchResult(txt, url)
+       end
+
        fun render: Template do
                var tpl = new Template
-               tpl.add "var nitdocQuickSearchRawList=\{ "
-               for mmodule in mmodules do
-                       tpl.add "\"{mmodule.name}\":["
-                       tpl.add "\{txt:\"{mmodule.full_name}\",url:\"{mmodule.nitdoc_url}\"\},"
-                       tpl.add "],"
-               end
-               for mclass in mclasses do
-                       var full_name = mclass.intro.mmodule.full_name
-                       tpl.add "\"{mclass.name}\":["
-                       tpl.add "\{txt:\"{full_name}\",url:\"{mclass.nitdoc_url}\"\},"
-                       tpl.add "],"
-               end
-               for mproperty, mprops in mpropdefs do
-                       tpl.add "\"{mproperty}\":["
-                       for mpropdef in mprops do
-                               var full_name = mpropdef.mclassdef.mclass.full_name
-                               var cls_url = mpropdef.mclassdef.mclass.nitdoc_url
-                               var def_url = "{cls_url}#{mpropdef.mproperty.nitdoc_id}"
-                               tpl.add "\{txt:\"{full_name}\",url:\"{def_url}\"\},"
-                       end
-                       tpl.add "],"
-               end
-               tpl.add " \};"
+               var buffer = new RopeBuffer
+               tpl.add buffer
+               buffer.append "var nitdocQuickSearchRawList="
+               table.append_json buffer
+               buffer.append ";"
                return tpl
        end
 end
 
+# The result map for QuickSearch.
+private class QuickSearchTable
+       super JsonMapRead[String, QuickSearchResultList]
+       super HashMap[String, QuickSearchResultList]
+
+       redef fun provide_default_value(key) do
+               var v = new QuickSearchResultList
+               self[key] = v
+               return v
+       end
+end
+
+# A QuickSearch result list.
+private class QuickSearchResultList
+       super JsonSequenceRead[QuickSearchResult]
+       super Array[QuickSearchResult]
+end
+
+# A QuickSearch result.
+private class QuickSearchResult
+       super Jsonable
+
+       # The text of the link.
+       var txt: String
+
+       # The destination of the link.
+       var url: String
+
+       redef fun to_json do
+               return "\{\"txt\":{txt.to_json},\"url\":{url.to_json}\}"
+       end
+end
+
 # Nitdoc base page
 # Define page structure and properties
 abstract class NitdocPage
@@ -636,7 +673,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)
@@ -647,9 +684,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
@@ -883,7 +918,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
@@ -997,15 +1032,15 @@ class NitdocModule
                # build graph
                var op = new RopeBuffer
                var name = "dep_module_{mmodule.nitdoc_id}"
-               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")
+               op.append("digraph \"{name.escape_to_dot}\" \{ rankdir=BT; node[shape=none,margin=0,width=0,height=0,fontsize=10]; edge[dir=none,color=gray]; ranksep=0.2; nodesep=0.1;\n")
                for mmodule in poset do
                        if mmodule == self.mmodule then
-                               op.append("\"{mmodule.name}\"[shape=box,margin=0.03];\n")
+                               op.append("\"{mmodule.name.escape_to_dot}\"[shape=box,margin=0.03];\n")
                        else
-                               op.append("\"{mmodule.name}\"[URL=\"{mmodule.nitdoc_url}\"];\n")
+                               op.append("\"{mmodule.name.escape_to_dot}\"[URL=\"{mmodule.nitdoc_url.escape_to_dot}\"];\n")
                        end
                        for omodule in poset[mmodule].direct_greaters do
-                               op.append("\"{mmodule.name}\"->\"{omodule.name}\";\n")
+                               op.append("\"{mmodule.name.escape_to_dot}\"->\"{omodule.name.escape_to_dot}\";\n")
                        end
                end
                op.append("\}\n")
@@ -1062,19 +1097,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)
+               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)
@@ -1134,15 +1167,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
@@ -1150,15 +1182,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
@@ -1241,30 +1272,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
+                               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
@@ -1317,25 +1339,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]
-               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
-                       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)
@@ -1378,7 +1381,7 @@ class NitdocClass
 
                var op = new RopeBuffer
                var name = "dep_class_{mclass.nitdoc_id}"
-               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")
+               op.append("digraph \"{name.escape_to_dot}\" \{ rankdir=BT; node[shape=none,margin=0,width=0,height=0,fontsize=10]; edge[dir=none,color=gray]; ranksep=0.2; nodesep=0.1;\n")
                var classes = poset.to_a
                var todo = new Array[MClass]
                var done = new HashSet[MClass]
@@ -1389,18 +1392,18 @@ class NitdocClass
                        if done.has(c) then continue
                        done.add c
                        if c == mclass then
-                               op.append("\"{c.name}\"[shape=box,margin=0.03];\n")
+                               op.append("\"{c.name.escape_to_dot}\"[shape=box,margin=0.03];\n")
                        else
-                               op.append("\"{c.name}\"[URL=\"{c.nitdoc_url}\"];\n")
+                               op.append("\"{c.name.escape_to_dot}\"[URL=\"{c.nitdoc_url.escape_to_dot}\"];\n")
                        end
                        var smallers = poset[c].direct_smallers
                        if smallers.length < 10 then
                                for c2 in smallers do
-                                       op.append("\"{c2.name}\"->\"{c.name}\";\n")
+                                       op.append("\"{c2.name.escape_to_dot}\"->\"{c.name.escape_to_dot}\";\n")
                                end
                                todo.add_all smallers
                        else
-                               op.append("\"...\"->\"{c.name}\";\n")
+                               op.append("\"...\"->\"{c.name.escape_to_dot}\";\n")
                        end
                end
                op.append("\}\n")
@@ -1408,6 +1411,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
@@ -1466,7 +1537,12 @@ class NitdocProperty
 
        private fun tpl_properties(parent: TplSection) do
                # intro title
-               var section = new TplSection.with_title("intro", "Introduction")
+               var ns = mproperty.intro.mclassdef.mmodule.tpl_namespace
+               var section = new TplSection("intro")
+               var title = new Template
+               title.add "Introduction in "
+               title.add ns
+               section.title = title
                section.summary_title = "Introduction"
                section.add_child tpl_mpropdef_article(mproperty.intro)
                parent.add_child section
@@ -1484,7 +1560,7 @@ class NitdocProperty
                                parent.add_child new TplSection(mentity.nitdoc_id)
                        else if mentity isa MModule then
                                var ssection = new TplSection(mentity.nitdoc_id)
-                               var title = new Template
+                               title = new Template
                                title.add "in "
                                title.add mentity.tpl_namespace
                                ssection.title = title