Toscead betweox fadungum "Brūcend:DaveZ122/spellingstyle.js"

Content deleted Content added
Nīwe tramet: /********************************************************* * Hakka Wikipedia transliterator * * Deals with the following orthographies: 漢字, Pha̍k-fa-sṳ * ...
(Nænig toscead)

Edniwung fram 05:58, 30 Sēremōnaþ 2013

/*********************************************************
 * Hakka Wikipedia transliterator                  *
 * Deals with the following orthographies: 漢字, Pha̍k-fa-sṳ *
 * Created by PiRSquared17                               *
 * Please report bugs on talk page or village pump       *
 * Currently supports: "漢字" and "Pha̍k-fa-sṳ"          *
 *********************************************************/

/**
 * Actually clone content, and transliterate
 *
 * @param event The event: event.data.outputstyle (see convertStyle args 2,3)
 * @returns nothing (undefined)
 */
function doConv(event) {
    var $ = jQuery;
    if ($("#mw-content-text-clone").length)
        $("#mw-content-text-clone").remove();
    //console.log("Entered doConv, (possibly removed old clone)");
    // get target style
    var targetstyle = event.data.outputstyle;
    var contentClone;
    $("#mw-content-text").show();
    contentClone = $("#mw-content-text").clone();
    contentClone.attr("id", "mw-content-text-clone");
    convertStyle(contentClone, "漢字", targetstyle);
    convertStyle(contentClone, "Pha̍k-fa-sṳ", targetstyle);
    $("#mw-content-text").hide();
    if (targetstyle == "漢字")
        contentClone.addClass("content-yoghwynn");
    else
        contentClone.addClass("content-" + targetstyle);
    contentClone.insertAfter("#mw-content-text");
    $(".click-gw").click({"outputstyle": "漢字"}, doConv);
    $(".click-yoghwynn").click({"outputstyle": "Pha̍k-fa-sṳ"}, doConv);
}

// any errors? Fix them
var runes = { // Chinese Character to Hakka substitutions
    "一": "yit", // one
    "二": "ngi", // two
    "三": "sâm", // three
    "四": "si", // four
    "五": "ńg", // five
    "六": "liuk", // six
    "七": "chhit", // seven
    "八": "pat", // eight
    "九": "kiú ", // nine
    "十": "sṳ̍p ", // ten
    "百": "pak", // hundred
    "千": "chhiên", // thousand
    "萬": "van", // ten thousand
    "億": "yi", // hundred million
    "兆": "seu",
    "京": "kîn",
    "ᛏ": "t",
    "ᛒ": "b",
    "ᛖ": "e",
    "ᛗ": "m",
    "ᛚ": "l",
    "ᛝ": "ng", // this is correct
    "ᛟ": "œ",
    "ᛞ": "d",
    "ᚪ": "a",
    "ᚫ": "æ",
    "ᚣ": "y",
    "ᛡ": "io", // not ia
    "ᛠ": "ea", // this is ok
    "ᛢ": "cw",
    "ᛣ": "c",
    "ᛤ": "cc",
    "ᛥ": "st",
    "ᚸ": "g"
};

// again, please report any errors, please include replacement for this
var runes2 = { // latin -> runes
    "a": "ᚪ",
    "ā": "ᚪ",
    "b": "ᛒ",
    "c": "ᚳ",
    "ċ": "ᚳ", // this is appropriate
    "d": "ᛞ",
    "e": "ᛖ",
    "ē": "ᛖ",
    "f": "ᚠ",
    "g": "ᚷ",
    "h": "ᚻ",
    "i": "ᛁ",
    "ī": "ᛁ",
    "ġ": "ᛄ",
    "k": "ᚳ",
    "l": "ᛚ",
    "m": "ᛗ",
    "n": "ᚾ",
    "o": "ᚩ",
    "ō": "ᚩ",
    "p": "ᛈ",
    "q": "ᛢ",
    "r": "ᚱ",
    "s": "ᛋ",
    "t": "ᛏ",
    "u": "ᚢ",
    "ū": "ᚢ",
    "v": "ᚹ", // maybe should convert to F instead of W?
    "w": "ᚹ",
    "x": "ᛉ",
    "y": "ᚣ",
    "ȳ": "ᚣ",
    "z": "ᛋ",
    "æ": "ᚫ",
    "œ": "ᛟ",
    "þ": "ᚦ",
    "ð": "ᚦ" // no way to distinguish, unless we want to check whether or not it's the last letter, etc. (???)
};

/**
 * Two or more characters -> a single rune or a pattern of runes
 * Put here anything that should be converted from two or more gw characters to one or more rune.
 */
rune2_exceptions = [
    ["ea", "ᛠ"],
    ["eo", "ᛇ"],
    ["io", "ᛡ"],
    ["ng", "ᛝ"],
    ["cc", "ᛤ"],
    ["cw", "ᛢ"]
];

/**
 * Replace occurrences of regex, possibly normalize
 *
 * For nodes (not text) input, modifies object
 * @param input the input text or jQuery object
 * @param regex the regular expression
 * @param replacement replacement
 * @param norm Normalize input? (also converts to lowercase)
 * @returns the converted version of input (as text/DOM node)
 */
var myReplace = function(input, regex, replacement, norm) {
    var $ = jQuery;
    if (typeof regex == "string")
        regex = new RegExp(regex, "g");
    if (typeof input == "string")
        return norm?normalizeString(input).toLowerCase():input.replace(regex,replacement);
    if (typeof input == "undefined") return;
    var node = input[0];
    if (typeof node == "undefined") node = input;
    if (!("nodeType" in node)) return node;
    if (node.nodeType == 3) {
        if (norm) node.nodeValue = normalizeString(node.nodeValue).toLowerCase();
        node.nodeValue = node.nodeValue.replace(regex, replacement);
    } else {
            if ($(node).hasClass("nochange")) return node;
        if (!("childNodes" in node)) return node;
        for (var i = 0, len = node.childNodes.length; i < len; i++)
            myReplace($(node.childNodes[i]), regex, replacement, norm);
    }
    return node;
}

/**
 * Convert style of text/jQuery node from one spelling style to another
 *
 * @param inputhtml input text or jQuery node
 * @param inputstyle possible values: "gw", "ȝƿ", "runes"
 * @param outputstyle possible values:"gw", "ȝƿ", "runes"
 * @returns converted version of inputhtml
 */
function convertStyle(inputhtml, inputstyle, outputstyle) {
    var temp = inputhtml;
    if (inputstyle == outputstyle) return inputhtml;
    if (inputstyle == "gw" && outputstyle == "ȝƿ") {
        temp = myReplace(temp, /G/g, 'Ȝ');
        temp = myReplace(temp, /g/g, 'ȝ');
        temp = myReplace(temp, /W/g, 'Ƿ');
        temp = myReplace(temp, /w/g, 'ƿ');
    } else if (inputstyle == "ȝƿ" && outputstyle == "gw") {
        temp = myReplace(temp, /Ȝ/g, "G");
        temp = myReplace(temp, /ȝ/g, "g");
        temp = myReplace(temp, /Ƿ/g, "W");
        temp = myReplace(temp, /ƿ/g, "w");
    } else if (inputstyle == "runes") {
        for (var rune in runes) {
            temp = myReplace(temp, rune, runes[rune]);
        }
        return convertStyle(temp, "gw", outputstyle);
    } else if (outputstyle == "runes") {
        temp = convertStyle(myReplace(temp,"","",1), inputstyle, "gw");
        for (var i = 0; i < rune2_exceptions.length; i++) {
            temp = myReplace(temp,rune2_exceptions[i][0],rune2_exceptions[i][1]);
        }
        for (var latin in runes2) {
            temp = myReplace(temp, latin, runes2[latin]);
        }
    }
    return temp;
}

/**
 * Remove some accents, in an Anglo-Saxon-centric manner
 *
 * @param r the string
 * @returns normalized version (same case, e.g., É -> E, é -> e)
 */
function normalizeString(r){
    
    // from http://stackoverflow.com/questions/8186302/javascript-removing-accents , modified
    // same code is found in another S.O. post
    r = myReplace(r, /ċ/g        , "c");
    r = myReplace(r, /Ċ/g        , "C");
    r = myReplace(r, "Ġ"         , "G");
    r = myReplace(r, "ġ"         , "g");
    r = myReplace(r, /[àáâãäåā]/g, "a");
    r = myReplace(r, /[ÀÁÂÃÄÅĀ]/g, "A");
    r = myReplace(r, /ç/g        , "s");
    r = myReplace(r, /Ç/g        , "S");
    r = myReplace(r, /[èéêëē]/g  , "e");
    r = myReplace(r, /[ÈÉÊËĒ]/g  , "E");
    r = myReplace(r, /[ìíîïī]/g  , "i");
    r = myReplace(r, /[ÌÍÎÏĪ]/g  , "I");
    r = myReplace(r, /ñ/g        , "n");
    r = myReplace(r, /Ñ/g        , "n");
    r = myReplace(r, /[òóôõöō]/g , "o");
    r = myReplace(r, /[ÒÓÔÕÖŌ]/g , "O");
    r = myReplace(r, /[ùúûüū]/g  , "u");
    r = myReplace(r, /[ÙÚÛÜŪ]/g  , "U");
    r = myReplace(r, /[ýÿȳ]/g    , "y");
    r = myReplace(r, /[ŸȲ]/g     , "Y");
    r = myReplace(r, /ǣ/g        , "æ");
    r = myReplace(r, /Ǣ/g        , "Æ");
    r = myReplace(r, /Ḡ/g        , "G");
    r = myReplace(r, /ḡ/g        , "g");
    return r;  
}

/*
 * This is code to be executed when the document is ready
 * Basically, add [ gw ] [ ȝƿ ] [ ᚱᚢᚾ ].
 * Clicking each link converts the document to that style.
 * (see doConv)
 */
$(function() {
    $ = jQuery;
    $("#mw-content-text").prepend($("<center class=\"nochange\"><small>[ <a href=\"javascript:void(0);\" class=\"click-gw\">gw</a> ] [ <a href=\"javascript:void(0);\" class=\"click-yoghwynn\">ȝƿ</a> ] [ <a href=\"javascript:void(0);\" class=\"click-runes\">ᚱᚢᚾ</a> ]</small></center>"));
    $(".click-gw").click({"outputstyle": "gw"}, doConv);
    $(".click-yoghwynn").click({"outputstyle": "ȝƿ"}, doConv);
    $(".click-runes").click({"outputstyle": "runes"}, doConv);
});