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

קפיצה לניווט קפיצה לחיפוש
(ניסוי)
(תיקון)
שורה 24: שורה 24:
     // בדיקה שהתיבה המקורית אכן מכילה תוכן עם עוגנים
     // בדיקה שהתיבה המקורית אכן מכילה תוכן עם עוגנים
     if ($('#wpTextbox1').length > 0) {
     if ($('#wpTextbox1').length > 0) {
       replaceDuplicateAnchors();
       replaceDuplicateAnchorsWithinTemplate();
     }
     }
   });
   });


function replaceDuplicateAnchorsWithinTemplate() {
  function replaceDuplicateAnchorsWithinTemplate() {
  // קביעת משתנים והבניית הביטויים הרגולריים
    // קביעת משתנים והבניית הביטויים הרגולריים
  var textArea = $('#wpTextbox1');
    var textArea = $('#wpTextbox1');
  var content = textArea.val();
    var content = textArea.val();
  var anchorRegex = /\{\{עוגן(?:1)?\|([^}|]+)(?:\|([^}]+))?\}\}/gi;
    var anchorRegex = /\{\{עוגן(?:1)?\|([^}|]+)(?:\|([^}]+))?\}\}/gi;
  var anchors = {};
    var anchors = {};
  var match;
    var match;


  // חיפוש והחלפת העוגנים הכפולים רק בתוך שטח התבנית
    // חיפוש והחלפת העוגנים הכפולים רק בתוך שטח התבנית
  var templateStart = content.indexOf("{{עוגן");
    var templateStart = content.indexOf("{{עוגן");
  var templateEnd = content.indexOf("}}", templateStart);
    var templateEnd = content.indexOf("}}", templateStart);
  var templateContent = content.substring(templateStart, templateEnd + 2);
    var templateContent = content.substring(templateStart, templateEnd + 2);
 
   
  while ((match = anchorRegex.exec(templateContent)) !== null) {
    while ((match = anchorRegex.exec(templateContent)) !== null) {
    var key = match[1].toLowerCase();
      var key = match[1].toLowerCase();
    var text = match[2] !== undefined ? match[2] : key; // אם אין טקסט חופשי, השתמש בפרמטר העוגן
      var text = match[2] !== undefined ? match[2] : key; // אם אין טקסט חופשי, השתמש בפרמטר העוגן
    if (!anchors[key]) {
      if (!anchors[key]) {
       anchors[key] = [];
        anchors[key] = [];
      }
       anchors[key].push({ index: match.index, length: match[0].length, text: text });
     }
     }
    anchors[key].push({ index: match.index, length: match[0].length, text: text });
  }


  $.each(anchors, function (key, positions) {
    $.each(anchors, function (key, positions) {
    if (positions.length > 1) {
      if (positions.length > 1) {
      var offset = 0;
        var offset = 0;
      var counter = 1; // מספר התוקף הנוכחי
        positions.forEach(function (pos, i) {
      positions.forEach(function (pos, i) {
          var replacement;
        var replacement;
          if (i > 0) { // מתחיל למספר מהעוגן השני
        if (i > 0) { // מתחיל למספר מהעוגן השני
            var counter = i + 1;
          counter++;
            replacement = `{{עוגן|${key}${counter}|${pos.text}}}`;
          replacement = `{{עוגן|${key}${counter}|${pos.text}}}`;
          } else {
        } else {
            replacement = `{{עוגן|${key}|${pos.text}}}`; // העוגן הראשון ללא מספור
          replacement = `{{עוגן|${key}|${pos.text}}}`; // העוגן הראשון ללא מספור
          }
        }
          var startIndex = pos.index + offset;
        var startIndex = pos.index + offset;
          templateContent = templateContent.slice(0, startIndex) + replacement + templateContent.slice(startIndex + pos.length);
        templateContent = templateContent.slice(0, startIndex) + replacement + templateContent.slice(startIndex + pos.length);
          offset += replacement.length - pos.length;
        offset += replacement.length - pos.length;
        });
      });
      }
    }
    });
  });


  // החלפת התבנית המתוקנת בתוך הטקסט המקורי
    // החלפת התבנית המתוקנת בתוך הטקסט המקורי
  content = content.substring(0, templateStart) + templateContent + content.substring(templateEnd + 2);
    content = content.substring(0, templateStart) + templateContent + content.substring(templateEnd + 2);


  // בדיקה שהתוכן של התיבה שונה והודעה למשתמש
    // בדיקה שהתוכן של התיבה שונה והודעה למשתמש
  if (textArea.val() !== content) {
    if (textArea.val() !== content) {
    textArea.val(content);
      textArea.val(content);
    alert('הוחלפו עוגנים כפולים בתוך התבנית בהצלחה');
      alert('הוחלפו עוגנים כפולים בתוך התבנית בהצלחה');
  } else {
    } else {
    alert('אין עוגנים כפולים בתוך התבנית');
      alert('אין עוגנים כפולים בתוך התבנית');
    }
   }
   }
}
});
});
       });
       });