nitcatalog: Update to use the package graph
[nit.git] / src / nitcatalog.nit
index d2aff2d..9dd3351 100644 (file)
 module nitcatalog
 
 import loader # Scan&load packages, groups and modules
-import doc::doc_down # Display mdoc
 import catalog
 
+import doc::templates::html_model
+
 # A HTML page in a catalog
 #
 # This is just a template with the header pre-filled and the footer injected at rendering.
@@ -131,62 +132,72 @@ g.defer=true; g.async=true; g.src=u+'piwik.js'; s.parentNode.insertBefore(g,s);
        end
 end
 
-redef class Catalog
-       # Return a empty `CatalogPage`.
-       fun new_page(rootpath: String): CatalogPage
+redef class NitdocDecorator
+       redef fun add_image(v, link, name, comment)
        do
-               return new CatalogPage(self, rootpath)
-       end
+               # Keep absolute links as is
+               if link.has_prefix("http://") or link.has_prefix("https://") then
+                       super
+                       return
+               end
 
-       # Add a contributor to a package
-       fun add_contrib(person: String, mpackage: MPackage, res: Template)
-       do
-               var name = person
-               var email = null
-               var page = null
-
-               # Regular expressions are broken, need to investigate.
-               # So split manually.
-               #
-               #var re = "([^<(]*?)(<([^>]*?)>)?(\\((.*)\\))?".to_re
-               #var m = (person+" ").search(re)
-               #print "{person}: `{m or else "?"}` `{m[1] or else "?"}` `{m[3] or else "?"}` `{m[5] or else "?"}`"
                do
-                       var sp1 = person.split_once_on("<")
-                       if sp1.length < 2 then
-                               break
-                       end
-                       var sp2 = sp1.last.split_once_on(">")
-                       if sp2.length < 2 then
-                               break
-                       end
-                       name = sp1.first.trim
-                       email = sp2.first.trim
-                       var sp3 = sp2.last.split_once_on("(")
-                       if sp3.length < 2 then
-                               break
-                       end
-                       var sp4 = sp3.last.split_once_on(")")
-                       if sp4.length < 2 then
-                               break
-                       end
-                       page = sp4.first.trim
+                       # Get the directory of the doc object to deal with the relative link
+                       var mdoc = current_mdoc
+                       if mdoc == null then break
+                       var source = mdoc.location.file
+                       if source == null then break
+                       var path = source.filename
+                       var stat = path.file_stat
+                       if stat == null then break
+                       if not stat.is_dir then path = path.dirname
+
+                       # Get the full path to the local resource
+                       var fulllink = path / link.to_s
+                       stat = fulllink.file_stat
+                       if stat == null then break
+
+                       # Get a collision-free catalog name for the resource
+                       var hash = fulllink.md5
+                       var ext = fulllink.file_extension
+                       if ext != null then hash = hash + "." + ext
+
+                       # Copy the local resource in the resource directory of the catalog
+                       var res = catalog.outdir / "res" / hash
+                       fulllink.file_copy_to(res)
+
+                       # Hijack the link in the html.
+                       link = ".." / "res" / hash
+                       super(v, link, name, comment)
+                       return
                end
 
-               var e = name.html_escape
-               res.add "<li>"
-               if page != null then
-                       res.add "<a href=\"{page.html_escape}\">"
-               end
-               if email != null then
-                       # TODO get more things from github by using the email as a key
-                       # "https://api.github.com/search/users?q={email}+in:email"
-                       var md5 = email.md5.to_lower
-                       res.add "<img src=\"https://secure.gravatar.com/avatar/{md5}?size=20&amp;default=retro\">&nbsp;"
-               end
-               res.add "{e}"
-               if page != null then res.add "</a>"
-               res.add "</li>"
+               # Something went bad
+               catalog.modelbuilder.toolcontext.error(current_mdoc.location, "Error: cannot find local image `{link}`")
+               super
+       end
+
+       # The registered catalog
+       #
+       # It is used to deal with relative links in images.
+       var catalog: Catalog is noautoinit
+end
+
+redef class Catalog
+       redef init
+       do
+               # Register `self` to the global NitdocDecorator
+               # FIXME this is ugly. But no better idea at the moment.
+               modelbuilder.model.nitdoc_md_processor.decorator.as(NitdocDecorator).catalog = self
+       end
+
+       # The output directory where to generate pages
+       var outdir: String is noautoinit
+
+       # Return a empty `CatalogPage`.
+       fun new_page(rootpath: String): CatalogPage
+       do
+               return new CatalogPage(self, rootpath)
        end
 
        # Recursively generate a level in the file tree of the *content* section
@@ -222,12 +233,19 @@ redef class Catalog
                var name = mpackage.name.html_escape
                res.more_head.add """<title>{{{name}}}</title>"""
 
-               res.add """
-<div class="content">
-<h1 class="package-name">{{{name}}}</h1>
-"""
+               res.add """<div class="content">"""
+
                var mdoc = mpackage.mdoc_or_fallback
-               if mdoc != null then res.add mdoc.html_documentation
+               if mdoc == null then
+                       res.add """<h1 class="package-name">{{{name}}}</h1>"""
+               else
+                       res.add """
+<div style="float: left">
+       <h1 class="package-name">{{{name}}}&nbsp;-&nbsp;</h1>
+</div>
+"""
+                       res.add mdoc.html_documentation
+               end
 
                res.add "<h2>Content</h2>"
                var ot = new OrderedTree[MConcern]
@@ -250,12 +268,12 @@ redef class Catalog
 <div class="sidebar">
 <ul class="box">
 """
-               var tryit = mpackage.metadata("upstream.tryit")
+               var tryit = mpackage.metadata.metadata("upstream.tryit")
                if tryit != null then
                        var e = tryit.html_escape
                        res.add "<li><a href=\"{e}\">Try<span style=\"color:white\">n</span>it!</a></li>\n"
                end
-               var apk = mpackage.metadata("upstream.apk")
+               var apk = mpackage.metadata.metadata("upstream.apk")
                if apk != null then
                        var e = apk.html_escape
                        res.add "<li><a href=\"{e}\">Android apk</a></li>\n"
@@ -263,16 +281,15 @@ redef class Catalog
 
                res.add """</ul>\n<ul class="box">\n"""
 
-               var homepage = mpackage.metadata("upstream.homepage")
+               var homepage = mpackage.metadata.metadata("upstream.homepage")
                if homepage != null then
                        var e = homepage.html_escape
                        res.add "<li><a href=\"{e}\">{e}</a></li>\n"
                end
-               var maintainer = mpackage.metadata("package.maintainer")
-               if maintainer != null then
-                       add_contrib(maintainer, mpackage, res)
+               for maintainer in mpackage.metadata.maintainers do
+                       res.add "<li>{maintainer.to_html}</li>"
                end
-               var license = mpackage.metadata("package.license")
+               var license = mpackage.metadata.metadata("package.license")
                if license != null then
                        var e = license.html_escape
                        res.add "<li><a href=\"http://opensource.org/licenses/{e}\">{e}</a> license</li>\n"
@@ -280,22 +297,22 @@ redef class Catalog
                res.add "</ul>\n"
 
                res.add "<h3>Source Code</h3>\n<ul class=\"box\">\n"
-               var browse = mpackage.metadata("upstream.browse")
+               var browse = mpackage.metadata.metadata("upstream.browse")
                if browse != null then
                        var e = browse.html_escape
                        res.add "<li><a href=\"{e}\">{e}</a></li>\n"
                end
-               var git = mpackage.metadata("upstream.git")
+               var git = mpackage.metadata.metadata("upstream.git")
                if git != null then
                        var e = git.html_escape
                        res.add "<li><tt>{e}</tt></li>\n"
                end
-               var last_date = mpackage.last_date
+               var last_date = mpackage.metadata.last_date
                if last_date != null then
                        var e = last_date.html_escape
                        res.add "<li>most recent commit: {e}</li>\n"
                end
-               var first_date = mpackage.first_date
+               var first_date = mpackage.metadata.first_date
                if first_date != null then
                        var e = first_date.html_escape
                        res.add "<li>oldest commit: {e}</li>\n"
@@ -306,16 +323,25 @@ redef class Catalog
                end
                res.add "</ul>\n"
 
+               res.add "<h3>Quality</h3>\n<ul class=\"box\">\n"
+               var errors = errors[mpackage]
+               if errors > 0 then
+                       res.add "<li>{errors} errors</li>\n"
+               end
+               res.add "<li>{warnings[mpackage]} warnings ({warnings_per_kloc[mpackage]}/kloc)</li>\n"
+               res.add "<li>{documentation_score[mpackage]}% documented</li>\n"
+               res.add "</ul>\n"
+
                res.add "<h3>Tags</h3>\n"
                var ts2 = new Array[String]
-               for t in mpackage.tags do
+               for t in mpackage.metadata.tags do
                        t = t.html_escape
                        ts2.add "<a href=\"../index.html#tag_{t}\">{t}</a>"
                end
                res.add_list(ts2, ", ", ", ")
 
-               if deps.has(mpackage) then
-                       var reqs = deps[mpackage].greaters.to_a
+               if deps.vertices.has(mpackage) then
+                       var reqs = deps.get_all_successors(mpackage)
                        reqs.remove(mpackage)
                        alpha_comparator.sort(reqs)
                        res.add "<h3>Requirements</h3>\n"
@@ -324,7 +350,7 @@ redef class Catalog
                        else
                                var list = new Array[String]
                                for r in reqs do
-                                       var direct = deps.has_direct_edge(mpackage, r)
+                                       var direct = deps.has_arc(mpackage, r)
                                        var s = "<a href=\"{r}.html\">"
                                        if direct then s += "<strong>"
                                        s += r.to_s
@@ -335,7 +361,7 @@ redef class Catalog
                                res.add_list(list, ", ", " and ")
                        end
 
-                       reqs = deps[mpackage].smallers.to_a
+                       reqs = deps.get_all_predecessors(mpackage)
                        reqs.remove(mpackage)
                        alpha_comparator.sort(reqs)
                        res.add "<h3>Clients</h3>\n"
@@ -344,7 +370,7 @@ redef class Catalog
                        else
                                var list = new Array[String]
                                for r in reqs do
-                                       var direct = deps.has_direct_edge(r, mpackage)
+                                       var direct = deps.has_arc(r, mpackage)
                                        var s = "<a href=\"{r}.html\">"
                                        if direct then s += "<strong>"
                                        s += r.to_s
@@ -356,11 +382,11 @@ redef class Catalog
                        end
                end
 
-               var contributors = mpackage.contributors
+               var contributors = mpackage.metadata.contributors
                if not contributors.is_empty then
                        res.add "<h3>Contributors</h3>\n<ul class=\"box\">"
                        for c in contributors do
-                               add_contrib(c, mpackage, res)
+                               res.add "<li>{c.to_html}</li>"
                        end
                        res.add "</ul>"
                end
@@ -401,18 +427,18 @@ redef class Catalog
        #
        # The list of keys is generated first to allow fast access to the correct `<h3>`.
        # `id_prefix` is used to give an id to the `<h3>` element.
-       fun list_by(map: MultiHashMap[String, MPackage], id_prefix: String): Template
+       fun list_by(map: MultiHashMap[Object, MPackage], id_prefix: String): Template
        do
                var res = new Template
                var keys = map.keys.to_a
                alpha_comparator.sort(keys)
-               var list = [for x in keys do "<a href=\"#{id_prefix}{x.html_escape}\">{x.html_escape}</a>"]
+               var list = [for x in keys do "<a href=\"#{id_prefix}{x.to_s.html_escape}\">{x.to_s.html_escape}</a>"]
                res.add_list(list, ", ", " and ")
 
                for k in keys do
                        var projs = map[k].to_a
                        alpha_comparator.sort(projs)
-                       var e = k.html_escape
+                       var e = k.to_s.html_escape
                        res.add "<h3 id=\"{id_prefix}{e}\">{e} ({projs.length})</h3>\n<ul>\n"
                        for p in projs do
                                res.add "<li>"
@@ -454,7 +480,7 @@ redef class Catalog
                res.add "<th data-field=\"name\" data-sortable=\"true\">name</th>\n"
                res.add "<th data-field=\"maint\" data-sortable=\"true\">maint</th>\n"
                res.add "<th data-field=\"contrib\" data-sortable=\"true\">contrib</th>\n"
-               if deps.not_empty then
+               if deps.vertices.not_empty then
                        res.add "<th data-field=\"reqs\" data-sortable=\"true\">reqs</th>\n"
                        res.add "<th data-field=\"dreqs\" data-sortable=\"true\">direct<br>reqs</th>\n"
                        res.add "<th data-field=\"cli\" data-sortable=\"true\">clients</th>\n"
@@ -465,25 +491,33 @@ redef class Catalog
                res.add "<th data-field=\"met\" data-sortable=\"true\">methods</th>\n"
                res.add "<th data-field=\"loc\" data-sortable=\"true\">lines</th>\n"
                res.add "<th data-field=\"score\" data-sortable=\"true\">score</th>\n"
+               res.add "<th data-field=\"errors\" data-sortable=\"true\">errors</th>\n"
+               res.add "<th data-field=\"warnings\" data-sortable=\"true\">warnings</th>\n"
+               res.add "<th data-field=\"warnings_per_kloc\" data-sortable=\"true\">w/kloc</th>\n"
+               res.add "<th data-field=\"doc\" data-sortable=\"true\">doc</th>\n"
                res.add "</tr></thead>"
                for p in mpackages do
                        res.add "<tr>"
                        res.add "<td><a href=\"p/{p.name}.html\">{p.name}</a></td>"
                        var maint = "?"
-                       if p.maintainers.not_empty then maint = p.maintainers.first
+                       if p.metadata.maintainers.not_empty then maint = p.metadata.maintainers.first.name.html_escape
                        res.add "<td>{maint}</td>"
-                       res.add "<td>{p.contributors.length}</td>"
-                       if deps.not_empty then
-                               res.add "<td>{deps[p].greaters.length-1}</td>"
-                               res.add "<td>{deps[p].direct_greaters.length}</td>"
-                               res.add "<td>{deps[p].smallers.length-1}</td>"
-                               res.add "<td>{deps[p].direct_smallers.length}</td>"
+                       res.add "<td>{p.metadata.contributors.length}</td>"
+                       if deps.vertices.not_empty then
+                               res.add "<td>{deps.get_all_successors(p).length-1}</td>"
+                               res.add "<td>{deps.successors(p).length}</td>"
+                               res.add "<td>{deps.get_all_predecessors(p).length-1}</td>"
+                               res.add "<td>{deps.predecessors(p).length}</td>"
                        end
                        res.add "<td>{mmodules[p]}</td>"
                        res.add "<td>{mclasses[p]}</td>"
                        res.add "<td>{mmethods[p]}</td>"
                        res.add "<td>{loc[p]}</td>"
                        res.add "<td>{score[p]}</td>"
+                       res.add "<td>{errors[p]}</td>"
+                       res.add "<td>{warnings[p]}</td>"
+                       res.add "<td>{warnings_per_kloc[p]}</td>"
+                       res.add "<td>{documentation_score[p]}</td>"
                        res.add "</tr>\n"
                end
                res.add "</table>\n"
@@ -498,6 +532,24 @@ redef class Catalog
        var piwik_site_id: Int = 1
 end
 
+redef class Person
+       redef fun to_html do
+               var res = ""
+               var e = name.html_escape
+               var page = self.page
+               if page != null then
+                       res += "<a href=\"{page.html_escape}\">"
+               end
+               var gravatar = self.gravatar
+               if gravatar != null then
+                       res += "<img src=\"https://secure.gravatar.com/avatar/{gravatar}?size=20&amp;default=retro\">&nbsp;"
+               end
+               res += e
+               if page != null then res += "</a>"
+               return res
+       end
+end
+
 var model = new Model
 var tc = new ToolContext
 
@@ -535,32 +587,22 @@ end
 
 # Get files or groups
 var args = tc.option_context.rest
+var mmodules
 if opt_no_parse.value then
-       modelbuilder.scan_full(args)
+       mmodules = modelbuilder.scan_full(args)
 else
-       modelbuilder.parse_full(args)
+       mmodules = modelbuilder.parse_full(args)
 end
+var mpackages = modelbuilder.model.mpackage_importation_graph.vertices
 
 # Scan packages and compute information
-for p in model.mpackages do
+for p in mpackages do
        var g = p.root
        assert g != null
        modelbuilder.scan_group(g)
-
-       # Load the module to process importation information
-       if opt_no_parse.value then continue
-
-       catalog.deps.add_node(p)
-       for gg in p.mgroups do for m in gg.mmodules do
-               for im in m.in_importation.direct_greaters do
-                       var ip = im.mpackage
-                       if ip == null or ip == p then continue
-                       catalog.deps.add_edge(p, ip)
-               end
-       end
 end
 
-if not opt_no_git.value then for p in model.mpackages do
+if not opt_no_git.value then for p in mpackages do
        catalog.git_info(p)
 end
 
@@ -571,6 +613,9 @@ end
 
 var out = opt_dir.value or else "catalog.out"
 (out/"p").mkdir
+(out/"res").mkdir
+
+catalog.outdir = out
 
 # Generate the css (hard coded)
 var css = """
@@ -674,11 +719,14 @@ css.write_to_file(out/"style.css")
 
 # PAGES
 
-for p in model.mpackages do
+for p in mpackages do
        # print p
        var f = "p/{p.name}.html"
        catalog.package_page(p)
        catalog.generate_page(p).write_to_file(out/f)
+       # copy ini
+       var ini = p.ini
+       if ini != null then ini.write_to_file(out/"p/{p.name}.ini")
 end
 
 # INDEX
@@ -694,11 +742,11 @@ index.add """
 index.add "<h2>Highlighted Packages</h2>\n"
 index.add catalog.list_best(catalog.score)
 
-if catalog.deps.not_empty then
+if catalog.deps.vertices.not_empty then
        index.add "<h2>Most Required</h2>\n"
        var reqs = new Counter[MPackage]
-       for p in model.mpackages do
-               reqs[p] = catalog.deps[p].smallers.length - 1
+       for p in mpackages do
+               reqs[p] = catalog.deps.get_all_successors(p).length - 1
        end
        index.add catalog.list_best(reqs)
 end
@@ -714,7 +762,7 @@ index.add """
 <div class="sidebar">
 <h3>Stats</h3>
 <ul class="box">
-<li>{{{model.mpackages.length}}} packages</li>
+<li>{{{mpackages.length}}} packages</li>
 <li>{{{catalog.maint2proj.length}}} maintainers</li>
 <li>{{{catalog.contrib2proj.length}}} contributors</li>
 <li>{{{catalog.tag2proj.length}}} tags</li>
@@ -746,6 +794,6 @@ page = catalog.new_page("")
 page.more_head.add "<title>Projets of Nit</title>"
 page.add """<div class="content">\n<h1>People of Nit</h1>\n"""
 page.add "<h2>Table of Projets</h2>\n"
-page.add catalog.table_packages(model.mpackages)
+page.add catalog.table_packages(mpackages.to_a)
 page.add "</div>\n"
 page.write_to_file(out/"table.html")