Editing Module:Infobox
Appearance
The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then publish the changes below to finish undoing the edit.
Latest revision | Your text | ||
Line 1: | Line 1: | ||
-- Adding support for conditional rendering of the 'Ambassador to' field | |||
local p = {} | local p = {} | ||
local args = {} | |||
local origArgs = {} | |||
-- Existing initialization remains unchanged | |||
local args = {} | local args = {} | ||
local origArgs = {} | local origArgs = {} | ||
Line 6: | Line 12: | ||
local category_in_empty_row_pattern = '%[%[%s*[Cc][Aa][Tt][Ee][Gg][Oo][Rr][Yy]%s*:[^]]*]]' | local category_in_empty_row_pattern = '%[%[%s*[Cc][Aa][Tt][Ee][Gg][Oo][Rr][Yy]%s*:[^]]*]]' | ||
local has_rows = false | local has_rows = false | ||
-- Function to render the 'Ambassador to' field | |||
local function renderAmbassadorTo() | |||
-- Check if the 'ambassador_to' field is explicitly set or if suppression is requested | |||
if args['hide_ambassador'] == 'yes' or args['diplomatic_role'] == 'no' then | |||
return nil -- Suppress the field | |||
end | |||
if args['ambassador_to'] and args['ambassador_to'] ~= '' then | |||
return { | |||
label = 'Ambassador to', | |||
data = args['ambassador_to'] | |||
} | |||
end | |||
return nil -- Default: do not render | |||
end | |||
local function has_list_class(args_to_check) | local function has_list_class(args_to_check) | ||
for _, list in pairs(lists) do | |||
if not list.found then | |||
for _, arg in pairs(args_to_check) do | |||
for _, pattern in ipairs(list.patterns) do | |||
if mw.ustring.find(arg or '', pattern) then | |||
list.found = true | |||
break | |||
end | |||
end | |||
if list.found then break end | |||
end | |||
end | |||
end | |||
end | end | ||
-- Adds a row to the infobox | |||
local function addRow(rowArgs) | |||
if rowArgs.header and rowArgs.header ~= '_BLANK_' then | |||
has_rows = true | |||
root | |||
:tag('tr') | |||
:addClass(rowArgs.rowclass) | |||
:cssText(rowArgs.rowstyle) | |||
:tag('th') | |||
:attr('colspan', '2') | |||
:addClass('infobox-header') | |||
:cssText(args.headerstyle) | |||
:cssText(rowArgs.rowcellstyle) | |||
:wikitext(rowArgs.header) | |||
elseif rowArgs.data and rowArgs.data:gsub(category_in_empty_row_pattern, ''):match('^%S') then | |||
has_rows = true | |||
local row = root:tag('tr') | |||
row:addClass(rowArgs.rowclass) | |||
row:cssText(rowArgs.rowstyle) | |||
if rowArgs.label then | |||
row | |||
:tag('th') | |||
:attr('scope', 'row') | |||
:addClass('infobox-label') | |||
:cssText(args.labelstyle) | |||
:cssText(rowArgs.rowcellstyle) | |||
:wikitext(rowArgs.label) | |||
:done() | |||
end | |||
local dataCell = row:tag('td') | |||
dataCell | |||
:attr('colspan', not rowArgs.label and '2' or nil) | |||
:addClass(not rowArgs.label and 'infobox-full-data' or 'infobox-data') | |||
:cssText(rowArgs.datastyle) | |||
:cssText(rowArgs.rowcellstyle) | |||
:wikitext(rowArgs.data) | |||
else | |||
table.insert(empty_row_categories, rowArgs.data or '') | |||
end | |||
end | |||
-- Main rendering function | |||
local function renderRows() | |||
-- Render other rows first (unchanged) | |||
local rownums = union(getArgNums('header'), getArgNums('data')) | |||
table.sort(rownums) | |||
for k, num in ipairs(rownums) do | |||
addRow({ | |||
header = args['header' .. tostring(num)], | |||
label = args['label' .. tostring(num)], | |||
data = args['data' .. tostring(num)], | |||
datastyle = args.datastyle, | |||
class = args['class' .. tostring(num)], | |||
rowclass = args['rowclass' .. tostring(num)], | |||
rowstyle = args['rowstyle' .. tostring(num)], | |||
rowcellstyle = args['rowcellstyle' .. tostring(num)] | |||
}) | |||
end | |||
-- Render the 'Ambassador to' field if applicable | |||
local ambassadorRow = renderAmbassadorTo() | |||
if ambassadorRow then | |||
addRow(ambassadorRow) | |||
end | |||
end | end | ||