MediaWiki:Gadget-Formatage/sections.js
Remarque: après avoir sauvegardé, vous devez vider le cache de votre navigateur pour que les changements prennent effet. Mozilla, cliquez sur Actualiser (ou ctrl-r). Internet Explorer / Opera: ctrl-f5. Safari: cmd-r. Konqueror ctrl-r.
// Copie partielle de [[Utilisateur:Automatik/sections.js]]
// Fonction de formatage des sections
// À charger d'une manière ou d'une autre (bouton, automatique...)
var wikibloc = document.getElementById( 'wpTextbox1' );
function maj_sections() {
var wikicode_fragmente = wikibloc.value.split("\n");
// Traitement de chaque texte de section
function maj_section( texte_section, noLigne ) {
var
nom_section,
code_langue,
num,
cle,
genre,
flexion = false,
locution = false,
niveau = "",
texte_final = "";
// Cas spéciaux
if ( /^\{\{trad-trier\}\} *$/.test(texte_section) ) {
return "===== {{S|traductions à trier}} =====";
}
texte_section = texte_section.replace(/ +$/, "");
var modele_S = false;
// Récupération du nom de la section
nom_section = /\{\{-([^|}]+)-/.exec( texte_section );
// Pas trouvé ? On essaye la nouvelle syntaxe
if (nom_section === null) {
nom_section = /\{\{S\| *([^|}]+) *[}|]/.exec( texte_section );
// Trouvé : on garde (sinon : erreur)
if (nom_section !== null) {
modele_S = true;
} else {
erreurs.push( [texte_section, noLigne, "Pas de nom de section reconnu"] );
return texte_section;
}
}
// Formatage
nom_section = $.trim(nom_section[1]);
nom_section = lcfirst(nom_section);
// Flexion ?
if ( nom_section.indexOf('flex') === 0 && nom_section.length >= 6 ) {
nom_section = nom_section.substring(5);
flexion = true;
}
// On enlève le -loc
if ( nom_section.indexOf('loc-') === 0 && nom_section.length >= 5 && nom_section != 'loc-phr') {
nom_section = nom_section.substring(4);
}
// Gestion du paramètre "s" de {-note-}
if ( nom_section == "note" && /\|s=[^|}]/.exec( texte_section ) !== null ) {
nom_section = "notes";
}
// Section non reconnue par le gadget
if ( !sections[nom_section] ) {
erreurs.push( [texte_section, noLigne, "Nom de section inconnu : " + nom_section] );
return texte_section;
}
// Récupération du code langue, si nécessaire
if ( sections[nom_section].hasCode ) {
if (modele_S) {
code_langue = /\{\{S\|[^\|\}]+?\|([^|}=]+?)[|}]/.exec( texte_section );
} else {
code_langue = /\|([^|}=]+?)[|}]/.exec( texte_section );
}
if (code_langue !== null) {
code_langue = $.trim(code_langue[1]);
} else {
erreurs.push( [texte_section, noLigne, "Code langue manquant"] );
return texte_section;
}
}
if (modele_S) {
// Récupération du paramètre "flexion", si présent
flex = /\|flexion[|}]/.exec( texte_section );
if (flex !== null) {
flexion = true;
}
// Récupération du paramètre "locution", si présent
locution = /locution *=([^|}]+?)[|}]/.exec( texte_section );
if (locution !== null) {
locution = $.trim(locution[1]);
}
}
// Récupération du paramètre "num", si présent
num = /num *=([^|}]+?)[|}]/.exec( texte_section );
if (num !== null) {
num = $.trim(num[1]);
}
// Récupération du paramètre "clé", si présent
cle = /clé *=([^|}]+?)[|}]/.exec( texte_section );
if (cle !== null) {
cle = $.trim(cle[1]);
}
// Gestion du paramètre "genre" de {-prénom-}
if ( nom_section == "prénom" && /genre *=([^|}]+?)[|}]/.exec( texte_section ) !== null ) {
genre = /genre *=([^|}]+?)[|}]/.exec( texte_section )[1];
}
// Génération du texte final qui remplace l'ancienne ligne de section
texte_final = "{{S|" + sections[nom_section].nom_maj;
if ( code_langue ) texte_final += ("|" + code_langue);
if ( flexion ) texte_final += "|flexion";
if ( locution ) texte_final += "|locution=" + locution;
if ( genre ) texte_final += ("|genre=" + genre);
if ( cle ) texte_final += ("|clé=" + cle);
if ( num ) texte_final += ("|num=" + num);
texte_final += "}}";
texte_final = ajoute_niveau( texte_final, sections[nom_section].niveau );
return texte_final;
}
// Écrit le nombre correct de signes = de part et d'autre du texte de titre
var ajoute_niveau = function( wikitext, niveau ) {
switch (niveau) {
case 3:
niveau = "===";
break;
case 4:
niveau = "====";
break;
case 5:
niveau = "=====";
break;
}
return niveau + " " + wikitext + " " + niveau;
};
// Parcours des lignes
for (var k = 0; k < wikicode_fragmente.length; k++) {
if ( /^\{\{-[^|}]+-[^}]*?\}\} *$/.test(wikicode_fragmente[k]) // anciens modèles
|| /^\{\{trad-trier\}\} *$/.test(wikicode_fragmente[k]) // ancien modèle non conventionnelle
|| /^\{\{n-vern\}\} *$/.test(wikicode_fragmente[k]) // idem
|| /^ *=+ *\{\{S\|[^|}]+[^}]*?\}\} *=+ *$/.test(wikicode_fragmente[k]) // nouveau modèle S
) {
// Mise à jour ou correction de la section s'il y a lieu
var wikichanged = wikibloc.value.replace(wikicode_fragmente[k], maj_section(wikicode_fragmente[k], k+1) );
if (wikichanged != wikibloc.value) {
modifs++;
wikibloc.value = wikichanged;
}
}
}
// Post traitement : espaces avant et après les sections
// (repris de https://fr.wiktionary.org/w/index.php?title=Discussion_module:section/analyse/test&oldid=15457586 )
wikiblocs_lignes = wikibloc.value;
wikiblocs_lignes = wikiblocs_lignes.replace( /[\r\n]+==/g, "\n\n==" );
wikiblocs_lignes = wikiblocs_lignes.replace( /==[\r\n]+/g, "==\n" );
if (wikiblocs_lignes != wikibloc.value) {
modifs++;
wikibloc.value = wikiblocs_lignes;
}
return true;
}