Modul:TemplUtl: Unterschied zwischen den Versionen

6.772 Bytes hinzugefügt ,  3. Juli 2020
K
21 Versionen importiert: Doku-Vorlage
(2017-04-28)
K (21 Versionen importiert: Doku-Vorlage)
 
(15 dazwischenliegende Versionen von 5 Benutzern werden nicht angezeigt)
Zeile 1: Zeile 1:
local TemplUtl = { suite  = "TemplUtl",
local TemplUtl = { suite  = "TemplUtl",
                   serial = "2017-04-28" };
                   serial = "2019-11-20",
                  item  = 52364930 };
local Failsafe = TemplUtl;
 
 
 
local fallible = function ( adjust, ahead )
    -- Check for leading character disturbing syntax
    -- Precondition:
    --    adjust  -- string; trimmed wikitext
    --    ahead  -- true, if leading syntax shall start on new line
    -- Postcondition:
    --    Returns string, modified if necessary
    local r = adjust;
    local c = r:byte( 1, 1 );
    if c <= 59  and
      ( c==35 or c==42 or c==58 or c==59 ) then
        if ahead then
            r = "\n" .. r;
        else
            r = mw.text.nowiki( r:sub( 1, 1 ) )  ..  r:sub( 2 );
        end
    elseif ahead then
        local c2 = r:byte( 2, 1 );
        if ( c==123 and c2==124 )  or
          ( c==124 and c2==45 ) then
            r = "\n" .. r;
        end
    end
    return r;
end -- fallible()




Zeile 9: Zeile 39:
     --    accept  -- string; trimmed title
     --    accept  -- string; trimmed title
     -- Postcondition:
     -- Postcondition:
     --     Returns string with pattern
     --   Returns string with pattern
     local start = mw.ustring.sub( accept, 1, 1 );
     local start = mw.ustring.sub( accept, 1, 1 );
     local r;
     local r;
Zeile 39: Zeile 69:
         end
         end
     end
     end
    return TemplUtl.frame;
end -- framing()
end -- framing()


Zeile 54: Zeile 85:
         if r == ""  then
         if r == ""  then
             r = TemplUtl.faculty( another, nil );
             r = TemplUtl.faculty( another, nil );
         elseif r == "1" then
         elseif r:find( "1", 1, true )  and
              r:match( "^[0%-]*1[01%-]*$") then
             r = true;
             r = true;
         elseif r == "0" then
         elseif r:match( "^[0%-]+$") then
             r = false;
             r = false;
         else
         else
             r = r:lower();
             r = r:lower();
             if r == "y"  or r == "yes"  or  r == "true" then
             if r == "y"  or
              r == "yes"  or
              r == "true" or
              r == "on" then
                 r = true;
                 r = true;
             elseif r == "n"  or r == "no"  or  r == "false" then
             elseif r == "n"  or
                  r == "no"  or
                  r == "false" or
                  r == "off" then
                 r = false;
                 r = false;
             else
             else
                 if not TemplUtl.L10N then
                 if not TemplUtl.boolang then
                     local lucky;
                    -- TODO: page language
                     s = string.format( "Module:%s/local",
                     local l, d = pcall( mw.ext.data.get, "i18n/01.tab" );
                                      TemplUtl.suite );
                     if type( d ) == "table"  and
                     lucky, TemplUtl.L10N = pcall( mw.loadData, s );
                      type( d.data ) == "table" then
                        local f = function ( at )
                                  local e = d.data[ at ];
                                  l = e[ 1 ];
                                  s = e[ 2 ];
                                  if type( l ) == "boolean"  and
                                    type( s ) == "string" then
                                      s = mw.text.split( s, "|" );
                                      for i = 1, #s do
                                          TemplUtl.boolang[ s[ i ] ] = l;
                                      end -- for i
                                  end
                              end
                        TemplUtl.boolang = { };
                        f( 1 );
                        f( 2 );
                     else
                        TemplUtl.boolang = true;
                    end
                 end
                 end
                 if type( TemplUtl.L10N ) == "table" then
                 if type( TemplUtl.boolang ) == "table" then
                     local entry;
                     s = TemplUtl.boolang[ r ];
                     if not TemplUtl.lang then
                     if type( s ) == "boolean" then
                         -- TODO: page language
                         r = s;
                        TemplUtl.lang =
                              mw.language.getContentLanguage():getCode();
                     end
                     end
                    entry = TemplUtl.L10N[ TemplUtl.lang ];
                    if type( entry ) == "table" then
                        s = entry[ r ];
                        if type( s ) == "boolean" then
                            r = s;
                        end
                    end
                else
                    TemplUtl.L10N = true;
                 end
                 end
                 if type( r ) ~= "boolean" then
                 if type( r ) ~= "boolean" then
                     if type( another ) ~= "nil" then
                     s = type( another );
                    if s == "nil" then
                        r = true;
                    elseif s == "boolean" then
                        r = another;
                    else
                         r = TemplUtl.faculty( another );
                         r = TemplUtl.faculty( another );
                    else
                        r = true;
                     end
                     end
                 end
                 end
Zeile 119: Zeile 167:
                         :addClass( "error" )
                         :addClass( "error" )
                         :wikitext( alert );
                         :wikitext( alert );
     local live;
     local live = ( framing( frame ):preprocess( "{{REVISIONID}}" )
    framing( frame );
                  == "" );
    live = ( TemplUtl.frame:preprocess( "{{REVISIONID}}" ) == "" );
     if type( addClass ) == "string" then
     if type( addClass ) == "string" then
         err:addClass( addClass )
         err:addClass( addClass )
Zeile 141: Zeile 188:
         sign = string.format( "[[#%s|%s]]",  sign,  tostring( btn ) );
         sign = string.format( "[[#%s|%s]]",  sign,  tostring( btn ) );
         top:wikitext( sign, "&#160;", alert );
         top:wikitext( sign, "&#160;", alert );
         mw.addWarning( tostring( top ) );
         mw.addWarning( tostring( top:attr( "role", "alert" ) ) );
     elseif not always then
     elseif not always then
         err:css( { ["display"]    = "none" } );
         err:css( { ["display"]    = "none" } );
Zeile 152: Zeile 199:
     return tostring( err );
     return tostring( err );
end -- TemplUtl.failure()
end -- TemplUtl.failure()
TemplUtl.fake = function ( access )
    -- Simulation of template transclusion
    -- Precondition:
    --    access  -- string; page name (template)
    if type( access ) == "string" then
        local s = mw.text.trim( access );
        if s ~= "" then
            local t = mw.title.new( s, 10 );
            if not mw.title.equals( mw.title.getCurrentTitle(), t )  and
              t.exists then
                t:getContent();
            end
        end
    end
end -- TemplUtl.fake()
TemplUtl.fakes = function ( array, frame, ahead, answer )
    -- Simulation of template transclusions
    -- Precondition:
    --    array  -- table, with template title strings
    --    frame  -- object, or nil
    --    ahead  -- string, or nil, with common prefix
    --    answer  -- true, or nil, for list creation
    -- Postcondition:
    --    Returns string, if answer requested
    local e = framing( frame );
    local f = function ( a )
                  e:expandTemplate{ title = a };
              end
    local s = ahead or "";
    local r;
    for k, v in pairs( array ) do
        if type( k ) == "number" and
          type( v ) == "string" then
            v = s .. mw.text.trim( v );
            pcall( f, v );
            if answer then
                if r then
                    r = r .. "\n";
                else
                    r = "";
                end
                r = string.format( "%s* [[Template:%s|%s]]", r, v, v );
            end
        end
    end -- for k, v
    return r;
end -- TemplUtl.fakes()




Zeile 160: Zeile 260:
     --    address  -- string; what to inspect, URL presumed
     --    address  -- string; what to inspect, URL presumed
     -- Postcondition:
     -- Postcondition:
     --     Returns true, if URL beginning
     --   Returns true, if URL beginning
     local start, r = address:match( "^%s*((%a*:?)//)" );
     local start, r = address:match( "^%s*((%a*:?)//)" );
     if start then
     if start then
Zeile 190: Zeile 290:
     --    after  -- true, if only to search for "}}"
     --    after  -- true, if only to search for "}}"
     -- Postcondition:
     -- Postcondition:
     --     Returns
     --   Returns
     --          -- number; byte position in area
     --          -- number; byte position in area
     --            -- before "|" or "}}", may be at end
     --            -- before "|" or "}}", may be at end
Zeile 327: Zeile 427:
     --    at    -- optional number; byte position in area of "{{"
     --    at    -- optional number; byte position in area of "{{"
     -- Postcondition:
     -- Postcondition:
     --     Returns
     --   Returns
     --          -- table
     --          -- table
     --              [0]      -- template, page, parser function name
     --              [0]      -- template, page, parser function name
Zeile 394: Zeile 494:
     --    ask  -- string; parameter name
     --    ask  -- string; parameter name
     -- Postcondition:
     -- Postcondition:
     --     Returns string with trimmed parameter value, or nil
     --   Returns string with trimmed parameter value, or nil
     --    Does not return value if template inside
     --    Does not return value if template inside
     local r;
     local r;
Zeile 425: Zeile 525:
     --                                no colon (:)
     --                                no colon (:)
     -- Postcondition:
     -- Postcondition:
     --     Returns ustring position of "{{" in area, or false
     --   Returns ustring position of "{{" in area, or false
     -- Requires:
     -- Requires:
     --    fiatTitleRegExp()
     --    fiatTitleRegExp()
Zeile 491: Zeile 591:
--      3 4 5 6
--      3 4 5 6
--        more like 2
--        more like 2
TemplUtl.firstbreak = function ( adjust )
    -- Precede leading character with newline if specific syntax
    -- Precondition:
    --    adjust  -- string; trimmed wikitext
    -- Postcondition:
    --    Returns string, modified if necessary
    return fallible( adjust, true );
end -- TemplUtl.firstbreak()




Zeile 497: Zeile 608:
     -- Remove syntax elements that hide effective syntax only
     -- Remove syntax elements that hide effective syntax only
     -- Precondition:
     -- Precondition:
     --    area  -- string; wikitext to be reduced
     --    area  -- string; unparsed wikitext to be reduced
     -- Postcondition:
     -- Postcondition:
     --     Returns cleared wikitext
     --   Returns cleared wikitext
     local delimiters = { { "<%s*NOWIKI%s*>", "<%s*/%s*NOWIKI%s*>" },
     local delimiters = { { "<%s*NOWIKI%s*>", "<%s*/%s*NOWIKI%s*>" },
                         { "<!--", "-->", true },
                         { "<!--", "-->", true },
Zeile 563: Zeile 674:
     return r;
     return r;
end -- TemplUtl.flat()
end -- TemplUtl.flat()
TemplUtl.nowiki1 = function ( adjust )
    -- HTML-escape leading character if disturbing syntax
    -- Precondition:
    --    adjust  -- string; trimmed wikitext
    -- Postcondition:
    --    Returns string, modified if necessary
    return fallible( adjust, false );
end -- TemplUtl.nowiki1()
Failsafe.failsafe = function ( atleast )
    -- Retrieve versioning and check for compliance
    -- Precondition:
    --    atleast  -- string, with required version or "wikidata" or "~"
    --                or false
    -- Postcondition:
    --    Returns  string  -- with queried version, also if problem
    --              false  -- if appropriate
    local last  = ( atleast == "~" )
    local since = atleast
    local r
    if last  or  since == "wikidata" then
        local item = Failsafe.item
        since = false
        if type( item ) == "number"  and  item > 0 then
            local entity = mw.wikibase.getEntity( string.format( "Q%d",
                                                                item ) )
            if type( entity ) == "table" then
                local seek = Failsafe.serialProperty or "P348"
                local vsn  = entity:formatPropertyValues( seek )
                if type( vsn ) == "table"  and
                  type( vsn.value ) == "string"  and
                  vsn.value ~= "" then
                    if last  and  vsn.value == Failsafe.serial then
                        r = false
                    else
                        r = vsn.value
                    end
                end
            end
        end
    end
    if type( r ) == "nil" then
        if not since  or  since <= Failsafe.serial then
            r = Failsafe.serial
        else
            r = false
        end
    end
    return r
end -- Failsafe.failsafe()




Zeile 573: Zeile 739:
                             frame.args[ 2 ] )  and  "1"
                             frame.args[ 2 ] )  and  "1"
           or  "";
           or  "";
end -- p.faculty()
end -- p.faculty


function p.failure( frame )
function p.failure( frame )
Zeile 586: Zeile 752:
     end
     end
     return TemplUtl.failure( scream, loud, select, frame );
     return TemplUtl.failure( scream, loud, select, frame );
end -- p.failure()
end -- p.failure
 
function p.fake( frame )
    TemplUtl.fake( frame.args[ 1 ]  or  "",  frame );
    return "";
end -- p.fake
 
function p.fakes( frame )
    local list = ( frame.args.list == "1" );
    local r    = TemplUtl.fakes( frame.args,
                                frame,
                                frame.args.prefix,
                                list );
    return r or "";
end -- p.fakes
 
function p.firstbreak( frame )
    local r = ( frame.args[ 1 ] );
    if r then
        r = mw.text.trim( r );
        if r ~= "" then
            r = TemplUtl.firstbreak( r );
        end
    end
    return r or "";
end -- p.firstbreak


function p.failsafe()
function p.from( frame )
     return TemplUtl.serial;
    local r = frame:getParent():getTitle();
end -- p.failsafe()
    if r then
        r = string.format( "&#123;&#123;%s&#125;&#125;", r );
    end
    return r or "";
end -- p.from
 
function p.isRedirect()
     return mw.title.getCurrentTitle().isRedirect and "1"  or  "";
end -- p.isRedirect
 
function p.nowiki1( frame )
    local r = ( frame.args[ 1 ] );
    if r then
        r = mw.text.trim( r );
        if r ~= "" then
            r = TemplUtl.nowiki1( r );
        end
    end
    return r or "";
end -- p.nowiki1
 
p.failsafe = function ( frame )
    -- Versioning interface
    local s = type( frame )
    local since
    if s == "table" then
        since = frame.args[ 1 ]
    elseif s == "string" then
        since = frame
    end
    if since then
        since = mw.text.trim( since )
        if since == "" then
            since = false
        end
    end
    return Failsafe.failsafe( since )  or  ""
end -- p.failsafe


p.TemplUtl = function ()
p.TemplUtl = function ()