/* =========================================================
alsweed.sa — تحويل |جدول| و |اسئلة| إلى جداول وأسئلة شائعة
متوافق مع ثيم سيليا React (المحتوى يُرسم بعد تحميل الصفحة)
========================================================= */
(function () {
'use strict';
// مسافات أو وسوم HTML محتملة داخل العلامة نفسها
var G = '(?:\\s| |<[^>]*>)*';
var TABLE_RE = new RegExp(
'(
]*>\\s*)?\\|' + G + 'جدول' + G + '\\|([\\s\\S]*?)\\|' + G + 'الجدول' + G + '\\|(\\s*
)?', 'g');
var FAQ_RE = new RegExp(
'(]*>\\s*)?\\|' + G + '[اأإ]سئلة' + G + '\\|([\\s\\S]*?)\\|' + G + 'نهاية' + G + '\\|(\\s*
)?', 'g');
var TABLE_TEXT = /\|\s*جدول\s*\|[\s\S]*?\|\s*الجدول\s*\|/;
var FAQ_TEXT = /\|\s*[اأإ]سئلة\s*\|[\s\S]*?\|\s*نهاية\s*\|/;
function stripTags(s) { return s.replace(/<[^>]*>/g, ''); }
function isEmpty(s) { return stripTags(s).replace(/ /g, ' ').trim() === ''; }
// تحويل كتلة HTML إلى أسطر مع الإبقاء على الروابط والتنسيق داخل السطر
function toLines(html) {
return html
.replace(/ /g, ' ')
.replace(/
/gi, '\n')
.replace(/<\/(p|div|li|h[1-6]|ul|ol)>/gi, '\n')
.replace(/<(p|div|li|h[1-6]|ul|ol)(\s[^>]*)?>/gi, '')
.split('\n')
.map(function (l) { return l.trim(); })
.filter(function (l) { return !isEmpty(l); });
}
// تقسيم السطر على | مع تجاهل أي | داخل وسوم HTML
function splitCells(line) {
var cells = [], buf = '', inTag = false;
for (var i = 0; i < line.length; i++) {
var c = line.charAt(i);
if (c === '<') inTag = true;
else if (c === '>') inTag = false;
if (c === '|' && !inTag) { cells.push(buf.trim()); buf = ''; }
else buf += c;
}
cells.push(buf.trim());
if (cells.length && isEmpty(cells[0])) cells.shift();
if (cells.length && isEmpty(cells[cells.length - 1])) cells.pop();
return cells;
}
function buildTable(inner) {
var lines = toLines(inner);
if (!lines.length) return null;
var header = splitCells(lines[0]);
var cols = header.length;
if (!cols) return null;
var html = '';
header.forEach(function (h) { html += '| ' + h + ' | '; });
html += '
';
for (var i = 1; i < lines.length; i++) {
var cells = splitCells(lines[i]);
while (cells.length < cols) cells.push('');
html += '';
cells.slice(0, cols).forEach(function (c) { html += '| ' + c + ' | '; });
html += '
';
}
return html + '
';
}
function removePrefix(line, letter) {
var re = new RegExp('^((?:\\s|<[^>]+>)*)' + letter + '\\s*[::\\-]\\s*');
return line.replace(re, '$1');
}
function buildFaq(inner) {
var lines = toLines(inner), items = [], cur = null;
lines.forEach(function (line) {
var text = stripTags(line).trim();
if (/^س\s*[::\-]/.test(text)) {
cur = { q: removePrefix(line, 'س'), a: [] };
items.push(cur);
} else if (/^ج\s*[::\-]/.test(text)) {
if (cur) cur.a.push(removePrefix(line, 'ج'));
} else if (cur) {
if (cur.a.length) cur.a.push(line);
else cur.q += ' ' + line;
}
});
if (!items.length) return null;
var html = '';
items.forEach(function (it) {
html += '
' + it.q + '
' + it.a.join('
') + '
';
});
return html + '
';
}
function processElement(el) {
var before = el.innerHTML;
var after = before
.replace(TABLE_RE, function (m, p1, inner) { return buildTable(inner) || m; })
.replace(FAQ_RE, function (m, p1, inner) { return buildFaq(inner) || m; });
if (after !== before) el.innerHTML = after;
}
// البحث عن أقرب عنصر يحتوي العلامة كاملة (بداية + نهاية)
function findTargets() {
var targets = [];
var walker = document.createTreeWalker(document.body, NodeFilter.SHOW_TEXT, {
acceptNode: function (n) {
return /\|\s*(جدول|[اأإ]سئلة)\s*\|/.test(n.nodeValue)
? NodeFilter.FILTER_ACCEPT : NodeFilter.FILTER_SKIP;
}
});
var node;
while ((node = walker.nextNode())) {
var el = node.parentElement;
if (!el || el.closest('script,style,textarea,[contenteditable="true"]')) continue;
while (el && el !== document.body) {
var t = el.textContent;
if (TABLE_TEXT.test(t) || FAQ_TEXT.test(t)) break;
el = el.parentElement;
}
if (el && el !== document.body && targets.indexOf(el) === -1) targets.push(el);
}
return targets;
}
var observer = null, timer = null;
function run() {
if (!document.body) return;
if (observer) observer.disconnect();
try {
findTargets().forEach(processElement);
} catch (e) {
console.error('[alsweed] تحويل الجداول/الأسئلة فشل:', e);
}
if (observer) observer.observe(document.body, { childList: true, subtree: true });
}
function schedule() {
clearTimeout(timer);
timer = setTimeout(run, 300);
}
function init() {
run();
// ثيم React يرسم المحتوى ويغيّره بعد التحميل، فنراقب التغييرات
observer = new MutationObserver(schedule);
observer.observe(document.body, { childList: true, subtree: true });
}
if (document.readyState === 'loading') document.addEventListener('DOMContentLoaded', init);
else init();
window.addEventListener('load', schedule);
})();
/* =========================================================
alsweed.sa — فهرس المقال
الكمبيوتر: في العمود الجانبي | الجوال: زر مصغّر تحت العنوان
متوافق مع ثيم سيليا React ويعيد البناء إذا أُعيد رسم الصفحة
========================================================= */
(function () {
'use strict';
var c = function (t) { return (t || '').replace(/\s+/g, ' ').trim(); };
// جمع بنود الفهرس من المقال وربطها بعناوين h2
function collect(art) {
var lbl = [].find.call(art.querySelectorAll('p'), function (p) {
return c(p.textContent).indexOf('محتويات المقال') > -1;
});
if (!lbl) return null;
var el = lbl.nextElementSibling;
while (el && !c(el.textContent)) el = el.nextElementSibling;
if (!el) return null;
var titles = el.tagName === 'OL' || el.tagName === 'UL'
? [].map.call(el.children, function (li) { return c(li.textContent); })
: c(el.textContent).split('|').map(c);
var items = [];
[].forEach.call(art.querySelectorAll('h2'), function (h, i) {
var t = c(h.textContent);
if (titles.indexOf(t) < 0) return;
h.id = h.id || 'sec-' + i;
h.style.scrollMarginTop = '110px';
items.push({ id: h.id, text: t });
});
if (!items.length) return null;
return { lbl: lbl, el: el, items: items };
}
function makeList(items) {
var ol = document.createElement('ol');
items.forEach(function (it) {
var li = document.createElement('li');
var a = document.createElement('a');
a.href = '#' + it.id;
a.textContent = it.text;
li.appendChild(a);
ol.appendChild(li);
});
return ol;
}
function build() {
var art = document.querySelector('article');
if (!art) return;
var hasDesktop = !!document.getElementById('alsweed-toc');
var hasMobile = !!document.getElementById('alsweed-toc-m');
if (hasDesktop && hasMobile) return;
var data = collect(art);
if (!data) return; // مقال بدون فهرس أو بدون عناوين مطابقة
var built = hasDesktop || hasMobile;
// نسخة الكمبيوتر: العمود الجانبي
var side = document.querySelector('aside[aria-label="Sidebar"]');
if (side && !hasDesktop) {
var box = document.createElement('nav');
box.id = 'alsweed-toc';
box.innerHTML = '📑 محتويات المقال';
box.appendChild(makeList(data.items));
side.prepend(box);
built = true;
}
// نسخة الجوال: زر مصغّر تحت العنوان وسطر التاريخ والكاتب
var h1 = art.querySelector('h1') || document.querySelector('main h1') || document.querySelector('h1');
if (h1 && !hasMobile) {
var anchor = h1;
var next = h1.nextElementSibling;
if (next && c(next.textContent).length < 120) anchor = next; // سطر التاريخ والكاتب
var det = document.createElement('details');
det.id = 'alsweed-toc-m';
var sum = document.createElement('summary');
sum.textContent = '📑 محتويات المقال (' + data.items.length + ')';
det.appendChild(sum);
det.appendChild(makeList(data.items));
det.addEventListener('click', function (e) {
if (e.target.tagName === 'A') det.open = false; // إغلاق القائمة بعد اختيار بند
});
anchor.insertAdjacentElement('afterend', det);
built = true;
}
// إخفاء الفهرس الأصلي داخل المقال فقط بعد نجاح بناء البديل
if (built) {
data.lbl.classList.add('alsweed-toc-inline');
data.el.classList.add('alsweed-toc-inline');
}
}
function safeBuild() {
try { build(); }
catch (e) { console.error('[alsweed] فشل بناء فهرس المقال:', e); }
}
function init() {
safeBuild();
// ثيم React قد يعيد رسم الصفحة ويمسح الفهرس (مثل خطأ 418)،
// لذلك نبقى نراقب ونعيد البناء كلما اختفى
var timer = null;
var obs = new MutationObserver(function () {
clearTimeout(timer);
timer = setTimeout(safeBuild, 250);
});
obs.observe(document.body, { childList: true, subtree: true });
}
if (document.readyState === 'loading') document.addEventListener('DOMContentLoaded', init);
else init();
})();