MediaWiki:Modernisering.js
Utseende
Merk: Etter publisering vil det kanskje være nødvendig å slette mellomlageret i nettleseren din for å se endringene.
- Firefox / Safari: Hold Shift mens du klikker på Oppdater, eller trykk enten Ctrl+F5 eller Ctrl+R (⌘+R på Mac)
- Google Chrome: Trykk Ctrl+Shift+R (⌘+Shift+R på Mac)
- Edge: Hold Ctrl mens du trykker på Oppdater eller trykk Ctrl+F5
/***************
moderniseringer
****************/
function mod_text(text) {
text = text.replace(/ſ/g, "s");
text = text.replace(/ff/g, "ff");
text = text.replace(/fl/g, "fl");
text = text.replace(/fi/g, "fi");
text = text.replace(/ffi/g, "ffi");
text = text.replace(/ffl/g, "ffl");
text = text.replace(/ſt/g, "st");
text = text.replace(/st/g, "st");
text = text.replace(/ö/g, "ø");
text = text.replace(/ß/g, "ss");
return text;
}
function mod_node(node) {
if (node.nodeType == 3) {
node.data = mod_text(node.data);
} else {
for (var i=0; i<node.childNodes.length; i++) {
if ((node.id != 'editform'))
mod_node(node.childNodes[i]);
}
}
}
function mod_typo_document() {
if ($.inArray(mw.config.get('wgNamespaceNumber'), [0 , 2 , 14 , 102, 104 ]) !== -1) {
document.title = mod_text(document.title);
mod_node(document.getElementById('content'));
}
}
/*********************************************************************************/
//reexp split a string; based on http://stevenlevithan.com/assets/misc/split.js
function reg_split(str, separator) {
var output = [],
lastLastIndex = 0,
flags = (separator.ignoreCase ? "i" : "") +
(separator.multiline ? "m" : "") +
(separator.sticky ? "y" : ""),
separator = RegExp(separator.source, flags + "g"), // make `global` and avoid `lastIndex` issues
separator2, match, lastIndex, lastLength;
var compliantExecNpcg = /()??/.exec("")[1] === undefined;
str = str + ""; // type conversion
if (!compliantExecNpcg)
separator2 = RegExp("^" + separator.source + "$(?!\\s)", flags); // doesn't need /g or /y, but they don't hurt
while (match = separator.exec(str)) {
lastIndex = match.index + match[0].length; // `separator.lastIndex` is not reliable cross-browser
if (lastIndex > lastLastIndex) {
output.push(str.slice(lastLastIndex, match.index));
// fix browsers whose `exec` methods don't consistently return `undefined` for nonparticipating capturing groups
if (!compliantExecNpcg && match.length > 1) {
match[0].replace(separator2, function () {
for (var i = 1; i < arguments.length - 2; i++) {
if (arguments[i] === undefined)
match[i] = undefined;
}
});
}
if (match.length > 1 && match.index < str.length)
Array.prototype.push.apply(output, match.slice(1));
lastLength = match[0].length;
lastLastIndex = lastIndex;
}
if (separator.lastIndex === match.index)
separator.lastIndex++; // avoid an infinite loop
}
if (lastLastIndex === str.length) {
if (!separator.test("") || lastLength)
output.push("");
} else {
output.push(str.slice(lastLastIndex));
}
return output;
}
function mod2_text(text, dict, build, udict) {
list = reg_split(text,/([^a-zçâàäāãéèêẽëîïôöōõûùüÿœæA-ZÀÂÄÉÈÊËÎÏÔÖÙÛÜŸÇŒÆ&ß]+)/);
text = "";
var prev_w="";
for (var i=0, len=list.length; i<len; i++) {
var w = list[i];
var sub_w = w.toLowerCase();
//lookahead search
if(i+2<len) {
var next_w = list[i+2];
var query = w + ' ' + next_w;
var nw2 = dict[query.toLowerCase()];
if(nw2){
text=text+nw2;
i=i+2;
continue;
}
}
if(sub_w=="sort") {
text=text+w;continue;
}
var nw = dict[sub_w];
if(nw) {
if(build) udict[sub_w] = nw;
if ( w == sub_w ) {
text=text+nw;
}
else if( w ==w.toUpperCase() ) {
text=text+nw.toUpperCase();
}
else if( w[0] == w[0].toUpperCase() && w[1] == w[1].toLowerCase() ){
text = text + nw[0].toUpperCase() + nw.substring(1);
}
else text=text+w;
}
else if(w=="&") {
pw = list[i-2];
if(pw && pw==pw.toUpperCase()) text=text="ET";
else text=text+"et";
}
else text=text+w;
}
if(build) return udict;
else return text;
}
function mod2_node(node,dict,build,udict) {
if (node.nodeType == 3) {
var data = mod2_text(node.data,dict,build,udict);
if(!build) node.data = data;
} else {
for (var i=0; i<node.childNodes.length; i++) {
if ((node.id != 'editform'))
mod2_node(node.childNodes[i],dict,build,udict);
}
}
}
/* if dictionary does not exist, create it */
function do_mod_callback(res) {
mod_typo_document();
for(var page in res.query.pages) {
if(!res.query.pages[page].pageid){
alert('ingen ordbok');
continue;
var udict = {};text="";
mod2_node(document.getElementById('content'), dico, true, udict);
for(var w in udict){
text = text + w+":"+udict[w]+",\n";
}
alert(text);
}
else {
var wsdict = [];
for(var page in res.query.pages) {
var str = res.query.pages[page].revisions[0]['*'];
lines = str.split('\n');
for( i in lines) {
var match2 = /^\*\s*(\S[^:]*?)\s*:\s*([\S].*?)\s*$/.exec(lines[i]);
if( match2 ) {
wsdict[match2[1]]=match2[2];
continue;
}
}
}
document.title = mod2_text(document.title, wsdict);
mod2_node(document.getElementById('content'), wsdict, false, null);
}
}
mm=document.getElementById("p-navigation");
if (($.inArray(mw.config.get('wgNamespaceNumber'), [0 , 2]) !== -1) && mm ) {
var wsdict = [];
str = mm.innerHTML;
lines = str.split('\n');
for( i in lines) {
var match2 = /^<li>\s*(\S[^:]*?)(\s| )*:\s*([\S].*?)\s*<\/li>$/i.exec(lines[i])
if( match2 ) {
wsdict[match2[1]]=match2[3];
continue;
}
}
mod2_node(document.getElementById('content'), wsdict, false, null);
}
li = document.getElementById("mod_option");
if(li) li.innerHTML="<a href=\""+location.href+"\">Orginaltekst</a>";
}
function do_mod(){
if($.inArray(mw.config.get('wgNamespaceNumber'), [0 , 2 , 14 , 102, 104 ]) === -1) return;
url = '//no.wikisource.org/w/api.php?action=query&format=json&prop=revisions&titles=Wikikilden:Ordliste/nb&rvprop=content&callback=do_mod_callback';
mw.loader.load(url);
}
function mod_lookup() {
var mm=document.getElementById("p-navigation");
if ( (mm && mw.config.get('wgNamespaceNumber') == 0 )|| $.inArray(mw.config.get('wgNamespaceNumber'), [104 , 2 ]) !== -1) {
var optlist = get_optlist();
optlist.innerHTML = optlist.innerHTML + "<li id=\"mod_option\" class=\"\"><a href='javascript:do_mod();' title='Versjon modifisert av WikiKilden'>Moderniser tekst</a> " +"</li>";
}
}
$(mod_lookup);
if(self.gadget_typographie && !self.gadget_mod2)
$(mod_typo_document);