User:PerfektesChaos/js/superscriptMagnifier/d.js

From Wikipedia, the free encyclopedia
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.
/// User:PerfektesChaos/js/superscriptMagnifier/d.js
/// 2018-08-24 PerfektesChaos@de.wikipedia
// Magnify superscript (and subscript) characters in HTML document
// ResourceLoader:  compatible;  dependencies: user
/// Fingerprint: #0#0#
/// @license GPL [//www.mediawiki.org/w/COPYING] (+GFDL, LGPL, CC-BY-SA)
/// <nowiki>
/* global window:false                                                 */
/* jshint bitwise:true, curly:true, eqeqeq:true, latedef:true,
          laxbreak:true,
          nocomma:true, strict:true, undef:true, unused:true           */



( function ( mw, $ ) {
   "use strict";
   var Version  =  -1.1,
       SSM      =  "superscriptMagnifier",
       Config   =  { dewiki: [     5551,   // Hilfe:Sonderzeichen
                                1014413,   // Hilfe:Sonderzeichenreferenz
                                  34226,   // WP:SVZ
                                9135891 ], // tool manual
                     enwiki: [ 48934384 ]  // tool manual

                   },
       SuMag;
   if ( typeof mw.libs[ SSM ]  !==  "object"   ||   ! mw.libs[ SSM ] ) {
      mw.libs[ SSM ]  =  { };
   }
   mw.libs[ SSM ].type  =  SSM;
   SSM                  =  mw.libs[ SSM ];
   if ( typeof SSM.vsn  ===  "undefined" ) {
      SSM.vsn        =  Version;
      SSM.signature  =  "ext.gadget." + SSM.type;
      SSM.support    =  "User:PerfektesChaos/js/" + SSM.type;
      SSM.doc        =  "[[w:en:" + SSM.support + "]]";
   }



   /*
    * This program is free software; you can redistribute it and/or
    * modify it under the terms of the GNU General Public License as
    * published by the Free Software Foundation; either version 2 of the
    * License, or (at your option) any later version.
    *
    * This program is distributed in the hope that it will be useful,
    * but WITHOUT ANY WARRANTY; without even the implied warranty of
    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    * GNU General Public License for more details.
    *
    * You should have received a copy of the GNU General Public License
    * along with this program;
    * if not, write to the Free Software Foundation, Inc.,
    * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
    * http://www.gnu.org/copyleft/gpl.html
    */



   SuMag  =  ( function ( $ ) {
      // Independent subsystem
      //    SuMag.factory()
      //    SuMag.fire()
      //    SuMag.found()
      // Precondition:
      //    $  -- jQuery library object
      // 2015-12-27 PerfektesChaos@de.wikipedia
      var config  =  { large: false,
                       latin: true,
                       max:   false,
                       sign:  false },
          offer   =  { },
          transl  =  {    178: "2",
                          179: "3",
                          185: "1",
                       0x0656: 0x0627,   // SUB ARABIC ALEF
                       0x1D62: "i",      // SUB LATIN SMALL I
                       0x1D63: "r",      // SUB LATIN SMALL R
                       0x1D64: "u",      // SUB LATIN SMALL U
                       0x1D65: "v",      // SUB LATIN SMALL V
                       0x1D66: 0x03B2,   // SUB GREEK SMALL BETA
                       0x1D67: 0x03B3,   // SUB GREEK SMALL GAMMA
                       0x1D68: 0x03C1,   // SUB GREEK SMALL RHO
                       0x1D69: 0x03C6,   // SUB GREEK SMALL PHI
                       0x1D6A: 0x03C7,   // SUB GREEK SMALL CHI
                       0x2070: "0",      // SUPER ZERO
                       0x2071: "i",      // SUPER LATIN SMALL I
                       0x2074: "4",      // SUPER FOUR
                       0x2075: "5",      // SUPER FIVE
                       0x2076: "6",      // SUPER SIX
                       0x2077: "7",      // SUPER SEVEN
                       0x2078: "8",      // SUPER EIGHT
                       0x2079: "9",      // SUPER NINE
                       0x207A: "+",      // SUPER PLUS SIGN
                       0x207B: 0x2212,   // SUPER MINUS
                       0x207C: "=",      // SUPER EQUALS SIGN
                       0x207D: "(",      // SUPER LEFT PARENTHESIS
                       0x207E: ")",      // SUPER RIGHT PARENTHESIS
                       0x207F: "n",      // SUPER LATIN SMALL N
                       0x2080: "0",      // SUB ZERO
                       0x2081: "1",      // SUB ONE
                       0x2082: "2",      // SUB TWO
                       0x2083: "3",      // SUB THREE
                       0x2084: "4",      // SUB FOUR
                       0x2085: "5",      // SUB FIVE
                       0x2086: "6",      // SUB SIX
                       0x2087: "7",      // SUB SEVEN
                       0x2088: "8",      // SUB EIGHT
                       0x2089: "9",      // SUB NINE
                       0x208A: "+",      // SUB PLUS SIGN
                       0x208B: 0x2212,   // SUB MINUS
                       0x208C: "=",      // SUB EQUALS SIGN
                       0x208D: "(",      // SUB LEFT PARENTHESIS
                       0x208E: ")",      // SUB RIGHT PARENTHESIS
                       0x2090: "a",      // SUB LATIN SMALL A
                       0x2091: "e",      // SUB LATIN SMALL E
                       0x2092: "o",      // SUB LATIN SMALL O
                       0x2093: "x",      // SUB LATIN SMALL X
                       0x2094: 0x0259,   // SUB LATIN SMALL SCHWA
                       0x2095: "h",      // SUB LATIN SMALL H
                       0x2096: "k",      // SUB LATIN SMALL K
                       0x2097: "l",      // SUB LATIN SMALL L
                       0x2098: "m",      // SUB LATIN SMALL M
                       0x2099: "n",      // SUB LATIN SMALL N
                       0x209A: "p",      // SUB LATIN SMALL P
                       0x209B: "s",      // SUB LATIN SMALL S
                       0x209C: "t",      // SUB LATIN SMALL T
                       0x2C7C: "j" },    // SUB LATIN SMALL J
          needles, needlesX, reGet, reHas;


      function facet( apply ) {
         // Process text node contents
         // Precondition:
         //    apply  -- string with one character
         // Postcondition:
         //    Returns jQuery element set
         // Uses:
         //    >  config.latin
         //    >  transl
         //    >  config.sign
         //    >  config.scream
         //    >< needles
         //    >< needlesX
         //    facet()
         // 2015-12-27 PerfektesChaos@de.wikipedia
         var k      =  apply.charCodeAt( 0 ),
             light  =  ( k < 256 ),
             low    =  false,
             s, $r;
         if ( light ) {
            needles++;
         } else {
            needlesX++;
            low  =  ( ( k >= 0x1D62  &&  k <= 0x1D65 )   ||
                      ( k >= 0x2080  &&  k <= 0x209C ) );
            if ( ! config.latin ) {
               low  =  low   ||
                       ( k >= 0x1D66  &&  k <= 0x03C7 );
            }
         }
         $r  =  $( low ? "<sub>" : "<sup>" );
         s   =  transl[ k ];
         if ( typeof s  ===  "number" ) {
            s  =  String.fromCharCode( s );
         }
         $r.text( s );
         if ( config.sign ) {
            $r.addClass( config.sign );
            if ( ! light ) {
               $r.addClass( config.sign + "-ucs" );
               if ( config.scream ) {
                  $r.addClass( config.scream );
               }
            }
         }
         return  $r;
      }   // facet()


      function fair( adjust ) {
         // Process text node contents
         // Precondition:
         //    adjust  -- string to be converted
         // Postcondition:
         //    Returns jQuery element set
         // Uses:
         //    >  reGet
         //    facet()
         // 2015-12-27 PerfektesChaos@de.wikipedia
         var s   =  adjust,
             $r  =  $( "<span>" ),
             got;
         while ( s ) {
            got  =  reGet.exec( s );
            if ( got ) {
               if ( got[ 1 ] ) {
                  $r.append( $( "<span>" ).text( got[ 1 ] ) );
               }
               $r.append( facet( got[ 2 ] ) );
               s  =  got[ 3 ];
            } else {
               $r.append( $( "<span>" ).text( s ) );
               s  =  false;
            }
         }   // while
         return  $r;
      }   // fair()


      function feed ( $assembly ) {
         // Process one node
         // Precondition:
         //    $assembly  -- jQuery element set to be manipulated
         // Postcondition:
         //    Executed
         // Uses:
         //    >  config.max
         //    >  reHas
         //    feed()  -- recursive
         //    fair()
         // 2015-12-27 PerfektesChaos@de.wikipedia
         var skip  =  " code form pre ",
             s     =  $assembly.text(),
             i, el, $cont;
         if ( config.max  &&  s.length > config.max ) {
            s  =  "";
         }
         if ( reHas.test( s ) ) {
            $cont = $assembly.contents();
            for ( i = $cont.length - 1;  i >= 0;  i-- ) {
               el  =  $cont.get( i );
               switch ( el.nodeType ) {
                  case 1:   // element
                     s  =  " " + el.nodeName.toLowerCase() + " ";
                     if ( skip.indexOf( s ) < 0 ) {
                        feed( $cont.eq( i ) );
                     }
                     break;
                  case 3:   // text node
                     s  =  el.nodeValue;
                     if ( reHas.test( s ) ) {
                        el.nodeValue  =  "";
                        $cont.eq( i ).after( fair( s ) );
                     }
                     break;
               }   // switch el.nodeType
            }   // for i--
         }
      }   // feed()


      offer.factory  =  function ( assign, apply ) {
         // Initialize SuMag subsystem
         // Precondition:
         //    assign  -- keyword, or boolean
         //               false:  complete setup
         //    apply   -- value
         // Uses:
         //    >< config.large
         //    >< config.latin
         //     < config.max
         //     < config.sign
         //     < config.scream
         // 2015-12-27 PerfektesChaos@de.wikipedia
         var s;
         if ( typeof assign  ===  "string" ) {
            config[ assign ]  =  apply;
            if ( ( assign ===  "latin"  &&  ! apply )   ||
                 ( assign ===  "scream"  &&  apply ) ) {
               config.large  =  true;
            }
         } else {
            s  =  String.fromCharCode( 178, 179, 185 );
            if ( config.large ) {
               s  =  s  +  String.fromCharCode( 0x1D62, 45, 0x1D65,
                                                0x2070, 0x2071,
                                                0x2074, 45, 0x208E,
                                                0x2090, 45, 0x209C );
               if ( ! config.latin ) {
                  s  =  s  +  String.fromCharCode( 0x0656,
                                                   0x1D66, 45, 0x1D6A );
                  // +  various
               }
            }
            reGet  =  new RegExp( "^([^" + s + "]*)([" + s + "])(.*)$" );
            reHas  =  new RegExp( "[" + s + "]" );
         }
      };   // offer.factory()


      offer.fire  =  function ( $assembly ) {
         // Perform SuMag
         // Precondition:
         //    $assembly  -- jQuery element set to be manipulated
         // Postcondition:
         //    Executed
         // Uses:
         //     < needles
         //     < needlesX
         //    feed()
         // 2015-12-27 PerfektesChaos@de.wikipedia
         needles  = 0;
         needlesX = 0;
         feed( $assembly );
      };   // offer.fire()


      offer.found  =  function ( alter ) {
         // Retrieve number of replacements
         // Precondition:
         //    alter  -- true: large value;  else: ANSI value
         // Postcondition:
         //    Returns number
         // Uses:
         //    >  needles
         //    >  needlesX
         // 2015-12-27 PerfektesChaos@de.wikipedia
         return  ( alter ? needlesX : needles );
      };    // offer.found()
      return  offer;
   } )( $ );   // SuMag



   function fiat( $assembly ) {
      // Perform replacement
      // Precondition:
      //    $assembly  -- jQuery element of content area
      // Uses:
      //    >  .locator
      //    >  .type
      //    >  .support
      //    SuMag.fire()
      //    SuMag.found()
      // 2017-01-01 PerfektesChaos@de.wikipedia
      var nA, nU, show, sign, $a, $div, $span;
      if ( ! $( "#wikiDiff" ).length ) {
         SuMag.fire( $assembly );
         if ( SSM.locator ) {
            sign  =  SSM.type + "-info";
            $( "#" + sign ).remove();    // How comes?
            nA  =  SuMag.found();
            nU  =  SuMag.found( true );
            if ( nA + nU ) {
               show  =  nA + "";
               if ( nU ) {
                  show  =  show + " / " + nU;
               }
               $a  =  $( "<a>" );
               $a.attr( { "href": "//en.wikipedia.org/wiki/"
                                  + SSM.support } )
                 .text( SSM.support );
               $span  =  $( "<span>" );
               $span.css( { "margin-left": "2em" } )
                    .text( show );
               $div  =  $( "<div>" );
               $div.append( $a, $span )
                   .attr( { "class": SSM.type,
                            "id":    sign } )
                    .css( { "border":        "#808080 1px solid",
                           "margin-bottom": "1em",
                           "margin-top":    "1em",
                           "padding":       "0.8em" } );
               $assembly.append( $div );
            }
         }
      }
   }   // fiat()



   function fill() {
      // User options arrived
      // Uses:
      //    >  Config
      //    >  .except
      //    >  .lapsus
      //    >  .large
      //    >  .latin
      //    >  .type
      //    >  .max
      //    >< .locator
      //    SuMag.factory()
      //    mw.hook()
      //    (fiat)
      // 2015-12-27 PerfektesChaos@de.wikipedia
      var exceptions;
      if ( typeof Config[ SSM.wgDBname ]  ===  "object" ) {
         exceptions  =  Config[ SSM.wgDBname ];
      } else {
         exceptions  =  [ ];
      }
      if ( typeof SSM.except  ===  "object"
           &&     SSM.except   &&
           typeof SSM.except.length  ===  "number" ) {
         exceptions.concat( SSM.except );
      }
      if ( $.inArray( SSM.wgArticleId, exceptions )  <  0 ) {
         if ( typeof SSM.lapsus  ===  "boolean" ) {
            SuMag.factory( "scream", "error" );
            SSM.locator  =  true;
         }
         if ( typeof SSM.large  ===  "boolean" ) {
            SuMag.factory( "large", SSM.large );
         }
         if ( typeof SSM.latin  ===  "boolean" ) {
            SuMag.factory( "latin", SSM.latin );
         }
         if ( typeof SSM.locator  ===  "boolean"
              &&     SSM.locator ) {
            SuMag.factory( "sign", SSM.type );
         }
         if ( typeof SSM.max  ===  "number"
              &&     SSM.max > 0 ) {
            SuMag.factory( "max", SSM.max );
         }
         SuMag.factory();
         mw.hook( "wikipage.content" ).add( fiat );
      }
   }   // fill()



   function fire() {
      // Start possible actions on current page
      // Uses:
      //    >  .signature
      //    >  .type
      //     < .wgDBname
      //    mw.loader.getState()
      //    mw.loader.state()
      //    mw.config.get()
      //    mw.loader.using()
      //    mw.hook()
      //    (fill)
      // 2018-08-24 PerfektesChaos@de.wikipedia
      var env, rls;
      if ( mw.loader.getState( SSM.signature ) !== "ready" ) {
         rls  =  { };
         rls[ SSM.signature ]  =  "ready";
         mw.loader.state( rls );
         env  =  mw.config.get( [ "wgAction",
                                  "wgArticleId",
                                  "wgDBname",
                                  "wgNamespaceNumber" ] );
         if ( env.wgNamespaceNumber >= 0   &&
              "|edit|submit|view|".indexOf( env.wgAction )  >  0 ) {
            SSM.wgArticleId  =  env.wgArticleId;
            SSM.wgDBname     =  env.wgDBname;
            mw.loader.using( [ "user" ],
                             fill );
         }
         mw.hook( SSM.type + ".ready" ).fire();
      }
   }   // fire()



   fire();
}( window.mediaWiki, window.jQuery ) );



// Emacs
// Local Variables:
// coding: iso-8859-1-dos
// End:

/// EOF </nowiki>   superscriptMagnifier/d.js