Revision 137481 of "User:PerfektesChaos/js/WikiSyntaxTextMod/dM.js" on testwiki

/// PerfektesChaos/js/WikisyntaxTextMod/?M.js
/// 2012-06-11 [email protected]
/// Fingerprint: #0#0#
/// <nowiki>
//  WikiSyntaxTextMod: Main functions, not initially loaded
/* jshint  curly:true, latedef:true, laxbreak:true,
           trailing:true, undef:true, white:false                      */
/* global  jQuery: true, mw: true, mediaWiki: false                    */
/*jslint   browser: true, plusplus: true, regexp: true, sloppy: true,
           unparam: true, vars: true, white: true, maxerr: 50          */
/*globals  jQuery: true, mw: true, mediaWiki: false                    */



if (typeof(mediaWiki) !== "object") {   // disconnected
   mw  =  { config: false,
            libs:   { WikiSyntaxTextMod:  {debugging: false}
                    },
            log:    function () {}
          };
}
if (typeof(mw.libs.WikiSyntaxTextMod) !== "object") {   // isolated
   mw.libs.WikiSyntaxTextMod  =  {debugging: false};
}
if (typeof(mw.libs.WikiSyntaxTextMod.main) !== "object") {
   mw.libs.WikiSyntaxTextMod.main  =  { };
}
mw.libs.WikiSyntaxTextMod.main.vsn  =  -4.57;
if (typeof(mw.libs.WikiSyntaxTextMod.bb) !== "object") {
   mw.libs.WikiSyntaxTextMod.bb  =  { };
}



/*
Uses:
   .g.s.re
   .hooks.fire()
   .l10n.lang.find()
   .str.***()
   .l10n.text.fetch()
   .util.isArray()
   .util.isbn.factory()
   .w.link.replace.factory()
   mw.log()
 */



//-----------------------------------------------------------------------



mw.libs.WikiSyntaxTextMod.bb.utilM  =  function (WSTM) {
   // Building block and run environment support
   // 2012-05-18 [email protected]
   if (typeof(WSTM.util) !== "object") {
      WSTM.util  =  { };
   }


   if (typeof(WSTM.util.fiatObjects) !== "function") {
      WSTM.util.fiatObjects  =  function (adult, activate, assign) {
         // Ensure existence of at least empty object
         // Precondition:
         //    adult     -- parent object
         //    activate  -- String with name of child object
         //    assign    -- optional object with initial definition
         //                 if containing object components,
         //                 they will be asserted as well
         // Postcondition:
         //    adult has been extended
         // Uses:
         //    .util.fiatObjects()  -- recursive
         // 2012-05-18 [email protected]
         var elt;
         var obj;
         var s;
         if (typeof( adult[activate] )  !==  "object") {
            adult[activate]  =  (assign  ?  assign  :  { } );
         }
         if (assign) {
            obj  =  adult[activate];
            for (s in assign) {
               if (assign.hasOwnProperty(s)) {
                  elt  =  assign[s];
                  if (typeof(elt)  ===  "object") {
                     WSTM.util.fiatObjects(obj, s, elt);
                  }
               }
            }  //  for s in obj
         }
      };   // .util.fiatObjects()
   }


   WSTM.util.fiatObjects(WSTM,  "debugging",  { loud: false });


};   // .bb.utilM()
mw.libs.WikiSyntaxTextMod.bb.utilM(mw.libs.WikiSyntaxTextMod);
delete mw.libs.WikiSyntaxTextMod.bb.utilM;



//-----------------------------------------------------------------------



mw.libs.WikiSyntaxTextMod.bb.apiM  =  function (WSTM) {
   // Top level functions (exposed)
   // Uses:
   //    .util.fiatObjects()
   // 2012-05-18 [email protected]
   WSTM.util.fiatObjects(WSTM,  "api",  { });



   WSTM.api.edit  =  function (arglist) {
      // Execute syntax and user defined replacement in user context
      // Precondition:
      //    arglist  -- object   .wikitext  string
      //                         .summary   (optional) string
      // Postcondition:
      //    Returns  false   if .wikitext remains unchanged
      //             object  .wikitext  string modified, or error found
      //                     .diffpage  true if required
      //                     .summary   false or non-empty string
      //                     .minor     if minor edits only (not userdef)
      // Uses:
      //    >  .errors.collection
      //    >  .main.less
      //    >  .config.mod
      //    >  .config.mod.summary
      //    >  .config.diffPage
      //    >  WikisyntaxTextMod_Diffpage
      //    >< .mod.lazy
      //    .api.textMod()
      //    .str.trimR()
      //    .str.trimL()
      //    .str.trim()
      // 2012-04-07 [email protected]
      var got    =  this.textMod(arglist.wikitext);
      var r      =  false;
      var seed;
      var sum;
      if (! got  &&  WSTM.errors.collection) {
         got  =  [ arglist.wikitext, false ];
      }
      if (got) {
         seed  =  false;
         sum   =  "";
         r  =  { wikitext: got[0],
                 minor:    (got[1] && WSTM.mod.lazy),
                 summary:  false,
                 diffpage: true};
         if (typeof(WSTM.main.less) === "boolean") {
            r.minor  =  r.minor && WSTM.main.less;
         }
         if (typeof(arglist.summary) === "string") {
            sum  =  WSTM.str.trimR(arglist.summary, true);
         }
         if (WSTM.config  &&  typeof(WSTM.config) === "object") {
            if (WSTM.config.mod  &&
                typeof(WSTM.config.mod) === "object") {
               if (typeof(WSTM.config.mod.summary) === "string") {
                  seed  =  WSTM.config.mod.summary;
               }
            }
         }
         if (typeof(seed) === "string") {
            if (sum.indexOf(seed) < 0) {
               sum  =  sum + " " + WSTM.str.trimL(seed, true);
            }
         }
         sum  =  WSTM.str.trim(sum, true);
         if (sum.length > 0) {
            r.summary  =  sum;
         }
         if (r.minor) {
            if (typeof(WikisyntaxTextMod_DiffPage) === "boolean") {
               r.diffpage  =  WikisyntaxTextMod_DiffPage;
            } else if (typeof(WikisyntaxTextMod_Diffpage)==="boolean") {
               r.diffpage  =  WikisyntaxTextMod_Diffpage;
            } else if (WSTM.config  &&
                       typeof(WSTM.config) === "object") {
               if (typeof(WSTM.config.diffPage) === "boolean") {
                  r.diffpage  =  WSTM.config.diffPage;
               }
            }
         }
      }   // any change
      return r;
   };   // .api.edit()



   WSTM.api.setContext  =  function (article, aNS, aDB) {
      // Define context for non-interactive article modifications
      // Precondition:
      //    article  -- string: wgTitle
      //    aNS      -- number: wgNamespaceNumber
      //    aDB      -- string: wgDBname
      //    If parameter omitted or false, that context is kept unchanged
      // Postcondition:
      //    Context is furnished
      // Uses:
      //     < .g.wTitle
      //     < .g.wNsNumber
      //     < .g.wDBname
      //    .g.factoryProject()
      // 2011-12-23 [email protected]
      if (typeof(article) === "string") {
         WSTM.g.wTitle  =  article;
      }
      if (typeof(aNS) === "number") {
         WSTM.g.wNsNumber  =  aNS;
      }
      if (typeof(aDB) === "string") {
         WSTM.g.wDBname  =  aDB;
         WSTM.g.factoryProject();
      }
   };   // .api.setContext()



};   // .bb.apiM()
mw.libs.WikiSyntaxTextMod.bb.apiM(mw.libs.WikiSyntaxTextMod);
delete mw.libs.WikiSyntaxTextMod.bb.apiM;



//-----------------------------------------------------------------------



mw.libs.WikiSyntaxTextMod.bb.errorsM  =  function (WSTM) {
   // Detected syntax error handling
   // Uses:
   //    .util.fiatObjects()
   // 2012-05-18 [email protected]
   WSTM.util.fiatObjects(WSTM,  "errors",  { });



   WSTM.errors.fill  =  function () {
      // Provide list of syntax error messages to insert into wiki text
      // Postcondition:
      //    Set .errors.story to string with entire text, or false
      // Uses:
      //    >  .config.errorlimit
      //    >  .errors.collection
      //     < .errors.story
      //    .l10n.text.fetch()
      //    .str.escapeLight()
      //    .errors.frozen()
      // 2012-03-07 [email protected]
      var e;
      var i;
      var l;
      var m;
      var n;
      var s;
      this.story  =  false;
      if (typeof(this.collection) === "object") {
         if (typeof(WSTM.config.errorlimit) === "number") {
            if (WSTM.config.errorlimit > 0) {
               m  =  WSTM.config.errorlimit;
            } else {
               m  =  0;
            }
         } else {
            m  =  7;
         }
         if (m) {
            s  =  "";
            n  =  this.collection.length;
            n  =  (n > m  ?  m  :  n);
            l  =  true;
            for (i = 0;  i < n;  i++) {
               e  =  this.collection[i];
               if (e[1]) {
                  if (l) {
                     s  =  WSTM.l10n.text.fetch( "_ErrorBeginAuto"
                                                +  (m===1 ? "1" : "N") )
                           + s;
                  }
                  s  =  s + "\n* " + WSTM.l10n.text.fetch(e[0]);
                  if (e[2]) {
                     s  =  s + ": " + WSTM.str.escapeLight(e[2]);
                  }
                  l  =  false;
               }
            }   // for i
            l  =  true;
            for (i = 0;  i < n;  i++) {
               e  =  this.collection[i];
               if ( ! e[1]) {
                  if (l) {
                     s  =  WSTM.l10n.text.fetch( "_ErrorBeginMan"
                                                +  (m===1 ? "1" : "N") )
                           + s;
                  }
                  s  =  s + "\n* " + WSTM.l10n.text.fetch(e[0]);
                  if (e[2]) {
                     s  =  s + ": " + WSTM.str.escapeLight(e[2]);
                  }
                  l  =  false;
               }
            }   // for i
            s  =  s.replace(/-->/g, "--&gt;");
            if (n > this.maximum) {
               s  =  s + "\n" + WSTM.l10n.text.fetch("_ErrorLimitMore");
            }
            this.story  =  this.frozen() + "\n" + s + "\n-->\n";
         }
      }
   };   // .errors.fill()



   WSTM.errors.format  =  function () {
      // Show list of syntax error messages on HTML page below headline
      // Precondition:
      //    Wikiserver HTML page in interactive mode
      //    .errors.story is defined; containing:  * plain text messages
      // Postcondition:
      //    Message list is removed from wiki text
      //    Message area is inserted into page
      // Uses:
      //    >  .errors.story
      //    >  mw.util.$content
      //    .l10n.text.fetch()
      //    jQuery()
      // 2012-05-03 [email protected]
      var re;
      var s;
      if (typeof(this.story) === "string") {
         s   =  this.story.replace(/</g, "&lt;").replace(/>/g, "&gt;");
         //                        " ' \
         re  =  new RegExp("\n\\* ", "g");
         s   =  s.replace(re, "</li>\n<li>");
         s   =  "\n" + s + "\n";
         re  =  /(\n[^<]?.*)<\/li>\n<li>/g;
         s   =  s.replace(re, "$1\n<ul class='error-detail'>\n<li>");
         re  =  /(\n<li>.+[^>])\n/g;
         s   =  s.replace(re, "$1</li>\n</ul>\n");
         s   =  "<p class='error-explanation'><em>"
                + WSTM.l10n.text.fetch( "_ErrorBegin" +
                           (this.story.search(/\n\*.+\n?.*\n\*/) === -1
                                               ?  "1"
                                               :  "N") )
                + "</em></p>"
                + s
                + "\n<p class='error-explanation'>"
                + WSTM.l10n.text.fetch("_ErrorEpilog")
                + "<br /><em>-- WikiSyntaxTextMod</em> "
                + "<small>(PerfektesChaos)</small></p>";
         s   =  "\n<div class='error' "
                           + "style='border: solid 3px; padding: 3px'>\n"
                + s + "\n</div>\n";
         mw.util.$content.before(jQuery(s));
      }
   };   // .errors.format()



   WSTM.errors.found  =  function (alert, auto, add) {
      // Manage detected error
      // Precondition:
      //    alert  -- number/identifier of error message
      //    auto   -- true: automatic attempt
      //    add    -- additional information as found, or false
      //              plain wikitext; will be HTML-escaped
      // Uses:
      //    >  .debugging
      //    >< .errors.collection
      //    mw.log()
      // 2012-03-25 [email protected]
      var err  =  [ alert, auto, add ];
      if (this.collection) {
         this.collection.push(err);
      } else {
         this.collection   =  [ err ];
      }
      if (WSTM.debugging) {
         mw.log(WSTM.debugging, ".errors.found()", 1, err);
      }
   };   // .errors.found()



};   // .bb.errorsM()
mw.libs.WikiSyntaxTextMod.bb.errorsM(mw.libs.WikiSyntaxTextMod);
delete mw.libs.WikiSyntaxTextMod.bb.errorsM;



//-----------------------------------------------------------------------



mw.libs.WikiSyntaxTextMod.bb.gM  =  function (WSTM) {
   // Variables for global usage in WSTM
   // Uses:
   //    .util.fiatObjects()
   // 2012-05-18 [email protected]
   WSTM.util.fiatObjects(WSTM,  "g",
                         { learnt: false,
                           re:     { },
                           s:      {  re: { }  }
                         } );



   WSTM.g.factory  =  function () {
      // Initialize global variables, e.g. build compiled RegExp
      // Precondition:
      //    None
      // Postcondition:
      //    set of dynamic global variables defined.
      // Uses:
      //    >< .g.learnt
      //    .g.factoryMediaWiki()
      //    .g.factoryRegExp()
      //    .g.factoryTextMod()
      // 2012-03-25 [email protected]
      if ( ! this.learnt) {
         this.factoryMediaWiki();
         this.factoryRegExp();
         this.factoryTextMod();
         this.learnt  =  true;
      }
   };   // .g.factory



   WSTM.g.factoryMediaWiki  =  function () {
      // Initialize global variables of mediaWiki context
      // Precondition:
      //    None
      // Postcondition:
      //    Set of dynamic global variables defined.
      // Uses:
      //     < .g.wDBname
      //     < .g.wNsNumber
      //     < .g.wTitle
      //    .g.fetch()
      //    .g.factoryProject()
      // 2012-03-23 [email protected]
      this.fetch(this.wDBname,   "wgDBname");
      this.fetch(this.wNsNumber, "wgNamespaceNumber");
      this.fetch(this.wTitle,    "wgTitle");
      if (typeof(this.wDBname) !== "string") {
         this.wDBname  =  "";
      }
      if (typeof(this.wNsNumber) !== "number") {
         this.wNsNumber  =  0;
      }
      if (typeof(this.wTitle) !== "string") {
         this.wTitle  =  "";
      }
      this.factoryProject();
   };   // .g.factoryMediaWiki()



   WSTM.g.factoryProject  =  function () {
      // Define global project context
      // Precondition:
      //    DB name (wgDBname) is defined
      // Postcondition:
      //    project context variables defined.
      // Uses:
      //    >  .g.wDBname
      //     < .g.projLang
      //     < .g.projType
      //     < .g.projSpace
      //    .hooks.fire()
      // 2011-12-23 [email protected]
      var got;
      var re;
      var sub;
      if (typeof(this.wDBname) === "string") {
         re   =  "^([a-z]+)"
                        + "(_[a-z_]+)?"
                + "(wik(i"
                      + "(books|news|quote|source|versity)?"
                    + "|tionary))$";
         re   =  new RegExp(re, "");
         got  =  re.exec(this.wDBname);
         if (got !== null) {
            this.projLang  =  got[1];
            this.projType  =  got[3];
            if (this.projType === "wiki") {
               this.projType   =  "wikipedia";
            }
            if (this.projLang.length > 3) {
               switch (this.projLang) {
                  case "simple":
                     break;
                  case "commons":
                  case "mediawiki":
                  case "meta":
                     this.projType  =  this.projLang;
                     this.projLang  =  "en";
                     break;
               }
            } else {
               sub  =  got[2];
               if (typeof(sub) === "string") {
                  if (sub !== "") {   //   "bat_smgwiki"   "be_x_oldwiki"
                     this.projLang  =  this.projLang + "-" + sub;
                  }
               }
            }
            this.projSpace     =  this.projType;
         }
         WSTM.hooks.fire("*");
      }
      // [[meta:Special:SiteMatrix]]
   };   // .g.factoryProject()



   WSTM.g.factoryRegExp  =  function () {
      // Initialize global variables, e.g. build general RegExp
      // Postcondition:
      //    set of dynamic global variables defined.
      // Uses:
      //    >  .str.spaces
      //     < .g.s.re.spaces
      //     < .g.s.re.yyyy
      // 2012-04-24 [email protected]
      this.s.re.spaces  =  "[" + WSTM.str.spaces + "]";
      this.s.re.yyyy    =  "(1[0-9][0-9][0-9]|20[0-9][0-9])";
      /*
      this.s.re.spacesEnt  =  this.s.re.spaces +
                           "|(&" +
                              "(" +
                               "thinsp|ensp|emsp" +
                               "|(#" +
                                  "(0*" +
                                     "(160" +
                                     "|8194|8195|8196|8197|8198|8199" +
                                     "|8200|8201|8202|8203" +
                                     "|8239)" +
                                   "|x0*" +
                                     "(A0|a0*" +
                                     "|2002|2003|2004|2005|2006|2007" +
                                     "|2008|2009|200A|200a|200B|200b" +
                                     "|202F|202f)" +
                                   ")" +
                                 ")" +
                                ");" +
                              ")";
      */
   };   // .g.factoryRegExp()



   WSTM.g.factoryTextMod  =  function () {
      // Initialize global variables, e.g. build wiki RegExp
      // Precondition:
      //    None
      // Postcondition:
      //    Set of dynamic global variables defined.
      // Uses:
      //    >  .g.projLang
      //    >  .g.s.re.*
      //     < .g.trslKw.*
      //     < .g.re.*
      //     < .str.sort_lang
      //    .l10n.word.fetch()
      //    .util.isbn.factory()
      // 2012-03-23 [email protected]
      var s;
      var tupel;
      this.trslKw         =  new Array(4);
      tupel               =  new Array(2);
      s                   =  WSTM.l10n.word.fetch("Category", 2);
      tupel[0]            =  "|" + s + "|";
      tupel[1]            =  WSTM.l10n.word.fetch("Category", 0);
      this.trslKw[1]      =  tupel;   // Category
      tupel               =  new Array(2);
      s                   =  WSTM.l10n.word.fetch("Special", 2);
      tupel[0]            =  "|" + s + "|";
      tupel[1]            =  WSTM.l10n.word.fetch("Special", 0);
      this.trslKw[2]      =  tupel;   // Special
      tupel               =  new Array(2);
      s                   =  WSTM.l10n.word.fetch("Page", 3);
      tupel[0]            =  "|" + s + "|";
      tupel[1]            =  WSTM.l10n.word.fetch("Page", 0);
      this.trslKw[3]      =  tupel;   // Page
      tupel               =  new Array(2);
      s                   =  WSTM.l10n.word.fetch("File", 1)  +  "|"  +
                             WSTM.l10n.word.fetch("Image", 1);
      tupel[0]            =  "|" + s + "|";
      tupel[1]            =  WSTM.l10n.word.fetch("File", 0);
      this.trslKw[0]      =  tupel;   // File
      s                   =  "( *)(" + s + "):";
      this.re.File        =  new RegExp(s, "i");
      this.re.iwikiMap    =  false;
      this.re.ISBN        =  new RegExp(WSTM.util.isbn.factory(),  "i");
      s                   =  "^ *(" + this.s.re.Lang + ") *$";
      this.re.Lang        =  new RegExp(s, "i");   // deprecated
      WSTM.str.sort_lang  =  this.projLang;
   };   // .g.factoryTextMod()



   WSTM.g.factoryURL  =  function (ancient) {
      // Initialize global variables for WMF URL
      // Precondition:
      //    ancient  -- true:  secure.wikimedia.org
      //                false: unified http or https since fall 2011
      // Uses:
      //    >  .g.s.re.ProtocolSecure
      //    >  .g.s.re.ProtocolRelative
      //    >  .g.s.re.Lang
      //     < .g.re.wikiSECURE
      //     < .g.re.wikiDOMAIN
      // 2012-03-24 [email protected]
      var reProj  =  "(commons"
                     + this.s.re.ProtocolSecure
                     + "meta"
                     + this.s.re.ProtocolRelative
                     + this.s.re.Lang
                     + ")";
      var reSist  =  "wik"
                     +  "(i"
                     +    "(books|media|news|pedia|quote|source|versity)"
                     +  "|tionary)";
      var reSite  =  "(" + reSist + "|mediawiki)";
      var source;
      if (ancient) {
         source              =  "^" + reSite + "/" + reProj + "/";
         this.re.wikiSECURE  =  new RegExp(source, "");
      } else {
         source              =  "^" + reProj + "\\." + reSite + "\\.org";
         this.re.wikiDOMAIN  =  new RegExp(source, "");
      }
   };   // .g.factoryURL



   WSTM.g.foundNamespace  =  function (ask) {
      // Is this a namespace name?
      // Precondition:
      //    ask  -- identifier (trimmed)
      // Postcondition:
      //    Returns true if recognized to be no template
      // Uses:
      //    >< .g.wNsNames
      //    .str.trim()
      //    .g.fetch()
      // mw.config.get()
      // Requires: JavaScript 1.3   charCodeAt()
      // 2012-03-04 [email protected]
      var r  =  false;
      var s  =  WSTM.str.trim(ask.toLowerCase(),  true);
      this.fetch(this.wNsNames, "wgNamespaceIds");
      if (typeof(this.wNsNames) !== "object") {// 2012-02 temp... 2012-06
         this.wNsNames  =  mw.config.get("wgNamespaceIds");
      }   // 2012-02 temporary
      r  =  (typeof(this.wNsNames[s]) === "number");
      return  r;
   };   // .g.foundNamespace()



};   // .bb.gM()
mw.libs.WikiSyntaxTextMod.bb.gM(mw.libs.WikiSyntaxTextMod);
delete mw.libs.WikiSyntaxTextMod.bb.gM;



//-----------------------------------------------------------------------



mw.libs.WikiSyntaxTextMod.bb.l10nM  =  function (WSTM) {
   // Invariant localization functions
   // Uses:
   //    .util.fiatObjects()
   // 2012-05-18 [email protected]
   WSTM.util.fiatObjects(WSTM,  "l10n",
                         { lang:      { },
                           proj:      { },
                           s:         { },
                           templates: { },
                           text:      { },
                           word:      { trslFilePar: false }
                         } );



   WSTM.l10n.fetchRegExp  =  function (access) {   //.template.isOther ??
      // Initialize a global RegExp
      // Precondition:
      //    access  -- kind of RegExp
      //               "Variables"
      // Uses:
      //    >  .g.projLang
      //    >  .g.s.re.Variables.*
      //    >  .l10n.s.re.Variables.*
      //    >< .g.re.Variables
      // 2012-02-04 [email protected]
      var s;
      if (typeof(WSTM.g.re[access]) !== "object") {
         s  =  WSTM.g.s.re[access]["$"];
         if (typeof(this.s.re[access][WSTM.g.projLang]) === "string") {
            s  =  s + "|" + this.s.re[access][WSTM.g.projLang];
         }
         WSTM.g.re[access]  =  new RegExp("^(" + s + ")$",
                                          WSTM.g.s.re[access]["_"]);
      }
   };   // .l10n.fetchRegExp()



WSTM.l10n.lang.failsafe  =  function (adjust) {   // "O" deprecated
   // Check presumed language code, e.g. ISO 639-2 three-letter-code
   // Precondition:
   //    adjust  -- presumed language code (trimmed)
   // Postcondition:
   //    Returns  downcased ISO 639 language code,
   //             or false if not identified
   // Uses:
   //    >  .l10n.lang.s.re.c3
   // 2012-04-30 [email protected]
   var r  =  false;
   var s;
   if (adjust.length === 3) {   // ISO 639-2
      s  =  adjust.toLowerCase();
      if (this.s.re.c3.indexOf(s) >= 0) {
         r  =  s;
      }
   }
   return  r;
};   // .l10n.lang.failsafe()



   WSTM.l10n.lang.flop  =  function (adjust) {
      // Analyze whether prefix is not a wikiproject language code
      // Precondition:
      //    adjust    -- heading characters before ':' in a wikilink
      // Postcondition:
      //    Returns false   iff language in any wikiproject
      //            String  iff heading part of page name
      //    RegExp was used.
      // Uses:
      //    >  .g.s.re.lang
      //    >  .l10n.lang.s.re.c3
      //    >< .g.re.lang
      //    .str.trimL()
      //    .util.isO_639_1()
      // 2012-06-07 [email protected]
      var got;
      var k;
      var r;
      var s     =  WSTM.str.trimL(adjust, false);
      if (! WSTM.g.re.lang) {
         if (! WSTM.g.s.re.lang) {
            WSTM.g.s.re.lang  =  WSTM.g.s.re.Lang;   // "L" deprecated
         }
         WSTM.g.re.lang  =  "^(" + WSTM.g.s.re.lang + ") *$";
         WSTM.g.re.lang  =  new RegExp(WSTM.g.re.lang, "i");
      }
      got  =  WSTM.g.re.lang.exec(s);
      if (got) {
         r    =  s;
         got  =  got[1];
         s    =  got.toLowerCase();
         k    =  got.length;
         if (k === 2) {
            if (s === "wp") {
               r  =  "WP";
            } else if (WSTM.util.isO_639_1(s)) {
               r  =  false;
            }
         } else if (k === 3) {
            if (s === "doi") {
               r  =  s;
            } else if (this.s.re.c3.indexOf(s) >= 0) {
               r  =  false;
            }
         } else if (s === "simple") {
   //    } else if (slang === "tokipona") {
         } else if (k > 5) {
            k  =  s.indexOf("-");
            if (k === 2  ||  k === 3) {
               /*  |bat-smg|be-x-old|cbk-zam|fiu-vro|map-bms|nds-nl
                   |roa-rup|roa-tara|ru-sib
                   |zh-classical|zh-min-nan|zh-yue|  */
               if (/^[a-z][a-z][a-z]?-[a-z][a-z][-a-z]*/.test(got)) {
                  r  =  false;
               }
            }
         }
      } else {
         r  =  s;
      }   // matching lang?
      return r;
   };   // .l10n.lang.flop()



   WSTM.l10n.lang.fragment  =  function (adjust) {
      // Remove RFC 1766 subtag from language code
      // Precondition:
      //    adjust  -- language code (trimmed)
      // Postcondition:
      //    Returns downcased ISO 639 language code
      // Requires: JavaScript 1.3   charCodeAt()
      // 2011-12-25 [email protected]
      var r  =  adjust.toLowerCase();
      if (r.length > 4) {
         if (r.charCodeAt(2) === 45) {   // '-'
            r  =  r.substr(0, 2);
         }
      }
      return  r;
   };   // .l10n.lang.fragment()



   WSTM.l10n.text.fetch  =  function (access) {
      // Retrieve localized text string
      // Precondition:
      //    access  -- identifier
      // Postcondition:
      //    Returns  string with most appropriate message
      // Uses:
      //    >  .l10n.text.trsl
      //    >< .g.wUserLang
      //    >< .g.userLang
      //    .g.fetch()
      //    .l10n.lang.find()
      // 2011-12-25 [email protected]
      var e  =  this.trsl[ access ];
      var r  =  false;
      if (typeof(e) === "object") {
         if (typeof(WSTM.g.userLang) !== "string") {
            WSTM.g.fetch(WSTM.g.wUserLang, "wgUserLanguage");
            WSTM.g.userLang  =  WSTM.l10n.lang.find(WSTM.g.wUserLang);
         }
         r  =  e[ WSTM.g.userLang ];
         if (typeof(r) !== "string") {
            r  =  e.en;
         }
      } else {
         r  =  "***" + access + "***";
      }
      return r;
   };   // .l10n.text.fetch()



   WSTM.l10n.word.fetch  =  function (ask, append) {
      // Define keywords in local languages and project
      // Precondition:
      //    ask     -- keyword in English
      //               Category File Image REDIRECT Template DEFAULTSORT
      //               or "image_" for parameter map
      //    append  -- 0  for current project and language
      //                  single localized major translation, keep case
      //               1  sequence of current project/language | English
      //               2  sequence of keyword in all languages requested
      // Postcondition:
      //    Return     0  keyword in major translation, case kept
      //                  parameter map for "image_"
      //              >0  sequence of keywords in append languages
      //                  lowercase and separated by '|'
      // Uses:
      //    >  .l10n.word.trsl
      //    >  .g.projLang
      //    >  .g.wDBname
      // 2012-03-22 [email protected]
      var d;
      var q;
      var s;
      var trsl  =  WSTM.l10n.word.trsl[ ask ];
      var r     =  false;
      if (typeof(trsl) === "object") {   // keyword found
         if (append < 2) {
            d  =  (ask === "image_"  ?  "object"  :  "string");
            q  =  trsl[ WSTM.g.wDBname ];
            if (typeof(q) !== d) {
               q  =  trsl[ WSTM.g.projLang ];
            }
            if (typeof(q) === d) {   // found
               if (append) {   // current project/language | English
                  r  =  ask.toLowerCase();
                  s  =  q.toLowerCase();
                  if (s !== r) {
                     r  =  s + "|" + r;
                  }
               } else {   // current project and language (localized)
                  r  =  q;
               }
            } else {   // project / language unknown, return itself
               if (d === "string") {   // return itself
                  r  =  ask;
               } else {   // return  image_  en default
                  r  =  trsl.$;
               }
            }
         } else {   // sequence in all languages requested
            r  =  "|" + ask.toLowerCase() + "|";
            for (q in trsl) {
               s  =  trsl[ q ];
               if (typeof(s) === "string") {   // cave prototype
                  s  =  s.toLowerCase();
                  if (r.indexOf("|" + s + "|")  <  0) {
                     r  =  r + s + "|";
                  }
               }
            }   // for q in trsl
            r  =  r.substr(1,  r.length - 2);
         }
      }
      return  r;
   };   // .l10n.word.fetch()



};   // .bb.l10nM()
mw.libs.WikiSyntaxTextMod.bb.l10nM(mw.libs.WikiSyntaxTextMod);
delete mw.libs.WikiSyntaxTextMod.bb.l10nM;



//-----------------------------------------------------------------------



mw.libs.WikiSyntaxTextMod.bb.mainM  =  function (WSTM) {
   // Top level functions (internal)
   // 2012-05-26 [email protected]
   WSTM.main.maxMsg  =  5;



WSTM.main.fault  =  function (alert, about) {
   // Submit error message caused by user input
   // Precondition:
   //    alert  -- error message
   //    about  -- user context, or false to suppress
   // Uses:
   //    >  window
   //    >< .main.maxMsg
   //    .l10n.text.fetch()
   // 2012-05-26 [email protected]
   var s;
   if (window) {
      s  =  (about  ?  " " + about  :  "");
      if (typeof(window.console) === "object") {
         window.console.info("PerfektesChaos::WikiSyntaxTextMod" + s
                             + "\n" + alert);
      }
      if (WSTM.main.maxMsg) {
         s  =  window.confirm(WSTM.l10n.text.fetch("ERROR") + ":\r\n"
                              + alert + "\r\n"
                              + "PerfektesChaos/js/WikiSyntaxTextMod"
                              + s);
         if (s) {
            WSTM.main.maxMsg--;
         } else {
            WSTM.main.maxMsg  =  0;
         }
      }
   }
};   // .main.fault()



WSTM.main.full  =  function () {
   // Execute syntax and user defined replacement connected to HTML page
   // Precondition:
   //    Wikiserver environment for editpage with "editform"
   // Postcondition:
   //    wpTextbox1 value of "editform" is modified, if appropriate.
   //    If modified, DiffView is to be displayed to the user
   //                 (if not suppressed by WikisyntaxTextMod_DiffPage).
   // Remark: Used as event handler -- 'this' is not WSTM.main
   // Uses:
   //    >  .ia.$editform
   //    >  .errors.story
   //    >  .debugging.live
   //    .main.textarea()
   //    .api.edit()
   //    .errors.fill()
   //    "editform"
   //        < .wpSummary
   //       >  .wpDiff
   // 2012-03-23 [email protected]
   var button;
   var perform;
   var s;
   var stuff;
   var task;
   var $field;
   stuff  =  WSTM.main.textarea(false, false);
   if (typeof(stuff) === "string") {
      task     =  { wikitext: stuff,
                    summary:  false };
      $field   =  WSTM.ia.$editform.find("#wpSummary");
      if ($field.length) {
         task.summary  =  $field.val();
      } else {
         $field  =  false;
      }
      perform  =  WSTM.api.edit(task);
      if (perform) {
         s  =  perform.wikitext;
         WSTM.errors.fill();
         if (WSTM.errors.story) {
            s  =  WSTM.errors.story + s;
         }
         if (perform.diffpage) {
            if (typeof(WSTM.debugging) === "object") {
               if (typeof(WSTM.debugging.live) === "boolean") {
                  perform.diffpage  =  ! WSTM.debugging.live;
                  if (WSTM.debugging.live) {
                     mw.log(WSTM.debugging, ".main.full() diffpage", 1);
                  }
               }
            }
         }
         WSTM.main.textarea(s, perform.diffpage);
         if (perform.summary && $field) {
            $field.val(perform.summary);
         }
         if (perform.diffpage) {
            /*
            var $button  =  WSTM.ia.$editform.find("#wpDiff");
            if ($button.length) {
               window.onbeforeunload  =  null;   // stop editwarner
               $button.click();
            }
            */
            // DOM, not hooked by editwarner:
            button  =  document.getElementById("wpDiff");
            if (button !== null) {
               button.click();
            }
         }   // human stupidity vs. artificial intelligence
      }   // any change
   }   // Textarea
};   // .main.full()



};   // .bb.mainM()
mw.libs.WikiSyntaxTextMod.bb.mainM(mw.libs.WikiSyntaxTextMod);
delete mw.libs.WikiSyntaxTextMod.bb.mainM;



//-----------------------------------------------------------------------



mw.libs.WikiSyntaxTextMod.bb.mod  =  function (WSTM) {
   // User modification request
   // 2012-04-07 [email protected]
   if (typeof(WSTM.mod) !== "object") {
      WSTM.mod  =  { "*":
                     { comment:  false,   // <!--  -->
                       file:     false,   // [[file:]] or gallery or .ext
                       hyper:    false,   // config.mod.link
                       lazy:     true,    // no visible change
                       lenient:  false,   // config.mod.tested
                       lock:     false,   // text replacement expected
                       luxury:   false,   // any no-comment replacement
                       plain:    false,   // any text outside protected
                       tag:      false,   // tag context
                       text:     false,   // no <poem> nor tag nor quote
                       template: false,   // {{any or [[template:any]]
                       url:      false,   // //x.y
                       wiki:     false    // [[any]]
                     },
                     visible: false   // Deprecated
                     };
   }



WSTM.mod.furnish  =  function () {
   // (Re-)initialize user modification request
   // Postcondition:
   // Uses:
   //    >  Modif_Comment
   //    >  Modif_Link
   //    >  Modif_Text
   //    >  .config.mod
   //    >  .config.mod.*
   //    >< .mod.*
   //     < .mod.luxury
   //     < .mod.lock
   //    .util.isArray()
   //    .w.link.replace.factory()
   //    .w.template.mod.factory()
   // 2012-06-11 [email protected]
   var base        =  this["*"];
   var hyperlinks  =  false;
   var p;
   for (p in base) {
      if (base.hasOwnProperty(p)) {
         this[p]  =  base[p];
      }
   }   // p in WSTM.mod.*
   if (typeof(Modif_Comment) === "object") {   // Benutzer:Chemiewikibm
      if (WSTM.util.isArray(Modif_Comment)) {
         this.comment  =  { name: "Modif_Comment",
                            raw:  Modif_Comment };
      }
   }
   if (typeof(Modif_Text) === "object") {
      if (WSTM.util.isArray(Modif_Text)) {
         this.plain  =  { name: "Modif_Text",
                          raw:  Modif_Text };
      }
   }
   if (typeof(Modif_Link) === "object") {
      if (WSTM.util.isArray(Modif_Link)) {
         hyperlinks  =  { name: "Modif_Link",
                          raw:  Modif_Link };
      }
   }
   if (WSTM.config  &&  typeof(WSTM.config) === "object") {
      if (WSTM.config.mod  &&  typeof(WSTM.config.mod) === "object") {
         if (typeof(WSTM.config.mod.tested) === "boolean") {
            this.lenient  =  WSTM.config.mod.tested;
         }
         if (WSTM.util.isArray(WSTM.config.mod.plain)) {
            this.plain  =  { name: ".config.mod.plain",
                             raw:  WSTM.config.mod.plain };
         }
         if (WSTM.util.isArray(WSTM.config.mod.link)) {
            hyperlinks  =  { name: ".config.mod.link",
                             raw:  WSTM.config.mod.link };
         }
         if (WSTM.util.isArray(WSTM.config.mod.comment)) {
            this.comment  =  { name: ".config.mod.comment",
                               raw:  WSTM.config.mod.comment };
         }
         if (WSTM.config.mod.template) {
if (WSTM.w.template.mod) {   // WSTM.4
            this.template  =
                   WSTM.w.template.mod.factory(WSTM.config.mod.template);
}
         }
      }
   }
   if (hyperlinks) {
      this.hyper  =  WSTM.w.link.replace.factory(hyperlinks);
   }
   if (this.plain) {
      this.lock  =  true;
   }
   this.luxury  =  (this.plain || this.hyper || this.template);
   this.lock    =  this.luxury;
};   // .mod.furnish()



};   // .bb.mod()
mw.libs.WikiSyntaxTextMod.bb.mod(mw.libs.WikiSyntaxTextMod);
delete mw.libs.WikiSyntaxTextMod.bb.mod;



// Start on import: callback to waiting ...
if (typeof(mw.libs.WikiSyntaxTextMod.main.wait) === "function") {
   mw.libs.WikiSyntaxTextMod.main.wait("M",
                                       mw.libs.WikiSyntaxTextMod.main.vsn);
}
delete mw.libs.WikiSyntaxTextMod.main.vsn;



// Emacs
// Local Variables:
// encoding: iso-8859-1-dos
// fill-column: 80
// End:

/// EOF </nowiki>   WikiSyntaxTextMod/?M.js