<commit_before>/*jslint browser: false */
/*globals */
/*! Copyright (C) 2013 by <NAME>, Switzerland. All Rights Reserved. !*/

var express    = require("express");
var fs         = require("fs");
var http       = require("http");
var md         = require("node-markdown").Markdown;
var path       = require("path");
var winston    = require("winston");
var app        = express();
var attachment = require("./attachment");
var pkg        = require("./pkg");

function readRegistry(app) {
  var registry = {};
  var registryFile = app.get("registryFile");
  if (fs.existsSync(registryFile)) {
    registry = JSON.parse(fs.readFileSync(registryFile, "utf8"));
  }
  app.set("registry", registry);
}

if (process.env.NOPAR_LOGFILE) {
  winston.add(winston.transports.File, {
    level    : process.env.NOPAR_LOGLEVEL || "info",
    colorize : false,
    filename : process.env.NOPAR_LOGFILE,
    maxsize  : 1*1024*1024*1024,
    maxFiles : 10,
    json     : false
  });

  var winstonStream = {
    write : function (message, encoding) {
      winston.info(message);
    }
  };
  app.use(express.logger({ stream : winstonStream }));
}

app.configure(function () {

  var registryPath = process.env.NOPAR_REGISTRY_PATH ||
                     path.normalize(path.join(__dirname, "..", "registry"));
  app.set("registryPath", registryPath);
  app.set("registryFile", path.join(registryPath, "registry.json"));

  var forwarder = {
    registry : "https://registry.npmjs.org"
  };
  app.set("forwarder", forwarder);
  app.set("hostname", process.env.NOPAR_HOSTNAME || "localhost");
  app.set("port", process.env.NOPAR_PORT || 5984);
  app.set("views", path.normalize(path.join(__dirname, "../views")));
  app.set("view engine", "ejs");
  app.use(express.favicon());
  app.use(express.logger("dev"));
  app.use(express.bodyParser());
  app.use(express.methodOverride());
  app.use(express.cookieParser("your secret here"));
  app.use(express.session());
  app.use(app.router);
  app.use("/-/css", require("stylus").middleware(
    path.normalize(path.join(__dirname, "..", "public", "css"))
  ));
  app.use("/-", express["static"](
    path.normalize(path.join(__dirname, "..", "public"))
  ));

  // Initialize registry
  winston.info("Registry Path: " + registryPath);
  if (!fs.existsSync(registryPath)) {
    fs.mkdirSync(registryPath, "750");
  }
  readRegistry(app);
});

app.configure("development", function() {
  app.use(express.errorHandler());
});

function renderIndex(req, res, message) {
  var me = require("../package.json");
  var registry = app.get("registry");
  var pkgName, i;
  var vars = {
    title    : me.name + "@" + me.version,
    version  : me.version,
    message  : typeof message === "string" ? message : null,
    query    : req.query.q || ""
  };

  if (req.query.q) {
    var found = {};
    for (pkgName in registry) {
      if (pkgName.indexOf(req.query.q) >= 0) {
        found[pkgName] = registry[pkgName];
      }
    }
    vars.registry = found;
  } else {
    vars.registry = registry;
  }

  for (pkgName in vars.registry) {
    var pkg = vars.registry[pkgName];
    var readme = pkg.readme || pkg.versions[pkg["dist-tags"].latest].readme;
    if (readme) {
      pkg["_readme"] = md(readme);
    }

    pkg["_proxied"] = pkg["_fwd-dists"] ? true : false;
    pkg["_local"] = !pkg["_proxied"];
  }

  res.render("index", vars);
}

/**
 * GET index.
 */
app.get("/", renderIndex);

/*<commit_msg>Make sure '<' and '>' are replaced by &lt; and &gt; in readme.<commit_after>/*jslint browser: false */
/*globals */
/*! Copyright (C) 2013 by <NAME>, Switzerland. All Rights Reserved. !*/

var express    = require("express");
var fs         = require("fs");
var http       = require("http");
var md         = require("node-markdown").Markdown;
var path       = require("path");
var winston    = require("winston");
var app        = express();
var attachment = require("./attachment");
var pkg        = require("./pkg");

function readRegistry(app) {
  var registry = {};
  var registryFile = app.get("registryFile");
  if (fs.existsSync(registryFile)) {
    registry = JSON.parse(fs.readFileSync(registryFile, "utf8"));
  }
  app.set("registry", registry);
}

if (process.env.NOPAR_LOGFILE) {
  winston.add(winston.transports.File, {
    level    : process.env.NOPAR_LOGLEVEL || "info",
    colorize : false,
    filename : process.env.NOPAR_LOGFILE,
    maxsize  : 1*1024*1024*1024,
    maxFiles : 10,
    json     : false
  });

  var winstonStream = {
    write : function (message, encoding) {
      winston.info(message);
    }
  };
  app.use(express.logger({ stream : winstonStream }));
}

app.configure(function () {

  var registryPath = process.env.NOPAR_REGISTRY_PATH ||
                     path.normalize(path.join(__dirname, "..", "registry"));
  app.set("registryPath", registryPath);
  app.set("registryFile", path.join(registryPath, "registry.json"));

  var forwarder = {
    registry : "https://registry.npmjs.org"
  };
  app.set("forwarder", forwarder);
  app.set("hostname", process.env.NOPAR_HOSTNAME || "localhost");
  app.set("port", process.env.NOPAR_PORT || 5984);
  app.set("views", path.normalize(path.join(__dirname, "../views")));
  app.set("view engine", "ejs");
  app.use(express.favicon());
  app.use(express.logger("dev"));
  app.use(express.bodyParser());
  app.use(express.methodOverride());
  app.use(express.cookieParser("your secret here"));
  app.use(express.session());
  app.use(app.router);
  app.use("/-/css", require("stylus").middleware(
    path.normalize(path.join(__dirname, "..", "public", "css"))
  ));
  app.use("/-", express["static"](
    path.normalize(path.join(__dirname, "..", "public"))
  ));

  // Initialize registry
  winston.info("Registry Path: " + registryPath);
  if (!fs.existsSync(registryPath)) {
    fs.mkdirSync(registryPath, "750");
  }
  readRegistry(app);
});

app.configure("development", function() {
  app.use(express.errorHandler());
});

function renderIndex(req, res, message) {
  var me = require("../package.json");
  var registry = app.get("registry");
  var pkgName, i;
  var vars = {
    title    : me.name + "@" + me.version,
    version  : me.version,
    message  : typeof message === "string" ? message : null,
    query    : req.query.q || ""
  };

  if (req.query.q) {
    var found = {};
    for (pkgName in registry) {
      if (pkgName.indexOf(req.query.q) >= 0) {
        found[pkgName] = registry[pkgName];
      }
    }
    vars.registry = found;
  } else {
    vars.registry = registry;
  }

  for (pkgName in vars.registry) {
    var pkg = vars.registry[pkgName];
    var readme = pkg.readme || pkg.versions[pkg["dist-tags"].latest].readme;
    if (readme) {
      pkg["_readme"] = md(readme.replace("<", "&lt;").replace(">", "&gt;"), true);
    }

    pkg["_proxied"] = pkg["_fwd-dists"] ? true : false;
    pkg["_local"] = !pkg["_proxied"];
  }

  res.render("index", vars);
}

/**
 * GET index.
 */
app.get("/", renderIndex);

/*
{'fraction_non_alphanumeric': 0.19014792474508113, 'fraction_numerical': 0.008042510412178659, 'mean_word_length': 3.3174209547427154, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 2, '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': '709556', 'n_tokens_mistral': 2408, 'n_tokens_neox': 2324, 'n_words': 439}