Modul:Vorlage:Anker: Unterschied zwischen den Versionen

3.122 Bytes hinzugefügt ,  3. Juli 2020
K
13 Versionen importiert: Doku-Vorlage
(2019-12-28)
K (13 Versionen importiert: Doku-Vorlage)
 
(5 dazwischenliegende Versionen von einem anderen Benutzer werden nicht angezeigt)
Zeile 1: Zeile 1:
local Anchor = { suite  = "AnchorTemplate",
local Anchors = { suite  = "Anchors",
                serial  = "2019-12-28",
                  serial  = "2020-04-10",
                item    = 79414611,
                  item    = 79414611,
                globals = { TemplUtl = 52364930 }
                  globals = { Multilingual = 47541920,
              }
                              TemplUtl     = 52364930 }
                }
--[=[
--[=[
Template:Anchor  {{Anker}}
Support Template:Anchor  {{Anker}}
]=]
]=]
local Failsafe   = Anchor
local Failsafe = Anchors
local GlobalMod = Anchor
local GlobalMod = Anchors


if mw.site.server:find( ".beta.wmflabs.org", 4, true ) then
    require( "Module:No Globals" )
end




local Config = {
local Config = {
     badPattern    = "[%%#'\"%[%]<>|]",
     badPattern    = "[#'\"%[%]<>|]",
     globalPage    = { "bodyContent",
     globalPage    = { "antispam-container",
                      "bodyContent",
                       "catlinks",
                       "catlinks",
                       "centralNotice",
                       "centralNotice",
                       "content",
                       "content",
                       "contentSub",
                       "contentSub",
                      "contentSub2",
                       "editform",
                       "editform",
                       "editpage-copywarn",
                       "editpage-copywarn",
Zeile 30: Zeile 28:
                       "fundraising",
                       "fundraising",
                       "jump-to-nav",
                       "jump-to-nav",
                      "language-settings-dialog",
                      "languagesettings-panels",
                       "page-actions",
                       "page-actions",
                       "siteNotice",
                       "siteNotice",
                       "siteSub",
                       "siteSub",
                       "toc",
                       "toc",
                       "top" },
                      "toctitle",
                       "top",
                      "wikiPreview" },
     globalPatterns = { "^mw%-[%l%-]+$",
     globalPatterns = { "^mw%-[%l%-]+$",
                       "^ca%-[%l%-]+$",
                       "^ca%-[%l%-]+$",
                       "^n%-[%l%-]+$",
                       "^n%-[%l%-]+$",
                       "^pt?%-[%l%-]+$",
                      "^p%-%a%a[%a%-]+$",
                       "^pt%-[%l%-]+$",
                       "^wp%u" },
                       "^wp%u" },
     errCat        = false,
     errCat        = false,
Zeile 66: Zeile 69:
     -- Postcondition:
     -- Postcondition:
     --    Returns whatever, probably table
     --    Returns whatever, probably table
     -- 2019-10-29
     -- 2020-01-01
     local storage = access
     local storage = access
     local finer = function ()
     local finer = function ()
Zeile 101: Zeile 104:
         end
         end
         if not lucky and alert then
         if not lucky and alert then
             error( "Missing or invalid page: " .. storage, 0 )
             error( "Missing or invalid page: " .. storage )
         end
         end
     end
     end
Zeile 113: Zeile 116:
     --    apply  -- string, with message key
     --    apply  -- string, with message key
     -- Returns message text; at least English
     -- Returns message text; at least English
    -- TODO: Might be extended by tabData and Multilingual
     local entry = Config[ apply ]
     local entry = Config[ apply ]
     local r
     local r
Zeile 136: Zeile 140:
     --    adjust  -- string or nil
     --    adjust  -- string or nil
     -- Returns boolean
     -- Returns boolean
     local r = false
     local r
     if adjust then
     if type( adjust ) == "string" then
         r = mw.text.trim( adjust )
         r = mw.text.trim( adjust )
         if r ~= ""  and  r ~= "0" then
         if r ~= ""  and  r ~= "0" then
             r = true
             r = true
         end
         end
    else
        r = adjust or false
     end
     end
     return r
     return r
Zeile 150: Zeile 156:
local function fair( assembly )
local function fair( assembly )
     -- Create HTML code for anchors
     -- Create HTML code for anchors
     --    assembly  -- sequence table, with identtifiers
     --    assembly  -- sequence table, with identifiers
     -- Returns HTML string
     -- Returns HTML string
     local collection = mw.html.create( "" )
     local collection = mw.html.create( "" )
Zeile 187: Zeile 193:
                                     true,
                                     true,
                                     false,
                                     false,
                                     Anchor.globals.TemplUtl )
                                     Anchors.globals.TemplUtl )
     local r, scope, style
     local r, scope, style
     if type( TemplUtl ) == "table"  and
     if type( TemplUtl ) == "table"  and
Zeile 249: Zeile 255:
     return r
     return r
end -- fault()
end -- fault()
local function features( adapt )
    -- Set parameters on compliance
    if type( adapt ) == "table" then
        Config.limitMarkup = faculty( adapt.rejectMarkup )
        Config.limitMW    = faculty( adapt.rejectMW )
        Config.limitXML    = faculty( adapt.onlyXML )
        Config.lightXML    = faculty( adapt.softXML )
    end
end -- features()




Zeile 258: Zeile 276:
     -- Returns appropriate string
     -- Returns appropriate string
     local r
     local r
     if argsF then
     if type( argsF ) == "table" then
         Config.errCat    = argsF.errCat
         Config.errCat    = argsF.errCat
         Config.errClasses = argsF.errClasses
         Config.errClasses = argsF.errClasses
         Config.errHide    = faculty( argsF.errHide )
         Config.errHide    = faculty( argsF.errHide )
         Config.errNS      = argsF.errNS
         Config.errNS      = argsF.errNS
if mw.site.server:find( "de.wikipedia.org", 1, true ) then
        features( argsF )
     Config.errClasses = "Linkwartung"
if mw.site.server:find( "de.wikipedia.", 1, true ) then
     Config.errHide = true
     Config.errClasses = "Linkwartung"
     Config.errNS = 0
     Config.errHide     = true
     Config.errNS       = 0
    Config.limitMarkup = true
    Config.limitMW    = false
    Config.limitXML    = true
    Config.lightXML    = true
end
end
     end
     end
     if type( argsT ) == "table" then
     if type( argsT ) == "table" then
         local checked = { }
         local e, got, less, s, unknown
        local tocheck = { }
        local further = function ( add )
                            local s = add:gsub( " +", "_" )
                                        :gsub( "_+", "_" )
                            table.insert( checked,s )
                        end
        local less, s, unknown
         for k, v in pairs( argsT ) do
         for k, v in pairs( argsT ) do
             if type( k ) == "number" then
             if type( k ) == "number" then
Zeile 296: Zeile 312:
                 table.insert( unknown, k )
                 table.insert( unknown, k )
             elseif v then
             elseif v then
                 if less then
                 got      = got  or  { }
                    further( v )
                v        = v:gsub( " +", "_" )
                else
                            :gsub( "_+", "_" )
                    table.insert( tocheck, v )
                 got[ v ] = less
                 end
             end
             end
         end -- for k, v
         end -- for k, v
         if unknown then
         if unknown then
             local e = mw.html.create( "code" )
             e = mw.html.create( "code" )
                        :css( "white-space", "nowrap" )
                  :css( "white-space", "nowrap" )
                        :wikitext( table.concat( unknown, " " ) )
                  :wikitext( table.concat( unknown, " " ) )
             s = string.format( " in [[%s]]",
             s = string.format( " in [[%s]]",
                               Config.frame:getTitle() )
                               Config.frame:getTitle() )
Zeile 312: Zeile 327:
             :wikitext( s )
             :wikitext( s )
             r = fault( "errUnkown", tostring( e ) )
             r = fault( "errUnkown", tostring( e ) )
         else
         elseif got then
             local bad, e
             local profile = { limitMarkup = false,
            local flop = function ( a, alter )
                              limitMW    = false,
                            if not bad then
                              limitXML    = false }
                                bad = { }
            local bad, checked, legal, o, s
                            end
             for k, v in pairs( got ) do
                            e = mw.html.create( "code" )
                 if v then
                                        :wikitext( a )
                     o = profile
                            if alter then
                                e:css( "white-space", "nowrap" )
                            end
                            table.insert( bad, tostring( e ) )
                        end
            local legal, s
             for i = 1, #tocheck do
                s = mw.text.unstripNoWiki( tocheck[ i ] )
                 if s:match( "^%d" )  or
                  s:sub( 1, 1 ) == "-"  or
                  s:match( Config.badPattern )  or
                  s:match( "&#%w+;" ) then
                     legal = false
                 else
                 else
                     legal = true
                     o = false
                    for k = 1, #Config.globalPatterns do
                end
                        if s:match( Config.globalPatterns[ k ] ) then
                s, legal = Anchors.feasible( k, o )
                            legal = false
                if s then
                            break    -- k = 1, #Config.globalPatterns
                    checked = checked  or  { }
                        end
                    table.insert( checked, s )
                    end -- k = 1, #Config.globalPatterns
                 end
                 end
                 if not legal then
                 if not legal then
                     flop( mw.text.encode( s ),  true )
                     bad = bad  or  { }
                else
                    e  = mw.html.create( "code" )
                     further( s )
                                :css( "white-space", "nowrap" )
                                :wikitext( mw.text.encode( k ) )
                     table.insert( bad, tostring( e ) )
                 end
                 end
             end -- i = 1, #tocheck
             end -- for k, v
             for i = 1, #checked do
             if checked then
                 s = checked[ i ]
                 r = fair( checked )
                for k = 1, #Config.globalPage do
             end
                    if s == Config.globalPage[ k ] then
                        flop( s )
                        break    -- k = 1, #Config.globalPage
                    end
                end -- k = 1, #Config.globalPage
             end -- i = 1, #checked
             if bad then
             if bad then
                 s = string.format( "%s in [[%s]]",
                 s = string.format( "%s in [[%s]]",
                                   table.concat( bad, ", " ),
                                   table.concat( bad, ", " ),
                                   Config.frame:getParent():getTitle() )
                                   Config.frame:getParent():getTitle() )
                 r = fault( "errInvalid", s )
                 r = string.format( "%s%s",
            elseif #checked == 0 then
                                  r or "",
                r = fault( "errNoFragments" )
                                  fault( "errInvalid", s ) )
            else
                r = fair( checked )
             end
             end
        else
            r = fault( "errNoFragments" )
         end
         end
     end
     end
     return r or ""
     return r or ""
end -- main()
end -- main()
Anchors.feasible = function ( analyse, allow )
    -- Check ID for compliance
    -- Precondition:
    --    analyse  -- string, with presumptive anchor
    --    allow    -- optional table, with compliance criteria
    --                  -- limitMarkup
    --                  -- limitMW
    --                  -- limitXML
    --                  -- lightXML
    -- Postcondition:
    --    Returns
    --        1.  -- string  -- with normalised anchor
    --              -- false  -- if not appropriate
    --        2.  -- true, if fully compliant
    local r1 = false
    local r2 = false
    if type( analyse ) == "string" then
        local s = mw.text.trim( analyse )
        if s ~= "" then
            local deny = { limitMarkup = Config.limitMarkup,
                          limitMW    = Config.limitMW,
                          limitXML    = Config.limitXML,
                          lightXML    = Config.lightXML }
            if type( allow ) == "table" then
                for k, v in pairs( deny ) do
                    if type( allow[ k ] ) == "boolean" then
                        deny[ k ] = allow[ k ]
                    end
                end -- for k, v
            end
            if deny.limitMW then
                r1 = true
            else
                s  = mw.text.unstripNoWiki( s )
                s  = mw.text.trim( s )
                r1 = ( s ~= "" )
            end
            if r1  and  deny.limitMarkup  and
              ( s:match( Config.badPattern )  or
                s:match( "&#%w+;" ) ) then
                r1 = false
            end
            if r1 then
                for k = 1, #Config.globalPage do
                    if s == Config.globalPage[ k ] then
                        r1 = false
                        break    -- k = 1, #Config.globalPage
                    end
                end -- k = 1, #Config.globalPage
                if r1 then
                    Config.badCtrl = Config.badCtrl  or
                                    string.format( "[%c-%c]", 1, 31 )
                    r1 = not s:find( Config.badCtrl )
                    if r1  and  s:match( "%%%x%x" ) then
                        r1 = false
                    end
                    if r1 then
                        for k = 1, #Config.globalPatterns do
                            if s:match( Config.globalPatterns[ k ] ) then
                                r1 = false
                                break    -- k = 1, #Config.globalPatterns
                            end
                        end -- k = 1, #Config.globalPatterns
                    end
                end
            end
            if r1 then
                r2 = true
                if deny.limitXML  and
                  ( s:match( "^%d" )  or
                    s:sub( 1, 1 ) == "-" ) then
                    r2 = false
                    if not deny.lightXML then
                        r1 = false
                    end
                end
            end
            if r1 then
                r1 = s:gsub( " +", "_" )
                      :gsub( "_+", "_" )
            end
        end
    end
    return r1, r2
end -- Anchors.feasible()




Zeile 436: Zeile 521:
end -- p.f()
end -- p.f()


p.flops = function ( frame )
p.feasible = function ( frame )
    local r1, r2
    Config.frame = frame
    features( frame.args )
    r1, r2 = Anchors.feasible( frame.args[ 1 ] )
    return r1 or ""
end -- p.feasible()
 
p.forbidden = function ()
     local r  = ""
     local r  = ""
     local sep = ""
     local sep = ""
Zeile 448: Zeile 541:
     end -- k = 1, #Config.globalPage
     end -- k = 1, #Config.globalPage
     return r
     return r
end -- p.flops()
end -- p.forbidden()


p.failsafe = function ( frame )
p.failsafe = function ( frame )
Zeile 468: Zeile 561:
end -- p.failsafe()
end -- p.failsafe()


 
p.Anchors = function ()
 
     return Anchors
p.test = function ( argsF, argsT )
end -- p.Anchors
    Config.frame = mw.getCurrentFrame()
     return main( argsF, argsT )
end -- p.test()


return p
return p