Jump to content

Editing Module:Infobox

Warning: You are not logged in. Your IP address will be publicly visible if you make any edits. If you log in or create an account, your edits will be attributed to your username, along with other benefits.

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 = {}
-- Existing initialization remains unchanged
local args = {}
local args = {}
local origArgs = {}
local origArgs = {}
Line 6: Line 9:
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
local lists = {
 
plainlist_t = {
-- Function to render the 'Ambassador to' field
patterns = {
local function renderAmbassadorTo()
'^plainlist$',
    -- Check if the 'ambassador_to' field is explicitly set or if suppression is requested
'%splainlist$',
    if args['hide_ambassador'] == 'yes' or args['diplomatic_role'] == 'no' then
'^plainlist%s',
        return nil -- Suppress the field
'%splainlist%s'
    end
},
 
found = false,
    if args['ambassador_to'] and args['ambassador_to'] ~= '' then
styles = 'Plainlist/styles.css'
        return {
},
            label = 'Ambassador to',
hlist_t = {
            data = args['ambassador_to']
patterns = {
        }
'^hlist$',
    end
'%shlist$',
 
'^hlist%s',
    return nil -- Default: do not render
'%shlist%s'
end
},
found = false,
styles = 'Hlist/styles.css'
}
}


local function has_list_class(args_to_check)
local function has_list_class(args_to_check)
for _, list in pairs(lists) do
    for _, list in pairs(lists) do
if not list.found then
        if not list.found then
for _, arg in pairs(args_to_check) do
            for _, arg in pairs(args_to_check) do
for _, pattern in ipairs(list.patterns) do
                for _, pattern in ipairs(list.patterns) do
if mw.ustring.find(arg or '', pattern) then
                    if mw.ustring.find(arg or '', pattern) then
list.found = true
                        list.found = true
break
                        break
end
                    end
end
                end
if list.found then break end
                if list.found then break end
end
            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
end


local function fixChildBoxes(sval, tt)
-- Main rendering function
local function notempty( s ) return s and s:match( '%S' ) end
local function renderRows()
    -- Render other rows first (unchanged)
if notempty(sval) then
    local rownums = union(getArgNums('header'), getArgNums('data'))
local marker = '<span class=special_infobox_marker>'
    table.sort(rownums)
local s = sval
 
-- start moving templatestyles and categories inside of table rows
    for k, num in ipairs(rownums) do
local slast = ''
        addRow({
while slast ~= s do
            header = args['header' .. tostring(num)],
slast = s
            label = args['label' .. tostring(num)],
s = mw.ustring.gsub(s, '(</[Tt][Rr]%s*>%s*)(%[%[%s*[Cc][Aa][Tt][Ee][Gg][Oo][Rr][Yy]%s*:[^]]*%]%])', '%2%1')
            data = args['data' .. tostring(num)],
s = mw.ustring.gsub(s, '(</[Tt][Rr]%s*>%s*)(\127[^\127]*UNIQ%-%-templatestyles%-%x+%-QINU[^\127]*\127)', '%2%1')
            datastyle = args.datastyle,
end
            class = args['class' .. tostring(num)],
-- end moving templatestyles and categories inside of table rows
            rowclass = args['rowclass' .. tostring(num)],
s = mw.ustring.gsub(s, '(<%s*[Tt][Rr])', marker .. '%1')
            rowstyle = args['rowstyle' .. tostring(num)],
s = mw.ustring.gsub(s, '(</[Tt][Rr]%s*>)', '%1' .. marker)
            rowcellstyle = args['rowcellstyle' .. tostring(num)]
if s:match(marker) then
        })
s = mw.ustring.gsub(s, marker .. '%s*' .. marker, '')
    end
s = mw.ustring.gsub(s, '([\r\n]|-[^\r\n]*[\r\n])%s*' .. marker, '%1')
 
s = mw.ustring.gsub(s, marker .. '%s*([\r\n]|-)', '%1')
    -- Render the 'Ambassador to' field if applicable
s = mw.ustring.gsub(s, '(</[Cc][Aa][Pp][Tt][Ii][Oo][Nn]%s*>%s*)' .. marker, '%1')
    local ambassadorRow = renderAmbassadorTo()
s = mw.ustring.gsub(s, '(<%s*[Tt][Aa][Bb][Ll][Ee][^<>]*>%s*)' .. marker, '%1')
    if ambassadorRow then
s = mw.ustring.gsub(s, '^(%{|[^\r\n]*[\r\n]%s*)' .. marker, '%1')
        addRow(ambassadorRow)
s = mw.ustring.gsub(s, '([\r\n]%{|[^\r\n]*[\r\n]%s*)' .. marker, '%1')
    end
s = mw.ustring.gsub(s, marker .. '(%s*</[Tt][Aa][Bb][Ll][Ee]%s*>)', '%1')
s = mw.ustring.gsub(s, marker .. '(%s*\n|%})', '%1')
end
if s:match(marker) then
local subcells = mw.text.split(s, marker)
s = ''
for k = 1, #subcells do
if k == 1 then
s = s .. subcells[k] .. '</' .. tt .. '></tr>'
elseif k == #subcells then
local rowstyle = ' style="display:none"'
if notempty(subcells[k]) then rowstyle = '' end
s = s .. '<tr' .. rowstyle ..'><' .. tt .. ' colspan=2>\n' ..
subcells[k]
elseif notempty(subcells[k]) then
if (k % 2) == 0 then
s = s .. subcells[k]
else
s = s .. '<tr><' .. tt .. ' colspan=2>\n' ..
subcells[k] .. '</' .. tt .. '></tr>'
end
end
end
end
-- the next two lines add a newline at the end of lists for the PHP parser
-- [[Special:Diff/849054481]]
-- remove when [[:phab:T191516]] is fixed or OBE
s = mw.ustring.gsub(s, '([\r\n][%*#;:][^\r\n]*)$', '%1\n')
s = mw.ustring.gsub(s, '^([%*#;:][^\r\n]*)$', '%1\n')
s = mw.ustring.gsub(s, '^([%*#;:])', '\n%1')
s = mw.ustring.gsub(s, '^(%{%|)', '\n%1')
return s
else
return sval
end
end
end


Please note that all contributions to BRUS Wikipedia may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see BRUS Wikipedia:Copyrights for details). Do not submit copyrighted work without permission!
Cancel Editing help (opens in new window)
Preview page with this template

Template used on this page: