Abush
March 30th, 2007, 15:12
/*
This script is a modified version of Alex benenson's cyrillic translitarator
*/
// default latinica - russian-oriented customized tranlit readable both ways
var defaultLatinicaTable = [["shch", "\u0429"],["yo", "\u0401"],["zh",
"\u0416"],["ch", "\u0427"],["sh", "\u0428"],["yu", "\u042E"],["ya",
"\u042F"],["e\'", "\u042D"],["eh", "\u042D"],["sj", "\u0429"],["a",
"\u0410"],["b", "\u0411"],["v","\u0412"],["g", "\u0413"],["d",
"\u0414"],["e", "\u0415"],["z", "\u0417"],["i", "\u0418"],["j",
"\u0419"],["k", "\u041A"],["l", "\u041B"],["m", "\u041C"],["n",
"\u041D"],["o", "\u041E"],["p", "\u041F"],["r", "\u0420"],["s",
"\u0421"],["t", "\u0422"],["u", "\u0423"],["f", "\u0424"],["``",
"\u042A"],["`", "\u044A", true],["y","\u042B"],["\'\'", "\u042C"],["\'",
"\u044C", true],["x", "\u0425"],["h", "\u0425"],["c", "\u0426"],["kh",
"\u0425"],["ji", "\u0406"],["jj","\u0407"],["w","\u040E"],["q", "'"],["\\", "'"],["´", "'"],["je","\u0404"]];
var amhaHash='{"h":"?","?e":"?","?u":"?","?i":"?","?a":"?","?e":"?","?E":"?","?o":"?","?W":"?","?h":"?","?2":"?","?e":"?","?u":"?","?i":"?","?a":"?","?e":"?","?E":"?","?o":"?","?W":"?","?e":"?","?u":"?","?i":"?","?a":"?","?e":"?","?E":"?","?\'":"?","l":"?","L":"?","?e":"?","?u":"?","?i":"?","?a":"?","?e":"?","?E":"?","?o":"?","?W":"?","?a":"?","H":"?","?e":"?","?u":"?","?i":"?","?a":"?","?e":"?","?E":"?","?o":"?","?W":"?","?a":"?","m":"?","M":"?","?e":"?","?u":"?","?i":"?","?a":"?","?e":"?","?E":"?","?o":"?","?W":"?","?a":"?","?Y":"?","?a":"?","r":"?","R":"?","?e":"?","?u":"?","?i":"?","?a":"?","?e":"?","?E":"?","?o":"?","?W":"?","?a":"?","?Y":"?","s":"?","?e":"?","?u":"?","?i":"?","?a":"?","?e":"?","?E":"?","?o":"?","?W":"?","?a":"?","?s":"?","?2":"?","?e":"?","?u":"?","?i":"?","?a":"?","?e":"?","?E":"?","?o":"?","?W":"?","?a":"?","x":"?","X":"?","?e":"?","?u":"?","?i":"?","?a":"?","?e":"?","?E":"?","?o":"?","?W":"?","?a":"?","q":"?","?e":"?","?u":"?","?i":"?","?a":"?","?e":"?","?E":"?","?o":"?","?W":"?","?e":"?","?u":"?","?i":"?","?a":"?","?e":"?","?E":"?","?\'":"?","Q":"?","?e":"?","?u":"?","?i":"?","?a":"?","?e":"?","?E":"?","?o":"?","?W":"?","?e":"?","?u":"?","?i":"?","?a":"?","?e":"?","?e":"?","?a":"?","b":"?","B":"?","?e":"?","?u":"?","?i":"?","?a":"?","?e":"?","?E":"?","?o":"?","?W":"?","?a":"?","v":"?","V":"?","?e":"?","?u":"?","?i":"?","?a":"?","?E":"?","?E":"?","?o":"?","?W":"?","?a":"?","t":"?","?e":"?","?u":"?","?i":"?","?a":"?","?e":"?","?E":"?","?o":"?","?W":"?","?W":"?","c":"?","?e":"?","?u":"?","?i":"?","?a":"?","?e":"?","?E":"?","?o":"?","?W":"?","?a":"?","n":"?","?e":"?","?u":"?","?i":"?","?a":"?","?e":"?","?E":"?","?o":"?","?W":"?","?a":"?","N":"?","?e":"?","?u":"?","?i":"?","?a":"?","?e":"?","?E":"?","?o":"?","?W":"?","?a":"?","a":"?","u":"?","U":"?","i":"?","A":"?","E":"?","e":"?","I":"?","?e":"?","?e":"?","?a":"?","o":"?","O":"?","?a":"?","?e":"?","?2":"?","?u":"?","?U":"?","?2":"?","?i":"?","?2":"?","?a":"?","?A":"?","?2":"?","?E":"?","?2":"?","?I":"?","?2":"?","?o":"?","?O":"?","?2":"?","k":"?","?e":"?","?u":"?","?i":"?","?a":"?","?e":"?","?E":"?","?o":"?","?W":"?","?e":"?","?o":"?","?u":"?","?i":"?","?a":"?","?e":"?","?E":"?","?\'":"?","K":"?","?e":"?","?u":"?","?i":"?","?a":"?","?e":"?","?E":"?","?o":"?","?W":"?","?e":"?","?u":"?","?i":"?","?a":"?","?u":"?","?E":"?","?\'":"?","w":"?","W":"?","?e":"?","?u":"?","?i":"?","?a":"?","?E":"?","?E":"?","?o":"?","z":"?","?e":"?","?u":"?","?i":"?","?a":"?","?E":"?","?E":"?","?o":"?","?W":"?","?a":"?","Z":"?","?e":"?","?u":"?","?i":"?","?a":"?","?e":"?","?E":"?","?o":"?","?W":"?","?a":"?","y":"?","Y":"?","?e":"?","?u":"?","?i":"?","?a":"?","?e":"?","?E":"?","?o":"?","d":"?","?e":"?","?u":"?","?i":"?","?a":"?","?e":"?","?E":"?","?o":"?","?W":"?","?a":"?","D":"?","?e":"?","?u":"?","?i":"?","?a":"?","?e":"?","?E":"?","?o":"?","?W":"?","?a":"?","j":"?","J":"?","?e":"?","?u":"?","?i":"?","?a":"?","?e":"?","?E":"?","?o":"?","?W":"?","?a":"?","g":"?","?e":"?","?u":"?","?i":"?","?a":"?","?e":"?","?E":"?","?o":"?","?W":"?","?e":"?","?o":"?","?u":"?","?i":"?","?a":"?","?e":"?","?E":"?","?\'":"?","G":"?","?e":"?","?u":"?","?i":"?","?a":"?","?e":"?","?E":"?","?o":"?","T":"?","?e":"?","?u":"?","?i":"?","?a":"?","?e":"?","?E":"?","?o":"?","?W":"?","?a":"?","C":"?","?e":"?","?u":"?","?i":"?","?a":"?","?e":"?","?E":"?","?o":"?","?W":"?","?a":"?","P":"?","?e":"?","?u":"?","?i":"?","?a":"?","?e":"?","?E":"?","?o":"?","?W":"?","?a":"?","S":"?","?e":"?","?u":"?","?i":"?","?a":"?","?e":"?","?E":"?","?o":"?","?W":"?","?a":"?","?S":"?","?2":"?","?e":"?","?u":"?","?i":"?","?a":"?","?e":"?","?E":"?","?o":"?","f":"?","F":"?","?e":"?","?u":"?","?i":"?","?a":"?","?e":"?","?E":"?","?o":"?","?W":"?","?a":"?","?Y":"?","?a":"?","p":"?","?e":"?","?u":"?","?i":"?","?a":"?","?e":"?","?E":"?","?W":"?","?W":"?","?a":"?","?o":"?","\'":"\'","\'1" :"?","\'2":"?","\'3" :"?","\'4" :"?","\'5":"?","\'6":"?","\'7":"?","\'8":"?","\'9" :"?","?0":"?"}';
var conversionTable = defaultLatinicaTable;
// for compatibility with bookmarklets
function cyr_translit(src) {
return to_cyrillic(src);
}
var conversionHash = undefined;
var maxcyrlength = 0;
function getConversionHash() {
if (conversionHash == undefined) {
conversionHash = eval("("+amhaHash+")");
maxcyrlength=2;
}
return conversionHash;
}
function to_cyrillic(src, output, chunks) {
if (src == undefined || src == "" || src == null)
return src;
if (output == undefined)
output = new String();
var hash = getConversionHash();
var location = 0;
while (location < src.length) {
var len = Math.min(maxcyrlength, src.length - location);
var arr = undefined;
var sub;
while (len > 0) {
sub = src.substr(location, len);
arr = hash[sub];
if (arr != undefined)
break;
else
len--;
}
// need this for translit on the fly
if (chunks != undefined)
chunks[chunks.length] = sub;
if (arr == undefined) {
output += sub;
location ++;
}
else {
// case analysis
var newChar = arr;
if (sub.toLowerCase() == sub.toUpperCase() && arr.length > 1 && arr[1] && (newChar.toUpperCase() != newChar.toLowerCase())) {
// need translit hash to determine if previous character (and possibly the one before it)
// were converted and are in upper case
// set prevDud to true previous is not a translated character or simply a blank
// set prevCap to true if previous was translated and was upper case
var prevCh = output.length == 0 ? null : output.substr(output.length - 1, 1);
var prevDud = !prevCh || !getTranslitString(prevCh);
var prevCap = (!prevDud && prevCh == prevCh.toUpperCase());
// sub is caseless but result isn't. case will depend on lookbehind and lookahead
if (prevDud || !prevCap) {
output += newChar.toLowerCase();
prevCap = false;
}
else {
var next = " ";
if (location + len < src.length)
next = src.substr(location + len, 1);
if (next != next.toUpperCase() && next == next.toLowerCase() ) {
//next is lowercase (and not caseless)
output += newChar.toLowerCase();
}
else if (next == next.toUpperCase() && next != next.toLowerCase() ) {
// next is uppercase (and not caseless)
output += newChar.toUpperCase();
}
else {
// next is caseless. output case determined by the case of output[length - 2]
var pprevCh = output.length == 1 ? null : output.substr(output.length - 2, 1);
var pprevDud = !pprevCh || !getTranslitString(pprevCh);
if (!pprevDud && (pprevCh == pprevCh.toUpperCase())) {
//pre-prev is in upper case. output is also uppercase
output += newChar.toUpperCase();
}
else {
output += newChar.toLowerCase();
}
}
}
}
else if ((sub.toLowerCase() == sub.toUpperCase()) && (arr.length < 2 || !arr[1])) {
// literal treatment of newChar
output += newChar;
}
else if (sub != sub.toLowerCase()) {
// sub not all-lowercase
output += newChar.toUpperCase();
}
else {
// sub is lowercase
output += newChar.toLowerCase();
}
location += len;
}
}
return output;
}
// split string on HTML tags, return array containing both the matches and the pieces of string between them, matches always in even positions - since IE does not support this in String.split
function splitHtmlString(string) {
var re = /<[\/]?[!A-Z][^>]*>/ig;
var result = new Array();
var lastIndex = 0;
var arr = null;
while ( (arr = re.exec(string)) != null) {
result[result.length] = string.substring(lastIndex, arr.index);
result[result.length] = string.substring(arr.index, re.lastIndex);
lastIndex = re.lastIndex;
}
result[result.length] = string.substr(lastIndex);
return result;
}
/* convert cyrillic to translit using to_translit-- similar to from_translit.... */
function to_translit_ext (src, skipHtml) {
return convertWithSkip(src, skipHtml, to_translit);
}
/* convert translit to cyrillic (using ToCyrillic.to_cyrillic above) */
function to_cyrillic_ext (src, skipHtml) {
return convertWithSkip(src, skipHtml, to_cyrillic);
}
function convertIt(src,converter){
var resultbuffer="";
for(var i=0;i<src.length;i++){
resultbuffer=converter(resultbuffer+src[i]);
}
return converter(resultbuffer);
}
function convertWithSkip(src, skipHtml, converter) {
if (src == "" || src == null)
return src;
if (!skipHtml)
return convertIt(src,converter);
else {
var arr = splitHtmlString(src);
for (var i = 0; i < arr.length; i++) {
if ( (i % 2) == 0)
arr[i] = convertIt(arr[i],converter);
}
return arr.join("");
}
}
more coming
Powered by vBulletin® Version 4.1.7 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.