Module:Sandbox/BrandonXLF/3: Difference between revisions

Content deleted Content added
Add historical-affiliation-flag class
Simpler form
Line 4:
local yesno = require('Module:Yesno')
local p = {}
 
local function getCountry(country)
local title = mw.title.new(country)
if not title or not title.exists then
return country
end
return '[[' .. country .. ']]'
end
 
local function getTimespan(timespan)
return ' (' .. timespan .. ')'
end
 
local function getImage(image)
local file = null
 
if string.match(image, '.-%.') then
file = mw.getCurrentFrame():expandTemplate{title = 'flagicon image', args = {image}}
elseif string.match(image, '.- %(.-%)') then
local country, var = string.match(image,'(.-) %((.-)%)')
file = mw.getCurrentFrame():expandTemplate{title = 'flagdeco', args = {country, var, noredlink = 'y'}}
else
file = mw.getCurrentFrame():expandTemplate{title = 'flagdeco', args = {image, noredlink = 'y'}}
end
return file
end
 
function p.main(frame)
local args = getArgs(frame)
local hasFlags = false
local entries = {}
local i = 1
local knownKeys = {
[''] = true,
['date'] = true,
flag = true
}
 
for i, entry in ipairs(args) do
local country
local timespan
local flag
local subArgs = {}
local data = {}
for line in entry:gmatch('[^\n]+') do
line = mw.text.trim(line)
local key, val = line:match('^([a-z]*):? *(.*)$')
if knownKeys[key] then
subArgs[key] = val
else
return require('Module:Error').error{'Invalid key ' .. key .. ' in argument ' .. i}
end
end
if subArgs[''] then
data[''] = getCountry(subArgs[''])
else
return require('Module:Error').error{'A country is required in argument ' .. i}
end
if subArgs['date'] then
data['date'] = getTimespan(subArgs['date'])
else
return require('Module:Error').error{'A date is required in argument ' .. i}
end
if subArgs.flag then
hasFlags = true
end
local noFlag = yesno(subArgs['flag']) == false
data['flag'] = getImage(
noFlag
and ''
or (subArgs['flag'] == '' or subArgs['flag'] == nil)
and subArgs['']
or subArgs['flag'])
 
-- Add placeholder image
if data['flag'] == '' then
data['flag'] = getImage('')
end
table.insert(entries, data)
end
local container = mw.html.create('div')
Line 112 ⟶ 23:
:addClass('historical-affiliation-list')
 
for i_, entryval in ipairs(entriesargs) do
local item = list:tag('li');:wikitext(val)
item:tag('span')
:addClass('historical-affiliation-flag')
:wikitext(entry.flag)
item:tag('span')
:wikitext(entry[''] .. entry['date'])
end