User:Prosody/charsetdisambig.js

//adapted from http://olivier.thill.perso.neuf.fr/russian/lcg.htm by en:ws:User:Rene1596 charsets = "Latin", [[0x41, 0x5A], [0x61, 0x7A], [0xC0, 0xFF, "lightyellow"], ["Greek", 0x370, 0x3E1, "lightcyan"], ["Cyrillic", 0x400, 0x46F, "lightgreen"], ["Other", [], "white"]];

function show_text_in_color(from) { var from_lg = from.length; var to = new String; var c; var typ, old_typ;

to = ""; old_typ = "?"; for(i=0; i < from_lg; i++) { c = from.charAt(i, 1); n = from.charCodeAt(i, 1); typ = "Other" for(j=0; j < charsets.length; j++) { for(k=0; k < charsets[j][1].length; k++) { if (n >= charsets[j][1][k][0] && n <= charsets[j][1][k][1]) typ = charsets[j][0] }   }    if (typ != old_typ) { if (old_typ != "?") to += " "; for(j=0; j < charsets.length; j++) { if (typ == charsets[j][0]) to += ""; }     old_typ = typ; }   to += c;    if (c == '\n') to += " "; } if (old_typ != "?") to += " ";

return to; }

function charset_dialog_header { h = " " for (i=0; i < charsets.length; i++) { h += "" + charsets[i][0] + ", "; } h = h.substr(0, h.length-2); h += " "; return h; }

// add button -- cribbed from http://www.mediawiki.org/wiki/Extension:WikiEditor/Toolbar_customization if ( typeof $j != 'undefined' && typeof $.fn.wikiEditor != 'undefined' ) { // To add a button to an existing toolbar group: $(document).ready( function {   $( '#wpTextbox1' ).wikiEditor( 'addDialog', { 'charset-disambig-dialog': { resizeme: false, titleMsg: 'charset-disambig-dialog', id: 'charset-disambig-dialog', init: function {}, html: charset_dialog_header, dialog: { width: 550, open: function { document.getElementById("disambiguated-text").innerHTML = show_text_in_color(document.getElementById("wpTextbox1").value); }, beforeclose: function {}, buttons: {} }     }    });    $( '#wpTextbox1' ).wikiEditor( 'addToToolbar', { 'section': 'main', 'group': 'insert', 'tools': { 'charsetdisambig': { label: 'Disambiguate character sets', // or use labelMsg for a localized label, see above type: 'button', icon: '//upload.wikimedia.org/wikipedia/commons/3/3e/Disambiguate_charset_button.png', action: { type: 'dialog', module: 'charset-disambig-dialog' }       }      }    });  }); }