<commit_before>/*
    ***** BEGIN LICENSE BLOCK *****
    
    This file is part of the citeproc-node Server.
    
    Copyright © 2010 Center for History and New Media
                     George Mason University, Fairfax, Virginia, USA
                     http://zotero.org
    
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Affero General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
    
    This program 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.  See the
    GNU Affero General Public License for more details.
    
    You should have received a copy of the GNU Affero General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
    
    ***** END LICENSE BLOCK *****
*/

//include required builtin modules
//var repl = require('repl');
var fs = require('fs');
var http = require('http');
var url = require('url');
var citeproc = require('./citeprocnode');
var async = require('async');
var _ = require('underscore')._;

//global namespace citation server variable
var zcite = {};
global.zcite = zcite;

zcite.config = JSON.parse(fs.readFileSync(__dirname + '/../citeServerConf.json', 'utf8'));
zcite.citeproc = citeproc;

//process command line args
var args = process.argv;
for(var i = 1; i < args.length; i++){
    if(args[i].substr(0, 4) == 'port'){
        zcite.config.listenport = parseInt(args[i].substr(5), 10);
    }
}

//zcite.CSL = require(zcite.config.citeprocmodulePath).CSL;
zcite.cslFetcher = require(zcite.config.cslFetcherPath).cslFetcher;
zcite.cslFetcher.init(zcite.config);

//set up debug/logging output
//logging, especially of errors, should be changed to be more consistent with other server log formats
if(zcite.config.debugLog === true){
    console.log("no debugLog");
    zcite.log = function(m){};
}
else{
    if(zcite.config.debugType == "file"){
        console.log("debug log file :" + zcite.config.logFile);
        zcite.logFile = process.stdout;
        /*
        zcite.logFile = fs.createWriteStream(zcite.config.logFile, {
            'flags' : 'w',
            'encoding' : 'utf8',
            'mode' : 0666
        });
        */
        zcite.log = function(m){
            zcite.logFile.write(m + '\n', 'utf8');
        };
    }
}

zcite.debug = function(m, level){
    if(typeof level == 'undefined'){level = 1;}
    if(level <= zcite.config.debugPrintLevel){
        console.log(m);
    }
};

//zcite exception response function
zcite.respondException = function(err, response, statusCode){
    zcite.debug("respondException", 5);
    zcite.debug(err, 3);
    zcite.debug(err.message);
    if(err.hasOwnProperty('stack')){
        zcite.debug(err.stack);
    }
    else{
        zcite.debug(console.trace());
    }
    if(typeof statusCode == 'undefined'){
        statusCode = 500;
    }
    if(typeof response != "undefined"){
        if(typeof err == "string"){
            response.writeHead(statusCode, {'Content-Type': 'text/plain'});
            response.end("An error occurred");
            //zcite.debug("caught exception : " + err, 1);
            return;
        }
        else{
            response.writeHead(statusCode, {'Content-Type': 'text/plain'});
            response.end("An error occurred");
            //zcite.debug(err, 1);
            return;
        }
    }
    if(typeof err == "string"){
        zcite.debug("unCaught exception: " + err, 1);
    }
    else{
        zcite.debug('unCaught exception: ' + err.name + " : " + err.message, 1);
    }
};

//preload locales into memory
zcite.localesDir = fs.readdirSync(zcite.config.localesPath);

zcite.locales = {};
for(var i = 0; i < zcite.localesDir.length; i++){
    if(zcite.localesDir[i].charAt(0) == '.'){ continue; }
    var localeCode = zcite.localesDir[i].slice(8, 13);
    zcite.locales[localeCode] = fs.readFileSync(zcite.config.localesPath + '/' + zcite.localesDir[i], 'utf8');
}
<commit_msg>quick fix for only reading actual locale files<commit_after>/*
    ***** BEGIN LICENSE BLOCK *****
    
    This file is part of the citeproc-node Server.
    
    Copyright © 2010 Center for History and New Media
                     George Mason University, Fairfax, Virginia, USA
                     http://zotero.org
    
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Affero General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
    
    This program 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.  See the
    GNU Affero General Public License for more details.
    
    You should have received a copy of the GNU Affero General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
    
    ***** END LICENSE BLOCK *****
*/

//include required builtin modules
//var repl = require('repl');
var fs = require('fs');
var http = require('http');
var url = require('url');
var citeproc = require('./citeprocnode');
var async = require('async');
var _ = require('underscore')._;

//global namespace citation server variable
var zcite = {};
global.zcite = zcite;

zcite.config = JSON.parse(fs.readFileSync(__dirname + '/../citeServerConf.json', 'utf8'));
zcite.citeproc = citeproc;

//process command line args
var args = process.argv;
for(var i = 1; i < args.length; i++){
    if(args[i].substr(0, 4) == 'port'){
        zcite.config.listenport = parseInt(args[i].substr(5), 10);
    }
}

//zcite.CSL = require(zcite.config.citeprocmodulePath).CSL;
zcite.cslFetcher = require(zcite.config.cslFetcherPath).cslFetcher;
zcite.cslFetcher.init(zcite.config);

//set up debug/logging output
//logging, especially of errors, should be changed to be more consistent with other server log formats
if(zcite.config.debugLog === true){
    console.log("no debugLog");
    zcite.log = function(m){};
}
else{
    if(zcite.config.debugType == "file"){
        console.log("debug log file :" + zcite.config.logFile);
        zcite.logFile = process.stdout;
        /*
        zcite.logFile = fs.createWriteStream(zcite.config.logFile, {
            'flags' : 'w',
            'encoding' : 'utf8',
            'mode' : 0666
        });
        */
        zcite.log = function(m){
            zcite.logFile.write(m + '\n', 'utf8');
        };
    }
}

zcite.debug = function(m, level){
    if(typeof level == 'undefined'){level = 1;}
    if(level <= zcite.config.debugPrintLevel){
        console.log(m);
    }
};

//zcite exception response function
zcite.respondException = function(err, response, statusCode){
    zcite.debug("respondException", 5);
    zcite.debug(err, 3);
    zcite.debug(err.message);
    if(err.hasOwnProperty('stack')){
        zcite.debug(err.stack);
    }
    else{
        zcite.debug(console.trace());
    }
    if(typeof statusCode == 'undefined'){
        statusCode = 500;
    }
    if(typeof response != "undefined"){
        if(typeof err == "string"){
            response.writeHead(statusCode, {'Content-Type': 'text/plain'});
            response.end("An error occurred");
            //zcite.debug("caught exception : " + err, 1);
            return;
        }
        else{
            response.writeHead(statusCode, {'Content-Type': 'text/plain'});
            response.end("An error occurred");
            //zcite.debug(err, 1);
            return;
        }
    }
    if(typeof err == "string"){
        zcite.debug("unCaught exception: " + err, 1);
    }
    else{
        zcite.debug('unCaught exception: ' + err.name + " : " + err.message, 1);
    }
};

//preload locales into memory
zcite.localesDir = fs.readdirSync(zcite.config.localesPath);

zcite.locales = {};
for(var i = 0; i < zcite.localesDir.length; i++){
    if(zcite.localesDir[i].slice(0, 8) != 'locales-'){ continue; }
    var localeCode = zcite.localesDir[i].slice(8, 13);
    //zcite.debug(zcite.config.localesPath + '/' + zcite.localesDir[i], 2);
    zcite.locales[localeCode] = fs.readFileSync(zcite.config.localesPath + '/' + zcite.localesDir[i], 'utf8');
}

{'fraction_non_alphanumeric': 0.1416034155597723, 'fraction_numerical': 0.008301707779886147, 'mean_word_length': 2.73141592920354, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '5065467', 'n_tokens_mistral': 2661, 'n_tokens_neox': 2595, 'n_words': 690}