Moduł:Łatki: Różnice pomiędzy wersjami
[wersja przejrzana] | [wersja przejrzana] |
+ nowa funkcja |
wsparcie dla wielu wyrażeń |
||
Linia 276: | Linia 276: | ||
end |
end |
||
local |
local i = 1 |
||
while true do |
|||
⚫ | |||
local p = frame.args[i] |
|||
⚫ | |||
⚫ | |||
⚫ | |||
end |
|||
⚫ | |||
if result then |
|||
return result |
|||
end |
|||
i = i + 1 |
|||
end |
end |
||
⚫ | |||
end |
end |
||
Wersja z 21:18, 18 lut 2015
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
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")
else
result:css("color", "red")
end
result:wikitext("ODN: ", problems)
return tostring(result).."[[Kategoria:Szablon odn do sprawdzenia]]"
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
function m.contentMatch(frame)
local content = mw.title.getCurrentTitle():getContent()
if not content then
return
end
local i = 1
while true do
local p = frame.args[i]
if not p or (#p==0) then
return
end
local result = mw.ustring.match(content, p)
if result then
return result
end
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
return m