MediaWiki:Common.js/OverhaulSources.js

Définition, traduction, prononciation, anagramme et synonyme sur le dictionnaire libre Wiktionnaire.

Remarque: après avoir sauvegardé, vous devez vider le cache de votre navigateur pour que les changements prennent effet. Mozilla, cliquez sur Actualiser (ou ctrl-r). Internet Explorer / Opera: ctrl-f5. Safari: cmd-r. Konqueror ctrl-r.

// <nowiki>
(function () {
  function replaceAll(matches, text, replacementFunction) {
    Array.from(matches).forEach(function (match) {
      text = text.replace(match[0], replacementFunction(match));
    });

    return text;
  }

  function escapeHtml(s) {
    return s.replace(/&/g, "&amp;").replace(/"/g, "&quot;")
        .replace(/'/g, "&apos;").replace(/</g, "&lt;");
  }

  /**
   * Parse les sources JS ou Lua pour ajouter des liens vers les URL ou des pages wiki.
   */
  function parseScript() {
    var e = $(this);
    var s = e.text();

    if (!s.match(/{\d+}/)) {
      var uri = new mw.Uri(window.location.href);
      var url = "{0}://{1}/wiki/".format(uri.protocol, uri.host);
      // Escape HTML tags
      var text = escapeHtml(s);
      var match;

      // Namespaced pages
      if (match = text.match(/^(&apos;|&quot;)([^\s\[{(%?^\-+*$/.|:<>&]+?:[^\/].*?)\1$/)) {
        var page = match[2];
        var quotes = match[1];
        // noinspection HtmlUnknownTarget
        text = '{0}<a style="text-decoration:underline;" href="{1}{2}">{2}</a>{0}'
            .format(quotes, url, page);
        e.html(text.replace(match[0], text));
      }
      else {
        var matches;

        // URLs
        if (matches = text.matchAll(/((?:https?:)?\/\/[^\s]+?\.\w+\/[^\s]+?)(?=\s|&apos;|&quot;|$)/g)) {
          text = replaceAll(matches, text, function (match) {
            var page = match[1];
            // noinspection HtmlUnknownTarget
            return '<a class="external" style="text-decoration:underline;" href="{0}" target="_blank">{0}</a>'
                .format(page);
          });
        }
        // Links
        if (matches = text.matchAll(/\[\[(.+?)(?=\||]])/g)) {
          text = replaceAll(matches, text, function (match) {
            var page = match[1].trim();
            // noinspection HtmlUnknownTarget
            return '[[<a style="text-decoration:underline;" href="{0}{1}">{1}</a>'
                .format(url, page);
          });
        }
        // Invoked modules
        if (matches = text.matchAll(/{{#invoke:([^{#]+?)(?=\||}})/g)) {
          text = replaceAll(matches, text, function (match) {
            var modulePage = match[1].trim();
            // noinspection HtmlUnknownTarget
            return '{{#invoke:<a style="text-decoration:underline;" href="{0}Module:{1}">{1}</a>'
                .format(url, modulePage);
          });
        }
        // Templates
        if (matches = text.matchAll(/{{([^{#]+?)(?=\||}})/g)) {
          text = replaceAll(matches, text, function (match) {
            var templatePage = match[1].trim();
            // noinspection HtmlUnknownTarget
            return '{{<a style="text-decoration:underline;" href="{0}Modèle:{1}">{1}</a>'
                .format(url, templatePage);
          });
        }
        // Lua doc @param tags
        if (matches = text.matchAll(/@param(\s+)((?:\w+|\.\.\.)+?)(\s+)((?:table|string|boolean|number|function|any|file|thread|void|nil|\||,)+?)(?=\s|$)/g)) {
          text = replaceAll(matches, text, function (match) {
            var paramName = match[1];
            var ws1 = match[2];
            var paramType = match[3];
            var ws2 = match[4];
            return '<span class="ct">@param</span>{0}<span class="cpn">{1}</span>{2}<span class="cpt">{3}</span>'
                .format(paramName, ws1, paramType, ws2);
          });
        }
        // Lua doc @return tag
        if (matches = text.matchAll(/@(returns?)(\s+)((?:table|string|boolean|number|function|any|file|thread|void|nil|\||,)+?)(?=\s|$)/g)) {
          text = replaceAll(matches, text, function (match) {
            var tag = match[1];
            var ws = match[2];
            var type = match[3];
            return '<span class="ct">@{0}</span>{1}<span class="cpt">{2}</span>'
                .format(tag, ws, type);
          });
        }
        // Doc comments
        if (match = text.match(/^(\s*)---(.*)$/)) {
          var ws = match[1];
          var comment = match[2];
          text = '{0}<span class="cd">---{1}</span>'.format(ws, comment);
        }

        if (matches || match) {
          e.html(text);
        }
      }
    }
  }

  $(".mw-highlight .s1").each(parseScript);
  $(".mw-highlight .s2").each(parseScript);
  $(".mw-highlight .sb").each(parseScript);
  $(".mw-highlight .sx").each(parseScript);
  $(".mw-highlight .c1").each(parseScript);
  $(".mw-highlight .cm").each(parseScript);
})();
// </nowiki>