Moduł:Łatki

Z GastroWiki
Przejdź do nawigacji Przejdź do wyszukiwania

local m = {}

function m.Plik(frame) if not frame then return nil end

local args = frame.args if not args then mw.log("brak argumentów") return nil end

local file = args[1] if not file then mw.log("brak pliku") return nil end

local multipleFiles = "" if string.match(file, "%]%s*%[") then multipleFiles = "" end

if string.match(file, "^%s*%[%[") then mw.log("to jest link: "..file) return file .. ""..multipleFiles end

if string.match(file, "^%s*%[") then mw.log("to jest link zewnętrzny: "..file) return file .. ""..multipleFiles end

local builder = {} table.insert(builder, "[[Plik:") for i, v in ipairs(args) do if i > 1 then table.insert(builder,"|") end

table.insert(builder,v) end

table.insert(builder, "]]") local result = table.concat(builder, "") mw.log("wynik: "..result) return result end

m.Join = function(frame) local result = {} local pf = frame:getParent() local i = 1

while true do local object = pf:getArgument(i) if object then object = object:expand() else break end

if object then if i > 1 then table.insert(result, "|") end

table.insert(result, object) i = i + 1 else break end end

return table.concat(result, "") end

m.PoliczLinki = function(frame) local pf = frame:getParent() local text = frame.args[1] or pf.args[1] local threshold = tonumber(frame.args["próg"] or pf.args["próg"]) or 1 if text then text = mw.text.trim(text) local _, count = mw.ustring.gsub(text, "(%[%[[^%[%]]-%]%])", "%1") if count >= threshold then local lang = mw.getContentLanguage() local number = lang:formatNum(count) local articles = lang:convertPlural(count, { "artykuł", "artykuły", "artykułów" }) return text .. " ("..number.." "..articles..")" else return text end end end

m["Liczba artykułów"] = function(frame) local text = frame.args[1] or frame:getParent().args[1] or "" local _, count = mw.ustring.gsub(text, "(%[%[[^%[%]]-%]%])", "%1") local _, files = mw.ustring.gsub(text, "(%[%[Plik:[^%[%]]-%]%])", "%1") count = count - files local lang = mw.getContentLanguage() local number = lang:formatNum(count) local articles = lang:convertPlural(count, { "artykuł", "artykuły", "artykułów" }) return number.." "..articles end

m["Liczba artykułów między"] = function(frame) local pf = frame:getParent() local start = frame.args[1] or pf.args[1] if not start then return end

local stop = frame.args[2] or pf.args[2] if not stop then return end

local content = mw.title.getCurrentTitle():getContent() if not content then return end

local startPattern = "" local startPosition = string.find(content, startPattern, 1, true) if not startPosition then return end

startPosition = startPosition + #startPattern

local stopPattern = "" local stopPosition = string.find(content, stopPattern, startPosition, true) if not stopPosition then return end

stopPosition = stopPosition - 1 if startPosition >= stopPosition then return end

local text = string.sub(content, startPosition, stopPosition) local _, count = mw.ustring.gsub(text, "(%[%[[^%[%]]-%]%])", "%1") local _, files = mw.ustring.gsub(text, "(%[%[Plik:[^%[%]]-%]%])", "%1") count = count - files

local threshold = tonumber(frame.args["próg"] or pf.args["próg"]) or 1 if count < threshold then return end

local lang = mw.getContentLanguage() local number = lang:formatNum(count) local articles = lang:convertPlural(count, { frame.args[4] or pf.args[4] or "artykuł", frame.args[5] or pf.args[6] or "artykuły", frame.args[6] or pf.args[6] or "artykułów" })

local result, _ = string.gsub(frame.args[3] or pf.args[3] or "($1)", "$1", number.." "..articles) return result end

m["Sprawdzenie argumentów odn"] = function(frame) local pf = frame:getParent() local i = 1 local problems = false local yeardetected = false while true do local arg = pf.args[i] if not arg then problems = i == 1 and "brak argumentów" or false break end

if (i > 5) or yeardetected then problems = "za dużo argumentów pozycyjnych" break end

if #arg == 0 then problems = "pusty argument" break end

if arg ~= mw.text.trim(arg) then problems = "nieoczekiwane odstępy na początku lub końcu argumentu" break end

if string.match(arg, "^%d+%l?$") then yeardetected = true if i == 1 then problems = "rok musi być ostatnim parametrem po nazwiskach autorów" break end elseif string.match(arg, "^s[%-%.:]%s*%d+") then problems = "prawdopodobnie nieprawidłowo podany numer strony" break elseif string.match(arg, "%s%s") then problems = "podwójne odstępy" break end

i = i + 1 end

if not problems then local odn = pf.args.odn if odn and ((#odn ~= 1) or (odn < "a") or (odn > "z")) then problems = "nieoczekiwany parametr odn" end end

if not problems then if pf.args.strona or pf.args.ss or pf.args.strony or pf.args.p or pf.args.page or pf.args.pp or pf.args.pages then problems = "przestarzały parametr z numerem strony" end end

	if not problems then
		return nil
	end
	
	local result = mw.html.create("span"):addClass("problemy-w-odn")
	if mw.title.getCurrentTitle().namespace == 0 then
		result:css("display", "none")
		result:wikitext("")
	else
		result:css("color", "red")
	end
	result:wikitext("ODN: ", problems)
	return tostring(result)

end

m["Numerowanie w tabeli"] = function(frame) local counter = 0 local result = {} local pf = frame:getParent() local i = 1

table.insert(result, "{") while true do local object = pf:getArgument(i) if object then object = object:expand() else break end

if object then table.insert(result, "|")

local length = #object if length > 0 then if string.byte(object,1) == 35 then counter = counter + 1 local counterText = counter..". " local userText = length > 1 and string.sub(object, 2, length) or "" object = counterText..userText end end

table.insert(result, object) i = i + 1 else break end end

table.insert(result, "|}") return table.concat(result, "") end

m["są interwiki"] = function(frame) local count = 0 local entity = mw.wikibase.getEntityObject() if entity and entity.sitelinks then for k, v in pairs(entity.sitelinks) do if k ~= "plwiki" then count = count + 1 end end end

return count > 0 and count or "" end

function m.contentMatch(frame) local content = mw.title.getCurrentTitle():getContent() if not content then mw.log("no content") return end

local i = 1 while true do local p = frame.args[i] if not p or (#p==0) then mw.log("no pattern "..i) return end

local result = mw.ustring.match(content, p) if result then mw.log("match "..result) return result end

mw.log("no match "..p) i = i + 1 end end

function m.match(frame) local s = frame.args[1] if not s or (#s==0) then return nil end

local p = frame.args[2] if not p or (#p == 0) then return nil end

return mw.ustring.match(s, p) end

function m.replace(frame) local text = frame.args[1] if not text or (#text == 0) then return nil end local pattern = frame.args[2] if not pattern or (#pattern == 0) then return text end local replacement = frame.args[3] or "" local result, _ = mw.ustring.gsub(text, pattern, replacement) return result end

function m.Sumuj(frame) local pf = frame:getParent() if not pf then return end

local name = pf.args[1] if not name or (#name == 0) then return end

local value = pf.args[2] if not value then return end

local lang = mw.getContentLanguage() local number = lang:parseFormattedNumber(value) if number then return lang:formatNum(number) end

if value ~= "RAZEM" then return value end

local pattern = "{{%s*[Ss]umuj%s*|"..mw.ustring.gsub( name, "([%(%)%.%%%+%-%*%?%[%^%$%]])", "%%%1" ).."|%s*([^}]-)%s*}}"; local total = 0 local items = 0 local sum = function(value) local number = lang:parseFormattedNumber(value) if number then total = total + number items = items + 1 end

return false end

local content = mw.title.getCurrentTitle():getContent() mw.ustring.gsub(content, pattern, sum)

if items ~= 0 then return lang:formatNum(total) end end

return m