Merge: nitunit: Fix documentation to reflect actual test discovery algorithm
authorJean Privat <jean@pryen.org>
Tue, 7 Jun 2016 16:07:41 +0000 (12:07 -0400)
committerJean Privat <jean@pryen.org>
Tue, 7 Jun 2016 16:07:41 +0000 (12:07 -0400)
Signed-off-by: Jean-Christophe Beaupré <jcbrinfo@users.noreply.github.com>

Pull-Request: #2166
Reviewed-by: Jean Privat <jean@pryen.org>
Reviewed-by: Alexandre Terrasa <alexandre@moz-code.org>

20 files changed:
lib/core/text/abstract_text.nit
lib/core/text/flat.nit
lib/core/text/test_abstract_text.nit [new file with mode: 0644]
lib/nitcorn/examples/src/htcpcp_server.nit
lib/nitcorn/http_request.nit
lib/nitcorn/http_response.nit
lib/nitcorn/media_types.nit
share/nitweb/directives/contributor-list.html [new file with mode: 0644]
share/nitweb/index.html
share/nitweb/javascripts/entities.js
share/nitweb/javascripts/index.js [new file with mode: 0644]
share/nitweb/javascripts/model.js
share/nitweb/javascripts/nitweb.js
share/nitweb/stylesheets/nitweb.css
share/nitweb/views/index.html
src/nitweb.nit
src/web/api_catalog.nit [new file with mode: 0644]
src/web/model_api.nit
src/web/web.nit
src/web/web_base.nit

index 8fefb24..e07e0d4 100644 (file)
@@ -590,10 +590,13 @@ abstract class Text
                return res.to_s
        end
 
-       # Escape " \ ' and non printable characters using the rules of literal C strings and characters
+       # Escape `"` `\` `'`, trigraphs and non printable characters using the rules of literal C strings and characters
        #
-       #     assert "abAB12<>&".escape_to_c         == "abAB12<>&"
+       #     assert "abAB12<>&".escape_to_c       == "abAB12<>&"
        #     assert "\n\"'\\".escape_to_c         == "\\n\\\"\\'\\\\"
+       #     assert "allo???!".escape_to_c        == "allo??\\?!"
+       #     assert "??=??/??'??(??)".escape_to_c == "?\\?=?\\?/??\\'?\\?(?\\?)"
+       #     assert "??!??<??>??-".escape_to_c    == "?\\?!?\\?<?\\?>?\\?-"
        #
        # Most non-printable characters (bellow ASCII 32) are escaped to an octal form `\nnn`.
        # Three digits are always used to avoid following digits to be interpreted as an element
@@ -617,6 +620,24 @@ abstract class Text
                                b.append("\\\'")
                        else if c == '\\' then
                                b.append("\\\\")
+                       else if c == '?' then
+                               # Escape if it is the last question mark of a ANSI C trigraph.
+                               var j = i + 1
+                               if j < length then
+                                       var next = chars[j]
+                                       # We ignore `??'` because it will be escaped as `??\'`.
+                                       if
+                                               next == '!' or
+                                               next == '(' or
+                                               next == ')' or
+                                               next == '-' or
+                                               next == '/' or
+                                               next == '<' or
+                                               next == '=' or
+                                               next == '>'
+                                       then b.add('\\')
+                               end
+                               b.add('?')
                        else if c.code_point < 32 then
                                b.add('\\')
                                var oct = c.code_point.to_base(8)
@@ -640,6 +661,7 @@ abstract class Text
        # The result might no be legal in C but be used in other languages
        #
        #     assert "ab|\{\}".escape_more_to_c("|\{\}") == "ab\\|\\\{\\\}"
+       #     assert "allo???!".escape_more_to_c("")     == "allo??\\?!"
        fun escape_more_to_c(chars: String): String
        do
                var b = new Buffer
index 9e70321..6273609 100644 (file)
@@ -225,6 +225,22 @@ redef class FlatText
                                req_esc += 1
                        else if c == 0x5Cu8 then
                                req_esc += 1
+                       else if c == 0x3Fu8 then
+                               var j = pos + 1
+                               if j < length then
+                                       var next = its[j]
+                                       # We ignore `??'` because it will be escaped as `??\'`.
+                                       if
+                                               next == 0x21u8 or
+                                               next == 0x28u8 or
+                                               next == 0x29u8 or
+                                               next == 0x2Du8 or
+                                               next == 0x2Fu8 or
+                                               next == 0x3Cu8 or
+                                               next == 0x3Du8 or
+                                               next == 0x3Eu8
+                                       then req_esc += 1
+                               end
                        else if c < 32u8 then
                                req_esc += 3
                        end
@@ -280,6 +296,27 @@ redef class FlatText
                                nns[opos] = 0x5Cu8
                                nns[opos + 1] = 0x5Cu8
                                opos += 2
+                       else if c == 0x3Fu8 then
+                               var j = pos + 1
+                               if j < length then
+                                       var next = its[j]
+                                       # We ignore `??'` because it will be escaped as `??\'`.
+                                       if
+                                               next == 0x21u8 or
+                                               next == 0x28u8 or
+                                               next == 0x29u8 or
+                                               next == 0x2Du8 or
+                                               next == 0x2Fu8 or
+                                               next == 0x3Cu8 or
+                                               next == 0x3Du8 or
+                                               next == 0x3Eu8
+                                       then
+                                               nns[opos] = 0x5Cu8
+                                               opos += 1
+                                       end
+                               end
+                               nns[opos] = 0x3Fu8
+                               opos += 1
                        else if c < 32u8 then
                                nns[opos] = 0x5Cu8
                                nns[opos + 1] = 0x30u8
diff --git a/lib/core/text/test_abstract_text.nit b/lib/core/text/test_abstract_text.nit
new file mode 100644 (file)
index 0000000..c67a991
--- /dev/null
@@ -0,0 +1,61 @@
+# This file is part of NIT ( http://www.nitlanguage.org ).
+#
+# This file is free software, which comes along with NIT.  This software is
+# distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
+# without  even  the implied warranty of  MERCHANTABILITY or  FITNESS FOR A
+# PARTICULAR PURPOSE.  You can modify it is you want,  provided this header
+# is kept unaltered, and a notification of the changes is added.
+# You  are  allowed  to  redistribute it and sell it, alone or is a part of
+# another product.
+
+module test_abstract_text is test_suite
+
+import test_suite
+import text
+intrude import ropes
+
+class TestText
+       super TestSuite
+
+       private var factories: Collection[TextFactory] = [
+               new ConcatFactory,
+               new RopeBufferFactory,
+               new FlatBufferFactory
+       : TextFactory]
+
+       fun test_escape_to_c do
+               for f in factories do
+                       assert f.create("abAB12<>&").escape_to_c       == "abAB12<>&"
+                       assert f.create("\n\"'\\").escape_to_c         == "\\n\\\"\\'\\\\"
+                       assert f.create("allo???!").escape_to_c        == "allo??\\?!"
+                       assert f.create("??=??/??'??(??)").escape_to_c == "?\\?=?\\?/??\\'?\\?(?\\?)"
+                       assert f.create("??!??<??>??-").escape_to_c    == "?\\?!?\\?<?\\?>?\\?-"
+               end
+       end
+end
+
+# A factory that creates instances of a particular implementation of `Text`
+interface TextFactory
+
+       # Create a `Text` instance from the specified string
+       fun create(s: String): Text is abstract
+end
+
+
+class ConcatFactory
+       super TextFactory
+
+       redef fun create(s) do return new Concat("", s)
+end
+
+class RopeBufferFactory
+       super TextFactory
+
+       redef fun create(s) do return new RopeBuffer.from(s)
+end
+
+class FlatBufferFactory
+       super TextFactory
+
+       redef fun create(s) do return new FlatBuffer.from(s)
+end
index 37fa87e..315d76f 100644 (file)
@@ -21,15 +21,19 @@ module htcpcp_server
 
 import nitcorn
 
+# Nitcorn Action used to answer requests.
 class HTCPCPAction
        super Action
+
+       # Brewing status.
        var brewing = false
-  var is_teapot = false
+
+       # Teapot status.
+       var is_teapot = false
 
        redef fun answer(http_request, turi) do
                var message: String
                var method = http_request.method
-               var headers = http_request.header
                var response: HttpResponse
 
                if is_teapot == true then
@@ -77,10 +81,13 @@ class HTCPCPAction
        end
 end
 
-
+# Nitcorn server.
 class HTCPCServer
+
+       # Port to listen to.
        var port: Int
 
+       # Start listening.
        fun run do
                var vh = new VirtualHost("localhost:{port}")
                vh.routes.add new Route("/", new HTCPCPAction)
index 9e94493..1833fca 100644 (file)
@@ -24,7 +24,7 @@ import core
 
 # A request received over HTTP, is build by `HttpRequestParser`
 class HttpRequest
-       private init do end
+       private init is old_style_init do end
 
        # HTTP protocol version
        var http_version: String
@@ -114,6 +114,7 @@ class HttpRequestParser
        # Words of the first line
        private var first_line = new Array[String]
 
+       # Parse the `first_line`, `header_fields` and `body` of `full_request`.
        fun parse_http_request(full_request: String): nullable HttpRequest
        do
                clear_data
index 3d0fd33..08d2968 100644 (file)
@@ -97,7 +97,8 @@ class HttpStatusCodes
        # All know code and their message
        var codes = new HashMap[Int, String]
 
-       protected init do insert_status_codes
+       # Init the status `codes` list.
+       protected init is old_style_init do insert_status_codes
 
        # Get the message associated to the status `code`, return `null` in unknown
        fun [](code: Int): nullable String
index 37dd9db..305f314 100644 (file)
@@ -20,6 +20,8 @@ module media_types
 
 # Map of known MIME types
 class MediaTypes
+
+       # MIME types by extensions.
        protected var types = new HashMap[String, String]
 
        # Get the type/subtype associated to a file extension `ext`
@@ -106,4 +108,5 @@ class MediaTypes
        end
 end
 
+# MIME types list.
 fun media_types: MediaTypes do return once new MediaTypes
diff --git a/share/nitweb/directives/contributor-list.html b/share/nitweb/directives/contributor-list.html
new file mode 100644 (file)
index 0000000..a46cce9
--- /dev/null
@@ -0,0 +1,11 @@
+<div ng-if='listContributors.length > 0'>
+       <h3 id={{listId}}>
+               <span>{{listTitle}}</span>
+       </h3>
+       <ul class='list-unstyled user-list'>
+               <li ng-repeat='contributor in listContributors'>
+                       <img class='avatar' src="https://secure.gravatar.com/avatar/{{contributor.hash}}?size=20&amp;default=retro">
+                       {{contributor.name}}
+               </li>
+       </ul>
+</div>
index 17bcd23..1b99a81 100644 (file)
@@ -56,5 +56,6 @@
                <script src='/javascripts/model.js'></script>
                <script src='/javascripts/entities.js'></script>
                <script src='/javascripts/ui.js'></script>
+               <script src='/javascripts/index.js'></script>
        </body>
 </html>
index 255602c..9d9a452 100644 (file)
@@ -94,6 +94,7 @@
                                restrict: 'E',
                                scope: {
                                        listEntities: '=',
+                                       listId: '@',
                                        listTitle: '@',
                                        listObjectFilter: '=',
                                },
diff --git a/share/nitweb/javascripts/index.js b/share/nitweb/javascripts/index.js
new file mode 100644 (file)
index 0000000..534f462
--- /dev/null
@@ -0,0 +1,92 @@
+/*
+ * Copyright 2016 Alexandre Terrasa <alexandre@moz-code.org>.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+(function() {
+       angular
+               .module('index', ['model', 'ngSanitize'])
+
+               .run(['$anchorScroll', function($anchorScroll) {
+                       $anchorScroll.yOffset = 80;
+               }])
+
+               .controller('IndexCtrl', ['Catalog', '$routeParams', '$sce', '$scope', '$location', '$anchorScroll', function(Catalog, $routeParams, $sce, $scope, $location, $anchorScroll) {
+                       this.loadHighlighted = function() {
+                               Catalog.loadHightlighted(
+                                       function(data) {
+                                               $scope.highlighted = data;
+                                       }, function(err) {
+                                               $scope.error = err;
+                                       });
+                       };
+
+                       this.loadMostRequired = function() {
+                               Catalog.loadMostRequired(
+                                       function(data) {
+                                               $scope.required = data;
+                                       }, function(err) {
+                                               $scope.error = err;
+                                       });
+                       };
+
+                       this.loadByTags = function() {
+                               Catalog.loadByTags(
+                                       function(data) {
+                                               $scope.bytags = data;
+                                       }, function(err) {
+                                               $scope.error = err;
+                                       });
+                       };
+
+                       this.loadStats = function() {
+                               Catalog.loadStats(
+                                       function(data) {
+                                               $scope.stats = data;
+                                       }, function(err) {
+                                               $scope.error = err;
+                                       });
+                       };
+
+                       this.loadContributors = function() {
+                               Catalog.loadContributors(
+                                       function(data) {
+                                               $scope.contributors = data;
+                                       }, function(err) {
+                                               $scope.error = err;
+                                       });
+                       };
+
+
+                       this.scrollTo = function(hash) {
+                               $anchorScroll(hash);
+                       }
+
+                       this.loadHighlighted();
+                       this.loadStats();
+                       this.loadContributors();
+               }])
+
+               .directive('contributorList', ['Model', function(Model) {
+                       return {
+                               restrict: 'E',
+                               scope: {
+                                       listId: '@',
+                                       listTitle: '@',
+                                       listContributors: '='
+                               },
+                               templateUrl: '/directives/contributor-list.html'
+                       };
+               }])
+})();
index 7d5051b..3bf927c 100644 (file)
@@ -22,6 +22,7 @@
 
                .factory('Model', [ '$http', function($http) {
                        return {
+
                                loadEntity: function(id, cb, cbErr) {
                                        $http.get(apiUrl + '/entity/' + id)
                                                .success(cb)
                                }
                        };
                }])
+
+               .factory('Catalog', [ '$http', function($http) {
+                       return {
+                               loadHightlighted: function(cb, cbErr) {
+                                       $http.get(apiUrl + '/catalog/highlighted')
+                                               .success(cb)
+                                               .error(cbErr);
+                               },
+
+                               loadMostRequired: function(cb, cbErr) {
+                                       $http.get(apiUrl + '/catalog/required')
+                                               .success(cb)
+                                               .error(cbErr);
+                               },
+
+                               loadByTags: function(cb, cbErr) {
+                                       $http.get(apiUrl + '/catalog/bytags')
+                                               .success(cb)
+                                               .error(cbErr);
+                               },
+
+                               loadStats: function(cb, cbErr) {
+                                       $http.get(apiUrl + '/catalog/stats')
+                                               .success(cb)
+                                               .error(cbErr);
+                               },
+
+                               loadContributors: function(cb, cbErr) {
+                                       $http.get(apiUrl + '/catalog/contributors')
+                                               .success(cb)
+                                               .error(cbErr);
+                               },
+                       }
+               }])
 })();
index 28291fb..93b7c3a 100644 (file)
  */
 
 (function() {
-       angular.module('nitweb', ['ngRoute', 'ngSanitize', 'entities'])
+       angular.module('nitweb', ['ngRoute', 'ngSanitize', 'entities', 'index'])
 
        .config(function($routeProvider, $locationProvider) {
                $routeProvider
                        .when('/', {
-                               templateUrl: 'views/index.html'
+                               templateUrl: 'views/index.html',
+                               controller: 'IndexCtrl',
+                               controllerAs: 'indexCtrl'
                        })
                        .when('/package/:id', {
                                templateUrl: 'views/package.html',
index 2d38457..eb82ee6 100644 (file)
@@ -206,6 +206,14 @@ entity-list:hover .btn-filter {
        margin-bottom: 0px;
 }
 /*
+ * Users
+ */
+
+.avatar {
+       border-radius: 2px;
+}
+
+/*
  * Code Highlighting
  */
 
index 9f9b93d..b7bee8e 100644 (file)
@@ -1 +1,70 @@
-<h1>Hello nitweb!</h1>
+<div class='container-fluid'>
+       <div class='page-header'>
+               <h2>Welcome to NitWeb!</h2>
+               <p class='text-muted'>The Nit knowledge base.</p>
+       </div>
+
+       <ul class='nav nav-tabs' role='tablist'>
+               <li role='presentation' class='active'>
+                       <a data-toggle='tab' role='tab' data-target='#highlighted' aria-controls='highlighted'>
+                               <span class='glyphicon glyphicon-book'/> Highlighed
+                       </a>
+               </li>
+               <li role='presentation'>
+                       <a data-toggle='tab' role='tab' data-target='#required' aria-controls='required'
+                               ng-click='indexCtrl.loadMostRequired()'>
+                               <span class='glyphicon glyphicon-book'/> Most required
+                       </a>
+               </li>
+               <li role='presentation'>
+                       <a data-toggle='tab' role='tab' data-target='#bytags' aria-controls='bytags'
+                               ng-click='indexCtrl.loadByTags()'>
+                               <span class='glyphicon glyphicon-book'/> By tags
+                       </a>
+               </li>
+       </ul>
+       <table class='table'>
+               <tr>
+                       <td ng-repeat='(key, value) in stats'>
+                               <h5><strong>{{value}}</strong>&nbsp;<span>{{key}}</span></h5>
+                       </td>
+               </tr>
+       </table>
+
+       <div class='container-fluid'>
+               <div class='col-xs-9'>
+                       <div class='tab-content'>
+                               <div role='tabpanel' class='tab-pane fade in active' id='highlighted'>
+                                       <entity-list list-title='Highlighted packages'
+                                               list-entities='highlighted'
+                                               list-object-filter='{}' />
+                               </div>
+                               <div role='tabpanel' class='tab-pane fade' id='required'>
+                                       <entity-list list-title='Most required'
+                                               list-entities='required'
+                                               list-object-filter='{}' />
+                               </div>
+                               <div role='tabpanel' class='tab-pane fade' id='bytags'>
+                                       <h3>Tags</h3>
+                                       <div class='container-fluid'>
+                                               <div class='col-xs-3' ng-repeat='(tag, packages) in bytags'>
+                                                       <span class='badge'>{{packages.length}}</span>
+                                                       <a ng-click='indexCtrl.scrollTo(tag)'>{{tag}}</a>
+                                               </div>
+                                       </div>
+                                       <div ng-repeat='(tag, packages) in bytags'>
+                                               <entity-list list-id='{{tag}}' list-title='{{tag}}'
+                                                       list-entities='packages'
+                                                       list-object-filter='{}' />
+                                       </div>
+                               </div>
+                       </div>
+               </div>
+               <div class='col-xs-3'>
+                       <contributor-list list-title='Maintainers'
+                                       list-contributors='contributors.maintainers' />
+                       <contributor-list list-title='Contributors'
+                                       list-contributors='contributors.contributors' />
+               </div>
+       </div>
+</div>
index 93d15aa..13e3dce 100644 (file)
@@ -43,20 +43,66 @@ private class NitwebPhase
                var model = mainmodule.model
                var modelbuilder = toolcontext.modelbuilder
 
+               # Build catalog
+               var catalog = new Catalog(modelbuilder)
+               for mpackage in model.mpackages do
+                       catalog.deps.add_node(mpackage)
+                       for mgroup in mpackage.mgroups do
+                               for mmodule in mgroup.mmodules do
+                                       for imported in mmodule.in_importation.direct_greaters do
+                                               var ip = imported.mpackage
+                                               if ip == null or ip == mpackage then continue
+                                               catalog.deps.add_edge(mpackage, ip)
+                                       end
+                               end
+                       end
+                       catalog.git_info(mpackage)
+                       catalog.package_page(mpackage)
+               end
+
                # Run the server
                var host = toolcontext.opt_host.value or else "localhost"
                var port = toolcontext.opt_port.value
 
                var app = new App
 
-               app.use("/api", new APIRouter(model, modelbuilder, mainmodule))
+               app.use_before("/*", new RequestClock)
+               app.use("/api", new APIRouter(model, modelbuilder, mainmodule, catalog))
                app.use("/doc/:namespace", new DocAction(model, mainmodule, modelbuilder))
                app.use("/*", new StaticHandler(toolcontext.share_dir / "nitweb", "index.html"))
+               app.use_after("/*", new ConsoleLog)
 
                app.listen(host, port.to_i)
        end
 end
 
+# Group all api handlers in one router.
+class APIRouter
+       super Router
+
+       # Model to pass to handlers.
+       var model: Model
+
+       # ModelBuilder to pass to handlers.
+       var modelbuilder: ModelBuilder
+
+       # Mainmodule to pass to handlers.
+       var mainmodule: MModule
+
+       # Catalog to pass to handlers.
+       var catalog: Catalog
+
+       init do
+               use("/catalog", new APICatalogRouter(model, mainmodule, catalog))
+               use("/list", new APIList(model, mainmodule))
+               use("/search", new APISearch(model, mainmodule))
+               use("/random", new APIRandom(model, mainmodule))
+               use("/entity/:id", new APIEntity(model, mainmodule))
+               use("/code/:id", new APIEntityCode(model, mainmodule, modelbuilder))
+               use("/uml/:id", new APIEntityUML(model, mainmodule))
+       end
+end
+
 # build toolcontext
 var toolcontext = new ToolContext
 var tpl = new Template
diff --git a/src/web/api_catalog.nit b/src/web/api_catalog.nit
new file mode 100644 (file)
index 0000000..72d207f
--- /dev/null
@@ -0,0 +1,138 @@
+# This file is part of NIT ( http://www.nitlanguage.org ).
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+module api_catalog
+
+import web_base
+import catalog
+
+# Group all api handlers in one router.
+class APICatalogRouter
+       super Router
+
+       # Model to pass to handlers.
+       var model: Model
+
+       # Mainmodule to pass to handlers.
+       var mainmodule: MModule
+
+       # Catalog to pass to handlers.
+       var catalog: Catalog
+
+       init do
+               use("/highlighted", new APICatalogHighLighted(model, mainmodule, catalog))
+               use("/required", new APICatalogMostRequired(model, mainmodule, catalog))
+               use("/bytags", new APICatalogByTags(model, mainmodule, catalog))
+               use("/contributors", new APICatalogContributors(model, mainmodule, catalog))
+               use("/stats", new APICatalogStats(model, mainmodule, catalog))
+       end
+end
+
+abstract class APICatalogHandler
+       super APIHandler
+
+       var catalog: Catalog
+
+       # List the 10 best packages from `cpt`
+       fun list_best(cpt: Counter[MPackage]): JsonArray do
+               var res = new JsonArray
+               var best = cpt.sort
+               for i in [1..10] do
+                       if i > best.length then break
+                       res.add best[best.length-i]
+               end
+               return res
+       end
+
+       # List packages by group.
+       fun list_by(map: MultiHashMap[Object, MPackage]): JsonObject do
+               var res = new JsonObject
+               var keys = map.keys.to_a
+               alpha_comparator.sort(keys)
+               for k in keys do
+                       var projs = map[k].to_a
+                       alpha_comparator.sort(projs)
+                       res[k.to_s.html_escape] = new JsonArray.from(projs)
+               end
+               return res
+       end
+end
+
+class APICatalogStats
+       super APICatalogHandler
+
+       redef fun get(req, res) do
+               var obj = new JsonObject
+               obj["packages"] = model.mpackages.length
+               obj["maintainers"] = catalog.maint2proj.length
+               obj["contributors"] = catalog.contrib2proj.length
+               obj["modules"] = catalog.mmodules.sum
+               obj["classes"] = catalog.mclasses.sum
+               obj["methods"] = catalog.mmethods.sum
+               obj["loc"] = catalog.loc.sum
+               res.json obj
+       end
+end
+
+class APICatalogHighLighted
+       super APICatalogHandler
+
+       redef fun get(req, res) do res.json list_best(catalog.score)
+end
+
+class APICatalogMostRequired
+       super APICatalogHandler
+
+       redef fun get(req, res) do
+               if catalog.deps.not_empty then
+                       var reqs = new Counter[MPackage]
+                       for p in model.mpackages do
+                               reqs[p] = catalog.deps[p].smallers.length - 1
+                       end
+                       res.json list_best(reqs)
+                       return
+               end
+               res.json new JsonArray
+       end
+end
+
+class APICatalogByTags
+       super APICatalogHandler
+
+       redef fun get(req, res) do res.json list_by(catalog.tag2proj)
+end
+
+class APICatalogContributors
+       super APICatalogHandler
+
+       redef fun get(req, res) do
+               var obj = new JsonObject
+               obj["maintainers"] = new JsonArray.from(catalog.maint2proj.keys)
+               obj["contributors"] = new JsonArray.from(catalog.contrib2proj.keys)
+               res.json obj
+       end
+end
+
+redef class Person
+       super Jsonable
+
+       redef fun to_json do
+               var obj = new JsonObject
+               obj["name"] = name
+               obj["email"] = email
+               obj["page"] = page
+               obj["hash"] = (email or else "").md5.to_lower
+               return obj.to_json
+       end
+end
index 9bbe5ad..080dcb2 100644 (file)
@@ -18,65 +18,6 @@ import web_base
 import highlight
 import uml
 
-# Specific handler for nitweb API.
-abstract class APIHandler
-       super ModelHandler
-
-       # The JSON API does not filter anything by default.
-       #
-       # So we can cache the model view.
-       var view: ModelView is lazy do
-               var view = new ModelView(model)
-               view.min_visibility = private_visibility
-               view.include_fictive = true
-               view.include_empty_doc = true
-               view.include_attribute = true
-               view.include_test_suite = true
-               return view
-       end
-
-       # Try to load the mentity from uri with `/:id`.
-       #
-       # Send 400 if `:id` is null.
-       # Send 404 if no entity is found.
-       # Return null in both cases.
-       fun mentity_from_uri(req: HttpRequest, res: HttpResponse): nullable MEntity do
-               var id = req.param("id")
-               if id == null then
-                       res.error 400
-                       return null
-               end
-               var mentity = find_mentity(view, id)
-               if mentity == null then
-                       res.error 404
-               end
-               return mentity
-       end
-end
-
-# Group all api handlers in one router.
-class APIRouter
-       super Router
-
-       # Model to pass to handlers.
-       var model: Model
-
-       # ModelBuilder to pass to handlers.
-       var modelbuilder: ModelBuilder
-
-       # Mainmodule to pass to handlers.
-       var mainmodule: MModule
-
-       init do
-               use("/list", new APIList(model, mainmodule))
-               use("/search", new APISearch(model, mainmodule))
-               use("/random", new APIRandom(model, mainmodule))
-               use("/entity/:id", new APIEntity(model, mainmodule))
-               use("/code/:id", new APIEntityCode(model, mainmodule, modelbuilder))
-               use("/uml/:id", new APIEntityUML(model, mainmodule))
-       end
-end
-
 # List all mentities.
 #
 # MEntities can be filtered on their kind using the `k` parameter.
index c6a12f0..2a22001 100644 (file)
@@ -17,3 +17,4 @@ module web
 
 import web_actions
 import model_api
+import api_catalog
index ec9bf75..fcaac53 100644 (file)
@@ -51,6 +51,42 @@ class ModelHandler
        end
 end
 
+# Specific handler for nitweb API.
+abstract class APIHandler
+       super ModelHandler
+
+       # The JSON API does not filter anything by default.
+       #
+       # So we can cache the model view.
+       var view: ModelView is lazy do
+               var view = new ModelView(model)
+               view.min_visibility = private_visibility
+               view.include_fictive = true
+               view.include_empty_doc = true
+               view.include_attribute = true
+               view.include_test_suite = true
+               return view
+       end
+
+       # Try to load the mentity from uri with `/:id`.
+       #
+       # Send 400 if `:id` is null.
+       # Send 404 if no entity is found.
+       # Return null in both cases.
+       fun mentity_from_uri(req: HttpRequest, res: HttpResponse): nullable MEntity do
+               var id = req.param("id")
+               if id == null then
+                       res.error 400
+                       return null
+               end
+               var mentity = find_mentity(view, id)
+               if mentity == null then
+                       res.error 404
+               end
+               return mentity
+       end
+end
+
 # A NitView is rendered by an action.
 interface NitView
        # Renders this view and returns something that can be written to a HTTP response.