Moduł:Partie polityczne
Wygląd
Dokumentacja modułu
[stwórz] [ ]
Zobacz podstrony tego modułu.
local function getPartyParams(country)
mw.logObject(country, "country")
local moduleprefix = "Moduł:Partie polityczne/dane/"
local moduletitle = mw.title.new(country)
if not moduletitle or (moduletitle.namespace ~= 828) then
moduletitle = mw.title.new(moduleprefix..country)
end
if not moduletitle then
mw.log("„"..country.."” nie jest prawidłową nazwą z danymi partii politycznych")
elseif moduletitle.exists then
local status, partiesData = pcall(mw.loadData, moduletitle.fullText)
if status then
return partiesData
end
else
mw.log("Nie mogę znaleźć definicji partii politycznych. Nie istnieje „"..moduleprefix..country.."”.")
end
end
local function selectPartyData(args)
local abbr = args[1]
if not abbr then
return
end
local group = args[2]
mw.logObject({abbr, group}, "abbr, group")
local data = getPartyParams(group or "Polska") or {}
if data[abbr] then
mw.logObject({abbr, group or false, data[abbr]}, "selected key")
return abbr, data[abbr]
end
local result = {}
-- look for name
for k, v in pairs(data) do
local key = type(k) == "string" and k or false
if (abbr == v.opis) or (abbr == v.link) then
table.insert(result, {key, v})
end
end
if #result == 1 then
mw.logObject({abbr, group or false, result[1]}, "selected name")
return result[1][1], result[1][2]
end
mw.logObject({abbr, group or false}, "not selected")
end
local function Name(party)
if not party then
return
end
local desc = party.opis
local link = party.link
mw.logObject({desc, link}, "desc, link")
if link and desc and (link ~= desc) then
mw.logObject("[["..link.."|"..desc.."]]", "return nazwa A")
return "[["..link.."|"..desc.."]]"
elseif link then
mw.logObject("[["..link.."]]", "return nazwa B")
return "[["..link.."]]"
elseif desc then
mw.logObject(desc, "return nazwa C")
return desc
end
end
local function Color(args)
local key, party = selectPartyData(args)
mw.logObject(party, "party")
if party and party.kolor then
return party.kolor
end
return args.kolor or "E6E6E6"
end
local function Short(args)
local abbr, party = selectPartyData(args)
mw.logObject(party, "party")
if party then
local link = party.link
if link and abbr then
return link == abbr and ("[["..link.."]]") or ("[["..link.."|"..abbr.."]]")
elseif abbr then
return abbr
else
return Name(party) or args[1]
end
end
return args[1]
end
return {
["skrót"] = function(frame)
local args = require('Module:Arguments').getArgs(frame)
local key, party = selectPartyData(args)
mw.logObject(party, "party")
return key or args[1]
end,
["kolor"] = function(frame)
return Color(require('Module:Arguments').getArgs(frame))
end,
["link"] = function(frame)
local args = require('Module:Arguments').getArgs(frame)
local key, party = selectPartyData(args)
mw.logObject(party, "party")
if party then
return party.link
end
end,
["opis"] = function(frame)
local args = require('Module:Arguments').getArgs(frame)
local key, party = selectPartyData(args)
mw.logObject(party, "party")
if party then
return party.opis
end
end,
["nazwa"] = function(frame)
local args = require('Module:Arguments').getArgs(frame)
local key, party = selectPartyData(args)
mw.logObject(party, "party")
return Name(party) or args[1]
end,
["krótko"] = function(frame)
return Short(require('Module:Arguments').getArgs(frame))
end,
["wybory"] = function(frame)
local args = require('Module:Arguments').getArgs(frame)
local country = args["państwo"] or false
local maxWidth = args["szerokość"] or '120px'
local input = args["dane"] or ''
local normScale = 0
local widthScale, widthUnit = string.match(maxWidth, "^([0-9]+)(.+)$")
local items = {}
local lang = mw.getContentLanguage()
for i, row in ipairs(mw.text.split(input, '\n')) do
local color, s, v = mw.ustring.match(row,"^(#[%d%x][%d%x][%d%x]) +(.-) +([%d,%.]+)%%? *$")
if not color then
color, s, v = mw.ustring.match(row,"^(#[%d%x][%d%x][%d%x][%d%x][%d%x][%d%x]) +(.-) +([%d,%.]+)%%? *$")
end
local doc = false
if not color then
s, v = mw.ustring.match(row,"^ *(.-) +([%d,%.]+)%%? *$")
if not s then
-- na potrzeby dokumentacji szablonów
s, v = mw.ustring.match(row,"^ *(.-) +({{{[^{}]+}}}) *$")
doc = true
end
end
if s then
local rowArgs = {mw.text.trim(s), country or ''}
local value = tonumber(v) or lang:parseFormattedNumber(v) or false
if value and (value > normScale) then normScale = value end
table.insert(items, {
color = color or '#'..Color(rowArgs),
desc = Short(rowArgs),
value = value,
v = v,
doc = doc,
})
end
end
if #items <= 0 then return nil end -- brak danych
if normScale <= 0 then normScale = 1 end -- nie dziel przez zero
local builder = mw.html.create(nil)
for i, item in ipairs(items) do
local value = item.doc and normScale or (item.value or 0)
local width = tostring(widthScale * value / normScale)..widthUnit
local container = builder:tag('div'):addClass('c')
container:tag('div'):addClass('d'):wikitext(item.desc)
container:tag('div'):addClass('b'):css('background', item.color):css('color', 'black'):css('width', width)
container:tag('div'):addClass('p'):wikitext(item.value and lang:formatNum(item.value) or item.v, '%')
end
return tostring(builder)
end,
["doc"] = function(frame)
local moduleName = mw.title.getCurrentTitle().fullText
local status, partiesData = pcall(mw.loadData, moduleName)
if not status then
-- logowanie komunikatu błędu może być przydatne
mw.log(partiesData)
return
end
local data = {}
local lang = mw.getContentLanguage()
for k, v in pairs(partiesData) do
table.insert(data, {
_sk = v.kolor and "1" or "0",
_ss = type(k) == "string" and "1" or "0",
_sn = lang:caseFold(v.opis or v.link or (type(k) == "string" and k or "")),
key = type(k) == "string" and k or "''brak''",
color = v.kolor and ("#"..v.kolor) or "none",
kolor = v.kolor or "''brak''",
name = Name(v) or "''brak''"
})
end
table.sort(data, function(a, b) return a._sn < b._sn end)
local builder = mw.html.create()
builder
:wikitext(
'Dane, które są używane przez szablony dotyczące partii politycznych.',
'\n{| class="wikitable collapsible collapsed" width="100%"',
'\n! Struktura rekordu opisującego jedną partię',
'\n|-',
'\n| '
)
:tag("pre"):wikitext(
'["..."] = {\t-- opcjonalny skrót nazwy partii',
'\n\tkolor = "...",\t-- 6 cyfr szesnastkowych kodujących kolor w HTML',
'\n\tlink = "...",\t-- nazwa artykułu opisującego partię',
'\n\topis = "...",\t-- nazwa partii gdy nie ma artykułu lub link zawiera nawias ujednoznaczniający',
'\n}'
):done()
:wikitext(
'\n|}',
'\n{| class="wikitable sortable"',
'\n|+ Lista partii politycznych',
'\n|-',
'\n! class="unsortable ordinal" | lp.',
'\n!Kolor',
'\n!Skrót',
'\n!Nazwa'
)
for i, v in ipairs(data) do
builder
:wikitext(
'\n|-',
'\n| ', i,
'\n| '
)
builder
:tag("span"):css({ display = "none"}):wikitext(v._sk):done()
:tag("span"):css({ border = "none", background = "none", ["text-aling"] = "left", margin = "0px!important"})
:tag("span"):css({border = "solid 1px silver", background = v.color, color = 'black', ["font-size"] = "80%!important", ["vertical-align"] = "middle", })
:wikitext(" ")
:done()
:wikitext(' ', v.kolor)
:done()
:wikitext('\n| '):tag("span"):css({ display = "none"}):wikitext(v._ss):done():wikitext(v.key)
:wikitext('\n| '):tag("span"):css({ display = "none"}):wikitext(#v._sn > 0 and "1" or "0", v._sn):done():wikitext(v.name)
end
builder:wikitext("\n|}")
local result = tostring(builder)
mw.log(result, "result")
return result
end,
}