add minitranslate v1.1.2

This commit is contained in:
Peter Dave Hello
2014-07-29 11:46:01 +08:00
parent 7318702eb2
commit 3380d7f080
3 changed files with 160 additions and 1 deletions
@@ -0,0 +1,147 @@
/**
* minitranslate.js
* http://bryce.io/minitranslate
* minitranslate.herokuapp.com
*
* @version
* 1.1.2 (July 28 2014)
*
* @license
* The MIT license.
*/
function mt(mt_lib, div) {
if ((div.innerHTML !== '' || div.value !== '') && (mt_lib.length > 0 && div.getAttribute("class") !== "mt-ignore")) {
if (div.children.length > 0) {
Array.prototype.forEach.call(div.children, function(el, i) {
if (el.getAttribute("class") !== "mt-ignore") {
apply_translation(el);
}
});
} else apply_translation(div);
}
}
function apply_translation(div) {
d = delouse(div);
iterate_lib(d[0], mt_lib);
append_punctuation(d[0], d[1]);
write_changes(div, d[0]);
}
function append_punctuation(txt, punct) {
punct.map(function(p) {
txt[p.idx] += p.c;
});
}
function write_changes(item, array) {
var txt = array.join(" ");
if (item.id === "mt-output") {
item.value = txt;
} else {
item.innerHTML = txt;
}
}
// Sanitizes & saves punctuation
function delouse(item) {
var txt = (item.id === "mt-output") ? item.value : item.innerText;
var tmp = txt.split(" ");
var punct = get_punct(tmp);
txt_arr = txt.replace(/[\.,-\/#!$%\^&\*;:{}=\-_`~()]/g, "").split(" ");
return [txt_arr, punct];
}
function iterate_lib(t, mt_lib) {
for (var j = 0; j < t.length; j++) {
mt_lib.every(function(entry) {
if (t[j].toLowerCase() === entry.w.toLowerCase()) {
var capitals = detect_capitals(t[j]);
t[j] = apply_capitals(entry.r, capitals);
return false;
} else return true;
});
}
}
function get_punct(tmp) {
var keep = [];
tmp.map(function(b, i) {
b.split('').map(function(c) {
if (c === "!" || c === "?" || c === "," || c === ".") {
keep.push(new Pun(c, i));
}
});
});
return keep;
}
function detect_capitals(word) {
return word.split('').map(function(letter, i) {
return (word.charAt(i) >= "A" && word.charAt(i) <= "Z") ? 1 : 0;
});
}
function apply_capitals(word, capitals) {
var ret = "";
var end = (word.length >= capitals.length) ? capitals.length : word.length;
for (var i = 0; i < end; i++) {
if (capitals[i]) {
ret += word.charAt(i).toUpperCase();
} else {
ret += word.charAt(i).toLowerCase();
}
}
if (word.length >= capitals.length) {
ret += word.substr(i, word.length - 1);
}
return ret;
}
// Associates index & char
function Pun(c, i) {
this.c = c;
this.idx = i;
}
// Static
function mt_translate(mt_lib) {
Array.prototype.forEach.call(document.querySelectorAll(".mt-translate"), function(el, i) {
mt(mt_lib, el);
});
}
// Dynamic
function mt_watch(mt_lib) {
var inp = document.getElementById("mt-input");
var outp = document.getElementById("mt-output");
var butt = document.getElementById("mt-button");
if (inp !== null && outp !== null) {
if (butt) {
butt.onclick = function() {
outp.value = inp.value;
mt(mt_lib, outp);
};
}
inp.onkeyup = function() {
if (inp.getAttribute("class").indexOf('mt-patient') === -1) {
outp.value = inp.value;
mt(mt_lib, outp);
}
};
}
}
// Validate
if (document.getElementById("mt-input") && !document.getElementById("mt-output")) {
console.log("mt.js: Input detected but no output. Check your output ID.");
}
if (!document.getElementById("mt-input") && document.getElementById("mt-output")) {
console.log("mt.js: Output detected but no input. Check your input ID.");
}
// Instantiate
if (document.getElementById("mt-input") && document.getElementById("mt-output")) {
mt_watch(mt_lib);
}
mt_translate(mt_lib);
+12
View File
@@ -0,0 +1,12 @@
/**
* minitranslate.js
* http://bryce.io/minitranslate
* minitranslate.herokuapp.com
*
* @version
* 1.1.2 (July 28 2014)
*
* @license
* The MIT license.
*/
function mt(a,b){(""!==b.innerHTML||""!==b.value)&&a.length>0&&"mt-ignore"!==b.getAttribute("class")&&(b.children.length>0?Array.prototype.forEach.call(b.children,function(a){"mt-ignore"!==a.getAttribute("class")&&apply_translation(a)}):apply_translation(b))}function apply_translation(a){d=delouse(a),iterate_lib(d[0],mt_lib),append_punctuation(d[0],d[1]),write_changes(a,d[0])}function append_punctuation(a,b){b.map(function(b){a[b.idx]+=b.c})}function write_changes(a,b){var c=b.join(" ");"mt-output"===a.id?a.value=c:a.innerHTML=c}function delouse(a){var b="mt-output"===a.id?a.value:a.innerText,c=b.split(" "),d=get_punct(c);return txt_arr=b.replace(/[\.,-\/#!$%\^&\*;:{}=\-_`~()]/g,"").split(" "),[txt_arr,d]}function iterate_lib(a,b){for(var c=0;c<a.length;c++)b.every(function(b){if(a[c].toLowerCase()===b.w.toLowerCase()){var d=detect_capitals(a[c]);return a[c]=apply_capitals(b.r,d),!1}return!0})}function get_punct(a){var b=[];return a.map(function(a,c){a.split("").map(function(a){("!"===a||"?"===a||","===a||"."===a)&&b.push(new Pun(a,c))})}),b}function detect_capitals(a){return a.split("").map(function(b,c){return a.charAt(c)>="A"&&a.charAt(c)<="Z"?1:0})}function apply_capitals(a,b){for(var c="",d=a.length>=b.length?b.length:a.length,e=0;d>e;e++)c+=b[e]?a.charAt(e).toUpperCase():a.charAt(e).toLowerCase();return a.length>=b.length&&(c+=a.substr(e,a.length-1)),c}function Pun(a,b){this.c=a,this.idx=b}function mt_translate(a){Array.prototype.forEach.call(document.querySelectorAll(".mt-translate"),function(b){mt(a,b)})}function mt_watch(a){var b=document.getElementById("mt-input"),c=document.getElementById("mt-output"),d=document.getElementById("mt-button");null!==b&&null!==c&&(d&&(d.onclick=function(){c.value=b.value,mt(a,c)}),b.onkeyup=function(){-1===b.getAttribute("class").indexOf("mt-patient")&&(c.value=b.value,mt(a,c))})}document.getElementById("mt-input")&&!document.getElementById("mt-output")&&console.log("mt.js: Input detected but no output. Check your output ID."),!document.getElementById("mt-input")&&document.getElementById("mt-output")&&console.log("mt.js: Output detected but no input. Check your input ID."),document.getElementById("mt-input")&&document.getElementById("mt-output")&&mt_watch(mt_lib),mt_translate(mt_lib);
+1 -1
View File
@@ -1,7 +1,7 @@
{
"name": "minitranslate",
"filename": "minitranslate.min.js",
"version": "1.1.0",
"version": "1.1.2",
"maintainers": [
{
"name": "Bryce Dorn",