Moduł:Lepsze główne: Różnice pomiędzy wersjami
Z Nonsensopedii, polskiej encyklopedii humoru
Polskacafe (dyskusja • edycje) (rev) Znacznik: rewert ręczny |
Polskacafe (dyskusja • edycje) |
||
(Nie pokazano 10 pośrednich wersji utworzonych przez tego samego użytkownika) | |||
Linia 2: | Linia 2: | ||
local p = {} |
local p = {} |
||
function |
function sanitizeOrElse(text, alt) |
||
if text ~= nil then |
if text ~= nil then |
||
return mw.text.nowiki(text) |
return mw.text.nowiki(text) |
||
Linia 8: | Linia 8: | ||
return alt |
return alt |
||
end |
end |
||
end |
|||
function allowOnly(value, allowed, alt) |
|||
local sanitized = mw.text.trim(sanitizeOrElse(value, alt)) |
|||
for k, v in ipairs(allowed) do |
|||
if v == sanitized then |
|||
return sanitized |
|||
end |
|||
end |
|||
return alt |
|||
end |
|||
function makeShowStatus(value) |
|||
local final = allowOnly(value, {"desktoponly", "mobileonly"}, '') |
|||
if final == "desktoponly" then |
|||
return {desktoponly=true, mobileonly=false, duplicateDesktoponly=false, duplicateMobileonly=false} |
|||
elseif final == "mobileonly" then |
|||
return {desktoponly=false, mobileonly=true, duplicateDesktoponly=false, duplicateMobileonly=false} |
|||
else |
|||
return {desktoponly=false, mobileonly=false, duplicateDesktoponly=false, duplicateMobileonly=false} |
|||
end |
|||
end |
|||
function showStatusToString(stat) |
|||
local val = "" |
|||
if stat["desktoponly"] or stat["duplicateDesktoponly"] then |
|||
val = val .. "desktoponly " |
|||
end |
|||
if stat["mobileonly"] or stat["duplicateMobileonly"] then |
|||
val = val .. "mobileonly" |
|||
end |
|||
return mw.text.trim(val) |
|||
end |
|||
function getShowStatusForPlatform(stat, platform) |
|||
if platform == "desktop" then |
|||
return stat.desktoponly or stat.duplicateDesktoponly |
|||
elseif platform == "mobile" then |
|||
return stat.mobileonly or stat.duplicateMobileonly |
|||
end |
|||
end |
|||
function willEverBeUsed(cur) |
|||
return not ((getShowStatusForPlatform(cur.showstatus, "desktop") == getShowStatusForPlatform(cur.showstatus, "mobile")) and (getShowStatusForPlatform(cur.showstatus, "desktop") == true)) |
|||
end |
end |
||
function makeElementTable(args, num) |
function makeElementTable(args, num) |
||
local cur = {} |
local cur = {} |
||
if args['ptytułszablon' .. num] ~= nil then |
|||
cur['title'] = '{{' .. mw.text.nowiki(args['ptytułszablon' .. num]) .. '}}' |
|||
else |
|||
cur['title'] = sanitizeOrElse(args['ptytuł' .. num], 'Nie podano tytułu') |
|||
end |
|||
cur['text'] = sanitizeOrElse(args['panel' .. num], '') |
|||
cur['num'] = num |
cur['num'] = num |
||
cur['showstatus'] = |
cur['showstatus'] = makeShowStatus(args["pwarunki" .. num]) |
||
cur['icon'] = |
cur['icon'] = sanitizeOrElse(args['pikona' .. num], '') |
||
cur['iconlink'] = |
cur['iconlink'] = sanitizeOrElse(args["pikonalink" .. num], '') |
||
cur['titlelink'] = sanitizeOrElse(args["plink" .. num], '') |
|||
return cur |
return cur |
||
end |
end |
||
Linia 26: | Linia 75: | ||
local first = {} |
local first = {} |
||
local second = {} |
local second = {} |
||
local curRight = |
local curLeft, curRight = 0, 0 |
||
for i = 1, 50 do |
|||
for i = 1, 50 do -- 50 jest tymczasowo, tak jest w portalach ostrzyciela ale nie wiem czy to dobrze idk |
|||
if args['panel' .. i] ~= nil then |
if args['panel' .. i] ~= nil then |
||
if args['panel' .. i] == '-' or args['panel' .. i] == '' then else |
if args['panel' .. i] == '-' or args['panel' .. i] == '' then else |
||
local cur = makeElementTable(args, i) |
local cur = makeElementTable(args, i) |
||
if i % 2 == 1 then |
if i % 2 == 1 then |
||
table.insert(first, cur) |
table.insert(first, cur) |
||
curRight = |
curLeft, curRight = 0, 0 |
||
else |
else |
||
cur |
cur.showstatus.duplicateDesktoponly = true |
||
if willEverBeUsed(cur) then |
|||
table.insert(second, cur) |
table.insert(second, cur) |
||
⚫ | |||
end |
|||
local cur2 = makeElementTable(args, i) |
local cur2 = makeElementTable(args, i) |
||
cur2 |
cur2.showstatus.duplicateMobileonly = true |
||
if willEverBeUsed(cur2) then |
|||
table.insert(first, cur2) |
|||
⚫ | |||
curLeft = curLeft + 1 |
|||
end |
|||
end |
end |
||
end |
end |
||
else break end |
else break end |
||
end |
end |
||
if curRight then |
if curRight > 0 then |
||
local maxSecond = table.maxn(second) |
|||
for i = 0, (curRight - 1) do |
|||
⚫ | |||
second[maxSecond-i].showstatus.duplicateDesktoponly = false |
|||
end |
|||
second[maxSec]["showstatus"] = string.gsub(ss, " desktoponly", "") |
|||
end |
|||
if curLeft > 0 then |
|||
⚫ | |||
for i = 0, (curLeft - 1) do |
|||
table.remove(first, maxFirst-i) |
|||
end |
|||
end |
end |
||
return first, second |
return first, second |
||
Linia 57: | Linia 117: | ||
function makePanel(tab) |
function makePanel(tab) |
||
local panelname = tab["title"] |
local panelname = tab["title"] |
||
local titlelink = tab["titlelink"] |
|||
local template = tab["text"] |
local template = tab["text"] |
||
local mobiledesktop = tab["showstatus"] |
local mobiledesktop = showStatusToString(tab["showstatus"]) |
||
local icon = tab["icon"] |
local icon = tab["icon"] |
||
local iconlink = tab["iconlink"] |
local iconlink = tab["iconlink"] |
||
local |
local id = "panel-" .. tab["num"] .. "-" .. mw.text.encode(panelname) |
||
local wikitext = "{{User:Polskacafe/panel|" .. panelname .. "|{{" .. template .. "}}|ikona=" .. icon .. "|link=" .. iconlink .. "|tytuł link=" .. titlelink .. "}}" |
|||
local panel = mw.html.create("div") |
local panel = mw.html.create("div") |
||
:addClass("glowna-panel") |
:addClass("glowna-panel") |
||
:attr("id", id) |
|||
:wikitext(wikitext) |
:wikitext(wikitext) |
||
if (mobiledesktop ~= nil) then |
if (mobiledesktop ~= nil) then |
||
Linia 75: | Linia 138: | ||
column = mw.html.create("div") |
column = mw.html.create("div") |
||
:addClass("glowna-kolumna") |
:addClass("glowna-kolumna") |
||
for k,v in ipairs(elements) |
for k,v in ipairs(elements) do |
||
do |
|||
local elem = makePanel(v) |
local elem = makePanel(v) |
||
column = column:node(elem) |
column = column:node(elem) |
||
Linia 98: | Linia 160: | ||
return frame:preprocess(tostring(mainList)) |
return frame:preprocess(tostring(mainList)) |
||
end |
end |
||
return p |
return p |
Aktualna wersja na dzień 03:00, 2 kwi 2022
Moduł automatycznie generujący semi-responsywny kolumnowy układ stron głównych. Zobacz też: Szablon:Generator głównych
local tools = require('Moduł:Narzędzia')
local p = {}
function sanitizeOrElse(text, alt)
if text ~= nil then
return mw.text.nowiki(text)
else
return alt
end
end
function allowOnly(value, allowed, alt)
local sanitized = mw.text.trim(sanitizeOrElse(value, alt))
for k, v in ipairs(allowed) do
if v == sanitized then
return sanitized
end
end
return alt
end
function makeShowStatus(value)
local final = allowOnly(value, {"desktoponly", "mobileonly"}, '')
if final == "desktoponly" then
return {desktoponly=true, mobileonly=false, duplicateDesktoponly=false, duplicateMobileonly=false}
elseif final == "mobileonly" then
return {desktoponly=false, mobileonly=true, duplicateDesktoponly=false, duplicateMobileonly=false}
else
return {desktoponly=false, mobileonly=false, duplicateDesktoponly=false, duplicateMobileonly=false}
end
end
function showStatusToString(stat)
local val = ""
if stat["desktoponly"] or stat["duplicateDesktoponly"] then
val = val .. "desktoponly "
end
if stat["mobileonly"] or stat["duplicateMobileonly"] then
val = val .. "mobileonly"
end
return mw.text.trim(val)
end
function getShowStatusForPlatform(stat, platform)
if platform == "desktop" then
return stat.desktoponly or stat.duplicateDesktoponly
elseif platform == "mobile" then
return stat.mobileonly or stat.duplicateMobileonly
end
end
function willEverBeUsed(cur)
return not ((getShowStatusForPlatform(cur.showstatus, "desktop") == getShowStatusForPlatform(cur.showstatus, "mobile")) and (getShowStatusForPlatform(cur.showstatus, "desktop") == true))
end
function makeElementTable(args, num)
local cur = {}
if args['ptytułszablon' .. num] ~= nil then
cur['title'] = '{{' .. mw.text.nowiki(args['ptytułszablon' .. num]) .. '}}'
else
cur['title'] = sanitizeOrElse(args['ptytuł' .. num], 'Nie podano tytułu')
end
cur['text'] = sanitizeOrElse(args['panel' .. num], '')
cur['num'] = num
cur['showstatus'] = makeShowStatus(args["pwarunki" .. num])
cur['icon'] = sanitizeOrElse(args['pikona' .. num], '')
cur['iconlink'] = sanitizeOrElse(args["pikonalink" .. num], '')
cur['titlelink'] = sanitizeOrElse(args["plink" .. num], '')
return cur
end
-- robi listę elementów wg kolumn, ignoruje - i puste
function makeElementList(args)
-- kolumny
local first = {}
local second = {}
local curLeft, curRight = 0, 0
for i = 1, 50 do
if args['panel' .. i] ~= nil then
if args['panel' .. i] == '-' or args['panel' .. i] == '' then else
local cur = makeElementTable(args, i)
if i % 2 == 1 then
table.insert(first, cur)
curLeft, curRight = 0, 0
else
cur.showstatus.duplicateDesktoponly = true
if willEverBeUsed(cur) then
table.insert(second, cur)
curRight = curRight + 1
end
local cur2 = makeElementTable(args, i)
cur2.showstatus.duplicateMobileonly = true
if willEverBeUsed(cur2) then
table.insert(first, cur2)
curLeft = curLeft + 1
end
end
end
else break end
end
if curRight > 0 then
local maxSecond = table.maxn(second)
for i = 0, (curRight - 1) do
second[maxSecond-i].showstatus.duplicateDesktoponly = false
end
end
if curLeft > 0 then
local maxFirst = table.maxn(first)
for i = 0, (curLeft - 1) do
table.remove(first, maxFirst-i)
end
end
return first, second
end
function makePanel(tab)
local panelname = tab["title"]
local titlelink = tab["titlelink"]
local template = tab["text"]
local mobiledesktop = showStatusToString(tab["showstatus"])
local icon = tab["icon"]
local iconlink = tab["iconlink"]
local id = "panel-" .. tab["num"] .. "-" .. mw.text.encode(panelname)
local wikitext = "{{User:Polskacafe/panel|" .. panelname .. "|{{" .. template .. "}}|ikona=" .. icon .. "|link=" .. iconlink .. "|tytuł link=" .. titlelink .. "}}"
local panel = mw.html.create("div")
:addClass("glowna-panel")
:attr("id", id)
:wikitext(wikitext)
if (mobiledesktop ~= nil) then
panel = panel:addClass(mobiledesktop)
end
return panel:done()
end
function makeColumn(elements)
column = mw.html.create("div")
:addClass("glowna-kolumna")
for k,v in ipairs(elements) do
local elem = makePanel(v)
column = column:node(elem)
end
return column:done()
end
function makeAll(args)
local first, second = makeElementList(args)
local mainList = mw.html.create("div")
:addClass("glowna-lista")
:node(makeColumn(first))
:node(makeColumn(second))
:done()
return mainList
end
function p.kolumny(frame)
local args = tools.getArgs(frame) -- argumenty szablonu to jest
local mainList = makeAll(args)
return frame:preprocess(tostring(mainList))
end
return p