Created
December 8, 2012 23:00
-
-
Save csalzman/4242398 to your computer and use it in GitHub Desktop.
Add Links
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Generated by CoffeeScript 1.3.3 | |
(function() { | |
var app, bcv, fs, get_previous_osis, handle_extra, handle_extra_result, http, index_already_exists, p, qs, remove_parens, request_count, restore_parens, sort_osises, total_time; | |
fs = require("fs"); | |
http = require("http"); | |
qs = require("querystring"); | |
bcv = require("./en_bcv_parser.min.js"); | |
p = new bcv.bcv_parser; | |
p.set_options({ | |
sequence_combination_strategy: "separate", | |
osis_compaction_strategy: "bc", | |
include_apocrypha: true | |
}); | |
handle_extra = function(type, osises, s) { | |
var extra, match, out, previous, re, start; | |
if (type === "numbers") { | |
start = "\\d+[:]\\d"; | |
} | |
if (type === "verses") { | |
start = "v(?:erses?|er|v|s?s?)"; | |
} | |
if (type === "chapters") { | |
start = "ch(?:apters?|a?pts?|a?p?s?)"; | |
} | |
out = []; | |
re = RegExp("\\b" + start + "(?:ch(?:apters?|a?pts?|a?p?s?)|a(?:nd|lso)|[\\d\\s.:,;a-e&\\(\\)\\[\\]\"=\\-\\u2013\\u2014]|ff?\\b|see|v(?:erses?|er|v|ss?)?)+\\w*", "gi"); | |
while (match = re.exec(s)) { | |
if (!/\d/.test(match[0])) { | |
continue; | |
} | |
previous = get_previous_osis(osises, match.index); | |
if (previous) { | |
out.push({ | |
indices: [match.index, match.index + match[0].length], | |
value: match[0], | |
prev: previous | |
}); | |
} | |
extra = handle_extra_result(osises, match[0], match.index); | |
} | |
console.log(out); | |
return out; | |
}; | |
get_previous_osis = function(osises, index) { | |
var osis, previous, _i, _len; | |
previous = ""; | |
for (_i = 0, _len = osises.length; _i < _len; _i++) { | |
osis = osises[_i]; | |
if (index >= osis.indices[0] && index <= osis.indices[1]) { | |
return ""; | |
} | |
if (index < osis.indices[0]) { | |
return previous; | |
} | |
previous = osis.osis; | |
} | |
return previous; | |
}; | |
handle_extra_result = function(osises, s, index) { | |
var offset, previous, result, results, _i, _len; | |
previous = get_previous_osis(osises, index); | |
if (previous.length === 0) { | |
return; | |
} | |
previous += ","; | |
results = p.parse(previous + s).osis_and_indices(); | |
results.shift(); | |
for (_i = 0, _len = results.length; _i < _len; _i++) { | |
result = results[_i]; | |
offset = index - previous.length; | |
result.indices[0] += offset; | |
result.indices[1] += offset; | |
result.extra = true; | |
if (!index_already_exists(osises, result.indices[0])) { | |
osises.push(result); | |
} | |
} | |
sort_osises(osises); | |
return osises; | |
}; | |
index_already_exists = function(osises, index) { | |
var osis, _i, _len; | |
for (_i = 0, _len = osises.length; _i < _len; _i++) { | |
osis = osises[_i]; | |
if (osis.indices[0] === index) { | |
return true; | |
} | |
} | |
return false; | |
}; | |
sort_osises = function(osises) { | |
osises.sort(function(a, b) { | |
if (a.indices[0] < b.indices[0]) { | |
return -1; | |
} | |
if (a.indices[0] > b.indices[0]) { | |
return 1; | |
} | |
return 0; | |
}); | |
return osises.length; | |
}; | |
remove_parens = function(par) { | |
var char, offset, parens, part, parts, _i, _j, _len, _len1, _ref; | |
if (!/[\(\)]/.test(par)) { | |
return [par, []]; | |
} | |
parens = []; | |
_ref = ["(", ")"]; | |
for (_i = 0, _len = _ref.length; _i < _len; _i++) { | |
char = _ref[_i]; | |
parts = par.split(char); | |
offset = -1; | |
for (_j = 0, _len1 = parts.length; _j < _len1; _j++) { | |
part = parts[_j]; | |
if (offset >= 0) { | |
parens.push([offset, char]); | |
offset += part.length + 1; | |
} else { | |
offset = part.length; | |
} | |
} | |
par = parts.join(" "); | |
} | |
return [par, parens]; | |
}; | |
restore_parens = function(par, parens) { | |
var char, offset, _i, _len, _ref; | |
if (!(parens.length > 0)) { | |
return par; | |
} | |
for (_i = 0, _len = parens.length; _i < _len; _i++) { | |
_ref = parens[_i], offset = _ref[0], char = _ref[1]; | |
par = par.substr(0, offset) + char + par.substr(offset + 1); | |
} | |
return par; | |
}; | |
request_count = 0; | |
total_time = 0; | |
app = http.createServer(function(req, res) { | |
var body; | |
body = ""; | |
req.on("data", function(data) { | |
return body += data; | |
}); | |
return req.on("end", function() { | |
var alreadys, elapsed, end, length, osis, osises, par, parens, start, start_time, tag, _i, _len, _ref, _ref1, _ref2; | |
start_time = new Date().getTime(); | |
par = qs.parse(body).q; | |
par = par.replace(/&#(\d+);/g, function(match, $1) { | |
return String.fromCharCode(parseInt($1, 10)); | |
}); | |
_ref = remove_parens(par), par = _ref[0], parens = _ref[1]; | |
osises = p.parse(par).osis_and_indices(); | |
par = restore_parens(par, parens); | |
length = osises.length; | |
alreadys = {}; | |
_ref1 = osises.reverse(); | |
for (_i = 0, _len = _ref1.length; _i < _len; _i++) { | |
osis = _ref1[_i]; | |
_ref2 = osis.indices, start = _ref2[0], end = _ref2[1]; | |
if (alreadys[start] != null) { | |
continue; | |
} | |
alreadys[start] = 1; | |
tag = "a"; | |
par = par.substr(0, start) + "<" + tag + (" class=\"bibleref\" data-bibleref=\"" + osis.osis + "\">") + par.substr(start, end - start) + "</" + tag + ">" + par.substr(end); | |
} | |
res.writeHead(200, ["Content-Type", "text/plain;charset=utf-8"]); | |
res.end(par); | |
if (!par.match(/<a/)) { | |
console.log(par); | |
} | |
elapsed = new Date().getTime() - start_time; | |
total_time += elapsed; | |
request_count++; | |
if (request_count % 100 === 0) { | |
return console.log("" + request_count + " requests in " + total_time + " ms"); | |
} | |
}); | |
}).listen(3000); | |
}).call(this); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment