Utilisateur:Phe/Michaud.js

La bibliothèque libre.

Note : après avoir enregistré vos modifications, il se peut que vous deviez forcer le rechargement complet du cache de votre navigateur pour voir les changements.

  • Firefox / Safari : Maintenez la touche Maj (Shift) en cliquant sur le bouton Actualiser ou pressez Ctrl-F5 ou Ctrl-R (⌘-R sur un Mac) ;
  • Google Chrome : Appuyez sur Ctrl-Maj-R (⌘-Shift-R sur un Mac) ;
  • Internet Explorer : Maintenez la touche Ctrl en cliquant sur le bouton Actualiser ou pressez Ctrl-F5 ;
  • Opera : Allez dans Menu → Settings (Opera → Préférences sur un Mac) et ensuite à Confidentialité & sécurité → Effacer les données d'exploration → Images et fichiers en cache.
var auto_michaud = {

    // try to replace ## "début" ## marker with the last section of the previous page
    last_match : function (regex, text) {
        var last_match = null;
        var match;
        while (match = regex.exec(text))
           last_match = match;
        return last_match;
    },

    has_section_begin : function () {
        var wpTextbox1 = document.getElementById("wpTextbox1");
        return !wpTextbox1 || wpTextbox1.value.search('## "début" ##') != -1
    },

    pagename : null,

    set_section_begin : function (data)  {
        var wpTextbox1 = document.getElementById("wpTextbox1");
        if (wpTextbox1 && this.has_section_begin()) {
            for (var ids in data.query.pages) {
                if (ids > 0) {
                    if (data.query.pages[ids].title == auto_michaud.pagename) {
                        var content = data.query.pages[ids].revisions[0]['*'];
                        var r = new RegExp("<section end=\"(.*?)\"", "g");
                        var match = this.last_match(r, content);
                        if (match) {
                            wpTextbox1.value = wpTextbox1.value.replace('## "début" ##', '## "' + match[1] + '" ##');
                        }
                        break;
                    }
                }
            }
        }

        mise_en_page.fill_rh(data);
        var header = document.getElementsByName('wpHeaderTextbox')[0];
        if (header && auto_michaud.rh_string)
            header.value = header.value.replace(/[A-Z][A-Z][A-Z]/, auto_michaud.rh_string);

        if (wpTextbox1) {
            wpTextbox1.value = wpTextbox1.value.replace(/^\n*/, "");
            wpTextbox1.value = wpTextbox1.value.replace(/(##.*##)\n\n/, "$1\n");
            wpTextbox1.value = wpTextbox1.value.replace(/\n*$/, "");
        }
    },

    rh_string : null,

    typographie_michaud : function () {
        var char_min = 'a-zéèàùâêîôûŷäëïöüÿçœæſñ';
        var char_maj = 'A-ZÉÈÀÂÙÊÎÔÛŶÄËÏÖÏŸÇŒÆÑ';
        var char = char_min + char_maj;
 
        var bw = '([^' + char + ']|^)';
        var ew = '([^' + char + ']|$)';
        var ch = '[' + char + ']';

        var typo_def = new Array(
             // Format: couple de regexp, remplacement, le flag est toujours "g".
             // L’ordre des définitions est parfois important
             'í', 'i',
             bw + 'siège' + ew, '$1siége$2',
             bw + 'sièges' + ew, '$1siéges$2',
             bw + 'succés' + ew, '$1succès$2',
             bw + 'aprés' + ew, '$1après$2',
             bw + 'progrés' + ew, '$1progrès$2',
             bw + 'Angouléme' + ew, '$1Angoulême$2',
             bw + 'étre' + ew, '$1être$2',
             bw + 'piéce' + ew, '$1pièce$2',
             bw + 'Genéve' + ew, '$1Genève$2',
             bw + 'agé' + ew, '$1âgé$2',
             bw + 'zéle' + ew, '$1zèle$2',
             bw + 'zéles' + ew, '$1zélés$2',
             bw + 'mére' + ew, '$1mère$2',
             bw + 'pére' + ew, '$1père$2',
             bw + 'régle' + ew, '$1règle$2',
             bw + 'rénes' + ew, '$1rênes$2',
             bw + 'poete' + ew, '$1poëte$2',
             bw + 'poetes' + ew, '$1poëtes$2',
             bw + 'Théatre' + ew, '$1Théâtre$2',
             bw + 'ainé' + ew, '$1aîné$2',
             bw + 'téte' + ew, '$1tête$2',
             bw + 'tète' + ew, '$1tête$2',
             bw + 'bète' + ew, '$1bête$2',
             bw + 'bètes' + ew, '$1bêtes$2',
             bw + 'féte' + ew, '$1fête$2',
             bw + 'fétes' + ew, '$1fêtes$2',
             bw + 'prétre' + ew, '$1prêtre$2',
             bw + 'prétres' + ew, '$1prêtres$2',
             bw + 'modéle' + ew, '$1modèle$2',
             bw + 'n’\\)', ' $1d’)',
             ' 1er([ ,])', ' 1{{er}}$1',
             bw + 'in—([0-9][0-9]*)', ' in-$1',
             bw + 'in-1°', ' in-4°',
             bw + 'tout a coup', '$1tout à coup$2',
             bw + 'in-foi\.', '$1in-fol.'
        );

        var wpTextbox1 = document.getElementById("wpTextbox1");
        if (!wpTextbox1)
            return;

        var text = wpTextbox1.value;

        if ($('.mw-newarticletext').length)
            this.rh_string = /[A-Z][A-Z][A-Z]/.exec(text);

        text = text.replace(/<noinclude>\s*<center>(.*?)<\/center>\s*<\/noinclude>\s*/g, '\n');
        text = mise_en_page.typographie(text, typo_def);
 
        wpTextbox1.value = text;
    },

    handle_nowiki_section : function () {
        var wpTextbox1 = document.getElementById("wpTextbox1");
        if (!wpTextbox1 || !$('.mw-newarticletext').length)
            return;

        var text = wpTextbox1.value;
        text = text.replace(/##([^#]*)##\n(?!<nowiki)/g, '##$1##\n<nowiki/>\n\n');
        text = text.replace(/^##([^#]*)##\n<nowiki\/>\n\n/g, '##$1##\n');

        wpTextbox1.value = text;
    },

    strip_text : function () {
        var wpTextbox1 = document.getElementById("wpTextbox1");
        if (!wpTextbox1)
            return;

        var text = wpTextbox1.value;

        // broken, if the lf is missing at start of textbox something remove the first section
        //text = text.replace(/^\n*/, "");
        //text = text.replace(/\n*$/, "");

        wpTextbox1.value = text;
    },

    auto_Michaud : function() {
        if (auto_michaud.has_section_begin() || !mise_en_page.has_rh() || !mise_en_page.has_tiret()) {
            var r = new RegExp("(\\d+)$");
            var page = Number(r.exec(wgPageName)[1]);
            auto_michaud.pagename = 'Page:' + wgTitle.replace(/\d+$/g, page - 1);
            mise_en_page.pagename = 'Page:' + wgTitle.replace(/\d+$/g, page - 2);
            mise_en_page.previous_page = auto_michaud.pagename;

            var url = wgServer + wgScriptPath
                + "/api.php?action=query&prop=revisions&callback=auto_michaud.set_section_begin&rvprop=content"
                + "&format=json&titles=" + encodeURIComponent(auto_michaud.pagename) + '|' + encodeURIComponent(mise_en_page.pagename);
            auto_michaud.create_script_obj(url)
        }
        auto_michaud.typographie_michaud();
        auto_michaud.handle_nowiki_section();
        auto_michaud.strip_text();
    },

    create_script_obj : function(url) {
        var scriptObj = document.createElement("script");
        scriptObj.setAttribute("type", "text/javascript");
        scriptObj.setAttribute("src", url);
        document.body.appendChild(scriptObj);
    }
};


if (mw.config.get("wgNamespaceNumber") == 104 && mw.config.get("wgAction") == "edit" && mw.config.get("wgTitle").search(/Michaud - Biographie universelle ancienne et moderne /) == 0) {
    window.no_mise_en_page = true;
    $(document).ready(auto_michaud.auto_Michaud);
}