Moduł:Łatki: Różnice pomiędzy wersjami
[wersja przejrzana] | [wersja przejrzana] |
+ wyjątek |
+ Z dla 'z' lub 'ze' |
||
Linia 468: | Linia 468: | ||
return table.concat(result, "") |
return table.concat(result, "") |
||
end |
|||
function m.Z(frame) |
|||
local ze = { |
|||
"^%s*[Mm][Nn][Ii][Ee]$", |
|||
"^%s*[Mm][Nn][Ii][Ee]%s", |
|||
"^%s*[Mm][Nn][Ąą]$", |
|||
"^%s*[Mm][Nn][Ąą]%s", |
|||
"^%s*[Ss][Oo][Bb][Ąą]$", |
|||
"^%s*[Ss][Oo][Bb][Ąą]%s", |
|||
"^%s*[Ss][Zz][BbCcĆćDdFfGgHhJjKkLlŁłMmNnŃńPpRrSsŚśTtWwZzŹźŻż]", |
|||
"^%s*[Ss][BbCcĆćDdFfGgHhJjKkLlŁłMmNnŃńPpRrSsŚśTtWwŹźŻż]", |
|||
"^%s*[ŚśZz][BbCcĆćDdFfGgHhJjKkLlŁłMmNnŃńPpRrSsŚśTtWwZzŹźŻż]", |
|||
} |
|||
local text = frame.args[1] |
|||
if text then |
|||
for _, regex in ipairs(ze) do |
|||
if mw.ustring.match(text, regex) then |
|||
return "ze" |
|||
end |
|||
end |
|||
end |
|||
return "z" |
|||
end |
end |
||
Wersja z 17:43, 17 sty 2016
Moduł techniczny do obsługi różnorakich wysokospecjalistycznych funkcji lub szablonów.
Plik
Funkcja poprawiająca niestandardowe wywołania grafiki w infoboksach.
PoliczLinki
Funkcja implementująca szablon {{Policz linki}}.
Liczba artykułów między
Funkcja obliczająca liczbę wikilinków między zadanymi znacznikami. Nazwy znaczników podaje się w pierwszym i drugim parametrze. W kodzie strony znaczniki muszą być umieszczone w komentarzu HTML.
Numerowanie w tabeli
Funkcja implementująca szablon {{Numerowanie w tabeli}}.
są interwiki
Funkcja zwracająca liczbę interwiki do projektów siostrzanych i innych wersji językowych. Wykorzystanie w {{EK}} do generowania dodatkowego ostrzeżenia.
contentMatch
Funkcja zwracająca wynik pierwszego wyrażenia regularnego Lua w treści źródłowej strony.
- Wyszukiwarka szablonów, które jej używają jest tutaj.
Interwiki
Funkcja do generowania większej liczby interwiki przez wykorzystanie cechy „uważa się za to samo co”.
- Wykorzystanie w {{imiona}} i {{więcej interwiki}}.
Z
Funkcja generująca przyimek „z” lub „ze” zależnie od podanego i następującego po nim słowa. Zastosowana w {{przekierowanie}}.
TEMPLATENAME
Funkcja zwracająca nazwę szablonu, w którym jest wywołana.
SUBST
Funkcja sprawdzająca czy szablon jest wywołany przez subst:
.
FormatPluralNum
Funkcja wspomagająca generowanie formatowanie liczby naturalnej z opisem w połączeniu z opcjonalnym dodaniem odpowiedniej formy rzeczownika.
{{#invoke:Łatki|FormatPluralNum|liczba|forma 1|forma 2|forma 3|forma 4}}
Przykłady:
{{#invoke:Łatki|FormatPluralNum|chyba 1 |skarpetka|skarpetki|skarpetek}}
→ Błąd skryptu: nie ma takiej funkcji „FormatPluralNum”.{{#invoke:Łatki|FormatPluralNum|mam 2002 |skarpetka|skarpetki|skarpetek}}
→ Błąd skryptu: nie ma takiej funkcji „FormatPluralNum”.{{#invoke:Łatki|FormatPluralNum|około 50000 |skarpetka|skarpetki|skarpetek}}
→ Błąd skryptu: nie ma takiej funkcji „FormatPluralNum”.
Powyższy opis jest dołączany ze strony Moduł:Łatki/opis. (edytuj | historia)
Edytorzy mogą eksperymentować w brudnopisie (edytuj | różnice) tego modułu. Zobacz podstrony tego modułu. |
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 = "[[Kategoria:Łatki - Kilka plików]]"
end
if string.match(file, "^%s*%[%[") then
mw.log("to jest link: "..file)
return file .. "[[Kategoria:Łatki - Plik]]"..multipleFiles
end
if string.match(file, "^%s*%[") then
mw.log("to jest link zewnętrzny: "..file)
return file .. "[[Kategoria:Łatki - Plik zewnętrzny]]"..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 .. "<small> ("..number.." "..articles..")</small>"
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 = "<!--"..start.."-->"
local startPosition = string.find(content, startPattern, 1, true)
if not startPosition then
return
end
startPosition = startPosition + #startPattern
local stopPattern = "<!--"..stop.."-->"
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 "<small>($1)</small>", "$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
elseif mw.ustring.match(arg, "^%a+%d") then
if not mw.ustring.match(arg, "^[%u%d]+$") then
problems = "prawdopodobnie sklejone argumenty (brak pionowej kreski)"
break
end
elseif mw.ustring.match(arg, "^%u%l+%u") then
local justification = {
["De"] = true,
["Del"] = true,
["Di"] = true,
["Le"] = true,
["Mac"] = true,
["Mc"] = true,
}
if not justification[mw.ustring.match(arg, "^%u%l+")] then
problems = "prawdopodobnie sklejone argumenty (brak pionowej kreski)"
break
end
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
local s = pf.args.s
if s and string.match(s, "&[a-z]+;") then
problems = "użyto encji HTML w numerze strony"
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("[[Kategoria:Szablon odn do sprawdzenia]]")
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
function m.Interwiki(frame)
local qid = frame.args.id
local data = mw.wikibase.getEntityObject(qid)
if not data then
return -- brak danych -> kategoria?
end
local links = {}
local appendLink = function(lang, title)
if not links[lang] then
links[lang] = { title }
else
table.insert(links[lang], title)
end
end
local knownLanguages = mw.loadData("Module:Lang/data")
local extractLinks = function(data)
if data.sitelinks then
for k, v in pairs(data.sitelinks) do
local lang = string.sub(k, 1, -5)
local project = string.sub(k, -4)
if (project == "wiki") and knownLanguages[lang] then
appendLink(lang, v.title)
end
end
end
end
extractLinks(data)
if data.claims and data.claims.P460 then
for _, v in ipairs(data.claims.P460) do
if v.mainsnak.snaktype == "value" then
local seeid = "Q"..tostring(v.mainsnak.datavalue.value["numeric-id"])
local seedata = mw.wikibase.getEntityObject(seeid)
if seedata then
extractLinks(seedata)
end
end
end
end
local result = {}
local content = mw.title.getCurrentTitle():getContent()
for k, v in pairs(links) do
local pattern = "%[%["..mw.ustring.gsub( k, "([%(%)%.%%%+%-%*%?%[%^%$%]])", "%%%1" )..":[^%[%]|]+%]%]"
local interwiki = mw.ustring.match(content, pattern)
if not interwiki and not data.sitelinks[k.."wiki"] then
table.insert(result, "[[")
table.insert(result, k)
table.insert(result, ":")
table.insert(result, v[1])
table.insert(result, "]]")
end
end
return table.concat(result, "")
end
function m.Z(frame)
local ze = {
"^%s*[Mm][Nn][Ii][Ee]$",
"^%s*[Mm][Nn][Ii][Ee]%s",
"^%s*[Mm][Nn][Ąą]$",
"^%s*[Mm][Nn][Ąą]%s",
"^%s*[Ss][Oo][Bb][Ąą]$",
"^%s*[Ss][Oo][Bb][Ąą]%s",
"^%s*[Ss][Zz][BbCcĆćDdFfGgHhJjKkLlŁłMmNnŃńPpRrSsŚśTtWwZzŹźŻż]",
"^%s*[Ss][BbCcĆćDdFfGgHhJjKkLlŁłMmNnŃńPpRrSsŚśTtWwŹźŻż]",
"^%s*[ŚśZz][BbCcĆćDdFfGgHhJjKkLlŁłMmNnŃńPpRrSsŚśTtWwZzŹźŻż]",
}
local text = frame.args[1]
if text then
for _, regex in ipairs(ze) do
if mw.ustring.match(text, regex) then
return "ze"
end
end
end
return "z"
end
return m