کاربر:Jeeputer/Aligner.js: تفاوت میان نسخه‌ها

از ویکی‌پدیا، دانشنامهٔ آزاد
محتوای حذف‌شده محتوای افزوده‌شده
صفحه‌ای تازه حاوی «// Originally written by en:User:Majavah at en:User:Majavah/Aligner.js // <nowiki> window.alignerDebug = false; window.alignerDe...» ایجاد کرد
 
فاصله و وصل مجازی را برای محاسبهٔ اندازه در نظر نگیر
خط ۱۶: خط ۱۶:
if (mw.config.get('wgAction') !== 'edit') {
if (mw.config.get('wgAction') !== 'edit') {
return;
return;
}
function getVisualLength(text) {
// .split('') isn't non-BMP aware yet it doesn't matter here also
return text.split('').filter(function (x) {
// Don't consider ZWNJ and ZWJ for length
return x !== '\u200c' && x !== '\u200d';
}).length;
}
}


خط ۱۲۸: خط ۱۳۶:
firstPart = firstPart.slice(1).trim();
firstPart = firstPart.slice(1).trim();


if (firstPart.length > maxLength) {
if (getVisualLength(firstPart) > maxLength) {
maxLength = firstPart.length;
maxLength = getVisualLength(firstPart);
}
}


خط ۱۵۲: خط ۱۶۰:
console.log({ firstPart, maxLength });
console.log({ firstPart, maxLength });


while (firstPart.length < maxLength) {
while (getVisualLength(firstPart) < maxLength) {
firstPart += ' ';
firstPart += ' ';
}
}

نسخهٔ ‏۱۴ آوریل ۲۰۲۱، ساعت ۰۶:۴۰

// Originally written by [[:en:User:Majavah]] at [[:en:User:Majavah/Aligner.js]]
// <nowiki>
window.alignerDebug = false;

window.alignerDefaultSearches = [
    "{{infobox",
    "{{speciesbox",
    "{{taxobox",
    '{{automatic taxobox',
    '{{جعبه اطلاعات',
];

window.alignerExtraSearches = [];

mw.loader.using(['mediawiki.util'], function () {
    if (mw.config.get('wgAction') !== 'edit') {
        return;
    }
    
    function getVisualLength(text) {
    	// .split('') isn't non-BMP aware yet it doesn't matter here also
    	return text.split('').filter(function (x) {
    		// Don't consider ZWNJ and ZWJ for length
    		return x !== '\u200c' && x !== '\u200d';
    	}).length;
    }

    mw.util.addPortletLink('p-cactions', 'javascript:void(0);', 'پارامترهای ترازشده', 'us-majavah-align');
    $('#us-majavah-align').click(function () {
        const splitParam = string => {
            const split = string.split('=');
            if (split.length <= 2) {
                return split;
            }

            let first = split.shift();
            return [first, split.join('=')];
        };

        const splitIntoParams = string => {
            if (string.startsWith('{{') && string.endsWith('}}')) {
                if (!string.includes('|')) {
                    return [string];
                }

                const results = splitIntoParams(string.slice(2, -2));
                return ['{{' + results[0]].concat(splitIntoParams(string.slice(2, -2)).slice(1), ['}}']);
            }

            const params = [];
            let temp = '';
            let open = 0;

            for (let i = 0; i < string.length; i++) {
                const char = string[i];
                temp += char;

                if (char === '{' || char === '[') {
                    open += 1;
                } else if (char === '}' || char === ']') {
                    open -= 1;
                } else if (char === '|' && open === 0 && temp.trim() !== '|') {
                    params.push(temp.slice(0, -1).trim());
                    temp = '|';
                }
            }

            params.push(temp);

            return params;
        };

        const debug = string => {
            if (window.alignerDebug) {
                mw.notify(string);
            }
        };

        const useWikEd = window.wikEd && window.wikEd.useWikEd;

        if (useWikEd) {
            window.wikEd.UpdateTextarea();
        }

        const editBox = $('#wpTextbox1');

        if (!editBox) {
            mw.notify('جعبهٔ ویرایش یافت نشد');
            return;
        }

        const text = editBox.val();

        if (!text || text.length === 0) {
            mw.notify('محتوای جعبه ویرایش یافت نشد');
            return;
        }

        const searches = window.alignerDefaultSearches.concat(window.alignerExtraSearches || []);

        let count = 0;

        const processInfobox = template => {
            if (template === '') {
                mw.notify('جعبه اطلاعات یافت نشد');
                return;
            }

            if (open !== 0) {
                console.error({ open });
                mw.notify('الگو به‌درستی بسته نشده‌است');
                return;
            }

            let maxLength = 0;

            const origTemplate = String(template);
            const lines = template.split("\n");
            const newLines = [];

            for (let lineNumber in lines) {
                const paramsInLine = splitIntoParams(lines[lineNumber].trim());
                console.log({
                    line: lines[lineNumber].trim(),
                    paramsInLine,
                });

                for (let paramNumber in paramsInLine) {
                    let line = paramsInLine[paramNumber].trim();
                    if (!line.startsWith('|') || line.split('=').length !== 2) {
                        newLines.push(line);
                        continue;
                    }

                    let [firstPart, lastPart] = splitParam(line);
                    firstPart = firstPart.slice(1).trim();

                    if (getVisualLength(firstPart) > maxLength) {
                        maxLength = getVisualLength(firstPart);
                    }

                    newLines.push('| ' + firstPart + '=' + lastPart);
                }
            }

            let output = '';

            maxLength += 2; // to include '| '

            for (let lineNumber in newLines) {
                let line = newLines[lineNumber];
                const parts = splitParam(line);

                if (parts.length < 2) {
                    output += line += "\n";
                    continue;
                }

                let firstPart = parts[0].trim();
                console.log({ firstPart, maxLength });

                while (getVisualLength(firstPart) < maxLength) {
                    firstPart += ' ';
                }

                output += firstPart + ' = ' + parts[1].trim() + "\n";
            }

            if (output.endsWith("\n")) {
                output = output.slice(0, -1);
            }

            editBox.val(editBox.val().replace(origTemplate, output));

            if (useWikEd) {
                wikEd.UpdateFrame();
            }
        };

        let template = '';
        let open = 0;

        for (let i = 0; i < text.length; i++) {
            let foo = false;

            for (let searchIndex in searches) {
                const search = searches[searchIndex];
                const searchLength = search.length;

                if (text.length - i > searchLength) {
                    if ((text.slice(i, i + searchLength).toLowerCase() === search) || (text.slice(i, i + searchLength).toLowerCase() === search.replace(" ", "_"))) {
                        open += 1;
                        template += text[i];
                        foo = true;
                    }
                }
            }

            if (open >= 1 && !foo) {
                template += text[i];

                if (text[i] == '{') {
                    open += 1;
                } else if (text[i] == '}') {
                    open -= 1;

                    if (open === 0) {
                        count += 1;
                        processInfobox(template);
                        template = '';
                    }
                }
            }
        }

        mw.notify('تعداد ' + String(count.toLocaleString('fa')) + ' الگو با موفقیت تراز شد. لطفاً بررسی پیش‌نمایش پیش از ذخیره را فراموش نکنید و هرگونه مشکل در عملکرد ابزار را در [[بحث کاربر:Jeeputer]] گزارش کنید');
    });
});
// </nowiki>