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
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)
# 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
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
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
--- /dev/null
+# 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
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
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)
# 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
# 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
# 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
# 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`
end
end
+# MIME types list.
fun media_types: MediaTypes do return once new MediaTypes
--- /dev/null
+<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&default=retro">
+ {{contributor.name}}
+ </li>
+ </ul>
+</div>
<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>
restrict: 'E',
scope: {
listEntities: '=',
+ listId: '@',
listTitle: '@',
listObjectFilter: '=',
},
--- /dev/null
+/*
+ * 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'
+ };
+ }])
+})();
.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);
+ },
+ }
+ }])
})();
*/
(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',
margin-bottom: 0px;
}
/*
+ * Users
+ */
+
+.avatar {
+ border-radius: 2px;
+}
+
+/*
* Code Highlighting
*/
-<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> <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>
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
--- /dev/null
+# 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
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.
import web_actions
import model_api
+import api_catalog
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.