انتقل إلى المحتوى

مستخدم:Khalil-2016/Replace.js

من ويكيبيديا، الموسوعة الحرة

ملاحظة: بعد الحفظ، قد يلزمك إفراغ الكاش لرؤية التغييرات.

/*
سكربت لعمل استبدالات وأصلاحات للنص محددة مسبقا
المكتبات المطلوبة: jQuery + jQuery UI
*/

// إنشاء الزر والخيارات
//ثم وضع الزر بعد مربع الملخص في صندوق التحرير
$( "#wpSummary" ).after('<button type="button" id="openOpts" >استبدالات مسبقة</button>'
+'<div id="mOptions" title="خيارات استبدال وإصلاح النص">'
+'<p><label><input type="checkbox" id="TransMonths" value="TransMonths">تعريب أشهر السنة </label></p>'
+'<p><label><input type="checkbox" id="HideParentheses" value="HideParentheses">إخفاء ما بين الأقواس من الوصلة </label></p>'
+'<p><label><input type="checkbox" id="EngTM" value="EngTM"> (بالإنجليزية:) إلى {{إنجليزية|}}</label></p>'
+'<p><label><input type="checkbox" id="MergeLines" value="MergeLines">دمج الأسطر الفارغة </label></p>'
+'<p><label><input type="checkbox" id="NoExtraSpace" value="NoExtraSpace"> مسح المسافة الفارغة الزائدة من صندوق المعلومات</label></p>'
+'<p><label><input type="checkbox" id="DeleteEngIW" value="DeleteEngIW">حذف الوصلة الداخلية الإنجليزية </label></p>'
+'<p><label><input type="checkbox" id="YearOnly" value="DeleteEngIW">جعل النص الظاهر من الوصلة هو السنة فقط </label></p'
+'<p><label><input type="checkbox" id="ArComma" value="ArComma">استبدال الفاصلة الإنجليزية بالعربية </label></p'
+'</div>');

//إنشاء النافذة
$(function() { 
        checkState = [];
        var buttons = {
                'اختيار الكل': select,
                'إلغاء الاختيار': deselect,
                'إلغاء': cancel,
                'حفظ': save
        };
    
        $('#openOpts').click(function() { $('#mOptions').dialog('open'); });

        $('#mOptions').dialog({ 
                autoOpen: false, 
                modal: true,
                buttons: buttons,
                width: 400,
                position: 'center',
                open: openDialog
        });
});

function openDialog() {
    $(':checkbox', this).each(function() {
        $(this).prop('original', $(this).is(':checked'));
    });
}

//------------------------------------------------------------------------------
// دالة الحفظ
function save() {
    var finalText = document.getElementById('wpTextbox1').value;   //النص الأصلي
    
   // استبدال بقالب إنجليزية
   if ($('#EngTM').is(':checked')) { 
     finalText = finalText.replace(/\(بالإنجليزية:(.+?)\)/g, '{{إنجليزية|$1}}');
   }
   //دمج الأسطر الفارغة
   if ($('#MergeLines').is(':checked')) { 
     finalText = finalText.replace(/\n{3,}/g, '\n\n');
   }
   //مسح المسافة الزائدة من صندوق المعلومات
   if ($('#NoExtraSpace').is(':checked')) { 
     finalText = finalText.replace(/\|(.+?)\s{2,}=/g, '|$1 =');
     finalText = finalText.replace(/\|\s{2,}(.+?)=/g, '| $1=');
   }
   //تعريب أشهر السنة
   if ($('#TransMonths').is(':checked')) { 
     finalText = finalText.replace(/\bJanuary\b/g, 'يناير');
     finalText = finalText.replace(/\bFebruary\b/g, 'فبراير');
     finalText = finalText.replace(/\bMarch\b/g, 'مارس');
     finalText = finalText.replace(/\bApril\b/g, 'أبريل');
     finalText = finalText.replace(/\bMay\b/g, 'مايو');
     finalText = finalText.replace(/\bJune\b/g, 'يونيو');
     finalText = finalText.replace(/\bJuly\b/g, 'يوليو');
     finalText = finalText.replace(/\bAugust\b/g, 'أغسطس');
     finalText = finalText.replace(/\bSeptember\b/g, 'سبتمبر');
     finalText = finalText.replace(/\bOctober\b/g, 'أكتوبر');
     finalText = finalText.replace(/\bNovember\b/g, 'نوفمبر');
     finalText = finalText.replace(/\bDecember\b/g, 'ديسمبر');  
     finalText = finalText.replace(/\bYear\b/g, 'عام');
     finalText = finalText.replace(/\bTitle\b/g, 'عنوان'); 
     finalText = finalText.replace(/\bRole\b/g, 'دور');  
     finalText = finalText.replace(/\bNotes\b/g, 'ملاحظة'); 
     finalText = finalText.replace(/\bFilmography\b/g, 'فيلموغرافيا');
     finalText = finalText.replace(/\bFilm\b/g, 'افلام');
     finalText = finalText.replace(/\bTelevision\b/g, 'تلفزيون');
     finalText = finalText.replace(/\bStage\b/g, 'مسرح');
     finalText = finalText.replace(/\bVenue\b/g, 'مكان');
    ;
   }
   //إخفاء ما بين الأقواس من الوصلة
   if ($('#HideParentheses').is(':checked')) { 
     finalText = finalText.replace(/\)\]\]/g, ')|]]');
   }   
   //حذف الوصلة الداخلية الإنجليزية
   if ($('#DeleteEngIW').is(':checked')) { 
     finalText = finalText.replace(/\[\[([A-Za-z].+?)\]\]|\[\[(\d+.+?[A-Za-z])\]\]/g, '');
   }   
   //جعل النص الظاهر من الوصلة هو السنة فقط
   if ($('#YearOnly').is(':checked')) { 
     finalText = finalText.replace(/\[\[(.+?) (\d\d\d\d)\]\]/g, '[[$1 $2|$2]]');
   }   
   //استبدال الفاصلة الإنجليزية بالعربية
   if ($('#ArComma').is(':checked')) { 
     finalText = finalText.replace(/, /g, '، ');
   }   
   
   $('#wpTextbox1').val(finalText);  //حفظ النص
   $(this).dialog('close');          //إغلاق النافذة
}
//------------------------------------------------------------------------------
// دالة اختيار الكل
function select() {
        $(':checkbox', this).prop('checked', true);
}

//دالة إلغاء الاختيار 
function deselect() {
        $(':checkbox', this).prop('checked', false);
}

//دالة إلغاء الأمر
function cancel() {
        $(this).find('input[type="checkbox"]').each(function() {
            $(this).prop('checked', $(this).prop('original'));
        });
        $(this).dialog('close');
}