User:Æk/advisor functions.js
Appearance
< User:Æk
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. This code will be executed when previewing this page. |
![]() | Documentation for this user script can be added at User:Æk/advisor functions. |
//<source lang=javascript>
/* ported from [[Wikipedia:AutoEd/extrabreaks.js]] */
function aekAdvisorExtraBreaks(s) {
var suggestions = [];
var match;
var make_suggestion = function (m) {
suggestions.push({
start: m.index,
end: m.index + m[0].length,
replacement: m[1] + m[2],
name: "extra-br",
description: "Remove unneeded br tags"
});
};
var regexes = [
// templates (}}), template parameters (|)
/[\t ]*<[\s\/\.]*br[\s\/\.]*>[\t ]*([\t\n ]*?)(\]\]|}}|\|)/gim,
// BR tag followed by at least two newlines
/[\t ]*<[\s\/\.]*br[\s\/\.]*>[\t ]*([\n])[\t ]*([\n])/gim
];
regexes.map(function (r) {
while(match = r.exec(s)) {
make_suggestion(match);
}
});
var make_suggestion1 = function (m) {
suggestions.push({
start: m.index,
end: m.index + m[0].length,
replacement: m[1],
name: "extra-br",
description: "Remove unneeded br tags"
});
}
while(match = /[\t ]*<[\s\/\.]*br[\s\/\.]*>[\t ]*([\s]*?[\n]\*)/gim.exec(s)) {
make_suggestion1(match);
}
return suggestions;
}
// ct.rules.push(aekAdvisorExtraBreaks);
/* General purpose function to convert regexp to suggestions */
function aekSuggestionsFromRe(regex, repl, str, skel) {
var match, sug;
var suggestions = [];
while (match = regex.exec(str)) {
var substr = str.substring(match.index, match.index + match[0].length - 1);
var r = substr.replace(regex, repl);
sug = skel;
sug.start = match.index;
sug.end = match.index + match[0].length;
sug.replacement = r;
suggestions.push(sug);
// HACK: regex.exec should get subsequent matches automatically, but it doesn't (?)
str = str.substring(match.index + match[0].length, str.length - 1);
}
return suggestions;
}
function aekAdvisorExtraBreaks2(s) {
var suggestions = [];
var skel = {
name: "extra-br",
description: "Remove unneeded br tags"
}
var res = [{
re: /[\t ]*<[\s\/\.]*br[\s\/\.]*>[\t ]*([\t\n ]*?)(\]\]|}}|\|)/gim,
rep: '$1$2'
},
{
re: /[\t ]*<[\s\/\.]*br[\s\/\.]*>[\t ]*([\s]*?[\n]\*)/gim,
rep: '$1'
},
{
re: /[\t ]*<[\s\/\.]*br[\s\/\.]*>[\t ]*([\n])[\t ]*([\n])/gim,
rep: '$1$2'
}];
res.map(function(p) {
suggestions.concat(aekSuggestionsFromRe(p.re, p.rep, s, skel));
});
return suggestions;
}
ct.rules.push(aekAdvisorExtraBreaks2);