Jump to content

User:Æk/advisor functions.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Æk (talk | contribs) at 09:39, 27 December 2009 (argh). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
//<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);
        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);
    }
    return suggestions || [];
}

function aekAdvisorExtraBreaks2(s) {
    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'
    }];

    var suggestions = res.map(function(p) {
        aekSuggestionsFromRe(p.re, p.rep, s, skel);
    });

    var sug2 = [];

    for (s in suggestions) {
        if(s) sug2.concat(s);
    }

    return suggestions;
}
ct.rules.push(aekAdvisorExtraBreaks2);