Modul:Vorlage:Anker: Unterschied zwischen den Versionen
Zur Navigation springen
Zur Suche springen
2020-01-30
(2019-12-29) |
(2020-01-30) |
||
Zeile 1: | Zeile 1: | ||
local | local Anchors = { suite = "Anchors", | ||
serial = "2020-01-30", | |||
item = 79414611, | |||
globals = { Multilinguall = 47541920, | |||
TemplUtl = 52364930 } | |||
} | |||
--[=[ | --[=[ | ||
Template:Anchor {{Anker}} | Support Template:Anchor {{Anker}} | ||
]=] | ]=] | ||
local Failsafe | local Failsafe = Anchors | ||
local GlobalMod | 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", | ||
Zeile 25: | Zeile 33: | ||
"fundraising", | "fundraising", | ||
"jump-to-nav", | "jump-to-nav", | ||
"language-settings-dialog", | |||
"languagesettings-panels", | |||
"page-actions", | "page-actions", | ||
"siteNotice", | "siteNotice", | ||
"siteSub", | "siteSub", | ||
"toc", | "toc", | ||
"top" }, | "top", | ||
"wikiPreview" }, | |||
globalPatterns = { "^mw%-[%l%-]+$", | globalPatterns = { "^mw%-[%l%-]+$", | ||
"^ca%-[%l%-]+$", | "^ca%-[%l%-]+$", | ||
"^n%-[%l%-]+$", | "^n%-[%l%-]+$", | ||
"^pt | "^p%-[%a%-]+$", | ||
"^pt%-[%l%-]+$", | |||
"^wp%u" }, | "^wp%u" }, | ||
errCat = false, | errCat = false, | ||
Zeile 108: | Zeile 120: | ||
-- 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 131: | Zeile 144: | ||
-- adjust -- string or nil | -- adjust -- string or nil | ||
-- Returns boolean | -- Returns boolean | ||
local r | 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 145: | Zeile 160: | ||
local function fair( assembly ) | local function fair( assembly ) | ||
-- Create HTML code for anchors | -- Create HTML code for anchors | ||
-- assembly -- sequence table, with | -- assembly -- sequence table, with identifiers | ||
-- Returns HTML string | -- Returns HTML string | ||
local collection = mw.html.create( "" ) | local collection = mw.html.create( "" ) | ||
Zeile 182: | Zeile 197: | ||
true, | true, | ||
false, | false, | ||
Anchors.globals.TemplUtl ) | |||
local r, scope, style | local r, scope, style | ||
if type( TemplUtl ) == "table" and | if type( TemplUtl ) == "table" and | ||
Zeile 244: | Zeile 259: | ||
return r | return r | ||
end -- fault() | end -- fault() | ||
local function features( adapt ) | |||
-- Set #invoke 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 253: | Zeile 280: | ||
-- 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. | 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 | local e, got, 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 291: | Zeile 316: | ||
table.insert( unknown, k ) | table.insert( unknown, k ) | ||
elseif v then | elseif v then | ||
got = got or { } | |||
v = v:gsub( " +", "_" ) | |||
:gsub( "_+", "_" ) | |||
got[ v ] = less | |||
end | end | ||
end -- for k, v | end -- for k, v | ||
if unknown then | if unknown then | ||
e = mw.html.create( "code" ) | |||
:css( "white-space", "nowrap" ) | |||
:wikitext( table.concat( unknown, " " ) ) | |||
s = string.format( " in [[%s]]", | s = string.format( " in [[%s]]", | ||
Config.frame:getTitle() ) | Config.frame:getTitle() ) | ||
Zeile 307: | Zeile 331: | ||
:wikitext( s ) | :wikitext( s ) | ||
r = fault( "errUnkown", tostring( e ) ) | r = fault( "errUnkown", tostring( e ) ) | ||
elseif got then | |||
local | local profile = { limitMarkup = false, | ||
limitMW = false, | |||
limitXML = false } | |||
local bad, checked, legal, o, s | |||
for k, v in pairs( got ) do | |||
if v then | |||
o = profile | |||
for | |||
if | |||
else | else | ||
legal = | o = false | ||
end | |||
s, legal = Anchors.feasible( k, o ) | |||
if s then | |||
checked = checked or { } | |||
table.insert( checked, s ) | |||
end | end | ||
if not legal then | if not legal then | ||
bad = bad or { } | |||
e = mw.html.create( "code" ) | |||
:css( "white-space", "nowrap" ) | |||
:wikitext( mw.text.encode( k ) ) | |||
table.insert( bad, tostring( e ) ) | |||
end | end | ||
end -- | end -- for k, v | ||
if checked then | |||
r = fair( checked ) | |||
end | |||
end | |||
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 = | r = string.format( "%s%s", | ||
r or "", | |||
fault( "errInvalid", s ) ) | |||
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 | |||
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 431: | Zeile 517: | ||
end -- p.f() | end -- p.f() | ||
p.flops = function ( | p.flops = function () | ||
-- TEMP migration | |||
return p.forbidden() | |||
end -- p.flops() | |||
p.forbidden = function () | |||
local r = "" | local r = "" | ||
local sep = "" | local sep = "" | ||
Zeile 443: | Zeile 534: | ||
end -- k = 1, #Config.globalPage | end -- k = 1, #Config.globalPage | ||
return r | return r | ||
end -- p. | end -- p.forbidden() | ||
p.failsafe = function ( frame ) | p.failsafe = function ( frame ) | ||
Zeile 462: | Zeile 553: | ||
return Failsafe.failsafe( since ) or "" | return Failsafe.failsafe( since ) or "" | ||
end -- p.failsafe() | end -- p.failsafe() | ||
p.Anchors = function () | |||
return Anchors | |||
end -- p.Anchors | |||
p.test = function ( argsF, argsT ) | |||
Config.frame = mw.getCurrentFrame() | |||
return main( argsF, argsT ) | |||
end -- p.test() | |||
return p | return p |