User:ZxxZxxZ/fixLinks.js

From Wiktionary, the free dictionary
Jump to navigation Jump to search

Note: You may have to bypass your browser’s cache to see the changes. In addition, after saving a sitewide CSS file such as MediaWiki:Common.css, it will take 5-10 minutes before the changes take effect, even if you clear your cache.

  • Mozilla / Firefox / Safari: hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (Command-R on a Macintosh);
  • Konqueror and Chrome: click Reload or press F5;
  • Opera: clear the cache in Tools → Preferences;
  • Internet Explorer: hold Ctrl while clicking Refresh, or press Ctrl-F5.


// <nowiki> to prevent interpretation of template syntax

// TODO: move it to a better title

function fixLinks(){
    var t = document.getElementById('wpTextbox1').value
    safeMode = false

    t = t.replace(/\{\{(.*?)-(Arab|Cyrl|Latn)\|(.*?)\}\}/g, '{{lang|$1|sc=$1-$2|$3}}')

    var sc2lang = {
        Armi: "arc",
        Buhd: "bku",
        Cakm: "ccp",
        Cari: "xcr",
        Cham: "cjm",
        Cher: "chr",
        Copt: "cop",
        Goth: "got",
        Guru: "pa",
        Java: "jv",
        Khmr: "km", // almost safe
        Kore: "ko",
        Lana: "nod",
        Latinx: "ang",
        Lepc: "lep",
        Limb: "lif",
        Linb: "gmy",
        Lyci: "xlc",
        Lydi: "xld",
        Olck: "sat",
        Phli: "pal",
        Prti: "xpr",
        Rjng: "rej",
        Sarb: "sem-srb",
        Saur: "saz",
        Sinh: "si", // almost safe
        Sund: "su",
        Tale: "tdd",
        Talu: "khb",
        Tavt: "blt",
        Tglg: "tl",
        Thaa: "dv",
        Ugar: "ug",
        Xpeo: "peo",
    }
    var sc2langUnsafe = {
        Arab: "ar",
        Beng: "bn",
        Cyrl: "ru",
        Geor: "ka",
        Gujr: "gu",
        Hebr: "he",
        Jpan: "ja",
        Laoo: "lo",
        Mero: "xmr",
        Syrc: "syc",
        Taml: "ta",
        Thai: "th",
    }

    if (safeMode) {
        for (sc in sc2langUnsafe) {
            t = t.replace(new RegExp('\\{\\{' + sc + '\\|lang=(.*?)\\|(.*?)\\}\\}', 'g'),
                          '{{lang|$1|sc=' + sc + '|$2}}')
            t = t.replace(new RegExp('\\{\\{' + sc + '\\|(.*?)\\|lang=(.*?)\\}\\}', 'g'),
                          '{{lang|$2|sc=' + sc + '|$1}}')
        }
    } else {
        for (sc in sc2langUnsafe) {
            sc2lang[sc] = sc2langUnsafe[sc]
        }
    }

    for (sc in sc2lang) {
        t = t.replace(new RegExp('\\{\\{' + sc + '\\|lang=(.*?)\\|(.*?)\\}\\}', 'g'),
                      '{{lang|$1|sc=' + sc + '|$2}}')
        t = t.replace(new RegExp('\\{\\{' + sc + '\\|(.*?)\\|lang=(.*?)\\}\\}', 'g'),
                      '{{lang|$2|sc=' + sc + '|$1}}')
        t = t.replace(new RegExp('\\{\\{' + sc + '\\|(.*?)\\}\\}', 'g'),
                      '{{lang|' + sc2lang[sc] + '|sc=' + sc + '|$1}}')
    }

    t = t.replace(/\{\{el-l\|/g, '{{l|el|');

    // remove "|xs=..."
    t = t.replace(/\{\{(l|t[\-\+ø0]?|term)(\|.*?|)\|xs=.*?(?=[\|\}])/g, '{{$1$2');

    // use "|tr="
    t = t.replace(/\{\{l\|(.*?)\}\}\s*\((.*?)\)/g, '{{l|$1|tr=$2}}');
    // remove "|tr=..." when not needed
    t = t.replace(/\{\{l\|(ab|abq|ady|av|axm|ce|dar|el|hy|inh|ka|kbd|lbe|lez|lzz|oge|os|sva|ug|xcl|xmf)(\|.*?|)\|tr=.*?(?=[\|\}])/g, '{{l|$1$2');

    // use "|g="
    t = t.replace(/\{\{l\|(.*?)\}\}\s*\((m|f|n|pl)\)/g, '{{l|$1|g=$2}}');

    // replace {{l}} with {{term}} in Etymology section
    t = t.replace(/(\=+Etymology ?\d*?\=+)\n(.*?)\{\{l\|(.*?)\|(.*?)\}\}/g, '$1\n$2{{term|$4|lang=$3}}');

    // replace {{l}} with normal link in |head=
    t = t.replace(/(\|head=|ru-phrase\|)(.*?)\{\{l\|[^\}]*?\|(.*?)\}\}/g, '$1$2[[$3]]');

    // replace sections from normal links in |head=
    t = t.replace(/(\|head=|ru-phrase\|)(.*?)\[\[([^\|\]]+?)#.*?\|(.*?)\]\]/g, '$1$2[[$3|$4]]');

    // use {{xx-noun}} instead of {{head}}
    t = t.replace(/\{\{head\|(axm|ba|fa|hy|ka|tg|ug|xcl)\|noun(\|sc\=.*?|)\}\}/g, '{{$1-noun}}');

    // remove "|tr=...", etc. from headword templates
    t = t.replace(/\n\{\{(axm|hy|ka|ru|tg|ug|xcl)(\-.*?)\|tr=.*?(?=[\|\}])/g, '\n{{$1$2');
    t = t.replace(/\{\{hy-noun(.*?)\|.*?\}\}/g, '{{hy-noun$1}}');
    t = t.replace(/\{\{fa-noun(\|.*?)\|pl\d?tr=.*?(?:hâ|ât|ân)(?=[\|\}])/g, '{{fa-noun$1');
    t = t.replace(/\{\{fa-adj(\|.*?)\|ZWNJ.*?(?=[\|\}])/g, '{{fa-noun$1');
    t = t.replace(/\{\{tg-(.*?)\|(?:tr|pltr)=.*?(?=[\|\}])/g, '{{tg-$1');

    t = t.replace(/\|lang=ira(?=[^\w\-])/g, '|lang=und');

    var sc2remove = {
        Arab: "ar",
        Beng: "bn",
        Buhd: "bku",
        Cakm: "ccp",
        Cari: "xcr",
        Cham: "cjm",
        Cher: "chr",
        Copt: "cop",
        Cyrl: "ru",
        Geor: "ka",
        Goth: "got",
        Gujr: "gu",
        Hebr: "he",
        Jpan: "ja",
        Khmr: "km",
        Kore: "ko",
        Lana: "nod",
        Laoo: "lo",
        Latinx: "ang",
        Lepc: "lep",
        Limb: "lif",
        Linb: "gmy",
        Lyci: "xlc",
        Lydi: "xld",
        Mero: "xmr",
        Olck: "sat",
        Rjng: "rej",
        Sarb: "sem-srb",
        Saur: "saz",
        Sinh: "si",
        Syrc: "syc",
        Tale: "tdd",
        Talu: "khb",
        Taml: "ta",
        Tavt: "blt",
        Thaa: "dv",
        Thai: "th",
        Ugar: "ug",
        Xpeo: "peo",
    }
    for (sc in sc2remove) {
        t = t.replace(new RegExp('\\{\\{(head|l|lang|term)\\|' + sc2remove[sc] + '(\\|.*?|)(?:\\|sc=' + sc + ')(?=[\\|\\}])', 'g'),
                      '{{$1|' + sc2remove[sc] + '$2');
    }

    document.getElementById('wpTextbox1').value = t;
}
 
$(function(){
    var cp = document.getElementById('editpage-copywarn');
    var wpSave = document.getElementById('wpSave');
    if (!cp || !wpSave) return
    var ins = ' <a style="border:1px solid gray; padding:3px" href="javascript:fixLinks();">fixLinks</a>';
    cp.innerHTML += ins;
    var warningDone = false;
})

// </nowiki>