משתמש:מהדורה קמא/בדיקת עוגנים.js: הבדלים בין גרסאות בדף

מתוך אוצר הספרים היהודי השיתופי
קפיצה לניווט קפיצה לחיפוש
אין תקציר עריכה
(עוד ניסוי)
שורה 19: שורה 19:
       var content = textArea.val();
       var content = textArea.val();


       var anchorRegex = /\{\{עוגן1\|([^|]+)\|([^}]+)\}\}/gi;
       var anchorRegex = /\{\{(עוגן1?|anchor1?)\|([^}]+)\}\}/gi;
       var anchors = {};
       var anchors = {};


       // אסוף את כל ההופעות של {{עוגן1|}} ואת הערך שלהן
       // אסוף את כל ההופעות של {{עוגן|}} ואת הערך שלהן
       var match;
       var match;
       while ((match = anchorRegex.exec(content)) !== null) {
       while ((match = anchorRegex.exec(content)) !== null) {
         var key = match[1].toLowerCase().trim();
         var key = match[2].toLowerCase().trim();
         var value = match[2].trim();
         var template = match[1].toLowerCase().trim();
         if (!anchors[key]) {
         if (!anchors[key]) {
           anchors[key] = [];
           anchors[key] = [];
         }
         }
         // שמירת מיקום ההופעה
         // שמירת מיקום ההופעה
         anchors[key].push({ value: value, index: match.index });
         anchors[key].push({ template: template, index: match.index });
       }
       }


שורה 39: שורה 39:
           var counter = 1;
           var counter = 1;
           positions.forEach(function (pos) {
           positions.forEach(function (pos) {
             // החלפה של כל הופעה כפולה בתבנית {{עוגן|}}
             // בדיקה אם המתקבל הוא תבנית {{עוגן|}} או {{עוגן1|}}
             var replacement = '{{עוגן|' + key + counter + '|' + pos.value + '}}';
             var replacement;
             // בדיקה של תיקון הסוגריים והמבנה של התבנית
            if (pos.template === 'עוגן1' || pos.template === 'anchor1') {
            if (validateReplacement(replacement)) {
              // אם זו תבנית {{עוגן1|}}
               content = content.substring(0, pos.index) + replacement + content.substring(pos.index + match[0].length);
              replacement = '{{עוגן|' + key + counter + '|' + key + '}}';
              counter++;
             } else {
              // אם זו תבנית {{עוגן|}}
               replacement = '{{עוגן|' + key + counter + '|' + key + '}}';
             }
             }
            // החלפה של כל הופעה כפולה בתבנית המתאימה
            content = content.substring(0, pos.index) + replacement + content.substring(pos.index + match[0].length);
            counter++;
           });
           });
         }
         }
שורה 54: שורה 59:
       alert('הוחלפו עוגנים כפולים בהצלחה');
       alert('הוחלפו עוגנים כפולים בהצלחה');
     });
     });
    // פונקציה לבדיקת תקינות ההחלפה
    function validateReplacement(replacement) {
      var match = replacement.match(/\{\{עוגן\|[^|]+\|[^}]+\}\}/);
      if (!match) {
        // אם התבנית לא מתאימה למבנה הרגולרי
        alert('תבנית ההחלפה אינה תקינה: ' + replacement);
        return false;
      }
      var numBrackets = (replacement.match(/\{/g) || []).length;
      if (numBrackets != 4) {
        // אם יש יותר או פחות משתי סוגריים בתבנית
        alert('תבנית ההחלפה אינה תקינה: ' + replacement);
        return false;
      }
      return true;
    }
   });
   });
});
});

גרסה מ־20:49, 30 באפריל 2024

// סקריפט לאיתור ותיקון עוגנים כפולים במדיה ויקי
mw.loader.using(['jquery.client'], function () {
  $(function () {
    // יצירת הכפתור
    var button = $('<button type="button">בדוק ותקן עוגנים כפולים</button>');
    button.css({
      marginTop: '10px',
      padding: '5px 10px',
      fontSize: '1rem',
      cursor: 'pointer'
    });

    // הוספת הכפתור לדף
    $('#editform').prepend(button);

    // פעולה שתתבצע עם לחיצה על הכפתור
    button.click(function () {
      var textArea = $('#wpTextbox1');
      var content = textArea.val();

      var anchorRegex = /\{\{(עוגן1?|anchor1?)\|([^}]+)\}\}/gi;
      var anchors = {};

      // אסוף את כל ההופעות של {{עוגן|}} ואת הערך שלהן
      var match;
      while ((match = anchorRegex.exec(content)) !== null) {
        var key = match[2].toLowerCase().trim();
        var template = match[1].toLowerCase().trim();
        if (!anchors[key]) {
          anchors[key] = [];
        }
        // שמירת מיקום ההופעה
        anchors[key].push({ template: template, index: match.index });
      }

      $.each(anchors, function (key, positions) {
        if (positions.length > 1) {
          // אם יש יותר מהופעה אחת של אותו ערך
          var counter = 1;
          positions.forEach(function (pos) {
            // בדיקה אם המתקבל הוא תבנית {{עוגן|}} או {{עוגן1|}}
            var replacement;
            if (pos.template === 'עוגן1' || pos.template === 'anchor1') {
              // אם זו תבנית {{עוגן1|}}
              replacement = '{{עוגן|' + key + counter + '|' + key + '}}';
            } else {
              // אם זו תבנית {{עוגן|}}
              replacement = '{{עוגן|' + key + counter + '|' + key + '}}';
            }
            // החלפה של כל הופעה כפולה בתבנית המתאימה
            content = content.substring(0, pos.index) + replacement + content.substring(pos.index + match[0].length);
            counter++;
          });
        }
      });

      // מעדכן את תיבת הטקסט עם השינויים
      textArea.val(content);
      alert('הוחלפו עוגנים כפולים בהצלחה');
    });
  });
});