Jump to content

Module:Infobox: Difference between revisions

BRUSpedia (talk | contribs)
No edit summary
Tag: Reverted
BRUSpedia (talk | contribs)
No edit summary
Tag: Manual revert
 
(3 intermediate revisions by the same user not shown)
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 9: Line 6:
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 = {
patterns = {
'^plainlist$',
'%splainlist$',
'^plainlist%s',
'%splainlist%s'
},
found = false,
styles = 'Plainlist/styles.css'
},
hlist_t = {
patterns = {
'^hlist$',
'%shlist$',
'^hlist%s',
'%shlist%s'
},
found = false,
styles = 'Hlist/styles.css'
}
}


-- Function to render the 'Ambassador to' field
local function has_list_class(args_to_check)
local function renderAmbassadorTo()
for _, list in pairs(lists) do
    -- Check if the 'ambassador_to' field is explicitly set or if suppression is requested
if not list.found then
    if args['hide_ambassador'] == 'yes' or args['diplomatic_role'] == 'no' then
for _, arg in pairs(args_to_check) do
        return nil -- Suppress the field
for _, pattern in ipairs(list.patterns) do
    end
if mw.ustring.find(arg or '', pattern) then
 
list.found = true
    if args['ambassador_to'] and args['ambassador_to'] ~= '' then
break
        return {
end
            label = 'Ambassador to',
end
            data = args['ambassador_to']
if list.found then break end
        }
end
    end
end
 
end
    return nil -- Default: do not render
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


-- Main rendering function
local function fixChildBoxes(sval, tt)
local function renderRows()
local function notempty( s ) return s and s:match( '%S' ) end
    -- Render other rows first (unchanged)
    local rownums = union(getArgNums('header'), getArgNums('data'))
if notempty(sval) then
    table.sort(rownums)
local marker = '<span class=special_infobox_marker>'
 
local s = sval
    for k, num in ipairs(rownums) do
-- start moving templatestyles and categories inside of table rows
        addRow({
local slast = ''
            header = args['header' .. tostring(num)],
while slast ~= s do
            label = args['label' .. tostring(num)],
slast = s
            data = args['data' .. tostring(num)],
s = mw.ustring.gsub(s, '(</[Tt][Rr]%s*>%s*)(%[%[%s*[Cc][Aa][Tt][Ee][Gg][Oo][Rr][Yy]%s*:[^]]*%]%])', '%2%1')
            datastyle = args.datastyle,
s = mw.ustring.gsub(s, '(</[Tt][Rr]%s*>%s*)(\127[^\127]*UNIQ%-%-templatestyles%-%x+%-QINU[^\127]*\127)', '%2%1')
            class = args['class' .. tostring(num)],
end
            rowclass = args['rowclass' .. tostring(num)],
-- end moving templatestyles and categories inside of table rows
            rowstyle = args['rowstyle' .. tostring(num)],
s = mw.ustring.gsub(s, '(<%s*[Tt][Rr])', marker .. '%1')
            rowcellstyle = args['rowcellstyle' .. tostring(num)]
s = mw.ustring.gsub(s, '(</[Tt][Rr]%s*>)', '%1' .. marker)
        })
if s:match(marker) then
    end
s = mw.ustring.gsub(s, marker .. '%s*' .. marker, '')
 
s = mw.ustring.gsub(s, '([\r\n]|-[^\r\n]*[\r\n])%s*' .. marker, '%1')
    -- Render the 'Ambassador to' field if applicable
s = mw.ustring.gsub(s, marker .. '%s*([\r\n]|-)', '%1')
    local ambassadorRow = renderAmbassadorTo()
s = mw.ustring.gsub(s, '(</[Cc][Aa][Pp][Tt][Ii][Oo][Nn]%s*>%s*)' .. marker, '%1')
    if ambassadorRow then
s = mw.ustring.gsub(s, '(<%s*[Tt][Aa][Bb][Ll][Ee][^<>]*>%s*)' .. marker, '%1')
        addRow(ambassadorRow)
s = mw.ustring.gsub(s, '^(%{|[^\r\n]*[\r\n]%s*)' .. marker, '%1')
    end
s = mw.ustring.gsub(s, '([\r\n]%{|[^\r\n]*[\r\n]%s*)' .. marker, '%1')
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