Jump to content

Module:Infobox: Difference between revisions

BRUSpedia (talk | contribs)
m 82 revisions imported from wikipedia:Module:Infobox
BRUSpedia (talk | contribs)
No edit summary
Tag: Manual revert
 
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 12: 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 = {
-- Function to render the 'Ambassador to' field
plainlist_t = {
local function renderAmbassadorTo()
patterns = {
    -- Check if the 'ambassador_to' field is explicitly set or if suppression is requested
'^plainlist$',
    if args['hide_ambassador'] == 'yes' or args['diplomatic_role'] == 'no' then
'%splainlist$',
        return nil -- Suppress the field
'^plainlist%s',
    end
'%splainlist%s'
 
},
    if args['ambassador_to'] and args['ambassador_to'] ~= '' then
found = false,
        return {
styles = 'Plainlist/styles.css'
            label = 'Ambassador to',
},
            data = args['ambassador_to']
hlist_t = {
        }
patterns = {
    end
'^hlist$',
 
'%shlist$',
    return nil -- Default: do not render
'^hlist%s',
end
'%shlist%s'
},
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


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