<commit_before>﻿var j2w = require('./json2wadl'),
    jade = require('jade'),
    fs = require('fs'),
    methods = require('./methods'),
    path = require('path');

var jadeTemplate = fs.readFileSync(path.join(__dirname, 'doc.jade'), 'utf8');

var DocRouter = function (connectRouter, baseUrl) {
    if (!connectRouter) throw new Error("Connect router function is missing.");
    if (!baseUrl) throw new Error("A base url is missing.");

    this.connectRouter = null;
    this.methodJsons = [];
    this.baseUrl = baseUrl;
    this.wadl = null;

    var self = this;
    self.connectRouter = connectRouter;

    this.connectRouter.get("/!!", function (req, res) {
        getWadl(req, res);
    });


    this.connectRouter.options("/", function (req, res) {
        getWadl(req, res);
    });

    var method,
        i,
        l;

    for (i = 0, l = methods.length; i < l; i++) {
        method = methods[i];
        // override the original router method
        this.connectRouter[method] = (function (method, originalMethodFunction) {
            return function (route) {
                var args = arguments;
                var handlersCount = args.length;
                // Multiple arguments may exist for multiple middlewares. The Json describing the method is the last argument.
                var methodJson = {};
                if (typeof(args[args.length - 1]) === 'object'){
                    methodJson = args[args.length - 1];
                    handlersCount -= 1;
                }
                methodJson.method = method.toUpperCase();
                methodJson.path = route;

                self.methodJsons.push(methodJson);

                // call the original router with the original arguments

                var originalArgs = Array.prototype.slice.call(args, 0, handlersCount);
                originalMethodFunction.apply(self.connectRouter, originalArgs);
            };
        }(method, self.connectRouter[method]));
    }

    function getWadl(req, res) {
        var htmlTemplate;
        if (req.headers.accept &&
            (~req.headers.accept.indexOf('application/json') || ~req.headers.accept.indexOf('text/json')))
        {
            res.writeHead(200, { 'Content-Type': 'application/json' });
            res.end(JSON.stringify(self.methodJsons));
            return;
        }

        if (req.headers.accept &&
            (~req.headers.accept.indexOf('text/html') || ~req.headers.accept.indexOf('text/plain')))
        {
            res.writeHead(200, { 'Content-Type': 'text/html' });
            if (!self.html) {
                try {
                    htmlTemplate = jade.compile(jadeTemplate);
                    self.html = htmlTemplate({methodJsons: self.methodJsons, baseUrl: self.baseUrl});
                } catch(e) {
                    console.error(e);
                }
            }
            res.end(self.html);
            return;
        }
<commit_msg>Change Jade module to Pug module.<commit_after>﻿var j2w = require('./json2wadl'),
    pug = require('pug'),
    fs = require('fs'),
    methods = require('./methods'),
    path = require('path');

var pugTemplate = fs.readFileSync(path.join(__dirname, 'doc.pug'), 'utf8');

var DocRouter = function (connectRouter, baseUrl) {
    if (!connectRouter) throw new Error("Connect router function is missing.");
    if (!baseUrl) throw new Error("A base url is missing.");

    this.connectRouter = null;
    this.methodJsons = [];
    this.baseUrl = baseUrl;
    this.wadl = null;

    var self = this;
    self.connectRouter = connectRouter;

    this.connectRouter.get("/!!", function (req, res) {
        getWadl(req, res);
    });


    this.connectRouter.options("/", function (req, res) {
        getWadl(req, res);
    });

    var method,
        i,
        l;

    for (i = 0, l = methods.length; i < l; i++) {
        method = methods[i];
        // override the original router method
        this.connectRouter[method] = (function (method, originalMethodFunction) {
            return function (route) {
                var args = arguments;
                var handlersCount = args.length;
                // Multiple arguments may exist for multiple middlewares. The Json describing the method is the last argument.
                var methodJson = {};
                if (typeof(args[args.length - 1]) === 'object'){
                    methodJson = args[args.length - 1];
                    handlersCount -= 1;
                }
                methodJson.method = method.toUpperCase();
                methodJson.path = route;

                self.methodJsons.push(methodJson);

                // call the original router with the original arguments

                var originalArgs = Array.prototype.slice.call(args, 0, handlersCount);
                originalMethodFunction.apply(self.connectRouter, originalArgs);
            };
        }(method, self.connectRouter[method]));
    }

    function getWadl(req, res) {
        var htmlTemplate;
        if (req.headers.accept &&
            (~req.headers.accept.indexOf('application/json') || ~req.headers.accept.indexOf('text/json')))
        {
            res.writeHead(200, { 'Content-Type': 'application/json' });
            res.end(JSON.stringify(self.methodJsons));
            return;
        }

        if (req.headers.accept &&
            (~req.headers.accept.indexOf('text/html') || ~req.headers.accept.indexOf('text/plain')))
        {
            res.writeHead(200, { 'Content-Type': 'text/html' });
            if (!self.html) {
                try {
                    htmlTemplate = pug.compile(pugTemplate);
                    self.html = htmlTemplate({methodJsons: self.methodJsons, baseUrl: self.baseUrl});
                } catch(e) {
                    console.error(e);
                }
            }
            res.end(self.html);
            return;
        }

{'fraction_non_alphanumeric': 0.13135593220338984, 'fraction_numerical': 0.0047457627118644066, 'mean_word_length': 1.8631732168850073, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1604342', 'n_tokens_mistral': 1672, 'n_tokens_neox': 1581, 'n_words': 363}