Edge Rewrite
// HTMLRewriter · presentation

This page was redesigned at the edge.

Cloudflare fetched the original article and streamed it through HTMLRewriter to apply an entirely new visual system without rebuilding the source page.

Jump to content

Module:Badge display

From Wikipedia, the free encyclopedia

--- Badge display module.
-- Initial code by John Dovey (19 April 2023) [[User:BoonDock]].
-- @module badge display

require('strict')
local p = {}
local data = mw.loadData('Module:Badge display/data')
local getArgs = require('Module:Arguments').getArgs
local GlobalTemplateArgs = '' -- define as global (to this module) variable

function p.DisplayImage(frame)
    -- Usage from a template etc:
    -- {{#invoke:BadgeDisplay|DisplayImage
    -- | image = NameOfImage.jpg
    -- | caption = Caption for the image
    -- | description = Description of the image
    -- | float = left/right/center
    -- | width = width in pixels or other units of the table
    -- | size = size in pixels or other units, e.g. "x25px"
    -- }}

    local args = getArgs(frame)

    -- Set default values for optional parameters
    local float = args.float or 'none'
    if float == "left" then 
        float = "floatleft"
    else
        if float == "right" then
            float = "floatright"
        end
    end
    if float == "none" then float = '' end
    
    local size = args.size or 'x200px'
    local width = args.width or ''
    local code =''
    code = args[1] or '' -- Taking a risk. Set code to first unnamed param but then overwriting it if code is named
    if string.len(code) < 2 then 
        code =  args.code
    end
    if not code or '' == code then
		-- return '<span style="color:#d33">Error: missing positional parameter 1</span>'
		return '<strong class="error"> (Badge display module error: <code>' .. code .. '</code> is invalid.</strong> )</span> [[Category:Errors reported by Module:BadgeDisplay]]'
	end
    
--	local code = code:upper() -- DO NOT DO THIS. Code is case sensitive -- Converts the first argument to uppercase
	if not data[code] then
		-- return '<span style="color:#d33">Error: invalid positional parameter 1: ' .. code .. '</span>'
		return '<strong class="error"> (Badge display module error: <code>' .. code .. '</code> code not found.</strong>)</span> [[Category:Errors reported by Module:BadgeDisplay]]'
	end

    -- {Code = "xx", Type = "", Description = "xx", Class="", Variation="", Image="XX", PageLink="XX",  Country="ZAR", Note="", Org="SANDF", RecipCat="" }
    -- Data from the record into local variables
    local Code = data[code].Code
    local Type = data[code].Type
    local Description = data[code].Description
    local Class = data[code].Class
    local Variation = data[code].Variation
    local Image = data[code].Image
    local PageLink = data[code].PageLink
    local Country = data[code].Country
    local Note = data[code].Note
    local Org = data[code].Org
    local caption = ''
    
   if string.len(Image) < 1 then
    Image="Ribbon - Question mark.png"
   end
    local NoTable = args.NoTable or ''
    if string.len(NoTable) > 0 then -- Do not output a table, just the badge
        local output = ''
        output = output .. "[[File:" .. Image .. "|".. size .. "|" .. Description .. "]]"
        return output -- This will exit the function and stop processing further
    end

    local DescOnly = args.DescOnly or ''
    if string.len(DescOnly) > 0 then -- Do not output a table, just the badge with a description
        if DescOnly ~= "yes" then -- Hidden option to over-ride the description
            Description=DescOnly
        end
        local output = ''
        output = output .. "[[File:" .. Image .. "|".. size .. "]]"
        output = output .. " [[" .. PageLink .. "|" .. Description .. "]]"
        return output -- This will exit the function and stop processing further
    end

        -- Build the wikitext table
        local wikitext=''
        wikitext = wikitext .. '{| class="wikitable ' .. float .. '" style="max-width: 18em; width: auto; margin: 4px; border: none;"\n'
        caption= "[[" .. PageLink .. "|" .. Description .. "]]"
        if string.len(Type) > 1 then
            caption = caption .. " (" .. Type .. ")"
        end
        wikitext = wikitext .. '|+ ' .. caption ..'\n|-\n'
        wikitext = wikitext .. '|aria-describedby="badge-display-' .. Image .. '" style="text-align: center; border: none; padding-bottom: 10px; | [[File:' .. Image .. '|' .. size .. '|alt=]]\n|-\n'
        wikitext = wikitext .. '|id="badge-display-' .. Image .. '" style="text-align: center; border: 0; border-top: 1px solid var(--border-color-subtle); padding-top: 4px; margin-top: 4px; text-wrap-style=pretty; "|' -- .. Description 
        if string.len(Class) > 1 then 
            wikitext = wikitext  .. Class .. ". "
        end
        if string.len(Variation) > 1 then
            wikitext = wikitext  .. Variation .. ". "
        end
        if string.len(Note) > 1 then
            wikitext = wikitext ..  "<i>" .. Note .. "</i>"
        end
        wikitext = wikitext .. '\n|-\n'
        wikitext = wikitext .. '|}\n'
        return wikitext
end
function p.BadgeTable(frame)
	
	local output = ''
	local templateArgs = getArgs(frame)
	local localfloat = templateArgs["float"] or ''
	if not localfloat then
		localfloat = "left"  -- NOTE TODO: Make the table float according to the value of locafloat ;-)
	end
	local tableCode = '{| class="wikitable sortable" \n'
	tableCode = tableCode .. "|+ List of badges available \n" -- Caption
	tableCode = tableCode .. "|-\n"
    -- xx = {Code = "xx", Type = "", Description = "xx", Class="", Variation="", Image="XX", PageLink="XX",  Country="ZAR", Note="", Org="SANDF", RecipCat="" },    
	tableCode = tableCode .. "! Code !! Type !! Description !! Class !! Variation !! Image !! Note !! Country\n"
	-- Iterate through the data in the table "GunnerData"
	for code, record in pairs(data) do
		-- Generate wiki table code for the Badges
		tableCode = tableCode .. "|-\n"
		tableCode = tableCode .. "|" ..code .. "\n"
		-- tableCode = tableCode .. "| " .. record.Code .. "\n"
		tableCode = tableCode .. "| " .. record.Type .. "\n"
		if string.len(record.PageLink) < 1 then
			tableCode = tableCode .. "| " .. record.Description 
		else
			tableCode = tableCode .. "| " .. "[[" .. record.PageLink .. "|" .. record.Description .. "]]"
		end
		tableCode = tableCode .. "\n"
		tableCode = tableCode .. "| " .. record.Class .. "\n"
		tableCode = tableCode .. "| " .. record.Variation .. "\n"
        tableCode = tableCode .. "| [[File:" .. record.Image .. "|80px]]\n"
		tableCode = tableCode .. '| style=\"text-align: left;\"|' .. record.Note .. "\n"
        tableCode = tableCode .. "| " .. record.Country .. "\n"
		
	end
	tableCode = tableCode .. "|}" -- End the table
	return tableCode
end

return p