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 = {}
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
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
end


local function fixChildBoxes(sval, tt)
-- Adds a row to the infobox
local function notempty( s ) return s and s:match( '%S' ) end
local function addRow(rowArgs)
    if rowArgs.header and rowArgs.header ~= '_BLANK_' then
if notempty(sval) then
        has_rows = true
local marker = '<span class=special_infobox_marker>'
        root
local s = sval
            :tag('tr')
-- start moving templatestyles and categories inside of table rows
            :addClass(rowArgs.rowclass)
local slast = ''
            :cssText(rowArgs.rowstyle)
while slast ~= s do
            :tag('th')
slast = s
            :attr('colspan', '2')
s = mw.ustring.gsub(s, '(</[Tt][Rr]%s*>%s*)(%[%[%s*[Cc][Aa][Tt][Ee][Gg][Oo][Rr][Yy]%s*:[^]]*%]%])', '%2%1')
            :addClass('infobox-header')
s = mw.ustring.gsub(s, '(</[Tt][Rr]%s*>%s*)(\127[^\127]*UNIQ%-%-templatestyles%-%x+%-QINU[^\127]*\127)', '%2%1')
            :cssText(args.headerstyle)
end
            :cssText(rowArgs.rowcellstyle)
-- end moving templatestyles and categories inside of table rows
            :wikitext(rowArgs.header)
s = mw.ustring.gsub(s, '(<%s*[Tt][Rr])', marker .. '%1')
    elseif rowArgs.data and rowArgs.data:gsub(category_in_empty_row_pattern, ''):match('^%S') then
s = mw.ustring.gsub(s, '(</[Tt][Rr]%s*>)', '%1' .. marker)
        has_rows = true
if s:match(marker) then
 
s = mw.ustring.gsub(s, marker .. '%s*' .. marker, '')
        local row = root:tag('tr')
s = mw.ustring.gsub(s, '([\r\n]|-[^\r\n]*[\r\n])%s*' .. marker, '%1')
        row:addClass(rowArgs.rowclass)
s = mw.ustring.gsub(s, marker .. '%s*([\r\n]|-)', '%1')
        row:cssText(rowArgs.rowstyle)
s = mw.ustring.gsub(s, '(</[Cc][Aa][Pp][Tt][Ii][Oo][Nn]%s*>%s*)' .. marker, '%1')
 
s = mw.ustring.gsub(s, '(<%s*[Tt][Aa][Bb][Ll][Ee][^<>]*>%s*)' .. marker, '%1')
        if rowArgs.label then
s = mw.ustring.gsub(s, '^(%{|[^\r\n]*[\r\n]%s*)' .. marker, '%1')
            row
s = mw.ustring.gsub(s, '([\r\n]%{|[^\r\n]*[\r\n]%s*)' .. marker, '%1')
                :tag('th')
s = mw.ustring.gsub(s, marker .. '(%s*</[Tt][Aa][Bb][Ll][Ee]%s*>)', '%1')
                :attr('scope', 'row')
s = mw.ustring.gsub(s, marker .. '(%s*\n|%})', '%1')
                :addClass('infobox-label')
end
                :cssText(args.labelstyle)
if s:match(marker) then
                :cssText(rowArgs.rowcellstyle)
local subcells = mw.text.split(s, marker)
                :wikitext(rowArgs.label)
s = ''
                :done()
for k = 1, #subcells do
        end
if k == 1 then
 
s = s .. subcells[k] .. '</' .. tt .. '></tr>'
        local dataCell = row:tag('td')
elseif k == #subcells then
        dataCell
local rowstyle = ' style="display:none"'
            :attr('colspan', not rowArgs.label and '2' or nil)
if notempty(subcells[k]) then rowstyle = '' end
            :addClass(not rowArgs.label and 'infobox-full-data' or 'infobox-data')
s = s .. '<tr' .. rowstyle ..'><' .. tt .. ' colspan=2>\n' ..
            :cssText(rowArgs.datastyle)
subcells[k]
            :cssText(rowArgs.rowcellstyle)
elseif notempty(subcells[k]) then
            :wikitext(rowArgs.data)
if (k % 2) == 0 then
    else
s = s .. subcells[k]
        table.insert(empty_row_categories, rowArgs.data or '')
else
    end
s = s .. '<tr><' .. tt .. ' colspan=2>\n' ..
end
subcells[k] .. '</' .. tt .. '></tr>'
 
end
-- Main rendering function
end
local function renderRows()
end
    -- Render other rows first (unchanged)
end
    local rownums = union(getArgNums('header'), getArgNums('data'))
-- the next two lines add a newline at the end of lists for the PHP parser
    table.sort(rownums)
-- [[Special:Diff/849054481]]
 
-- remove when [[:phab:T191516]] is fixed or OBE
    for k, num in ipairs(rownums) do
s = mw.ustring.gsub(s, '([\r\n][%*#;:][^\r\n]*)$', '%1\n')
        addRow({
s = mw.ustring.gsub(s, '^([%*#;:][^\r\n]*)$', '%1\n')
            header = args['header' .. tostring(num)],
s = mw.ustring.gsub(s, '^([%*#;:])', '\n%1')
            label = args['label' .. tostring(num)],
s = mw.ustring.gsub(s, '^(%{%|)', '\n%1')
            data = args['data' .. tostring(num)],
return s
            datastyle = args.datastyle,
else
            class = args['class' .. tostring(num)],
return sval
            rowclass = args['rowclass' .. tostring(num)],
end
            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


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: